OCP JP7 Exam Dump

1. Consider the following program:



class StrEqual {

public static void main(String []args) {

String s1 = "hi";

String s2 = new String("hi");

String s3 = "hi";

if(s1 == s2) {

System.out.println("s1 and s2 equal");

} else {

System.out.println("s1 and s2 not equal");

}

if(s1 == s3) {

System.out.println("s1 and s3 equal");

} else {

System.out.println("s1 and s3 not equal");

}

}

}

Which one of the following options provides the output of this program when executed?


a)

s1 and s2 equal

s1 and s3 equal

b)

s1 and s2 equal

s1 and s3 not equal

c)

s1 and s2 not equal

s1 and s3 equal

d)

s1 and s2 not equal

s1 and s3 not equal

2. Consider the following program:



class Point2D {

private int x, y;

public Point2D(int x, int y) {

x = x;

}

public String toString() {

return "[" + x + ", " + y + "]";

}

Chapter 2 Pretest



13

public static void main(String []args) {

Point2D point = new Point2D(10, 20);

System.out.println(point);

}

}

Which one of the following options provides the output of this program when executed?


a) point

b) Point

c) [0, 0]

d) [10, 0]

e) [10, 20]

3. Consider the following program:



class Increment {

public static void main(String []args) {

Integer i = 10;

Integer j = 11;

Integer k = ++i; // INCR

System.out.println("k == j is " + (k == j));

System.out.println("k.equals(j) is " + k.equals(j));

}

}

Which one of the following options correctly describes the behavior of this program?


a) When executed, this program prints

k == j is false

k.equals(j) is false

b) When executed, this program prints

k == j is true

k.equals(j) is false

c) When executed, this program prints

k == j is false

k.equals(j) is true

d) When executed, this program prints

k == j is true

k.equals(j) is true

e) When compiled, the program will result in a compiler error in the line marked with the comment INCR.

4. Consider the following program:



class ArrayCompare {

public static void main(String []args) {

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

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

System.out.println("arr1 == arr2 is " + (arr1 == arr2));

System.out.println("arr1.equals(arr2) is " + arr1.equals(arr2));

Chapter 2 Pretest



14

System.out.println("Arrays.equals(arr1, arr2) is " +

java.util.Arrays.equals(arr1, arr2));

}

}

Which one of the following options provides the output of this program when executed?


a) arr1 == arr2 is false

arr1.equals(arr2) is false

Arrays.equals(arr1, arr2) is true

b) arr1 == arr2 is true

arr1.equals(arr2) is false

Arrays.equals(arr1, arr2) is true

c) arr1 == arr2 is false

arr1.equals(arr2) is true

Arrays.equals(arr1, arr2) is true

d) arr1 == arr2 is true

arr1.equals(arr2) is true

Arrays.equals(arr1, arr2) is false

e) arr1 == arr2 is true

arr1.equals(arr2) is true

Arrays.equals(arr1, arr2) is true

5. Consider the following program:



class NullInstanceof {

public static void main(String []args) {

String str = null;

if(str instanceof Object) // NULLCHK

System.out.println("str is Object");

else

System.out.println("str is not Object");

}

}

Which one of the following options correctly describes the behavior of this program?


a) This program will result in a compiler error in line marked with comment NULLCHK.

b) This program will result in a NullPointerException in line marked with comment NULLCHK.

c) When executed, this program will print the following: str is Object.

d) When executed, this program will print the following: str is not Object.

6. Consider the following program:



interface Side { String getSide(); }

class Head implements Side {

public String getSide() { return "Head "; }

}

class Tail implements Side {

Chapter 2 Pretest



15

public String getSide() { return "Tail "; }

}

class Coin {

public static void overload(Head side) { System.out.print(side.getSide()); }

public static void overload(Tail side) { System.out.print(side.getSide()); }

public static void overload(Side side) { System.out.print("Side "); }

public static void overload(Object side) { System.out.print("Object "); }

public static void main(String []args) {

Side firstAttempt = new Head();

Tail secondAttempt = new Tail();

overload(firstAttempt);

overload((Object)firstAttempt);

overload(secondAttempt);

overload((Side)secondAttempt);

}

}

What is the output of this program when executed?


a) Head Head Tail Tail

b) Side Object Tail Side

c) Head Object Tail Side

d) Side Head Tail Side

7. Consider the following program:



class Overloaded {

public static void foo(Integer i) { System.out.println("foo(Integer)"); }

public static void foo(short i) { System.out.println("foo(short)"); }

public static void foo(long i) { System.out.println("foo(long)"); }

public static void foo(int ... i) { System.out.println("foo(int ...)"); }

public static void main(String []args) {

foo(10);

}

}

Which one of the following options correctly describes the output of this program?


a) foo(Integer)

b) foo(short)

c) foo(long)

d) foo(int ...)

8. Consider the following program:



class Base {

public static void foo(Base bObj) {

System.out.println("In Base.foo()");

bObj.bar();

}

Chapter 2 pretest



16

public void bar() {

System.out.println("In Base.bar()");

}

}

class Derived extends Base {

public static void foo(Base bObj) {

System.out.println("In Derived.foo()");

bObj.bar();

}

public void bar() {

System.out.println("In Derived.bar()");

}

}

class OverrideTest {

public static void main(String []args) {

Base bObj = new Derived();

bObj.foo(bObj);

}

}

What is the output of this program when executed?


a)

In Base.foo()

In Base.bar()

b)

In Base.foo()

In Derived.bar()

c)

In Derived.foo()

In Base.bar()

d)

In Derived.foo()

In Derived.bar()

9. Consider the following program:



class CannotFlyException extends Exception {}

interface Birdie {

public abstract void fly() throws CannotFlyException;

}

interface Biped {

public void walk();

}

abstract class NonFlyer {

public void fly() { System.out.print("cannot fly "); } // LINE A

}

Chapter 2 Pretest