Assignment Statement - Computer Science - Quiz, Exercises of Computer Science

Main points of this past exam are: Assignment Statement, Logically Equivalent, Independent Statements, Code Fragment, Value of Nag, Public Static, Independent Statements

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 2009 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 output of this code? (Circle correct letter.)
public class Test1
{
public static void main(String[] args)
{
int x = 1;
if (x < 2)
System.out.print("Hello, ");
if (x > 1)
System.out.print("How are you? ");
else
System.out.println("I am fine.");
}
}
2) Which of the statements below is logically equivalent to the if-conditional:
if( !P && Q )
Assume for any two independent statements P and Q. (Circle correct letter.)
A. if( !!P || Q )
B. if( !(P || !Q) )
C. if( !(P && !Q) )
D. if( !Q && P )
3) What is the value of Nag after the assignment statement below? (Circle correct letter.)
double Nag = 25;
Nag = Nag + Nag * (1/5);
A. 25.0
B. 30.0
C. 10.0
D. None of the above or an error of some kind exists (compile time or run time)
A. Hello, I am fine.
B. Hello, How are you?
C. Hello,
D.
H
ow are
y
ou? I am fine.
pf2

Partial preview of the text

Download Assignment Statement - Computer Science - Quiz and more Exercises Computer Science in PDF only on Docsity!

Signature _____________________ CSE 11 Name ________________________

Quiz 2

cs11f ____ Fall 2009 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 output of this code? (Circle correct letter.)

public class Test { public static void main(String[] args) { int x = 1; if (x < 2) System.out.print("Hello, "); if (x > 1) System.out.print("How are you? "); else System.out.println("I am fine."); } }

2) Which of the statements below is logically equivalent to the if-conditional:

if( !P && Q )

Assume for any two independent statements P and Q. (Circle correct letter.)

A. if( !!P || Q )

B. if( !(P || !Q) )

C. if( !(P && !Q) )

D. if( !Q && P )

3) What is the value of Nag after the assignment statement below? (Circle correct letter.)

double Nag = 25;

Nag = Nag + Nag * (1/5);

A. 25.

B. 30.

C. 10.

D. None of the above or an error of some kind exists (compile time or run time)

A. Hello, I am fine.

B. Hello, How are you?

C. Hello,

D. How are you? I am fine.

4) Assume a program had the following declarations:

Location loc1 = new Location( 2, 3 ); Location loc2 = new Location( loc1 ); Location loc3 = loc1;

What result would be produced by the following expressions?

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

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

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 + 3;

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

this.a = b + 1;

return a + 1; } }

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 = __________

this.a = __________