ProgrammingHardSSC CGL Tier 2 · IBPS PO Mains
For i from 0 to 30 inclusive, count the values of i with ii <= 30. What is printed?
For i from 0 to 30 inclusive, count the values of i with i*i <= 30. What is printed?
count = 0
for i = 0 to 30
if i * i <= 30 then count = count + 1
end for
Print count- A. 31
- B. 7
- C. 5
- D. 6
Correct answer: D. 6
The squares 0, 1, 4, 9, 16 and 25 are at most 30, so six values of i count, including i = 0. Dropping i = 0 gives 5, comparing against 36 adds a value, and comparing i with 30 instead of i squared counts every value.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Array [4, 1, 3, 2] is sorted by selection sort: for each position i, find the minimum of the unsorted part and swap it into position i…
- Sorted array a = [1, 2, 3, 5, 8, 9] and limit L = 10. Count the pairs of distinct positions i < j with a[i] + a[j] <= L using two…
- The string s = "aaabbbcc" is cut down so that no two adjacent letters are equal, by deleting characters. The greedy keeps a character when…
- A binary string s = "1100101" is to be made alternating. Two targets are possible: "0101010..." and "1010101...", each the same length as…
- Count how many copies of the word BALLOON can be formed from the letters of "lloolldnaubbalo" (each letter used at most once per copy)…
- Children have ratings r = [1, 0, 2, 2, 1]. Each child gets at least one candy, and a child with a higher rating than a neighbour gets more…