How many times will the word "Hello" be printed?

public class Inc {
	static int x = 0;
	static boolean ok = true;
	static boolean f() {
		return (ok = !ok);
	public static void main(String[] args) {
		if (f()||f()|f()) {
			for (int i=0; i<x; i++)
The code flow is as follows: The if statement calls the f() method first time, x becomes 1, ok = !ok evaluates to ok = !true, f() returns false. Since the first argument passed to || is false, the compiler tries to evaluate the second argument, which is f() | f() (| is the bitwise inclusive or operator).
The | operator calls the f() method twice, x incremented twice will become 3, the second f() method call will return true and the third call will return false.
Consequently if (f()||f()|f()) evaluates to if ( false || true | false) . true | false = true. false || true = true. x = 3.

for (int i=0; i<3; i++)
will print "Hello" three times.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback