Pseudocode Questions with Answers for Placement Tests
Q. 1 Find the output of given peudocode
BEGIN DECLARE counter = 1 WHILE counter <= 5 DO OUTPUT counter counter = counter + 1 ENDWHILE END
Check Solution
Ans: B
Explanation: The pseudocode initializes a counter to 1. The WHILE loop continues as long as the counter is less than or equal to 5. Inside the loop, the current value of the counter is outputted, and then the counter is incremented by 1. This process repeats until the counter becomes 6. The output will be the numbers from 1 to 5, each on a new line (or separated by spaces/commas if specified by the environment).
Q. 2 Find the output of given peudocode
FUNCTION LinearSearchPlacementTest(searchList: ARRAY OF INTEGER, searchValue: INTEGER) -> INTEGER // This function performs a linear search on the given list. // Input: searchList (array of integers), searchValue (integer to search for) // Output: index of the searchValue if found, -1 otherwise. FOR i FROM 0 TO LENGTH(searchList) - 1 DO IF searchList[i] == searchValue THEN RETURN i // Return the index if the value is found ENDIF END FOR RETURN -1 // Return -1 if the value is not found END FUNCTION // Hardcoded Input list := [5, 10, 15, 20, 25, 30] searchTarget := 20 // Call the linear search function resultIndex := LinearSearchPlacementTest(list, searchTarget) // Output the result PRINT resultIndex
Check Solution
Ans: D
Explanation: The pseudocode implements a linear search algorithm. It iterates through the `searchList` element by element. In the hardcoded input, the `list` is [5, 10, 15, 20, 25, 30] and `searchTarget` is 20. The loop will compare `searchValue` (20) with each element in `list`. When `i` is 3, `searchList[3]` which is 20, will equal to `searchValue` (20), so the function returns 3.
Q. 3 Find the output of given peudocode
FUNCTION factorial(n): IF n == 0: RETURN 1 ELSE: RETURN n * factorial(n - 1) ENDIF // Hardcoded input input_number = 5 // Calculate the factorial result = factorial(input_number) // Output the result PRINT result
Check Solution
Ans: B
Explanation: The pseudocode calculates the factorial of a number. The `factorial` function uses recursion. When `n` is 0, it returns 1 (base case). Otherwise, it returns `n` multiplied by the factorial of `n-1`.
For an input of 5:
- factorial(5) = 5 * factorial(4)
- factorial(4) = 4 * factorial(3)
- factorial(3) = 3 * factorial(2)
- factorial(2) = 2 * factorial(1)
- factorial(1) = 1 * factorial(0)
- factorial(0) = 1
Therefore, factorial(5) = 5 * 4 * 3 * 2 * 1 = 120.
Q. 4 Find the output of given peudocode
BEGIN INPUT numerator = 10 INPUT denominator = 2 SET result = numerator DIVIDED BY denominator OUTPUT result END
Check Solution
Ans: D
Explanation: The pseudocode performs integer division. The numerator (10) is divided by the denominator (2). Integer division discards any remainder, resulting in the quotient. In this case, 10 divided by 2 is 5.
Q. 5 Find the output of given peudocode
FUNCTION FloorAfterMultiplicationTest() // Given values for testing num1 = 7.5 num2 = 3 // Calculate the product product = num1 * num2 // Apply the floor operation result = FLOOR(product) // Display the result DISPLAY result END FUNCTION // Call the function to execute the test CALL FloorAfterMultiplicationTest()
Check Solution
Ans: C
Explanation: The pseudocode calculates the product of num1 (7.5) and num2 (3), which is 22.5. The FLOOR function then rounds this product down to the nearest whole number.
Q. 6 Find the output of given peudocode
FUNCTION PlacementTestNestedLoops() DECLARE int rows = 3 DECLARE int cols = 4 DECLARE int matrix[rows][cols] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} } DECLARE int sum = 0 FOR i FROM 0 TO rows - 1 DO FOR j FROM 0 TO cols - 1 DO sum = sum + matrix[i][j] END FOR END FOR OUTPUT sum END FUNCTION CALL PlacementTestNestedLoops()
Check Solution
Ans: B
Explanation: The pseudocode initializes a 3x4 matrix and calculates the sum of all its elements using nested loops. The outer loop iterates through the rows (0 to 2), and the inner loop iterates through the columns (0 to 3). In each iteration of the inner loop, the value of the current matrix element is added to the sum. Therefore, the sum will be 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 = 78.
Q. 7 Find the output of given peudocode
FUNCTION cube(number) RETURN number * number * number END FUNCTION // Test case 1 SET input1 = 2 SET result1 = cube(input1) // Test case 2 SET input2 = 3 SET result2 = cube(input2) // Test case 3 SET input3 = 0 SET result3 = cube(input3) // Test case 4 SET input4 = -2 SET result4 = cube(input4)
Check Solution
Ans: C
Explanation: The pseudocode defines a function `cube` that calculates the cube of a given number (number * number * number). The test cases then call this function with different inputs and store the results.
Test case 1: input1 = 2, result1 = 2 * 2 * 2 = 8
Test case 2: input2 = 3, result2 = 3 * 3 * 3 = 27
Test case 3: input3 = 0, result3 = 0 * 0 * 0 = 0
Test case 4: input4 = -2, result4 = -2 * -2 * -2 = -8
Q. 8 Find the output of given peudocode
INPUT: An integer array named 'numbers' initialized with the values: [10, 20, 30, 40, 50] FOR each element in 'numbers' DO PRINT the element END FOR
Check Solution
Ans: A
Explanation: The pseudocode iterates through the 'numbers' array. In each iteration, it prints the current element's value. Therefore, it will print each number in the array sequentially.
Q. 9 Find the output of given peudocode
FUNCTION CountConsonants(inputString) // Initialize consonant count consonantCount = 0 // Define a string containing all consonants (lowercase) consonants = "bcdfghjklmnpqrstvwxyz" // Iterate through each character in the input string FOR each character IN inputString // Convert the character to lowercase for case-insensitive comparison lowercaseChar = LOWERCASE(character) // Check if the character is in the consonants string IF lowercaseChar IS IN consonants // Increment the consonant count consonantCount = consonantCount + 1 ENDIF ENDFOR // Return the final consonant count RETURN consonantCount ENDFUNCTION // Hardcoded input string with no consonants input = "aeiou" // Call the function with the hardcoded input result = CountConsonants(input) // Output the result DISPLAY result
Check Solution
Ans: B
Explanation: The pseudocode defines a function `CountConsonants` that takes a string as input and returns the number of consonants in the string. It initializes a `consonantCount` to 0. It defines a string `consonants` containing all lowercase consonants. The code then iterates through each character of the input string, converts it to lowercase, and checks if it is present in the `consonants` string. If it is a consonant, `consonantCount` is incremented. Finally, the function returns the `consonantCount`. The main part of the code calls this function with the input string "aeiou", which has no consonants.
Q. 10 Find the output of given peudocode
FUNCTION isPalindrome(text): IF length of text is 0 THEN RETURN TRUE ENDIF text_length = length of text FOR i FROM 0 TO text_length / 2 - 1 DO IF character at index i of text is NOT equal to character at index text_length - 1 - i of text THEN RETURN FALSE ENDIF ENDFOR RETURN TRUE ENDFUNCTION //Test Case: Empty String input_string = "" result = isPalindrome(input_string) OUTPUT result
Check Solution
Ans: B
Explanation: The pseudocode defines a function `isPalindrome` that checks if a given string `text` is a palindrome (reads the same forwards and backward).
1. **Base Case:** If the string is empty (length 0), it's considered a palindrome, and the function returns `TRUE`.
2. **Iteration:** The code iterates through the first half of the string using a `FOR` loop.
3. **Comparison:** In each iteration, it compares the character at index `i` with the character at index `text_length - 1 - i`. This effectively compares characters from both ends of the string, moving towards the middle.
4. **Non-Palindrome Check:** If any pair of characters doesn't match, the function immediately returns `FALSE`.
5. **Palindrome Confirmation:** If the loop completes without finding any mismatched characters, it means the string is a palindrome, and the function returns `TRUE`.
In the test case, the input string is `""` (empty string). The `isPalindrome` function is called with an empty string. The code first checks if the length of the string is 0 which is `TRUE`. The function immediately returns `TRUE`.
Next Topic: C programming MCQs with Answers for Placement Tests
Practice Aptitude Questions for Placements
Crack Placement Tests: Faster & Smarter
Adaptive Practice | Real Time Insights | Resume your Progress
