ProgrammingVery hardAccenture · TCS · Infosys
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function isEven(Integer n)
if (n == 0)
return true
end if
return isOdd(n - 1)
end Function
Function isOdd(Integer n)
if (n == 0)
return false
end if
return isEven(n - 1)
end Function
Print isEven(58)- A. true
- B. 0
- C. error
- D. false
Correct answer: A. true
isEven and isOdd call each other, each decrementing n by 1 and flipping the base case. The recursion bottoms out at n = 0, and isEven(58) = true because 58 is even.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer x, y Set x = 17, y =…
- What will be the output of the following pseudocode? Integer s = 0 for (each i from 1 to 10) if (i mod 2 == 0) s = s + i end if end for…
- What will be the output of the following pseudocode? Integer a = 6, b = 4 a = a + b b = a - b a = a - b Print a, b
- What will be the output of the following pseudocode? Integer n = 5, f = 1 while (n > 1) f = f n n = n - 1 end while Print f
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[4] = {4, 9, 2, 7} Integer m = arr[0] for…
- What will be the output of the following pseudocode? Integer x = 10 if (x > 5) x = x 2 else x = x + 2 end if if (x > 15) x = x - 3 end if…