ProgrammingHardTCS NQT · Accenture
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer n = 5, spaces = 0, stars = 0
for (each i from 1 to n)
for (each j from 1 to n - i) spaces = spaces + 1
for (each k from 1 to 2 * i - 1) stars = stars + 1
end for
Print spaces, stars- A. 10 25
- B. 15 25
- C. 10 15
- D. 10 30
Correct answer: A. 10 25
This is the pyramid pattern. Spaces: 4 + 3 + 2 + 1 + 0 = 10. Stars: 1 + 3 + 5 + 7 + 9 = 25 (the sum of the first 5 odd numbers is 5²). Output 10 25.
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 NOT, & is AND, | is OR, >> is right shift) Integer x = 11 Integer a =…
- What will be the output of the following pseudocode? (^ is XOR, & is AND) Integer a = 10, b = 20, x, flips = 0 x = a ^ b while (x > 0) x =…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function f(Integer n) if (n <=…
- What will be the output of the following pseudocode? Integer calls = 0 Procedure f(Integer n) calls = calls + 1 for (each i from 1 to n…
- What will be the output of the following pseudocode? Integer t[0..10] t[0] = 0 t[1] = 1 t[2] = 1 for (each i from 3 to 10) t[i] = t[i - 1]…
- What will be the output of the following pseudocode? (strings are indexed from 0) String s = "{[()]}([)]" Stack st = empty Integer bad =…