Quiz 2 in CSE 11: Operator Precedence, Logic Gates, and Turtle Graphics, Exercises of Computer Science

A quiz for students in cse 11 (computer science fundamentals i) covering operator precedence, logical operators, and turtle graphics. The quiz includes multiple-choice questions, a coding challenge, and a java programming exercise. Students are required to determine the values of variables after code execution, write java expressions without using negation operators, draw a t shape using turtle graphics, and evaluate equality and inheritance between objects.

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 2
cs11f ____
Fall 2012
Student ID ____________________
This quiz is to be taken by yourself with closed books, closed notes, no calculators.
(Partial) Operator Precedence Table
Operators Associativity
* / % left to right
+ - left to right
< <= > >= left to right
== != left to right
&& left to right
|| left to right
= right to left
1) What are the values of x, y, z (left) and a, b, c (right) after the following code segments are executed?
2) What is the equivalent Java expression for the following expression such that no
!
operators are used?
!( x < 17 && y >= 3 )
____________________________________________
3) Using only the statements below, select the order of the statements to draw a T such that the width of the T is
size pixels and the height of the T is twice size pixels. Do not worry about where it is drawing. Assume the
turtle is pointing up when the method is called and is positioned at the upper left corner of where we want to
draw the T. Start drawing the T at the upper left corner of the T. Have the turtle end at the bottom of the T.
Write the letter corresponding to each statement
in the correct order to draw a T. Do it in exactly 5 statements.
A) this.forward( 2 * size );
B) this.forward( size );
C) this.turn( 90 ); // turn right
D) this.forward( -(size/2) );
E) this.forward( size/2 );
x =
y =
z =
int x = 6, y = 3, z = 0;
if ( --x > 5 || y++ > 3 )
z = ++x + y--;
else
z = x++ + --y;
int a = 8, b = 5, c = 0;
if ( ++a <= 8 && b-- >= 4 )
c = a++ + --b;
else
c = ++a + b--;
a =
b =
c =
public void drawT( int size )
{
_____
_____
_____
_____
_____
}
pf2

Partial preview of the text

Download Quiz 2 in CSE 11: Operator Precedence, Logic Gates, and Turtle Graphics and more Exercises Computer Science in PDF only on Docsity!

Signature _____________________ CSE 11 Name ________________________

Quiz 2

cs11f ____ Fall 2012 Student ID ____________________

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

(Partial) Operator Precedence Table

Operators Associativity

* / % left to right

+ - left to right

< <= > >= left to right

== != left to right

&& left to right

|| left to right

= right to left

1) What are the values of x, y, z (left) and a, b, c (right) after the following code segments are executed?

2) What is the equivalent Java expression for the following expression such that no! operators are used?

!( x < 17 && y >= 3 ) ____________________________________________

3) Using only the statements below, select the order of the statements to draw a T such that the width of the T is

size pixels and the height of the T is twice size pixels. Do not worry about where it is drawing. Assume the

turtle is pointing up when the method is called and is positioned at the upper left corner of where we want to

draw the T. Start drawing the T at the upper left corner of the T. Have the turtle end at the bottom of the T.

Write the letter corresponding to each statement

in the correct order to draw a T. Do it in exactly 5 statements.

A) this.forward( 2 * size );

B) this.forward( size );

C) this.turn( 90 ); // turn right

D) this.forward( -(size/2) );

E) this.forward( size/2 );

x =

y =

z =

int x = 6, y = 3, z = 0;

if ( --x > 5 || y++ > 3 )

z = ++x + y--;

else

z = x++ + --y;

int a = 8, b = 5, c = 0;

if ( ++a <= 8 && b-- >= 4 )

c = a++ + --b;

else

c = ++a + b--;

a =

b =

c =

public void drawT( int size )

_____

_____

_____

_____

_____

  1. Assume a program had the following definitions (Point has an x and a y value with proper equals() defined):

Point p1 = new Point( 37, 23 );

Point p2 = new Point( p1 );

Point p3 = p2;

What results would be produced by evaluating the following expressions (left to right; top to bottom)?

p1 == p2 ____________ p1 == p3 ____________ p2 == p3 ____________

p1.equals(p2) ____________ p1.equals(p3) ____________ p2.equals(p3) ____________

p1.translate(1, 1); // Add 1 to the x and y coordinates in the Point object ref'ed by p

p1.equals(p2) ____________ p1.equals(p3) ____________ p2.equals(p3) ____________

p2.translate(1, 1); // Add 1 to the x and y coordinates in the Point object ref'ed by p

p1.equals(p2) ____________ p1.equals(p3) ____________ p2.equals(p3) ____________

p3.translate(1, 1); // Add 1 to the x and y coordinates in the Point object ref'ed by p

p1.equals(p2) ____________ p1.equals(p3) ____________ p2.equals(p3) ____________

  1. What output is produced with the following code fragment? Assume method1() is invoked as

Quiz2 q2 = new Quiz2();

q2.method1( 11 );

public class Quiz { private int a; // Line 3

public void method1( int x ) { int a; // Line 7 int b = x;

a = b % 5; this.a = b / 3;

System.out.println( "a = " + a ); System.out.println( "b = " + b ); System.out.println( "this.a = " + this.a ); System.out.println( "method2() result = " + method2( x ) ); System.out.println( "a = " + a ); System.out.println( "b = " + b ); System.out.println( "this.a = " + this.a ); }

private int method2( int x ) { int a = x; int b = this.a;

b = b * 2;

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

this.a = b + 3;

return a + 3; } }

What is the initial value of a on Line 7? ______________

What is the initial value of a on Line 3? ______________

Output:

a = __________ b = __________ this.a = __________ a = __________ b = __________ this.a = __________ method2() result = __________ a = __________ b = __________ this.a = __________