ProgrammingVery hardTCS NQT · Infosys · Cognizant GenC
Consider the pseudocode. global calls = 0 f(n): calls = calls + 1 if n <= 2 return n return f(n - 1) + f(n - 3) r = f(7) print r, calls What is printed?
- A. 10 15
- B. 8 17
- C. 10 19
- D. 10 17
Correct answer: D. 10 17
f(1)=1, f(2)=2, f(3)=f(2)+f(0)=2, f(4)=f(3)+f(1)=3, f(5)=f(4)+f(2)=5, f(6)=f(5)+f(3)=7, f(7)=f(6)+f(4)=10. The call count c(n) = 1 + c(n-1) + c(n-3) with c(n)=1 for n<=2 gives c(7) = 17.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Integers are passed by value and arrays by reference (the reference itself is copied). g(a, x): a[0] = a[0] + x x = x 2 a = [9, 9] a[1] =…
- Consider this Java-style code. int a = 5, b = 0, c = 0; if (a > 3 || ++b > 0) c += 1; if (a < 3 && ++b > 0) c += 10; if ((a > 3) | (++b >…
- Consider the pseudocode (letters a..z are 0..25; mod wraps within the alphabet). r = "" for i = 0 to length(s) - 1: r = r +…
- What will be the output of the following pseudocode? (Print writes its value followed by a space.) Function f(n) If n <= 0 Then Return 0…
- What will be the output of the following pseudocode? (Parameters marked ref are passed by reference; the others by value.) Procedure…
- What will be the output of the following pseudocode? (AND has higher precedence than OR, and && style short-circuit evaluation is used…