Quiz Written 1

advertisement
Name : .....................................................................
Comp 240 Quiz Written 1
ID # : .....................................................................
Feb 24, 2005
1/2
State clearly all of your assumptions.
Good luck.
Q1 (35)
Q2 (15)
Q3(20)
Q4 (20)
Q5 (10)
Sum
Q1. (35 pt)
Fill in the blanks in each of the following statements. The answers can be found within the list of alternatives
below.
1.a.
A(n) left brace ({) begins the body of every method, and a(n) right brace (}) ends the body of
every method.
1.b.
Every statement ends with a(n) semicolon (;).
1.c.
The statement if is used to make decisions.
1.d.
// begins an end-of-line comment.
1.e.
Blank lines, space characters, newline characters and tab characters are called white space.
1.f.
Java applications begin execution at method main.
1.g.
Methods System.out.print, System.out.printf, and System.out.println display information in the
command window.
Alternatives:
 tab characters
 /*
 space characters
 class
 if
 newline characters






blank lines
System.out.print
*/
left brace ({)
public
System.out.println
Q2. (15 pt)
What is the value of result after the following Java statements execute?
int a, b, c, d;
a = 4;
b = 12;
c = 37;
d = 51;
result = d % a * c + a % b + a;
119






right brace (})
semicolon (;)
//
main
System.out.printf
input.nextInt();
Name : .....................................................................
Comp 240 Quiz Written 1
ID # : .....................................................................
Feb 24, 2005
2/2
Q3. (20 pt)
Identify and correct the errors in each of the following statements:
3.a.
if ( c < 7 );
System.out.println( "c is less than 7" );
Error:
Semicolon after the right parenthesis of the condition ( c < 7 ) in the if.
Correction: Remove the semicolon after the right parenthesis. [Note: As a result, the output
statement will execute regardless of whether the condition in the if is true.]
3.b.
if ( c => 7 )
System.out.println( "c is equal to or greater than 7" );
Error:
The relational operator => is incorrect.
Correction: Change => to >=.
Q4. (20 pt)
What displays in the message dialog when each of the given Java statements is performed?
Assume that x = 2 and y = 3.
4.a.
System.out.printf( "x = %d\n", x );
x = 2
4.b.
System.out.printf( "Value of %d + %d is %d\n", x, x, ( x + x ) );
Value of 2 + 2 is 4
4.c.
System.out.printf( "x =" );
x =
4.d.
System.out.printf( "%d = %d\n", ( x + y ), ( y + x ) );
5 = 5
Q5. (10 pt)
What does the following code print?
System.out.println( "*\n**\n***\n****\n*****" );
*
**
***
****
*****
Download