ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
The loop below multiplies i by 3 each time while i is less than 1000, starting from i = 1, and counts the iterations. What is printed?
The loop below multiplies i by 3 each time while i is less than 1000, starting from i = 1, and counts the iterations. What is printed?
i = 1; count = 0
while i < 1000
i = i * 3; count = count + 1
end while
Print count- A. 7
- B. 333
- C. 10
- D. 5
Correct answer: A. 7
The values 1, 3, 9, 27, 81, 243 and 729 each start an iteration, which is 7 iterations, and 2187 is past 1000. Adding 3 instead of multiplying gives 333 iterations, doubling gives 10, and stopping at 100 gives 5.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- A nested loop counts the pairs (i, j) with 1 <= j <= i <= 6 that also satisfy i + j even. The procedure prints the count. What is printed?…
- The procedure below counts the numbers i from 1 to 100 whose binary form has exactly three 1 bits (bits are counted by repeatedly taking n…
- A string of parentheses "(()())((" is scanned by the procedure below. The variable open counts unmatched '(' at the end, and the procedure…
- The procedure below scans the string "abracadabra" from left to right and counts how many letters are repeats of an earlier letter (a…
- A string is a rotation of another when it can be obtained by moving some prefix to the end. The procedure below checks whether…
- The array [1, 2, 3, 4, 5, 6, 7] is shifted left by 2 positions by the procedure below: the element at index k is placed at index (k - 2)…