State Clearly All of Your Assumptions
Total Page:16
File Type:pdf, Size:1020Kb
Name :...... Comp 240 Quiz Written 2 ID # :...... Mar 17, 2005 1/3 State clearly all of your assumptions. Q1 (49) Q2 (21) Q3(30) Sum Good luck.
1 (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( "apple" ); break; case 2: System.out.println( "orange" ); break; case 3: System.out.println( "banana" ); break; case 4: System.out.println( "pear" ); case 5: System.out.println( "grapes" ); default: System.out.println( "kiwi" ); }
q 0 1 2 3 4 5 6
kiwi apple orange banana pear grapes kiwi grapes kiwi kiwi Output 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
* ** *** **** *****