ProgrammingHardInfosys · Cognizant GenC · Capgemini
Consider this Java-style code. int a = 5, b = 0, c = 0; if (a > 3 || ++b > 0) c += 1; if (a < 3 && ++b > 0) c += 10; if ((a > 3) | (++b > 0)) c += 100; System.out.println(b + " " + c); What is printed?
- A. 1 101
- B. 0 101
- C. 2 101
- D. 3 111
Correct answer: A. 1 101
|| short-circuits (a > 3 is true) so b stays 0 and c becomes 1. && short-circuits (a < 3 is false), no change. The non-short-circuit | evaluates ++b, making b = 1, and the condition is true, so c = 101.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Consider the pseudocode (letters a..z are 0..25; mod wraps within the alphabet). r = "" for i = 0 to length(s) - 1: r = r +…
- What will be the output of the following pseudocode? (Print writes its value followed by a space.) Function f(n) If n <= 0 Then Return 0…
- What will be the output of the following pseudocode? (Parameters marked ref are passed by reference; the others by value.) Procedure…
- What will be the output of the following pseudocode? (AND has higher precedence than OR, and && style short-circuit evaluation is used…
- What will be the output of the following pseudocode? (^ is bitwise XOR, & is bitwise AND, >> is a right shift; all values are non-negative…
- What will be the output of the following pseudocode? (break leaves only the innermost loop; continue skips to the next iteration of the…