Java quiz 36: Conditional statement with boolean

<< Java Quizzes   Green = Easy,  Blue = Normal,  Red = Hard
 Q25   Q26   Q27   Q28   Q29   Q30   Q31   Q32   Q33   Q34   Q35   Q36   Q37   Q38   Q39   Q40 
 By title 

This is a simple conditional statement using a boolean variable.

Java quiz 36: Conditional statement with boolean

Level: Easy

What happens if the following code is compiled and run?

public class MyClass
  public static void main(String[] args)
    boolean isGood = false;
    for(int i = 0; i < 3; i++)
        isGood = false;
        isGood = true;
Author: Sar Maroof

Select the correct answer.

a. This program writes “yyy” to the standard output.
b. This program writes “yxy” to the standard output.
c. This program writes “yyx” to the standard output.
d. This program writes “xyx” to the standard output.
e. This program writes “xyy” to the standard output.
f. Illegal at compile time.
g. Illegal at run time.

Answer explanation

  1. The first if statement if(isGood) returns false, because the initial value of the boolean variable isGood is false. The statement System.out.print(“y”); writes y to the standard output.
  2. The statement isGood = true; changes the value of the variable isGood to true. The next time the statement if(isGood) returns true and the statement System.out.print(“x”);
    writes x to the standard output. The statement isGood = false; assigns false to the variable isGood.
  3. The next time when the statement if(isGood) is executed, y is written to the standard output.

The correct answer is: b.

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