CIS 265/506Exam1 – Spring 2012Prof. V. Matos

Exam

Last Name______First Name: ______

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

1)

Suppose x = 1, y =-1, and z = 1. What is the printout of the following statement?

if (x > 0)

if (y > 0)

System.out.println("x > 0 and y > 0");

else if (z > 0)

System.out.println("x < 0 and z > 0");

1)

______

A)

x < 0 and z < 0;

B)

x < 0 and z > 0;

C)

x > 0 and y > 0;

D)

no printout.

2)

The following code displays ______.

double temperature = 50;

if (temperature >= 100)

System.out.println("too hot");

else if (temperature <= 40)

System.out.println("too cold");

else

System.out.println("just right");

2)

______

A)

too hot

B)

too hot too cold just right

C)

just right

D)

too cold

3)

Analyze the following code:

Code 1:

boolean even;

if (number % 2 == 0)

even= true;

else

even= false;

Code 2:

boolean even = (number % 2 == 0);

3)

______

A)

Code 2 has compile errors.

B)

Both Code 1 and Code 2 have compile errors.

C)

Code 1 has compile errors.

D)

Both Code 1 and Code 2 are correct, but Code 2 is better.

4)

Suppose income is 4001, what is the output of the following code:

if (income > 3000) {

System.out.println("Income is greater than 3000");

}

else if (income > 4000) {

System.out.println("Income is greater than 4000");

4)

______

A)

Income is greater than 4000

B)

Income is greater than 4000 followed by Income is greater than 3000

C)

Income is greater than 3000 followed by Income is greater than 4000

D)

Income is greater than 3000

E)

no output

5)

Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?

5)

______

A)

1 < x < 100 & x < 0

B)

(1 > x > 100) || (x < 0)

C)

((x < 100) & (x > 1)) & (x < 0)

D)

((x < 100) & (x > 1)) || (x < 0)

6)

What is the output of the following code?

charch= 'F';

if (ch>= 'A' & ch<= 'Z')

System.out.println(ch);

6)

______

A)

F

B)

f

C)

F f

D)

nothing

7)

What is y after the following statement is executed?

x = 0;

y = (x > 0) ?10 :-10;

7)

______

A)

0

B)

20

C)

-10

D)

10

E)

Illegal expression

8)

How many times will the following code print "Welcome to Java"?

int count = 0;

while (count++ 10) {

System.out.println("Welcome to Java");

}

8)

______

A)

11

B)

8

C)

0

D)

9

E)

10

9)

What is the output for y?

int y = 0;

for (inti= 0; i<10; ++i) {

y +=i;

}

System.out.println(y);

9)

______

A)

12

B)

10

C)

45

D)

11

E)

13

10)

What isi after the following for loop?

int y = 0;

for (inti= 0; i<10; ++i) {

y +=i;

}

10)

______

A)

9

B)

11

C)

10

D)

undefined

11)

What is the number of iterations in the following loop:

for (inti= 1; i n; i++) {

// iteration

}

11)

______

A)

2*n

B)

n + 1

C)

n - 1

D)

n

12)

Analyze the following code:

class Test {

public static void main(String[ ] args) {

System.out.println(xmethod(5));

}

public static intxmethod(int n, long t) {

System.out.println("int");

return n;

}

public static long xmethod(long n) {

System.out.println("long");

return n;

}

}

12)

______

A)

The program displays long followed by 5.

B)

The program displays int followed by 5.

C)

The program runs fine but displays things other than 5.

D)

The program does not compile because the compiler cannot distinguish which xmethod to invoke.

13)

If you declare an array double[ ] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ______.

13)

______

A)

5.5

B)

2.0

C)

3.4

D)

3.4

E)

undefined

14)

If you declare an array double[ ] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is ______.

14)

______

A)

2

B)

3

C)

1

D)

4

E)

0

15)

How many elements are in array double[ ] list = new double[5]?

15)

______

A)

6

B)

5

C)

4

D)

0

16)

In the following code, what is the printout for list2?

class Test {

public static void main(String[ ] args) {

int[ ] list1 = {1, 2, 3};

int[ ] list2 = {1, 2, 3};

list2 = list1;

list1[0] = 0; list1[1] = 1; list2[2] = 2;

for (inti= 0; i list2.length; i++)

System.out.print(list2[i] + " ");

}

}

16)

______

A)

0 1 3

B)

0 1 2

C)

1 11

D)

1 2 3

17)

When you pass an array to a method, the method receives ______.

17)

______

A)

a copy of the first element

B)

a copy of the array

C)

the reference of the array

D)

the length of the array

18)

Show the output of the following code:

public class Test {

public static void main(String[ ] args) {

int[ ] x = {1, 2, 3, 4, 5};

increase(x);

int[ ] y = {1, 2, 3, 4, 5};

increase(y[0]);

System.out.println(x[0] + " " + y[0]);

}

public static void increase(int[ ] x) {

for (inti= 0; ix.length; i++)

x[i]++;

}

public static void increase(int y) {

y++;

}

}

18)

______

A)

1 1

B)

0 0

C)

1 2

D)

2 1

E)

2 2

19)

When you return an array from a method, the method returns ______.

19)

______

A)

a copy of the array

B)

a copy of the first element

C)

the length of the array

D)

the reference of the array

20)

______is a construct that defines objects of the same type.

20)

______

A)

A class

B)

A method

C)

An object

D)

A data field

21)

An object is an instance of a ______.

21)

______

A)

data

B)

class

C)

program

D)

method

22)

______is invoked to create an object.

22)

______

A)

The main method

B)

A method with the void return type

C)

A method with a return type

D)

A constructor

23)

Analyze the following code.

public class Test {

int x;

public Test(String t) {

System.out.println("Test");

}

public static void main(String[ ] args) {

Test test= new Test();

System.out.println(test.x);

}

}

23)

______

A)

The program has a compile error because System.out.println method cannot be invoked from the constructor.

B)

The program has a compile error because Test does not have a default constructor.

C)

The program has a compile error because you cannot create an object from the class that defines the object.

D)

The program has a compile error because x has not been initialized.

24)

Which is the advantage of encapsulation?

24)

______

A)

It changes a class's contract without changing the implementation and causes no consequential changes to other code.

B)

Only public methods are needed.

C)

Making the class final causes no consequential changes to other code.

D)

It changes the implementation without changing a class's contract and causes no consequential changes to other code.

25)

When invoking a method with an object argument, ______is passed.

25)

______

A)

the contents of the object

B)

the object is copied, then the reference of the copied object

C)

a copy of the object

D)

the reference of the object

26)

Suppose s is a string with the value "java". What will be assigned to x if you execute the following code?

char x =s.charAt(4);

26)

______

A)

'v'

B)

'a'

C)

Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.

27)

What is the output of the following code?

public class Test {

public static void main(String[ ] args) {

String s1 = new String("Welcome to Java!");

String s2 = new String("Welcome to Java!");

if (s1 == s2)

System.out.println("s1 and s2 reference to the same String object");

else

System.out.println("s1 and s2 reference to different String objects");

}

}

27)

______

A)

s1 and s2 reference to different String objects

B)

s1 and s2 reference to the same String object

28)

What is the output of the following code?

public class Test {

public static void main(String[ ] args) {

String s1 = new String("Welcome to Java!");

String s2 = new String("Welcome to Java!");

if (s1.equals(s2))

System.out.println("s1 and s2 have the same contents");

else

System.out.println("s1 and s2 have different contents");

}

}

28)

______

A)

s1 and s2 have the same contents

B)

s1 and s2 have different contents

29)

What is the output of running class C?

class A {

public A() {

System.out.println(

"The default constructor of A is invoked");

}

}

class B extends A {

public B() {

System.out.println(

"The default constructor of B is invoked");

}

}

public class C {

public static void main(String[ ] args) {

B b= new B();

}

}

29)

______

A)

"The default constructor of B is invoked"

B)

"The default constructor of A is invoked"

C)

"The default constructor of A is invoked""The default constructor of B is invoked"

D)

Nothing displayed

E)

"The default constructor of B is invoked""The default constructor of A is invoked"

30)

Analyze the following code:

public class Test {

public static void main(String[ ] args) {

B b= new B();

b.m(5);

System.out.println("i is " +b.i);

}

}

class A {

inti;

public void m(inti) {

this.i=i;

}

}

class B extends A {

public void m(String s) {

}

}

30)

______

A)

The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

B)

The program has a runtime error on b.i, because iis not accessible from b.

C)

The program has a compilation error, because m is overridden with a different signature in B.

D)

The program has a compilation error, because b.m(5) cannot be invoked since the method m(int) is hidden in B.

(20 pts) Part 2. Java Programming CIS265/506 Exam1 –Spring 2012

Last Name: ______First name: ______

This problem is based on the GeometricObject–Rectangle–Circle classes discussed in class, see diagram.
Assume you are given an ArrayList holding various Rectangle objects. Write a method to be added to the Driver class to find the location of the rectangle having the largest side (either height or width side). The method
returns the position of the selected rectangle in the list, or -1 when the list is empty. The method’s signature
is given below.

public static intfindPositionOfRectangleWithLargestSide ( ArrayList list ) {

1)

B

2)

C

3)

D

4)

D

5)

D

6)

A

7)

C

8)

E

9)

C

10)

D

11)

C

12)

A

13)

B

14)

B

15)

B

16)

B

17)

C

18)

D

19)

D

20)

A

21)

B

22)

D

23)

B

24)

D

25)

D

26)

C

27)

A

28)

A

29)

C

30)

A