Sample questions
Question 1Easy
Predict the output of the following pseudocode.
Function Integer f(Integer x)
if (x > 10)
return x - 10
End if
return x + 3
End function
Print f(f(15))
- 9
- 7
- 10
- 8
Answer: D. 8
Explanation
Apply f twice, choosing the branch each time: 8.
Step-by-step solution
- f(15): 15 is greater than 10, so f(15) = 15 − 10 = 5.
- f(5): 5 is not greater than 10, so f(5) = 5 + 3 = 8.
- The output is 8, the correct answer.
Why the other options are wrong
- A. 9 is wrong; f(5) = 8.
- B. 7 is wrong; f(5) adds 3 to 5, which is 8.
- C. 10 is wrong; f(5) = 8.
Tip: For nested calls, work out the inner call first and use its result as the argument of the outer call.
Question 2Easy
What is the output of the following pseudocode?
Function Integer f(Integer a, Integer b)
return a * 2 + b
End function
Print f(3, 7) + f(7, 3)
Printed values are separated by spaces.
- 32
- 31
- 30
- 29
Answer: C. 30
Explanation
f(a, b) = 2a + b, so f(3, 7) + f(7, 3) = 3(3 + 7) = 30.
Step-by-step solution
- f(a, b) = a × 2 + b.
- f(3, 7) = 6 + 7 = 13.
- f(7, 3) = 14 + 3 = 17.
- 13 + 17 = 30, the correct answer.
Why the other options are wrong
- A. 32 is wrong; the two results are 13 and 17, which sum to 30.
- B. 31 is wrong; 13 + 17 = 30.
- D. 29 is wrong; 13 + 17 = 30.
Tip: Work out each function call first, then add the results. In a × 2 + b, the first argument is doubled.
Question 3Easy
What will be printed by the following pseudocode?
Function Integer f(Integer n)
if (n == 1)
return 1
End if
return n * f(n - 1)
End function
Print f(7) / f(5)
/ denotes integer division (the fractional part is discarded).
- 41
- 43
- 0
- 42
Answer: D. 42
Explanation
f is the factorial, so f(7) / f(5) = 42.
Step-by-step solution
- f(7) = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040.
- f(5) = 5 × 4 × 3 × 2 × 1 = 120.
- 5040 ÷ 120 = 42 exactly, so the output is 42, the correct answer.
Why the other options are wrong
- A. 41 is wrong; 5040 ÷ 120 = 42 exactly.
- B. 43 is wrong; the division is exact at 42.
- C. 0 is wrong; 5040 is much bigger than 120, so the division is not 0.
Tip: For factorials, f(7) ÷ f(5) = 7 × 6 = 42, because the shared factors cancel.
Question 4Easy
What is the output of the following pseudocode?
Function Integer f(Integer x)
if (x > 10)
return x - 10
End if
return x + 3
End function
Print f(f(9))
- 1
- 3
- 2
- 4
Answer: C. 2
Explanation
Apply f twice, choosing the branch each time: 2.
Step-by-step solution
- f(9): 9 is not greater than 10, so f(9) = 9 + 3 = 12.
- f(12): 12 is greater than 10, so f(12) = 12 − 10 = 2.
- The output is 2, the correct answer.
Why the other options are wrong
- A. 1 is wrong; f(12) = 2.
- B. 3 is wrong; f(9) = 12 first, then f(12) = 2.
- D. 4 is wrong; f(12) = 2.
Tip: For nested calls, work out the inner call first and use its result as the argument of the outer call.
Question 5Medium
What will be the output of the following pseudocode?
Function Integer f(Integer n)
if (n <= 2)
return n
End if
return f(n - 1) * 2 - f(n - 2)
End function
Print f(7)
- 9
- 7
- 8
- 6
Answer: B. 7
Explanation
With f(1) = 1 and f(2) = 2, f(n) = 2f(n-1) - f(n-2) keeps adding the same difference, so f(n) = n:
f(7) = 7.
Step-by-step solution
- f(1)=1, f(2)=2.
- f(3)=2*2-1=3, f(4)=2*3-2=4, f(5)=5, f(6)=6, f(7)=7.
- The recurrence gives f(n)=n.
Why the other options are wrong
- A. 9 is wrong.
- C. 8 is wrong.
- D. 6 is f(6).
Question 6Medium
Predict the output of the following pseudocode.
Function Integer f(Integer x)
x = x * 2
return x + 1
End function
Integer a, b
Set a = 12
b = f(a)
Print a, b
Printed values are separated by spaces.
- 12 25
- 12 24
- 13 25
- 12 26
Answer: A. 12 25
Explanation
x is a copy of a (pass by value), so changing x inside f does not change a: a = 12 and b = 25.
Step-by-step solution
- A is set to 12.
- f(a) calls function with x = 12.
- x = 12 * 2 = 24.
- Return x + 1 = 24 + 1 = 25.
- b = 25, a remains 12.
- Printed values are 12 25.
Why the other options are wrong
- B. 12 24 is wrong; the function returns 24 plus 1, which is 25.
- C. 13 25 is wrong; a stays 12, not 13.
- D. 12 26 is wrong; 24 plus 1 gives 25, not 26.
Tip: Remember pass by value keeps original variable unchanged.
Question 7Medium
What is the output of the following pseudocode?
Function Integer f(Integer x, Integer y)
if (y == 0)
return 0
End if
if (y mod 2 == 0)
return f(x + x, y / 2)
End if
return x + f(x + x, y / 2)
End function
Print f(9, 13)
/ denotes integer division (the fractional part is discarded). Printed values are separated by spaces.
- 117
- 118
- 9
- 63
Answer: A. 117
Explanation
f multiplies by doubling x and halving y, adding x when y is odd: 9 * 13 = 117.
Step-by-step solution
- f(x, y) multiplies x by y: x doubles and y halves each time, and x is added whenever y is odd.
- f(9, 13): 13 is odd, so the result is 9 + f(18, 6).
- f(18, 6): 6 is even, so it is f(36, 3). Then f(36, 3) = 36 + f(72, 1), and f(72, 1) = 72 + 0.
- The added parts are 9, 36 and 72, which total 117, the correct answer.
Why the other options are wrong
- B. 118 is wrong; the added parts 9, 36 and 72 total 117.
- C. 9 is only the first added part; the odd steps also add 36 and 72.
- D. 63 is wrong; the function gives 9 × 13 = 117.
Tip: This is Russian peasant multiplication: halve one number, double the other, and add the doubled number when the halved one is odd.
Question 8Medium
What is the output of the following pseudocode?
Function Integer f(Integer n)
if (n == 0)
return 0
End if
return 1 + f(n / 3)
End function
Print f(80)
/ denotes integer division (the fractional part is discarded).
- 3
- 4
- 6
- 5
Answer: B. 4
Explanation
f counts how many times n can be divided by 3 (integer division) before reaching 0, i.e. the number of base-3 digits: 4.
Step-by-step solution
- f(n) adds 1 each time n is divided by 3 (integer division) until n becomes 0.
- 80 ÷ 3 gives 26, then 8, then 2, then 0.
- That is 4 divisions, each adding 1, so f(80) = 4, the correct answer.
Why the other options are wrong
- A. 3 is wrong; 80 needs four divisions by 3 to reach 0.
- C. 6 is wrong; 80 reaches 0 after four divisions.
- D. 5 is wrong; there are four divisions, not five.
Tip: Counting how many times you can divide by a base until you reach 0 gives the number of digits in that base. 80 is 2222 in base 3.
Question 9Hard
Predict the output of the following pseudocode.
Function Integer f(Integer n, Integer d)
if (d * d > n)
return 1
End if
if (n mod d == 0)
return 0
End if
return f(n, d + 1)
End function
Integer i, c
Set c = 0
for (each i from 2 to 100)
c = c + f(i, 2)
End for
Print c
- 26
- 25
- 29
- 99
Answer: B. 25
Explanation
f(n, 2) returns 1 exactly when n has no divisor d with d*d <= n, i.e. n is prime; c counts primes up to 100: 25.
Step-by-step solution
f(i,2) returns 1 when i is prime (trial division up to sqrt).c counts primes in 2..100.- There are 25 primes below 100.
Why the other options are wrong
- A. 26 overcounts.
- C. 29 is wrong.
- D. 99 counts all numbers.
Tip: 25 primes up to 100.
Question 10Hard
What will be printed by the following pseudocode?
Function Integer f(Integer a, Integer b)
if (a == 0)
return b
End if
return f(b mod a, a)
End function
Function Integer g(Integer a, Integer b)
return a / f(a, b) * b
End function
Print f(7, 9), g(7, 9)
/ denotes integer division (the fractional part is discarded). Printed values are separated by spaces.
- 7 9
- 1 64
- 1 63
- 1 0
Answer: C. 1 63
Explanation
f is the GCD (Euclid with the arguments swapped) and g is the LCM computed as a / gcd * b: 1 63.
Step-by-step solution
- F is Euclid's method. f(7, 9) calls f(9 mod 7, 7), which is f(2, 7).
- f(2, 7) calls f(7 mod 2, 2), which is f(1, 2), and then f(0, 1).
- f(0, 1) returns 1, so the gcd is 1.
- g(7, 9) = 7 ÷ 1 × 9 = 63. The output is 1 63, the correct answer.
Why the other options are wrong
- A. 7 9 is wrong; the gcd of 7 and 9 is 1, not 7.
- B. 1 64 is wrong; g is 7 ÷ 1 × 9 = 63, not 64.
- D. 1 0 is wrong; g is 7 ÷ 1 × 9 = 63, not 0.
Tip: The LCM of two numbers is a ÷ gcd × b. When the gcd is 1, the LCM is the product of the two numbers.
Practise all 86 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →