ProgrammingHardTCS NQT · Accenture 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 = 17, i = 2
Boolean isPrime = true
while (i * i <= n)
if (n mod i == 0)
isPrime = false
break
end if
i = i + 1
end while
Print isPrime, i- A. false 5
- B. true 4
- C. true 17
- D. true 5
Correct answer: D. true 5
17 is prime, so the if-branch never triggers; the loop simply keeps incrementing i while i*i<=17 (i.e. i=2,3,4), and stops when i=5 since 5*5=25>17. isPrime stays true, i ends at 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? (Arrays are indexed from 0.) Integer arr[8] = {3, 1, 4, 1, 5, 9, 2, 6} Integer n = 8…
- What will be the output of the following pseudocode? Function countdownSum(Integer n) if (n == 0) return 0 end if return n +…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 16, c = 0, i = n…
- 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…