ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
The Fibonacci sequence 1, 1, 2, 3, 5, ... is generated by the loop below, and the procedure prints how many terms are less than or equal to 100. What is printed?
The Fibonacci sequence 1, 1, 2, 3, 5, ... is generated by the loop below, and the procedure prints how many terms are less than or equal to 100. What is printed?
a = 1; b = 1; count = 0
while a <= 100
count = count + 1
(a, b) = (b, a + b)
end while
Print count- A. 12
- B. 100
- C. 11
- D. 10
Correct answer: C. 11
The terms counted are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 and 89, which is 11 terms. Starting from 0 adds the term 0, a broken update of the pair gives a different sequence, and testing the second variable shifts the count.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The string "aabcccdddd" is compressed by the procedure below, which replaces each run of equal letters by the letter followed by the run…
- The procedure below scans the word "leetcode" from the left and prints the index of the first letter that occurs exactly once in the word…
- The procedure below counts the integers n with 1 <= n <= 60 that are not divisible by 2, 3 or 5 (coprime to 30). What is printed? count =…
- The procedure below counts the pairs i < j in the array [3, 1, 4, 1, 5] whose sum is even. What is printed? count = 0 for i = 0 to n - 2…
- The procedure below collects x mod 5 for every x from 1 to 20 into a set and prints the size of the set. What is printed? seen = empty set…
- Array a = [1, 1, 1, 1] (0-based). From index i you may jump forward by at most a[i] positions. The greedy below counts the minimum number…