Docsity

Quiz 1 for Introduction to Computer Science: Java | CSE 8B, Quizzes of Computer Science

Toggle sidebar
bg1
CSE 8B Name ________________________
Quiz 1
Java Fundamentals cs8b________
Fall 2002
This quiz is to be taken by yourself with closed books, closed notes, no calculators.
Operator Precedence Table
Operators Associativity
- (unary) ++ -- ! right to left
* / % left to right
+ - left to right
< <= > >= left to right
== != left to right
&& left to right
|| left to right
= += -= *= /= right to left
1) Fully insert parentheses in the expressions below to show correct evaluation following the rules of precedence and associativity.
What gets printed? (2 pt)
public class Q1_1
{
public static void main( String[] args )
{
System.out.println( -1 + 2 * 6 + 14 / 4 ); _________
System.out.println( 6 + 7 - 2 % 11 + 3 ); _________
}
}
2) What gets printed? (5 pts)
public class Q1_2
{
public static void main( String[] args )
{
final int MAX = 2;
int i = -2, j = -3;
for (i = 0; i < MAX; ++i )
{
j = 1;
while ( j <= MAX )
{
System.out.println( i + " " + j );
++j;
}
}
System.out.println( i + " " + j );
} // end main()
}
(over)
pf2

Partial preview of the text

Download Quiz 1 for Introduction to Computer Science: Java | CSE 8B and more Quizzes Computer Science in PDF only on Docsity!

CSE 8B Name ________________________

Quiz 1

Java Fundamentals cs8b________

Fall 2002

This quiz is to be taken by yourself with closed books, closed notes, no calculators.

Operator Precedence Table Operators Associativity

  • (unary) ++ --! right to left
  • / % left to right
    • left to right < <= > >= left to right == != left to right && left to right || left to right = += -= *= /= right to left

1) Fully insert parentheses in the expressions below to show correct evaluation following the rules of precedence and associativity. What gets printed? (2 pt)

public class Q1_ { public static void main( String[] args ) { System.out.println( -1 + 2 * 6 + 14 / 4 ); _________

System.out.println( 6 + 7 - 2 % 11 + 3 ); _________ } }

2) What gets printed? (5 pts)

public class Q1_ { public static void main( String[] args ) { final int MAX = 2; int i = -2, j = -3;

for (i = 0; i < MAX; ++i ) { j = 1; while ( j <= MAX ) { System.out.println( i + " " + j ); ++j; } }

System.out.println( i + " " + j );

} // end main() }

(over)

3) What gets printed? (4 pts)

public class Q1_ { public static void main( String[] args ) { int i = 1, j = 2, k = 3, m = 2;

System.out.println( i >= 1 && j < 4 ); System.out.println( j >= i || k == m ); System.out.println( !( k > m ) ); System.out.println( !((i > 4) || (j <= 6)) == ((i <= 4) && (j > 6)) ); } }

4) Which line(s) [after line 6] would cause a compiler error and how would you fix that/those line(s) so it would compile correctly?

(4 pts)

1 public class Q1_4 { 2 public static void main( String[] args ) { Line number(s)__________________________ 3 int x; 4 long y; 5 double z; 6 7 x = 15; 8 z = 4.5; 9 y = z; 10 y = x; 11 x = y;

12 z = y; 13 } 14 }