ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode? (Arrays are indexed from 0.)
What will be the output of the following pseudocode? (Arrays are indexed from 0.)
Integer arr[6] = {4, 8, 15, 16, 23, 42}
Integer target = 23, steps = 0, found = -1
for (each i from 0 to length(arr) - 1)
steps = steps + 1
if (arr[i] == target)
found = i
break
end if
end for
Print found, steps- A. 4 4
- B. 4 5
- C. 3 4
- D. 4 6
Correct answer: B. 4 5
Linear search scans from index 0; it checks indices 0,1,2,3 (values 4,8,15,16, none matching) before finding 23 at index 4 on the 5th check, so found=4 and steps=5.
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.) Function countDigits(Integer…
- What will be the output of the following pseudocode (Pascal's identity for combinations)? Function C(Integer n, Integer r) if (r == 0 or r…
- What will be the output of the following pseudocode (Armstrong number check)? Function isArmstrong(Integer n) String s = toString(n)…
- 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…
- 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…