Technical Pseudocode Assessment
10 Computer Science Logic & Algorithmic Questions
1. What will be printed when Main() executes?
CLASS Parent:
FUNCTION Greet():
PRINT "Hello"
CLASS Child EXTENDS Parent:
FUNCTION Greet():
PRINT "Hi"
FUNCTION Main():
obj = NEW Child()
obj.Greet()
Child overrides the
Greet method of Parent, so invoking obj.Greet() executes the overridden implementation and prints “Hi”.
2. How many total comparisons will the loop make to search for target = 16 in array arr = [4, 8, 15, 16, 23, 42]?
FUNCTION LinearSearch(arr, target):
FOR i FROM 0 TO LENGTH(arr) - 1 DO:
IF arr[i] == target THEN:
RETURN i
END IF
END FOR
RETURN -1
Linear search checks index 0 (val 4), index 1 (val 8), index 2 (val 15), and index 3 (val 16 == target). That is a total of 4 comparisons.
3. What integer value is returned after executing BitwiseTest()?
FUNCTION BitwiseTest():
A = 29 // Binary: 11101
B = 14 // Binary: 01110
result = (A & B) | (A ^ B)
RETURN result
A & B = 29 & 14 = 12 (01100). A ^ B = 29 ^ 14 = 19 (10011). 12 | 19 = 31 (11111). Note that (A & B) | (A ^ B) is identity-equivalent to A | B.
4. What integer value is returned when calling Calculate(4)?
FUNCTION Calculate(n):
IF n <= 1 THEN:
RETURN 2
ELSE:
RETURN Calculate(n - 1) + Calculate(n - 2) + 1
END IF
Calculate(0) = Calculate(1) = 2.
Calculate(2) = Calculate(1) + Calculate(0) + 1 = 2 + 2 + 1 = 5.
Calculate(3) = Calculate(2) + Calculate(1) + 1 = 5 + 2 + 1 = 8.
Calculate(4) = Calculate(3) + Calculate(2) + 1 = 8 + 5 + 1 = 14.
5. What is the final value of sum returned by the function?
FUNCTION ProcessArray():
ARRAY arr = [3, 8, 2, 11, 6, 14]
sum = 0
FOR i FROM 0 TO LENGTH(arr) - 1 DO:
IF arr[i] % 2 == 0 THEN:
sum = sum + (arr[i] * 2)
ELSE:
sum = sum - arr[i]
END IF
END FOR
RETURN sum
Iterating elements:
• 3 (odd): 0 – 3 = -3
• 8 (even): -3 + (8*2) = 13
• 2 (even): 13 + (2*2) = 17
• 11 (odd): 17 – 11 = 6
• 6 (even): 6 + (6*2) = 18
• 14 (even): 18 + (14*2) = 46.
6. What integer value is printed by CombinedOperations()?
FUNCTION CombinedOperations():
CREATE STACK s
CREATE QUEUE q
ENQUEUE 10, 20, 30 INTO q
PUSH DEQUEUE(q) ONTO s
PUSH DEQUEUE(q) ONTO s
ENQUEUE POP(s) INTO q
PUSH 40 ONTO s
PRINT TOP(s) + FRONT(q)
1. Queue q = [10, 20, 30] (front is 10).
2. Dequeue 10, push onto s: s = [10]. q = [20, 30].
3. Dequeue 20, push onto s: s = [10, 20]. q = [30].
4. Pop 20, enqueue into q: s = [10]. q = [30, 20] (front is 30).
5. Push 40 onto s: s = [10, 40] (top is 40).
6. TOP(s) + FRONT(q) = 40 + 30 = 70.
7. How many total times will the statement count = count + 1 execute?
FUNCTION NestedLoop():
count = 0
FOR i FROM 1 TO 3 DO:
FOR j FROM 1 TO 4 DO:
count = count + 1
END FOR
END FOR
RETURN count
The outer loop runs 3 times (i = 1, 2, 3). For each iteration of the outer loop, the inner loop runs 4 times (j = 1, 2, 3, 4). Total executions = 3 * 4 = 12.
8. In a binary search for target = 16 on sorted array [2, 5, 8, 12, 16, 23, 38], what is the value of arr[mid] on the very first comparison?
FUNCTION BinarySearch(arr, target):
low = 0
high = LENGTH(arr) - 1
WHILE low <= high DO:
mid = (low + high) / 2
IF arr[mid] == target THEN RETURN mid
IF arr[mid] < target THEN low = mid + 1
ELSE high = mid - 1
END WHILE
Array length is 7 (indices 0 to 6). Initial
low = 0 and high = 6.First mid calculation:
mid = (0 + 6) / 2 = 3.The value at index 3 is
arr[3] = 12.
9. What value is returned when calling SumEvens([1, 2, 3, 4, 5, 6])?
FUNCTION SumEvens(numbers):
total = 0
FOR EACH num IN numbers DO:
IF num % 2 == 0 THEN:
total = total + num
END IF
END FOR
RETURN total
The function filters even numbers from the list: 2, 4, and 6.
Total sum = 2 + 4 + 6 = 12.
10. What integer value does Fibonacci(5) return?
FUNCTION Fibonacci(n):
ARRAY dp OF SIZE n + 1
dp[0] = 0
dp[1] = 1
FOR i FROM 2 TO n DO:
dp[i] = dp[i - 1] + dp[i - 2]
END FOR
RETURN dp[n]
dp array values:
• dp[0] = 0
• dp[1] = 1
• dp[2] = 0 + 1 = 1
• dp[3] = 1 + 1 = 2
• dp[4] = 1 + 2 = 3
• dp[5] = 2 + 3 = 5.
