revised: 10/04/03, 10/29/2012, 07/17/17


quiz on the if statement

This is a practice quiz. The results are not recorded anywhere and do not affect your grade. The questions on this quiz might not appear in any quiz or test that does count toward your grade.

Instructions: For each question, choose the single best answer. Make your choice by clicking on its button. You can change your answers at any time. When the quiz is graded, the correct answers will appear in the box after each question.


1. How many choices are possible when using a single if-else statement?

A.    1

B.    2

C.    3

D.    4

Correct Answer:


2. What does the following code fragment write to the monitor?

int sum = 14;
if ( sum < 20 )
  System.out.print("Under ");
else
{  
  System.out.print("Over  ");
  System.out.println("the limit.");
}

A.    Under

B.    Over

C.    Under the limit.

D.    Over the limit.

Correct Answer:


3. What does the following code fragment write to the monitor?

int sum =  7; 
if ( sum > 20 )
{
  System.out.print("You win ");
}
else
{
  System.out.print("You lose  ");
}

System.out.println("the prize.");

A.    You win

B.    You lose

C.    You win the prize.

D.    You lose the prize.

Correct Answer:


4. What does the following code fragment write to the monitor?

int sum =  21; 
if ( sum != 20 )
  System.out.print("You win ");
else
  System.out.print("You lose  ");

System.out.println("the prize.");

(Notice that the program has changed from the previous question!)

A.    You win

B.    You lose

C.    You win the prize.

D.    You lose the prize.

Correct Answer:


5. Evaluate (to true or false) each of the following expressions:

14 <= 14     14 < 14      -9 > -25     -25  >  -9

A.    true     true     true     true

B.    true     false     false     false

C.    true     false     true     true

D.    true     false     true     false

Correct Answer:


6. What does the following code fragment write to the monitor?

int roll =  13; 

if ( roll > 20 )
  System.out.println("Jackpot!");
else
{
  if ( roll < 10 )
     System.out.print("You lose ");
  else
     System.out.print("You win ");
  System.out.println("the prize.");
}

A.    Jackpot!

B.    You lose

C.    You win

D.    You win the prize.

Correct Answer:


7. What does the following code fragment write to the monitor?

int temp =  62; 

if ( temp > 60 )
{
  if ( temp > 80 )
     System.out.println("Hot");
  else
     System.out.println("Moderate");
}
else
  System.out.print("Chilly");


A.    Hot

B.    Moderate

C.    Chilly

D.    Nothing is printed

Correct Answer:


8. What does the following code fragment write to the monitor?

int temp =  32; 

if ( temp >= 60 )
{
  if ( temp >= 80 )
     System.out.println("Hot");
  else
     System.out.println("Moderate");
}
else
{
  if ( temp >= 40 )
     System.out.println("Chilly");
  else
     System.out.println("Frigid");
}


A.    Hot

B.    Moderate

C.    Chilly

D.    Frigid

Correct Answer:


9. What does the following code fragment write to the monitor?

int temp =  40;  // Note change

if ( temp >= 60 )
{
  if ( temp >= 80 )
     System.out.println("Hot");
  else
     System.out.println("Moderate");
}
else
{
  if ( temp >= 40 )
     System.out.println("Chilly");
  else
     System.out.println("Frigid");
}


A.    Hot

B.    Moderate

C.    Chilly

D.    Frigid

Correct Answer:


10. What does the following code fragment write to the monitor?

int score =  82;   

if ( score >= 80 )
{
  if ( score >= 90 )
     System.out.println("A");
  else
     System.out.println("B");
}
else
{
  if ( score >= 70 )
     System.out.println("C");
  else
     System.out.println("D");
}


A.    A

B.    B

C.    C

D.    D

Correct Answer:


The number you got right:       Percent Correct:       Letter Grade:   


Click here If you have returned here from another page, or have re-loaded this page, you will need to click again on each of your choices for the grading program to work correctly. You may want to press the SHIFT KEY while clicking to clear the old answers.