revised: 05/26/03 10/05/03, 10/31/2012, 07/12/2016


Quiz on the Conditional Operator and the Switch Statement

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

correct answer:


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

correct answer:


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

correct answer:


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

correct answer:


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

A.    x = x%2 == 1 ? x+1 : x-1;

B.    x = x%2 == 0 ? x+1 : x;

C.    x += x%2 == 0 ? 0 : 1 ;

D.    x += 2*x ;

correct answer:


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

correct answer:


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

correct answer:


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

correct answer:


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.

correct answer:


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.

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.