Question 1
EasyWhat will be the output of the following pseudocode?
Function f(Integer n)
if (n <= 1)
return 1
end if
return n * f(n - 1)
End Function
Print f(5)
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Pseudocode and Output Prediction
What will be the output of the following pseudocode?
Function f(Integer n)
if (n <= 1)
return 1
end if
return n * f(n - 1)
End Function
Print f(5)
24
5
120
15
Answer: C
f computes the factorial: 5 × 4 × 3 × 2 × 1 = 120.
What will be the output of the following pseudocode?
Function add(Integer a, Integer b)
return a + b
End Function
Print add(3, add(4, 5))
12
9
7
345
Answer: A
The inner call gives 9, then add(3, 9) = 12.
What will be the output of the following pseudocode?
Function square(Integer x)
return x * x
End Function
Print square(3) + square(4)
7
12
49
25
Answer: D
square(3) = 9 and square(4) = 16, so the sum is 25.
What will be the output of the following pseudocode?
Function s(Integer n)
if (n == 0)
return 0
end if
return (n mod 10) + s(n / 10) // integer division
End Function
Print s(472)
274
7
13
472
Answer: C
It adds the digits recursively: 2 + 7 + 4 = 13.
What will be the output of the following pseudocode?
Function max(Integer a, Integer b)
if (a > b)
return a
end if
return b
End Function
Print max(max(3, 9), 5)
9
3
17
5
Answer: A
max(3, 9) = 9, then max(9, 5) = 9.
What will be the output of the following pseudocode?
Function change(Integer x)
x = x + 10
End Function
Integer a = 5
change(a)
Print a
15
0
5
10
Answer: C
Integers are passed by value: the function changes its own copy, so a stays 5.
What will be the output of the following pseudocode?
Function p(Integer b, Integer e)
if (e == 0)
return 1
end if
return b * p(b, e - 1)
End Function
Print p(3, 4)
12
81
27
64
Answer: B
p computes b to the power e: 3⁴ = 81.
What will be the output of the following pseudocode?
Function square(Integer n)
return n * n
End Function
Print square(3) + square(4)
7
25
49
14
Answer: B
square(3) = 9 and square(4) = 16, so the sum is 25.
What will be the output of the following pseudocode?
Function f(Integer n)
if (n <= 1)
return 1
end if
return n * f(n - 1)
End Function
Print f(4)
16
120
24
10
Answer: C
f is the factorial: 4 × 3 × 2 × 1 = 24.
What will be the output of the following pseudocode?
Function sumTo(Integer n)
if (n == 0)
return 0
end if
return n + sumTo(n - 1)
End Function
Print sumTo(6)
15
21
6
720
Answer: B
6 + 5 + 4 + 3 + 2 + 1 + 0 = 21.