ProgrammingModerateTCS NQT · Accenture
What will be the output of the following pseudocode? (All variables are integers, so / is integer division; , / and mod have equal precedence)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division; *, / and mod have equal precedence)
Integer a = 7, b = 3, c = 2 Print a - b * c / 4 + a mod b * c
- A. 8
- B. 0
- C. 9
- D. 7.5
Correct answer: A. 8
*, / and mod share one level and go left to right: b * c / 4 = 6 / 4 = 1 and a mod b * c = 1 * 2 = 2, so 7 − 1 + 2 = 8. Doing c / 4 first gives 9, real division gives 7.5, and ignoring precedence entirely, ((((a − b) * c) / 4 + a) mod b) * c, gives 0.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (& is bitwise AND, >> is right shift) Integer s = 0, x, t for (each i from 1 to 16) x…
- What will be the output of the following pseudocode? Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} for (each i from 0 to 2) for…
- What will be the output of the following pseudocode? Integer n = 0, c = 0 do c = c + 1 n = n + 3 while (n mod 5 != 0) Print c, n
- What will be the output of the following pseudocode? Integer cat[0..6] cat[0] = 1 for (each i from 1 to 6) cat[i] = 0 for (each j from 0…
- What will be the output of the following pseudocode? Integer a[6] = {3, 1, 2, 6, 4, 5}, swaps = 0, m, n = 6 for (each i from 0 to n - 2) m…
- What will be the output of the following pseudocode? Integer a[6] = {5, 2, 4, 6, 1, 3}, shifts = 0, key, j for (each i from 1 to 5) key =…