Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element.
Skip Breadcrumb Navigation
Home  arrow Student Resources  arrow Chapter Quizzes  arrow Chapter 3

Chapter 3

Flow of Control

This activity contains 15 questions.

Question 1.
After this fragment executes, what is the value of num?
int num = 17;
switch (--num) {
	case 14:  num = num - 4;
	case 15:  num = num - 6;
	case 16:  num = num - 8;
	case 17:  num = num - 10;
}


Open Hint for Question 1 in a new window.
 
End of Question 1


Question 2.
An assertion check statement:
  • Requires a Boolean expression
  • Can be used anywhere a normal statement can
  • Terminates the program if the Boolean expression evaluates to true
  • Is used to say something (to assert) about the state of a program


Open Hint for Question 2 in a new window.
 
End of Question 2


Question 3.

Consider these three fragments:
// Fragment One
final int LIMITx = 16;
int sumx = 0;
for (int cntx = 1; cntx < LIMITx; cntx *= 2)
	sumx += cntx;
System.out.println("sum = " + sumx);

// Fragment Two
final int LIMITz = 16;
int sumz = 0;
int cntz = 1;
while (cntz < LIMITz) {
	sumz += cntz;
	cntz *= 2;
}
System.out.println("sum = " + sumz);

// Fragment Three
final int LIMITy = 16;
int sumy = 0;
int cnty = 1;
do {
	sumy += cnty;
	cnty *= 2;
} while (cnty < LIMITy);
System.out.println("sum = " + sumy);

Open Hint for Question 3 in a new window.
 
End of Question 3


Question 4.
If you forget to place a break in a switch statement, what happens?


Open Hint for Question 4 in a new window.
 
End of Question 4


Question 5.

The break and continue statements:
  • can be used within any kind of loop -- while, do-while, and for
  • are controversial and should be avoided if possible
  • are unnecessary -- any program written using break or continue within a loop can be rewritten not using break and continue
  • just as break also can be used within a switch statement, so can continue -- with a similar effect

Open Hint for Question 5 in a new window.
 
End of Question 5


Question 6.
The difference between "short-circuit" and "complete" evaluation is:


Open Hint for Question 6 in a new window.
 
End of Question 6


Question 7.
This fragment:
if (alpha >= beta)
    charlie = delta;
else
    epsilon = foxtrot;


is equivalent to which of the following?


Open Hint for Question 7 in a new window.
 
End of Question 7


Question 8.
What are the final values of a and b?
int a = 2, b = 7;
b -= a + 4;
a *= b - a;


Open Hint for Question 8 in a new window.
 
End of Question 8


Question 9.
What are the final values of a, b, and c?
int  a = 4, b = 9, c = -3;
if (a >= b)
    if (c < b)
        a = b + c;
    else
        c = a -- c;
else if (c < a)
    b = c -- b;


Open Hint for Question 9 in a new window.
 
End of Question 9


Question 10.
What are the final values of a, b, and c?
int a = 4, b = -9, c = 7;
if (a > b && b > c)
	if (b < a)
		b = -b;
	else
		a = -a;
else if (a > b || b > c)
	if (a < c)
		c = -c;
	else if (! (c > b))
	b = -a;


Open Hint for Question 10 in a new window.
 
End of Question 10


Question 11.
What does this fragment print?
int result = 9;
for (int cnt = 2; cnt < result; cnt += 3)
    result = cnt;
System.out.println("result = " + result);


Open Hint for Question 11 in a new window.
 
End of Question 11


Question 12.
What is printed?
int num = 12345678;
int result = 0;
do {
	result = 10 * result + num % 10;
	num /= 100;
} while (num > 0);
System.out.println(result);


Open Hint for Question 12 in a new window.
 
End of Question 12


Question 13.
What is printed?
int result = 2;
int cnt = 5;
for (cnt = 0; cnt < 5; cnt++);
	result += cnt;
System.out.println("result = " + result);


Open Hint for Question 13 in a new window.
 
End of Question 13


Question 14.
What is the difference between lexicographic and alphabetic ordering?


Open Hint for Question 14 in a new window.
 
End of Question 14


Question 15.
What sequence of ints is printed?
final int LIMIT = 10;
int cnt = 2;
while (cnt < LIMIT) {
	System.out.print(" " + cnt);
	cnt += cnt;
}
System.out.println();


Open Hint for Question 15 in a new window.
 
End of Question 15





Pearson Copyright © 1995 - 2010 Pearson Education . All rights reserved. Pearson Addison Wesley is an imprint of Pearson .
Legal Notice | Privacy Policy | Permissions

Return to the Top of this Page