Java puzzle 11: Operators

<< Java Puzzles   Green = Easy,  Blue = Normal,  Red = Hard
 P01   P02   P03   P04   P05   P06   P07   P08   P09   P10   P11   P12   P13   P14   P15   P16   P17   P18   P19   P20   P21   P22   P23   P24 
 By title 

The combination of operators and conditional statements appear in almost every program. Every Java programmer should master all types
of conditional statements. Therefore, I focus to offer that in different ways in my puzzles and quizzes. Please, always check out the explanation of the answer if you have doubts.

Java puzzle 11: Operators and conditional statements

Level: Easy

What is the output of this program and why?

public class Operator
  public static void main(String[] args)
    int i = 2;
    int i2 = 4;
    int i3 = 7;
    if(i + i2 > i3 || i2 > i3 || i + i3 >= 9)
      if(i2 == i + 2 && i2 < i)
Author: Sar Maroof

Answer explanation

  1. The first if statement if(i + i2 > i3 || i2 > i3 || i + i3 >= 9) uses the conditional operator || (OR). By using || operator, the condition is true if one of the operands returns true.
    i + i2 > i3 returns false. i2 > i3 returns false. i + i3 >= 9 returns true. Therefore the if block is executed and the else block is ignored.
  2. By using the conditional operator && (AND) see the second statement if(i2 == i + 2 && i2 < i)  both operands must return true otherwise the block wouldn’t be executed.          i2 == i + 2 returns true.
    i2 < i returns false. Therefore the block is not executed and the letter x is not written to the standard output.
  3. The statement System.out.print(“z”); is inside the first conditional statement, which returns true. Therefore, the letter z is written to the standard output.

If you found this difficult, you might consider reading my book Java quizmaster for beginners. It is easy to understand, organized to learn Java in 17 days and it guides you to master Java code by solving 105 quizzes and 117 assignments. This book explains each chapter properly before starting with exercises and assignments. It is available on Amazon. See inside the book here!

Please, leave your questions, feedback and suggestions in the comments below! offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.

Sar Maroof is graduated from HBO Amsterdam "higher professional education". He is a professional computer programming teacher, gives master classes and publishes technical articles. His experience with developing software started from 2001 and he has worked as a software developer for several big as well as small companies and later as a freelancer. He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof


 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017