revised: 05/26/03; 10/05/03


Quiz on the Conditional Operator and the Switch 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. What value is placed in var?

var = 12 > 9 ? 0 : 1;

A.    0
B.    1
C.    9
D.    12

2. What value is placed in awk?

int x = 5, y = 19;

awk = y-x > x-y ? y-x : x-y ;

A.    5
B.    19
C.    14
D.    -14

3. What value is placed in choice?

int a=5, b=10, c=15 ;

choice = a>b && a>c ? a : (b > c ? b : c) ;

A.    5
B.    10
C.    15
D.    0

4. What value is placed in sum?

double sum = 10.0, price=100;

sum +=  price>=100 ? price*1.1 : price;

A.    90
B.    100
C.    110
D.    120

5. Which statement makes sure that x is an even number?

A.    x = x%2 == 1 ? x++ : x;
B.    x = x%2 == 0 ? x+1 : x;
C.    x += x%2 == 0 ? 0 : 1 ;
D.    x += 2*x ;

6. What value is assigned to discount ?

double discount;
char   code = 'C' ;

switch ( code )
{
  case 'A':
    discount = 0.0;
    break;

  case 'B':
    discount = 0.1;
    break;

  case 'C':
    discount = 0.2;
    break;

  default:
    discount = 0.3;
} 

A.    0.0
B.    0.1
C.    0.2
D.    0.3

7. What value is assigned to discount ?

double discount;
char   code = 'C' ;

switch ( code )
{
  case 'A':
    discount = 0.0;

  case 'B':
    discount = 0.1;

  case 'C':
    discount = 0.2;

  default:
    discount = 0.3;
} 

A.    0.0
B.    0.1
C.    0.2
D.    0.3

8. What value is assigned to discount ?

double discount;
char   code = 'X' ;

switch ( code )
{
  case 'A':
    discount = 0.0;
    break;

  case 'B':
    discount = 0.1;
    break;

  case 'C':
    discount = 0.2;
    break;

  default:
    discount = 0.3;
} 

A.    0.0
B.    0.1
C.    0.2
D.    0.3

9. What value is assigned to discount ?

double discount;
String code = "A" ;

switch ( code )
{
  case 'A':
    discount = 0.0;
    break;

  case 'B':
    discount = 0.1;
    break;

  case 'C':
    discount = 0.2;
    break;

  default:
    discount = 0.3;
} 

A.    0.0
B.    0.1
C.    0.2
D.    The program will not compile because code is the wrong type.

10. What value is assigned to discount ?

double discount;
char   code = 'b' ;

switch ( code )
{
  case 'a':
  case 'A':
    discount = 0.0;
    break;

  case 'b':
  case 'B':
    discount = 0.1;
    break;

  case 'c':
  case 'C':
    discount = 0.2;
    break;

  default:
    discount = 0.3;
} 

A.    0.0
B.    0.1
C.    0.2
D.    The code will not compile because there are missing statements.

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.