ProgrammingModerateCognizant GenC · Capgemini
Consider the function. f(n): if n <= 1 return n if n mod 2 == 0 return f(n / 2) + 1 return 2 * f(n - 1) + 1 What is f(13)?
- A. 14
- B. 15
- C. 16
- D. 13
Correct answer: B. 15
f(2)=2, f(3)=2*2+1=5, f(6)=6, f(12)=7, f(13)=2*7+1=15.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Consider the pseudocode. sum = 0 for i = 1 to 15: n = i while n != 0: n = n AND (n - 1) sum = sum + 1 print sum What is printed?
- Consider the pseudocode. s = 0 for i = 1 to 7: if i mod 3 == 0: continue j = i while j > 0: if j mod 4 == 0: break s = s + j j = j - 2…
- 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…
- 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 +…