Quiz 2 in CSE 11: Operator Precedence and Java Programming - Prof. Richard Ord, Quizzes of Computer Science

A quiz for students in cse 11 (fall 2010) related to operator precedence and java programming. The quiz includes questions about the values of variables after code segments are executed, the equivalent java expression without ! operators, and unix commands to compile, run, and run as an applet a java program. Additionally, there are questions about the results of evaluating expressions involving object references and the output of a java code fragment.

Typology: Quizzes

2010/2011

Uploaded on 06/06/2011

koofers-user-lg9
koofers-user-lg9 🇺🇸

10 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 2010 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 and y (left) and a and b (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?
!( a > 5 && b != -9 ) ____________________________________________
3) Assume we have a Java source file named Program.java and it uses at least one class in the objectdraw
library.
Write the full Unix command to compile this Java program.
________________________________________________________________________
This command will produce a file named:
________________________________________________________________________
Write the full Unix command to run this as a Java application.
________________________________________________________________________
Assume we have correctly written a Program.html file. Write the full Unix command to run the above program
as an applet.
________________________________________________________________________
x =
y =
int x = 2, y = 4;
if ( x++ >= 3 || --y >= 3 )
x = x++ + --y;
else
x = ++x + y--;
a =
b =
int a = 2, b = 4;
if ( a++ >= 3 && --b >= 3 )
a = a++ + --b;
else
a = ++a + b--;
pf2

Partial preview of the text

Download Quiz 2 in CSE 11: Operator Precedence and Java Programming - Prof. Richard Ord and more Quizzes Computer Science in PDF only on Docsity!

Signature _____________________ CSE 11 Name ________________________

Quiz 2

cs11f ____ Fall 2010 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 and y (left) and a and b (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?

!( a > 5 && b != -9 ) ____________________________________________

  1. Assume we have a Java source file named Program.java and it uses at least one class in the objectdraw library.

Write the full Unix command to compile this Java program.


This command will produce a file named:


Write the full Unix command to run this as a Java application.


Assume we have correctly written a Program.html file. Write the full Unix command to run the above program as an applet.


x = y =

int x = 2, y = 4;

if ( x++ >= 3 || --y >= 3 ) x = x++ + --y; else x = ++x + y--;

a = b =

int a = 2, b = 4;

if ( a++ >= 3 && --b >= 3 ) a = a++ + --b; else a = ++a + b--;

  1. Assume a program had the following declarations:

Location loc1 = new Location( 42, 420 ); Location loc2 = loc1; Location loc3 = new Location( loc2 );

What results would be produced by evaluating the following expressions?

( loc1 == loc2 ) ____________ loc1.equals( loc3 ) ____________

( loc2 == loc3 ) ____________ loc3.equals( new Location( loc2 ) ) ____________

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

Quiz2 q2 = new Quiz2(); q2.method1( 17 );

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( "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 + 2;

return a + 2; } }

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

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

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