Pseudocode Practice Questions
10 Practice Questions with Step-by-Step Logic Explanations
Question 1
Bitwise OperationWhat is the output of the following pseudocode for a = 12 and b = 25?
Integer a, b, c
Set a = 12, b = 25
c = a ^ b
c = c ^ a
Print c
Correct Answer: 25
Explanation: The XOR operator (^) has a property where (a ^ b) ^ a = b. First, c gets 12 ^ 25. Next, XORing c with a cancels out a, leaving the value of b (25).
Question 2
ArrayWhat will be displayed after executing the code block below?
Integer arr[] = {2, 4, 6, 8, 10}
Integer sum = 0
For i = 0 to 4 Step 2
sum = sum + arr[i]
End For
Print sum
Correct Answer: 18
Explanation: The loop iterates with a step of 2, accessing indices 0, 2, 4:
arr[0] = 2, arr[2] = 6, arr[4] = 10.
Sum = 2 + 6 + 10 = 18.
Question 3
Bitwise OperationWhat is the output of the following right-shift pseudocode?
Integer x = 40
Integer result
result = x >> 3
Print result
Correct Answer: 5
Explanation: Bitwise right-shift by n bits divides the number by $2^n$.
Here, 40 >> 3 is equivalent to $40 / 2^3 = 40 / 8 = 5$.
Question 4
StringWhat will be printed by the following string manipulation code?
String str = "HCLTech"
String rev = ""
Integer len = length(str)
For i = len - 1 DownTo 0
rev = rev + str[i]
End For
Print rev
Correct Answer: hceTLCH
Explanation: The loop iterates from the last character index down to 0, constructing the reversed string character by character.
Question 5
SearchingHow many total comparisons occur when searching for key = 7 in array [1, 3, 5, 7, 9, 11, 13] using Binary Search?
Integer arr[] = {1, 3, 5, 7, 9, 11, 13}
Integer low = 0, high = 6, key = 7
While low <= high
mid = (low + high) / 2
If arr[mid] == key Then
Return mid
Else If arr[mid] < key Then
low = mid + 1
Else
high = mid - 1
End If
End While
Correct Answer: 1 Comparison
Explanation:
1. low = 0, high = 6 $\rightarrow$ mid = (0 + 6) / 2 = 3.
2. arr[3] = 7, which matches key = 7 on the very first attempt!
Question 6
SortingWhat will be the array state after Pass 1 of Bubble Sort on [5, 1, 4, 2, 8]?
Integer arr[] = {5, 1, 4, 2, 8}
For i = 0 to 3
If arr[i] > arr[i+1] Then
Swap(arr[i], arr[i+1])
End If
End For
Correct Answer: [1, 4, 2, 5, 8]
Step-by-Step Pass 1 Swaps:
- Swap 5 & 1 $\rightarrow$ [1, 5, 4, 2, 8]
- Swap 5 & 4 $\rightarrow$ [1, 4, 5, 2, 8]
- Swap 5 & 2 $\rightarrow$ [1, 4, 2, 5, 8]
- Compare 5 & 8 $\rightarrow$ No swap.
Question 7
Bitwise OperationWhat does this pseudocode check for integer n?
Integer n = 16
If (n > 0) AND ((n & (n - 1)) == 0) Then
Print "Condition Met"
Else
Print "Condition Failed"
End If
Correct Answer: Checks if n is a Power of 2 (Outputs: Condition Met).
Explanation: Numbers that are powers of 2 (e.g., 16 = 10000_2) have only one set bit. Subtracting 1 gives 15 (01111_2). The bitwise AND (10000 & 01111) results in 0.
Question 8
Array & LogicWhat is the output of the code snippet below?
Integer A[] = {1, 2, 3, 4, 5}
Integer N = 5
For i = 0 to N/2 - 1
Integer temp = A[i]
A[i] = A[N - 1 - i]
A[N - 1 - i] = temp
End For
Print A[1]
Correct Answer: 4
Explanation: This logic reverses the array in-place. The reversed array becomes [5, 4, 3, 2, 1]. Index 1 contains 4.
Question 9
StringWhat will be printed by this character condition pseudocode?
String s = "a1b2c3"
Integer count = 0
For i = 0 to length(s) - 1
If isDigit(s[i]) Then
count = count + (s[i] - '0')
End If
End For
Print count
Correct Answer: 6
Explanation: The code filters digits (1, 2, 3) and calculates their numerical sum: 1 + 2 + 3 = 6.
Question 10
Sorting & ArrayGiven Selection Sort, how many swaps are executed to sort [3, 1, 2] in ascending order?
Integer arr[] = {3, 1, 2}
For i = 0 to 1
minIdx = i
For j = i + 1 to 2
If arr[j] < arr[minIdx] Then
minIdx = j
End If
End For
If minIdx != i Then
Swap(arr[i], arr[minIdx])
End If
End For
Correct Answer: 2 Swaps
Explanation:
- i = 0: Minimum in [3, 1, 2] is 1 (index 1). Swap 3 & 1 $\rightarrow$ [1, 3, 2]. (Swap 1)
- i = 1: Minimum in [3, 2] is 2 (index 2). Swap 3 & 2 $\rightarrow$ [1, 2, 3]. (Swap 2)
Arjun Series Mock Test for All Assessments
Prepare for upcoming off-campus placement drives with full-length mock assessment tests, section-wise practice sets, and detailed performance analysis.
Enroll / Start Test Now