What is the result of the following code execution?

1: class Foo {
2:   public static void main(String[] args) {
3:     Integer i = 42;
4:     String s = (i < 40) ? "life" : (i > 50) ? "universe" : "everything";
5:     System.out.print(s);
6:   }
7: }
Explanation
Ternary operator does this, in a way:

if (i < 40) { //false
    return "life";
} else if (i > 50) { //false
    return "universe"
} else { 
    return "everything"
}

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes