ProgrammingHardCapgemini · Infosys pattern
What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
Integer n = 16, c = 0, i = n
while (i >= 1)
for (each j from 0 to i - 1)
c = c + 1
end for
i = i / 2
end while
Print c- A. 31
- B. 32
- C. 16
- D. 15
Correct answer: A. 31
i takes values 16, 8, 4, 2, 1 (then i=0 stops the loop), and the inner loop runs i times each pass: 16+8+4+2+1 = 31.
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 n = 1024, c = 0 while…
- What is the time complexity of the following pseudocode, in terms of n, where the array has n elements? for (each i from 0 to n - 1) for…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[6] = {5, 2, 9, 1, 5, 6} for (each i from 1…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[5] = {2, 4, 6, 8, 10} Integer prefix[6]…
- What will be the output of the following pseudocode? Integer moves = 0 Function hanoi(Integer n) if (n == 0) return end if hanoi(n - 1)…
- What will be the output of the following pseudocode? Function isPalindrome(String s) Integer i = 0, j = length(s) - 1 while (i < j) if…