State clearly all of your assumptions

advertisement
Name : .....................................................................
Comp 240 Quiz Written 2
ID # : .....................................................................
Mar 17, 2005
1/3
State clearly all of your assumptions.
Good luck.
Q1 (49)
Q2 (21)
Q3(30)
Sum
Q1. (49 pt)
In the following questions, check all that apply.
1.a.
What is the difference between a float and a double?
 double variables store integers and float variables store floating-point numbers.
 double variables store numbers with smaller magnitude and coarser detail.
 double variables store numbers with larger magnitude and finer detail.
1.b.
What is the default initial value of a String?
 ""
 "default"
 default
 null
1.c.
In an expression containing values of the types int and double, the ________ values are ________ to
________ values for use in the expression.
 int, promoted, double.
 int, demoted, double.
 double, promoted, int.
 double, demoted, int.
1.d.
What does the expression x %= 10 do?
 Adds 10 to the value of x, and stores the result in x.
 Divides x by 10 and stores the remainder in x.
 Divides x by 10 and stores the integer result in x.
1.e.
Which of the following will count down from 10 to 1 correctly?
 for ( int j = 10; j <= 1; j++ )
 for ( int j = 1; j <= 10; j++ )
 for ( int j = 10; j <= 1; j-- )
 for ( int j = 10; j > 1; j-- )
 for ( int j = 10; j >= 1; j-- )
Name : .....................................................................
Comp 240 Quiz Written 2
ID # : .....................................................................
Mar 17, 2005
2/3
1.f.
Which of the following statements about the break statement is false?
 The break statement is used to exit a repetition structure early and continue execution after the
loop.
 The break statement, when executed in a while, for or do…while, skips the remaining
statements in the loop body and proceeds with the next iteration of the loop.
 Common uses of the break statement are to escape early from a loop or to skip the remainder of a
switch.
1.g.
Suppose variable gender is MALE and age equals 60, how is the expression
( gender == FEMALE ) && ( age >= 65 )
evaluated?
 The condition ( gender == FEMALE ) is evaluated first and the evaluation stops
immediately.
 The condition ( age >= 65 ) is evaluated first and the evaluation stops immediately.
 Both conditions are evaluated, from left to right.
 Both conditions are evaluated, from right to left.
Q2. (21 pt)
What is the output of the code segment below for varying values of q?
switch( q ) {
case 1:
System.out.println(
break;
case 2:
System.out.println(
break;
case 3:
System.out.println(
break;
case 4:
System.out.println(
case 5:
System.out.println(
default:
System.out.println(
}
q
0
kiwi
Output
1
apple
2
orange
"apple" );
"orange" );
"banana" );
"pear" );
"grapes" );
"kiwi" );
3
banana
4
pear
grapes
kiwi
5
grapes
kiwi
6
kiwi
Name : .....................................................................
Comp 240 Quiz Written 2
ID # : .....................................................................
Mar 17, 2005
3/3
Q3. (30 pt)
What it the output of the following program?
public class Printing {
public static void main(String[] args) {
for ( int i = 1; i <= 5; i++ ) {
for ( int j = 1; j <= i; j++ )
System.out.print( '*' );
System.out.println();
} // end of outer for
} // end of method main
} // end of class Printing
*
**
***
****
*****
Download