CSE 11 Quiz 1 - Operator Precedence and Short-Circuit Evaluation, Exercises of Computer Science

A cse 11 quiz 1 focused on operator precedence and short-circuit evaluation. Students are required to evaluate given boolean expressions using the provided operator precedence table and determine the printed values. The quiz also includes multiple-choice questions related to boolean variable assignments and operator precedence rules.

Typology: Exercises

2012/2013

Uploaded on 04/07/2013

shabi_564
shabi_564 🇮🇳

4.5

(13)

188 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Signature _____________________
CSE 11
Name ________________________
Quiz 1
cs11f ____
Fall 2011
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 = 7;
int b = -1;
int c = 2;
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.print( "z = " + z ); ____________________
z = exp1 || exp2 && exp3;
System.out.print( "z = " + z ); ____________________
b = --b + ++a % 4 * c++ * 2;
System.out.print( "a = " + a ); ____________________
System.out.print( "b = " + b ); ____________________
System.out.print( "c = " + c ); ____________________
2. Consider the following code segment:
x = y;
y = !x;
x = !y;
Assume x and y are initialized boolean variables. Which of the following statements is true?
Circle the letter in front of the true statement.
A. The final value of x is the same as the initial value of x.
B. The final value of x is the same as the initial value of y.
C. The final value of y is the same as the initial value of y.
D. The final value of y is the same as the initial value of x.
E. It is not possible to say anything about the final values of x and y without knowing their initial values.
(Continued on other side)
pf2

Partial preview of the text

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

Signature _____________________ CSE 11 Name ________________________

Quiz 1

cs11f ____ Fall 2011 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 = 7; int b = -1; int c = 2; 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.print( "z = " + z ); ____________________

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

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

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

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

2. Consider the following code segment:

x = y; y = !x; x = !y;

Assume x and y are initialized boolean variables. Which of the following statements is true?

Circle the letter in front of the true statement.

A. The final value of x is the same as the initial value of x.

B. The final value of x is the same as the initial value of y.

C. The final value of y is the same as the initial value of y.

D. The final value of y is the same as the initial value of x.

E. It is not possible to say anything about the final values of x and y without knowing their initial values.

(Continued on other side)

3. If b is a boolean variable, then the statement

b = ( b == false );

has what effect? _____

A. It causes a compile-time error message.

B. It causes a run-time error message.

C. It causes b to have the value false regardless of its value just before the statement was executed.

D. It always changes the value of b.

E. It changes the value of b if and only if b had value true just before the statement was executed.

Which of the following is equivalent to and has the same effect as b = ( b == false );? _____

A. b = ( b == true );

B. b = ( b != true );

C. b = ( b != false );

D. b = ( b == b );

E. b = ( b != b );

F. More than one of the above statements is equivalent

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

int a = 2; int b = 0; int c = 5;

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 );


5. Assume that x, y, and z are all int variables. Consider the following code segment:

if ( x == 0 ) { if ( y == 1 ) z = z + 2; } else { z = z + 4; }

System.out.println( z ); Answer: ________

What is printed if x, y, and z are all equal to zero before the code segment executes?

6. If addition had higher precedence than multiplication, then the value of the expression

2 * 3 + 4 * 5

would be ___________.