Exercise 01: Simple conditional statement

  Quizzes     Assignments     Puzzles     Easy exercises     Required knowledge  
Easy Exercises   For Beginners
Select    01   02   03   04   05   By title 

I started this easy Java exercises, because some members wrote to me that the easy quizzes, puzzles and assignments are not easy for them. Therefore, I realized that this serie might help the beginners even better. In this serie I keep the code as simple as I can.

Read More: Java Conditional Statements

Java exercise 01: A simple conditional statement

Level: Very easy
What happens if the following code is compiled and run?

public class MyClass
{
  public static void main(String[] args)
  {
    int x = 3;
    if(x < 3)
    {
      System.out.print("x");
    }
    else
    {
      System.out.print("y");
    }
  }
}
Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. The first statement if(x > 3) returns false, because the value of x is not greater than 3.
  2. The body of the else statement is executed, because the if statement returns false. The statement System.out.print(“y”); writes y to the standard output.

The correct answer is: b.





Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.

 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 *