ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer c = 0
for (each i from 1 to 3)
for (each j from 1 to 3)
if (j == 2)
break
end if
c = c + 1
end for
end for
Print c- A. 9
- B. 6
- C. 3
- D. 0
Correct answer: C. 3
For every outer i, the inner loop increments c only once (at j=1) before breaking when j reaches 2. With 3 outer iterations, c ends at 3.
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 (Kadane's maximum subarray sum)? (Arrays are indexed from 0.) Integer arr[9] = {-2, 1…
- What is the time complexity of binary search on a sorted array of n elements? Function binarySearch(Array arr, Integer target) Integer lo…
- What is the worst-case time complexity of the following pseudocode (bubble sort) on an array of n elements? for (each i from 0 to n - 2)…
- What is the time complexity of the following recursive pseudocode (computing n choose k via Pascal's identity), in the worst case, as a…
- What is the space complexity (auxiliary stack space) of the following recursive factorial function for input n? Function fact(Integer n)…
- What will be the output of the following pseudocode (anagram check via sorting)? Function isAnagram(String a, String b) return…