Code Fragment - Computer Science - Quiz, Exercises of Computer Science

Main points of this past exam are: Code Fragment, Integer Value, Decimal Point, Double Value, Decimal Point, Code Fragment, Equivalent Java Expression

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 2008
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 is the value of
amt
for each of the expressions below? Note the type declarations for each. To represent
an integer value, do not write a decimal point (for example: 99 or 42). To represent a double value, write a
decimal point and one digit to the right of the decimal point (for example: 99.0 or 42.5).
int amt = 10;
amt = amt + amt * (5 / 100); ____________
int amt = 10;
amt = (int) (amt + amt * (5 / 100.0)); ____________
double amt = 10;
amt = amt + amt * (5 / 100); ____________
double amt = 10;
amt = amt + amt * (5 / 100.0); ____________
2) Assume a program contained the following declarations:
private Location loc1 = new Location( 37, 24 );
private Location loc2 = new Location( 37, 24 );
private Location loc3 = loc1;
What result would be produced by the expressions
loc1 == loc2 ______________
loc1 == loc3 ______________
loc2.equals( loc1 ) ______________
loc3.equals( loc1 ) ______________
3) What is the result of each of the following expressions
2 + 3 + "4" ______________
"2" + 3 + 4 ______________
pf2

Partial preview of the text

Download Code Fragment - Computer Science - Quiz and more Exercises Computer Science in PDF only on Docsity!

Signature _____________________ CSE 11 Name ________________________

Quiz 2

cs11f ____ Fall 2008 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 is the value of amt for each of the expressions below? Note the type declarations for each. To represent

an integer value, do not write a decimal point (for example: 99 or 42). To represent a double value, write a

decimal point and one digit to the right of the decimal point (for example: 99.0 or 42.5).

int amt = 10;

amt = amt + amt * (5 / 100); ____________

int amt = 10;

amt = (int) (amt + amt * (5 / 100.0)); ____________

double amt = 10;

amt = amt + amt * (5 / 100); ____________

double amt = 10;

amt = amt + amt * (5 / 100.0); ____________

2) Assume a program contained the following declarations:

private Location loc1 = new Location( 37, 24 );

private Location loc2 = new Location( 37, 24 );

private Location loc3 = loc1;

What result would be produced by the expressions

loc1 == loc2 ______________

loc1 == loc3 ______________

loc2.equals( loc1 ) ______________

loc3.equals( loc1 ) ______________

3) What is the result of each of the following expressions

2 + 3 + "4" ______________

"2" + 3 + 4 ______________

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

!( x <= 0 || x > 15 ) _________________________

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

Quiz2 q2 = new Quiz2();

q2.method1( 5 );

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

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

a = b * 2; 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; } }

Output:

a = __________

b = __________

this.a = __________

a = __________

b = __________

this.a = __________

method2() result = __________

this.a = __________

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

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