OCPJP 6 Mock Exam – 21

1 . Given:

AnInterface is an interface.
AnAdapter0 is a non-abstract, non-final class with a zero argument constructor.
An Adapterl is a non-abstract, non-final class without a zero argument constructor, but with a
constructor that takes one int argument.

Which two construct an anonymous inner class?

Choose the two below:

  1. AnAdapter1 aa = new AnAdapter1 () {}
  2. AnAdapter0 aa = new AnAdapter0 () {}
  3. AnAdapter0 aa = new AnAdapter0 (5) {}
  4. AnAdapter1 aa = new AnAdapter1 (5) {}
  5. AnInterface ai = new Anlnterface (5)) {

2 . What will be the output?

 class A {
 public String to String() {
 return ?4?;
 }
 }
 class B extends A {
 public String toString() {
 return super.toString() + ?3?;
 }
 }
 public class Test {
 public static void main (String[] args) {
 System.out.printIn(new B());
 }
 }

Choose the one below:

  1. Compilation succeeds and 4 is printed
  2. Compilation ????? is printed
  3. An error on line 9 cause compilation to fail
  4. An error on line 14 cause compilation to fail
  5. Compilation succeeds but an exception is thrown at line 9

3 . What will be the output?

 public class Method Over {
 public void set Var (int a, int b, float c) {
 }
 }

Choose the two below:

  1. private void set Var(int a, float c, int b) {}
  2. protected void set Var(int a, int b, float c) {}
  3. public int set Var(int a, float c, int b) {return a}
  4. public int set Var(int a, int b, float c) {return a}
  5. protected float set Var(int a, int b, float c) {return c}

4 . What will be the output?

 class A {
 public byte file Number () {
 return l;
 }
 }

 Class B extends A {
 public short getNumber() {
 return 2;
 }

 public short getNumber() {
 B b = new B();
 System.out.printIn(b.getNumber());
 }
 }

Choose the one below:

  1. Compilation succeeds and l is printed
  2. Compilation succeeds and 2 printed
  3. An error at line 8 cause compilation to fail
  4. An error at line 14 cause complication to fail
  5. Complication succeeds but an exception is thrown at line 14

5 . What will be the output?

 public class Foo {
 public static void main(String[] args) {
 StringBuffer a = new StringBuffer(?A?);
 StrinbBuffer b = new StringBuffer(?B?);
 operate (a, b);
 System.out.printIn(a + ?,? + b)
 }
 static void operate (StringBuffer x, StringBuffer y) {
 y.append(x);
 y = x;
 }
 }

Choose the one below:

  1. The code compiles and prints ?A,B?
  2. The code compiles and prints ?A,BA?
  3. The code compiles and prints ?AB,B?
  4. The code compiles and prints ?AB,AB?
  5. The code compiles and prints ?BA,BA?
  6. The code does not compile because ?+? cannot be overloaded for StringBuffer

6 . What will be the output?

 public class X {
 private static int a

 public static void main (String [] args) {
 modify (a)
 System.out.printIn(a) {
 }

 public static void modify (int a) {
 a++;
 }
 }

Choose the one below:

  1. The program runs and prints ?0?
  2. The program runs and prints ?1?
  3. The program runs but aborts with an exception
  4. An error ?possible undefined variable? at line 5 cause compilation to fail
  5. An error ?possible undefined variable? at line 10 cause compilation to fail

7 . What will be the output?

 public class Test {
 private static int j = 0;

 public static Boolean method B (int k) {
 j + = k;
 return true
 }

 public static void method A (int i) {
 Boolean b;
 b = I < 10 | method b (4)
 b = I < 10 || method b(8)
 }

 public static void main (String args []) {
 method A (0);
 System.out.printIn(i);
 }
 }

Choose the one below:

  1. The program prints ?0?
  2. The program prints ?4?
  3. The program prints ?8?
  4. The program prints ?12?
  5. The code does not compile

8 . Which two are equivalent?

  1. 16 > 4
  2. 16 / 2
  3. 16 * 4
  4. 16 >> 2
  5. 16 / 22
  6. 16 >>> 2

9 . What will be the output?

 public class X {
 public static void main (String [] args) {
 byte b = 1277;
 byte c = 126;
 byte d = b + C;
 }
 }

Choose the one below:

  1. Compilation succeed and d take the value 253
  2. Line 5 contains an error that prevents compilation
  3. Line 5 throws an exception indicating ?out of range?
  4. Line 3 and 4 contain errors that prevent compilation
  5. The compilation succeed and d takes the value of 1

10 . What will be the output?

 int index = 1;
 int[]foo = new int [3];
 int bar = foo [index];
 int baz = bar + index;

Choose the one below:

  1. baz has a value of 0
  2. baz has a value of 1
  3. baz has the value of 2
  4. An exception is thrown
  5. The code will not compile

Answers

1 : 2 & 4 is correct.

2 : 2 is correct.

3 : 1 & 3 is correct.

4 : 3 is correct.

5 : 2 is correct.

6 : 1 is correct.

7 : 2 is correct.

8 : 4 & 6 is correct.

9 : 2 is correct.

10 : 2 is correct.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*