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

public class Inc {
	static int x = 0;
	static boolean ok = true;
	static boolean f() {
		x++; 
		return (ok = !ok);
	}
	
	public static void main(String[] args) {
		if (f()||f()|f()) {
			for (int i=0; i<x; i++)
				System.out.println("Hello");
		}
	}
}
Explanation
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.
Obviously,

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

Obviously :)

2018 Nov 30, 5:49:35 PM

Java
Java Quiz
Start Quiz

or Read more about Java Quiz

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback