ProgrammingVery hardTCS NQT · Accenture
Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn = Fn-1 + Fn-2. The procedure below computes F50 by iteration and then prints F50 mod 10 (the last digit). What is printed?
Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn = Fn-1 + Fn-2. The procedure below computes F50 by iteration and then prints F50 mod 10 (the last digit). What is printed?
a = 0; b = 1 repeat 50 times: (a, b) = (b, a + b) Print a mod 10
- A. 9
- B. 4
- C. 12586269025
- D. 5
Correct answer: D. 5
F50 = 12586269025, whose last digit is 5. The loop stopping at F49 or F51 gives last digits 9 or 4, and printing the full value without the mod gives 12586269025.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- For a = 240 and b = 46, the extended Euclid procedure below keeps x and y so that ax + by equals the gcd. What is the value of x printed…
- Array a = [-2, 1, -3, 4, -1, 2, 1, -5, 4]. Kadane's algorithm below finds the maximum sum of a non-empty contiguous subarray. What is…
- Array a = [3, 4, 1, 2, 4, 2, 1, 3] and target K = 7. The procedure below counts contiguous subarrays whose sum is exactly K, using prefix…
- Insertion sort on the array [5, 2, 4, 6, 1, 3] shifts an element one place right each time it moves past a larger element. The procedure…
- Array a = [3, 8, 7, 2, 7]. A linear scan returns the 0-based index of the first occurrence of 7. What is printed? for i = 0 to n - 1 if…
- Binary search for the value 13 in the sorted array [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] uses lo = 0, hi = n - 1 and mid = (lo + hi) div 2…