Question 61
HardWhat will be the output of the following pseudocode?
Function f(Integer n)
if (n <= 0)
return 0
end if
return f(n - 1) + f(n - 2) + 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 <= 0)
return 0
end if
return f(n - 1) + f(n - 2) + 1
End Function
Print f(5)
8
12
13
15
Sign up free to see the answer and a step-by-step explanation.
What is the output of this pseudocode?
Function g(Integer a, Integer b)
if (b == 0)
return a
end if
return g(b, a mod b)
End Function
Function l(Integer a, Integer b)
return a / g(a, b) * b
End Function
Print g(462, 1071), l(12, 18)
21 216
7 36
21 36
42 36
Sign up free to see the answer and a step-by-step explanation.
What will be printed by the following pseudocode?
Function p(Integer x, Integer n)
if (n == 0)
return 1
end if
Integer half = p(x, n / 2)
if (n mod 2 == 0)
return half * half
end if
return half * half * x
End Function
Print p(3, 13) mod 1000
387
159
323
13
Sign up free to see the answer and a step-by-step explanation.
What is the output of this pseudocode?
Function c(Integer n)
if (n == 1)
return 0
end if
if (n mod 2 == 0)
return 1 + c(n / 2)
end if
return 1 + c(3 * n + 1)
End Function
Print c(27) > 100, c(6)
true 6
false 8
false 6
true 8
Sign up free to see the answer and a step-by-step explanation.
What will be the output of the following pseudocode?
Integer depth = 0, maxDepth = 0
Function r(Integer n)
depth = depth + 1
if (depth > maxDepth) maxDepth = depth end if
if (n > 1)
r(n / 2)
r(n / 2)
end if
depth = depth - 1
End Function
r(16)
Print maxDepth
16
31
5
4
Sign up free to see the answer and a step-by-step explanation.
What will be the output of the following pseudocode?
Function s(Integer n, Integer k)
if (k == 0 or k == n)
return 1
end if
return s(n - 1, k - 1) + s(n - 1, k)
End Function
Integer total = 0, k
for (each k from 0 to 6)
total = total + s(6, k)
end for
Print total
128
64
32
21
Sign up free to see the answer and a step-by-step explanation.
What does the following pseudocode print?
Function mystery(Integer n)
if (n < 10)
return n
end if
return mystery(n / 10) * 10 + 0 * (n mod 10) + (n mod 10)
End Function
Print mystery(4096) + 4
6904
4096
4100
4
Sign up free to see the answer and a step-by-step explanation.
What does the following pseudocode print?
Function rev(Integer n, Integer acc)
if (n == 0)
return acc
end if
return rev(n / 10, acc * 10 + n mod 10)
End Function
Integer x = 12321
if (rev(x, 0) == x)
Print "palindrome", rev(1200, 0)
end if
palindrome 0021
palindrome 1200
palindrome 21
21
Sign up free to see the answer and a step-by-step explanation.
What is the output of this pseudocode?
Integer calls = 0
Function f(Integer n)
calls = calls + 1
if (n <= 2)
return 1
end if
return f(n - 1) + f(n - 3)
End Function
Integer v = f(7)
Print v, calls
9 17
13 17
9 13
6 11
Sign up free to see the answer and a step-by-step explanation.
What will be printed by the following pseudocode?
Function h(Integer n, Char a, Char b, Char c)
if (n == 0)
return 0
end if
return h(n - 1, a, c, b) + 1 + h(n - 1, c, b, a)
End Function
Print h(6, 'A', 'B', 'C')
32
63
64
36
Sign up free to see the answer and a step-by-step explanation.