ProgrammingVery hardCAT · MBA placement
The procedure below sums the integers from 1 to 100 that are NOT divisible by 2 and NOT divisible by 3. What is printed?
The procedure below sums the integers from 1 to 100 that are NOT divisible by 2 and NOT divisible by 3. What is printed?
total = 0
for n = 1 to 100
if n mod 2 != 0 and n mod 3 != 0 then total = total + n
end for
Print total- A. 1633
- B. 3417
- C. 33
- D. 4234
Correct answer: A. 1633
The numbers not divisible by 2 or 3 are 1, 5, 7, 11, 13 and so on, and their sum is 1633. Summing the excluded numbers gives 3417, counting instead of summing gives 33, and using or instead of and gives 4234.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Count the numbers n with 1 <= n <= 100 such that both n and n + 2 are prime (twin primes, lower member n). The procedure below checks…
- The procedure below counts the odd divisors of 1000 by testing every d from 1 to 1000. What is printed? count = 0 for d = 1 to 1000 if…
- 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…
- 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…