edited 05/26/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 just 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 on reload to clear the old answers.)