Docsity

CSE 11 Quiz 1 - Operator Precedence and Identifiers, Exercises of Computer Science

Toggle sidebar
bg1
Signature _____________________
CSE 11
Name ________________________
Quiz 1
cs11f ____
Fall 2012
Student ID ____________________
This quiz is to be taken by yourself with closed books, closed notes, no electronic devices.
(Partial) Operator Precedence Table
Operators Associativity
! ++ -- (pre & post inc/dec) 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. Using the operator precedence table above, evaluate each expression and state what gets printed. Remember
short-circuit evaluation with
&&
and
||
.
int a = 2;
int b = -1;
int c = 7;
boolean exp1 = a + b * c >= a + c * b; _________ (value of exp1)
boolean exp2 = !(c + a < b); _________ (value of exp2)
boolean exp3 = b - a >= c; _________ (value of exp3)
boolean z = exp1 && !exp2 || exp3;
System.out.println( "z = " + z ); ____________________
z = exp1 || exp2 && exp3;
System.out.println( "z = " + z ); ____________________
b = --b + ++a % 4 * c++ * 2;
System.out.println( "a = " + a ); ____________________
System.out.println( "b = " + b ); ____________________
System.out.println( "c = " + c ); ____________________
2. Which of the following are valid Java identifiers? (Circle your answer(s).)
3rdLine The_Avengers one/two Int
nine2five lower-half Seven%Solution Ex2_3_4
3. The naming convention for a method name is (Circle correct letter)
A. start with uppercase, rest mixed
B. start with lowercase, rest mixed
C. all lowercase letters with underscores
D. all uppercase letters with underscores
(Continued on other side)
pf2

Partial preview of the text

Download CSE 11 Quiz 1 - Operator Precedence and Identifiers and more Exercises Computer Science in PDF only on Docsity!

Signature _____________________ CSE 11 Name ________________________

Quiz 1

cs11f ____ Fall 2012 Student ID ____________________

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

(Partial) Operator Precedence Table

Operators Associativity

! ++ -- (pre & post inc/dec) 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. Using the operator precedence table above, evaluate each expression and state what gets printed. Remember

short-circuit evaluation with && and ||.

int a = 2; int b = -1; int c = 7; boolean exp1 = a + b * c >= a + c * b; _________ (value of exp1)

boolean exp2 = !(c + a < b); _________ (value of exp2)

boolean exp3 = b - a >= c; _________ (value of exp3)

boolean z = exp1 && !exp2 || exp3; System.out.println( "z = " + z ); ____________________

z = exp1 || exp2 && exp3; System.out.println( "z = " + z ); ____________________

b = --b + ++a % 4 * c++ * 2; System.out.println( "a = " + a ); ____________________

System.out.println( "b = " + b ); ____________________

System.out.println( "c = " + c ); ____________________

2. Which of the following are valid Java identifiers? (Circle your answer(s).)

3rdLine The_Avengers one/two Int

nine2five lower-half Seven%Solution Ex2_3_

3. The naming convention for a method name is (Circle correct letter)

A. start with uppercase, rest mixed

B. start with lowercase, rest mixed

C. all lowercase letters with underscores

D. all uppercase letters with underscores

(Continued on other side)

4. What is the symbol '!' affectionately known as in computer lingo?

______________________________________

5. What gets printed with each of the following statements?

int a = 9; int b = 3; int c = 4;

System.out.println( (a + b) + c + " = " + a + b + c );


System.out.println( a + (b + c) + " = " + (a + b) + c );


System.out.println( a + b + c + " = " + a + (b + c) );


int x = 5, y = 5, z = 5;

if ( x == 5 ) { if ( y >= 7 ) z = z + 4; } else { z = z + 2; }

System.out.println( z ); Answer: ________ What gets printed?

int x = 5, y = 5, z = 5;

if ( x == 5 ) if ( y >= 7 ) z = z + 4; else z = z + 2;

System.out.println( z ); Answer: ________ What gets printed?

Note - No curly braces.

7. If multiplication had lower precedence than addition, then the value of the expression

3 * 2 + 5 * 4

would be ___________.

8. What Java keyword is used to specify an identifier is constant (cannot be changed once it is set)?

__________________