The knowledge sharing zone - Guruzon.com

OCJP Core Java Questions

This page contains the lot of Core Java questions and answers for OCJP exam preparation. All these are object questions or multiple choice questions with program code snippet.
reviews

OCJP Questions and Answers : 1

Question : Given :

public class Person {
    private String name, comment;
    private int age;

    public Person(String n, int a, String c) {
        name = n;
        age = a;
        comment = c;
    }

    public boolean equals(Object o) {
        if (!(o instanceof Person))
            return false;
        Person p = (Person) o;
        return age == p.age && name.equals(p.name);
    }
}

What is the appropriate definition of the hashCode method in class Person?

    A.    return super.hashCode();
    B.    return name.hashCode() + age * 7;
    C.    return name.hashCode() + comment.hashCode() / 2;
    D.    return name.hashCode() + comment.hashCode() / 2 - age * 3;

Answer : B


SCJP Questions and Answers : 2

Question : Given :

1. public class Person {
2.  private String name;
3.  public Person(String name) { this.name = name; }
4.  public boolean equals(Person p) {
5.   return p.name.equals(this.name);
6.  }
7. }

Which statement is true?

A.   The equals method does NOT properly override the Object.equals method.
B.   Compilation fails because the private attribute p.name cannot be accessed in line 5.
C.   To work correctly with hash-based data structures, this class must also implement the hashCode method.
D.   When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent duplicates.

Answer : A


OCJP Questions and Answers : 3

Question : Given :

11. public static void parse(String str) {
12.  try {
13.   float f = Float.parseFloat(str);
14.  } catch (NumberFormatException nfe) {
15.     f = 0;
16.  } finally {
17.    System.out.println(f);
18.  }
19. }
20. public static void main(String[] args) {
21.  parse("invalid");
22. }

What is the result?

A.  0.0
B.  Compilation fails.
C.  A ParseException is thrown by the parse method at runtime.
D.  A NumberFormatException is thrown by the parse method at runtime.

Answer : B


SCJP Questions and Answers : 4

Question : Given :

10. class Line {
11.  public static class Point {}
12. }
13.
14. class Triangle {
15.  // insert code here
16. }

 Which code, inserted at line 15, creates an instance of the Point class defined in Line?

A.  Point p = new Point();
B.  Line.Point p = new Line.Point();
C.  The Point class cannot be instatiated at line 15.
D.  Line l = new Line() ; l.Point p = new l.Point();

Answer : B


OCJP Questions and Answers : 5

Question : Given :

10. package com.sun.scjp;
11. public class Geodetics {
12.  public static final double DIAMETER = 12756.32; // kilometers
13. }

 Which two correctly access the DIAMETER member of the Geodetics class? (Choose two.)

A.  import com.sun.scjp.Geodetics;
public class TerraCarta {
public double halfway()
{ return Geodetics.DIAMETER/2.0; }
B.  import static com.sun.scjp.Geodetics;
public class TerraCarta{
public double halfway() { return DIAMETER/2.0; } }
C.  import static com.sun.scjp.Geodetics.*;
public class TerraCarta {
public double halfway() { return DIAMETER/2.0; } }
D.  package com.sun.scjp;
public class TerraCarta {
public double halfway() { return DIAMETER/2.0; } }

Answer : A, C


SCJP Questions and Answers : 6

Question : Given :

10. public class Bar {
11.  static void foo( int... x ) {
12.   // insert code here
13.  }
14. }

 Which two code fragments, inserted independently at line 12, will allow the class to compile? (Choose
two.)

A.  foreach( x ) System.out.println(z);
B.  for( int z : x ) System.out.println(z);
C.  while( x.hasNext() ) System.out.println( x.next() );
D.  for( int i=0; i< x.length; i++ ) System.out.println(x[i]);

Answer : B, D


OCJP Questions and Answers : 7

Question : Given :

Which statement is true about the classes and interfaces in the exhibit?

1. public interface A {
2.   public void doSomething(String thing)
3. }

1. public class AImpl implements A {
2.   public void doSomething(String msg) { }
3. }

1. public class B {
2.   public A doit() {
3.     // more code here
4.   }
5.
6.   public String execute() {
7.     // more code here
8.   }
9. }

1. public class C extends B {
2.   public AImpl doit() {
3.     // more code here
4.   }
5.
6.   public Object execute() {
7.     // more code here
8.   }
9. }

A.   Compilation will succeed for all classes and interfaces.
B.   Compilation of class C will fail because of an error in line 2.
C.   Compilation of class C will fail because of an error in line 6.
D.   Compilation of class AImpl will fail because of an error in line 2.

Answer : C


SCJP Questions and Answers : 8

Question : Given :

1. public class Plant {
2.  private String name;
3.  public Plant(String name) { this.name = name; }
4.  public String getName() { return name; }
5. }
1. public class Tree extends Plant {
2.  public void growFruit() { }
3.  public void dropLeaves() { }
4. }

Which statement is true?

A.  The code will compile without changes.
B.  The code will compile if public Tree() { Plant(); } is added to the Tree class.
C.  The code will compile if public Plant() { Tree(); } is added to the Plant class.
D.  The code will compile if public Plant() { this("fern"); } is added to the Plant class.
E.  The code will compile if public Plant() { Plant("fern"); } is added to the Plant class.

Answer : 


OCJP Questions and Answers : 9

Question : Given :

11. public class Test {
12.  public enum Dogs {collie, harrier, shepherd};
13.  public static void main(String [] args) {
14.   Dogs myDog = Dogs.shepherd;
15.   switch (myDog) {
16.    case collie:
17.     System.out.print("collie ");
18.    case default:
19.     System.out.print("retriever ");
20.    case harrier:
21.     System.out.print("harrier ");
22.   }
23.  }
24. }

 What is the result?

A.  harrier
B.  shepherd
C.  retriever
D.  Compilation fails.
E.  retriever harrier
F.  An exception is thrown at runtime.

Answer : D


SCJP Questions and Answers : 10

Question : Given :

8. public class test {
9.  public static void main(String [] a) {
10.   assert a.length == 1;
11.  }
12. }

 Which two will produce an AssertionError? (Choose two.)

A.  java test
B.  java -ea test
C.  java test file1
D.  java -ea test file1
E.  java -ea test file1 file2
F.  java -ea:test test file1

Answer : B, E


OCJP Questions and Answers : 11

Question : Given :

10. interface Foo {}
11. class Alpha implements Foo {}
12. class Beta extends Alpha {}
13. class Delta extends Beta {
14.  public static void main( String[] args ) {
15.   Beta x = new Beta();
16.   // insert code here
17.  }
18. }

 Which code, inserted at line 16, will cause a java.lang.ClassCastException?

A. Alpha a = x;
B. Foo f = (Delta)x;
C. Foo f = (Alpha)x;
D. Beta b = (Beta)(Alpha)x;

Answer : B


SCJP Questions and Answers : 12

Question : Given :

31. // some code here
32. try {
33.  // some code here
34. } catch (SomeException se) {
35.  // some code here
36. } finally {
37.  // some code here
38. }

Under which three circumstances will the code on line 37 be executed? (Choose three.)

A. The instance gets garbage collected.
B. The code on line 33 throws an exception.
C. The code on line 35 throws an exception.
D. The code on line 31 throws an exception.
E. The code on line 33 executes successfully.

Answer : B, C, E 


OCJP Questions and Answers : 13

Question : Given :

11. class A {
12.  public void process() { System.out.print("A,"); }
13. class B extends A {
14.  public void process() throws IOException {
15.   super.process();
16.   System.out.print("B,");
17.   throw new IOException();
18.  }
19. public static void main(String[] args) {
20.  try { new B().process(); }
21.  catch (IOException e) { System.out.println("Exception"); }}

 What is the result?

A.  Exception
B.  A,B,Exception
C.  Compilation fails because of an error in line 20.
D.  Compilation fails because of an error in line 14.
E.  A NullPointerException is thrown at runtime.

Answer : D


SCJP Questions and Answers : 14

Question : Given :


11. public void someMethod(Object value) {
12.  // check for null value
...
20.  System.out.println(value.getClass());
21. }

What, inserted at line 12, is the appropriate way to handle a null value?

A.  assert value == null;
B.  assert value != null, "value is null";
C.  if (value == null) {
throw new AssertionException("value is null");
}
D.  if (value == null) {
throw new IllegalArgumentException("value is null");

Answer : D


OCJP Questions and Answers : 15

Question : Given :

11. static void test() throws Error {
12.   if (true) throw new AssertionError();
13.   System.out.print("test ");
14. }
15. public static void main(String[] args) {
16.   try { test(); }
17.   catch (Exception ex) { System.out.print("exception "); }
18.   System.out.print("end ");
19. }

What is the result?

A.   end
B.   Compilation fails.
C.   exception end
D.   exception test end
E.   A Throwable is thrown by main.
F.   An Exception is thrown by main.

Answer : E


SCJP Questions and Answers : 16

Question : Given :

11. Float pi = new Float(3.14f);
12. if (pi > 3) {
13.  System.out.print("pi is bigger than 3. ");
14. }
15. else {
16.  System.out.print("pi is not bigger than 3. ");
17. }
18. finally {
19.  System.out.println("Have a nice day.");
20. }

 What is the result?

A.   Compilation fails.
B.   pi is bigger than 3.
C.   An exception occurs at runtime.
D.   pi is bigger than 3. Have a nice day.
E.   pi is not bigger than 3. Have a nice day.

Answer : A


OCJP Questions and Answers : 17

Question : Given :

11. String test = "This is a test";
12. String[] tokens = test.split("\s");
13. System.out.println(tokens.length);

 What is the result?

A.   0
B.   1
C.   4
D.   Compilation fails.
E.   An exception is thrown at runtime.

Answer : D


SCJP Questions and Answers : 18

Question : Given :

11. public class Yikes {
12.
13. public static void go(Long n) {System.out.println("Long ");}
14. public static void go(Short n) {System.out.println("Short ");}
15. public static void go(int n) {System.out.println("int ");}
16. public static void main(String [] args) {
17.   short y = 6;
18.   long z = 7;
19.   go(y);
20.   go(z);
21.  }
22. }

 What is the result?

A.   int Long
B.   Short Long
C.   Compilation fails.
D.   An exception is thrown at runtime.

Answer : A


OCJP Questions and Answers : 19

Question : Given :

12. System.out.format("Pi is approximately %d.", Math.PI);

What is the result?

A.   Compilation fails.
B.   Pi is approximately 3.
C.   Pi is approximately 3.141593.
D.   An exception is thrown at runtime.

Answer : D


SCJP Questions and Answers : 20

Question : Given :

33. Date d = new Date(0);
34. String ds = "December 15, 2004";
35. // insert code here
36. try {
37.  d = df.parse(ds);
38. }
39. catch(ParseException e) {
40.  System.out.println("Unable to parse " + ds);
41. }
42. // insert code here too

What creates the appropriate DateFormat object and adds a day to the Date object?

A.   35. DateFormat df = DateFormat.getDateFormat();
42. d.setTime( (60 * 60 * 24) + d.getTime());
B.   35. DateFormat df = DateFormat.getDateInstance();
42. d.setTime( (1000 * 60 * 60 * 24) + d.getTime());
C.   35. DateFormat df = DateFormat.getDateFormat();
42. d.setLocalTime( (1000*60*60*24) + d.getLocalTime());
D.   35. DateFormat df = DateFormat.getDateInstance();
42. d.setLocalTime( (60 * 60 * 24) + d.getLocalTime());

Answer : B


OCJP Questions and Answers : 21

Question : Given :

12. NumberFormat nf = NumberFormat.getInstance();
13. nf.setMaximumFractionDigits(4);
14. nf.setMinimumFractionDigits(2);
15. String a = nf.format(3.1415926);
16. String b = nf.format(2);

 Which two statements are true about the result if the default locale is Locale.US? (Choose two.)

A.   The value of b is 2.
B.   The value of a is 3.14.
C.   The value of b is 2.00.
D.   The value of a is 3.141.
E.   The value of a is 3.1415.
F.   The value of a is 3.1416.
G.   The value of b is 2.0000.

Answer : C, F


SCJP Questions and Answers : 22

Question : Given :

10. class Foo {
11.  static void alpha() { /* more code here */ }
12.  void beta() { /* more code here */ }
13. }

 Which two statements are true? (Choose two.)

A.   Foo.beta() is a valid invocation of beta().
B.   Foo.alpha() is a valid invocation of alpha().
C.   Method beta() can directly call method alpha().
D.   Method alpha() can directly call method beta().

Answer : B, C


OCJP Questions and Answers : 23

Question : Given :

21. public String toString() {
22.  StringBuffer buffer = new StringBuffer();
23.  buffer.append('<');
24.  buffer.append(this.name);
25.  buffer.append('>');
26.  return buffer.toString();
27. }

Which statement is true?

A.   This code is NOT thread-safe.
B.   The programmer can replace StringBuffer with StringBuilder with no other changes.
C.   This code will perform poorly. For better performance, the code should be rewritten:
return "<" + this.name + ">";
D.   This code will perform well and converting the code to use StringBuilder will not enhance the performance.

Answer : B


SCJP Questions and Answers : 24

Question : Given :

1. package geometry;
2. public class Hypotenuse {
3.  public InnerTriangle it = new InnerTriangle();
4.  class InnerTriangle {
5.   public int base;
6.   public int height;
7.  }
8. }

 Which statement is true about the class of an object that can reference the variable base?

A.  It can be any class.
B.  No class has access to base.
C.  The class must belong to the geometry package.
D.  The class must be a subclass of the class Hypotenuse.

Answer : C


OCJP Questions and Answers : 25

Question : Given :

10: public class Hello {
11:  String title;
12:  int value;
13:  public Hello() {
14:   title += " World";
15:  }
16:  public Hello(int value) {
17:   this.value = value;
18:   title = "Hello";
19:   Hello();
20:  }
21: }


and:
30: Hello c = new Hello(5);
31: System.out.println(c.title);

What is the result?

A. Hello
B. Hello World
C. Compilation fails.
D. Hello World 5
E. The code runs with no output.
F. An exception is thrown at runtime.

Answer : C


SCJP Questions and Answers : 26

Question : Given :

1. class Super {
2.  private int a;
3.  protected Super(int a) { this.a = a; }
4. }

...
11. class Sub extends Super {
12.  public Sub(int a) { super(a); }
13.  public Sub() { this.a = 5; }
14. }

Which two, independently, will allow Sub to compile? (Choose two.)

A.   Change line 2 to:
public int a;
B.   Change line 2 to:
protected int a;
C.   Change line 13 to:
public Sub() { this(5); }
D.   Change line 13 to:
public Sub() { super(5); }
E. Change line 13 to:
public Sub() { super(a); }

Answer : C, D


OCJP Questions and Answers : 27

Question : Given :

1. class ClassA {
2.  public int numberOfInstances;
3.  protected ClassA(int numberOfInstances) {
4.   this.numberOfInstances = numberOfInstances;
5.  }
6. }
7. public class ExtendedA extends ClassA {
8.  private ExtendedA(int numberOfInstances) {
9.   super(numberOfInstances);
10.  }
11.  public static void main(String[] args) {
12.   ExtendedA ext = new ExtendedA(420);
13.   System.out.print(ext.numberOfInstances);
14.  }
15. }

 Which statement is true?

A.   420 is the output.
B.   An exception is thrown at runtime.
C.   All constructors must be declared public.
D.   Constructors CANNOT use the private modifier.
E.   Constructors CANNOT use the protected modifier.

Answer : A


SCJP Questions and Answers : 28

Question : Given :

1. interface A { public void aMethod(); }
2. interface B { public void bMethod(); }
3. interface C extends A,B { public void cMethod(); }
4. class D implements B {
5.  public void bMethod(){}
6. }
7. class E extends D implements C {
8.  public void aMethod(){}
9.  public void bMethod(){}
10.  public void cMethod(){}
11. }

What is the result?

A.  Compilation fails because of an error in line 3.
B.  Compilation fails because of an error in line 7.
C.  Compilation fails because of an error in line 9.
D.  If you define D e = new E(), then e.bMethod() invokes the version of bMethod() defined in Line 5.
E.  If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 5.
F.  If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 9.

Answer : F


OCJP Questions and Answers : 29

Question : Given :

1. public class Base {
2.  public static final String FOO = "foo";
3.  public static void main(String[] args) {
4.   Base b = new Base();
5.   Sub s = new Sub();
6.   System.out.print(Base.FOO);
7.   System.out.print(Sub.FOO);
8.   System.out.print(b.FOO);
9.   System.out.print(s.FOO);
10.   System.out.print(((Base)s).FOO);
11. } }
12. class Sub extends Base {public static final String FOO="bar";}

 What is the result?

A.   foofoofoofoofoo
B.   foobarfoobarbar
C.   foobarfoofoofoo
D.   foobarfoobarfoo
E.   barbarbarbarbar
F.   foofoofoobarbar
G.  foofoofoobarfoo

Answer : D


SCJP Questions and Answers : 30

Question : Given :

1. package util;
2. public class BitUtils {
3.  public static void process(byte[]) { /* more code here */ }
4. }

1. package app;
2. public class SomeApp {
3.  public static void main(String[] args) {
4.   byte[] bytes = new byte[256];
5.   // insert code here
6.  }
7. }

 What is required at line 5 in class SomeApp to use the process method of BitUtils?

A.  process(bytes);
B.  BitUtils.process(bytes);
C.  util.BitUtils.process(bytes);
D.  SomeApp cannot use methods in BitUtils.
E.  import util.BitUtils.*; process(bytes);

Answer : C


OCJP Questions and Answers : 31

Question : Given :

13. public class Pass {
14.  public static void main(String [] args) {
15.   int x = 5;
16.   Pass p = new Pass();
17.   p.doStuff(x);
18.   System.out.print(" main x = " + x);
19.  }
20.
21.  void doStuff(int x) {
22.   System.out.print(" doStuff x = " + x++);
23.  }
24. }

 What is the result?

A.  Compilation fails.
B.  An exception is thrown at runtime.
C.  doStuff x = 6 main x = 6
D.  doStuff x = 5 main x = 5
E.  doStuff x = 5 main x = 6
F.  doStuff x = 6 main x = 5

Answer : D


SCJP Questions and Answers : 32

Question : Given :

11. public static void test(String str) {
12.  if (str == null | str.length() == 0) {
13.    System.out.println("String is empty");
14.  } else {
15.    System.out.println("String is not empty");
16.  }
17. }

 And the invocation:

 31. test(null);

What is the result?

A.  An exception is thrown at runtime.
B.  "String is empty" is printed to output.
C.  Compilation fails because of an error in line 12.
D.  "String is not empty" is printed to output.

Answer : A


OCJP Questions and Answers : 33

Question : Given :

12. public class Yippee2 {
13.
14.  static public void main(String [] yahoo) {
15.   for(int x = 1; x < yahoo.length; x++) {
16.    System.out.print(yahoo[x] + " ");
17.   }
18.  }
19. }

and the command line invocation:

java Yippee2 a b c

What is the result?

A.  a b
B.  b c
C.  a b c
D.  Compilation fails.
E.  An exception is thrown at runtime.

Answer : B


SCJP Questions and Answers : 34

Question : Given :

1. public class GC {
2.  private Object o;
3.  private void doSomethingElse(Object obj) { o = obj; }
4.  public void doSomething() {
5.   Object o = new Object();
6.   doSomethingElse(o);
7.   o = new Object();
8.   doSomethingElse(null);
9.   o = null;
10.  }
11. }

 When the doSomething method is called, after which line does the Object created in line 5 become
 available for garbage collection?

A.  Line 5
B.  Line 6
C.  Line 7
D.  Line 8
E.  Line 9
F.  Line 10

Answer : D


OCJP Questions and Answers : 35

Question : Given :

Which three code fragments, added individually at line 29, produce the output 100? (Choose three.)

10.  class Ineer {
11.     private int x;
12.     public void setX( int x ) { this.x = x;
}
13.     public int getX( ) { return x; }
14.  }
15. 
16.  class Outer {
17.     private Inner y;
18.     public void setY( Inner y ) { this.y = 
y;  }
19.     public Ineer getY( )  {return y; }
20.  }
21. 
22.  public class Gamma {
23.     public static void main( String[] args 
) {
24.         Outer o = new Outer( );
25.          Inner i  = new Inner( );
26           int n = 10;
27.          i . setX( n );
28.          o . setY( I );
29.         // insert code here 
30.       System.out.println(o . getY( ) . getX( ) );
31.       }
32.   }

A.  n = 100;
B.  i.setX( 100 );
C.  o.getY().setX( 100 );
D.  i = new Inner(); i.setX( 100 );
E.  o.setY( i ); i = new Inner(); i.setX( 100 );
F.  i = new Inner(); i.setX( 100 ); o.setY( i );

Answer : B, C, F


SCJP Questions and Answers : 36

Question : Given :

15. public class Yippee {
16.  public static void main(String [] args) {
17.   for(int x = 1; x < args.length; x++) {
18.    System.out.print(args[x] + " ");
19.   }
20.  }
21. } a

nd two separate command line invocations:

java Yippee
java Yippee 1 2 3 4

What is the result?

A. No output is produced.
1 2 3
B. No output is produced.
2 3 4
C. No output is produced.
1 2 3 4
D. An exception is thrown at runtime.
1 2 3
E. An exception is thrown at runtime.
2 3 4
F. An exception is thrown at runtime.
1 2 3 4

Answer : B


OCJP Questions and Answers : 37

Question : Given :

A class games.cards.Poker is correctly defined in the jar file Poker.jar. A user
wants to execute the main method of Poker on a UNIX system using the command:

java games.cards.Poker

What allows the user to do this?

A.  put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java
B.  put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java/*.jar
C.  Put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java/Poker.jar
D.  put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include /stuff/java
E.  put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include /stuff/java/*.jar
F.  put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include /stuff/java/Poker.jar

Answer : C


SCJP Questions and Answers : 38

Question : Given :

1. package utils;
2.
3. public class Repetition {
4.  public static String twice(String s) { return s + s; }
5. }

and given another class Demo:

1. // insert code here
2.
3. public class Demo {
4.  public static void main(String[] args) {
5.   System.out.println(twice("pizza"));
6.  }
7. }

 

Which code should be inserted at line 1 of Demo.java to compile and run Demo to print "pizzapizza"?

A.  import utils.*;
B.  static import utils.*;
C.  import utils.Repetition.*;
D.  static import utils.Repetition.*;
E.  import utils.Repetition.twice();
F.  import static utils.Repetition.twice;
G.  static import utils.Repetition.twice;

Answer : F


OCJP Questions and Answers : 39

Question : Given :

11. public static void main(String[] args) {
12.  String str = "null";
13.  if (str == null) {
14.   System.out.println("null");
15.  } else (str.length() == 0) {
16.   System.out.println("zero");
17.  } else {
18.   System.out.println("some");
19.  }
20. }

 What is the result?

A.  null
B.  zero
C.  some
D.  Compilation fails.
E.  An exception is thrown at runtime.

Answer : D


SCJP Questions and Answers : 40

Question : Given :

11. static class A {
12.  void process() throws Exception { throw new Exception(); }
13. }
14. static class B extends A {
15.  void process() { System.out.println("B "); }
16. }
17. public static void main(String[] args) {
18.  A a = new B();
19.  a.process();
20. }

 What is the result?

A.   B
B.   The code runs with no output.
C.   An exception is thrown at runtime.
D.   Compilation fails because of an error in line 15.
E.   Compilation fails because of an error in line 18.
F.   Compilation fails because of an error in line 19.

Answer : F


OCJP Questions and Answers : 41

Question : Given :

11. public void testIfA() {
12.  if (testIfB("True")) {
13.   System.out.println("True");
14.  } else {
15.   System.out.println("Not true");
16.  }
17. }
18. public Boolean testIfB(String str) {
19.  return Boolean.valueOf(str);
20. }

 What is the result when method testIfA is invoked?

A.   True
B.   Not true
C.   An exception is thrown at runtime.
D.   Compilation fails because of an error at line 12.
E.   Compilation fails because of an error at line 19.

Answer : A


SCJP Questions and Answers : 42

Question : Given :

1. public class Boxer1{
2.  Integer i;
3.  int x;
4.  public Boxer1(int y) {
5.   x = i+y;
6.   System.out.println(x);
7.  }
8.  public static void main(String[] args) {
9.   new Boxer1(new Integer(4));
10.  }
11. }

 What is the result?

A.  The value "4" is printed at the command line.
B.  Compilation fails because of an error in line 5.
C.  Compilation fails because of an error in line 9.
D.  A NullPointerException occurs at runtime.
E.  A NumberFormatException occurs at runtime.
F.  An IllegalStateException occurs at runtime.

Answer : D


OCJP Questions and Answers : 43

Question : Given :

25. try {
26.  A a = new A();
27.  a.method1();
28. } catch (Exception e) {
29.  System.out.print("an error occurred");
30. }

 Which two statements are true if a NullPointerException is thrown on line 3 of class C? (Choose two.)

1. public class A {
2.    public void method1( ) {
3.       B b = new B( );
4.       b.method2( );
5.       // more code here
6.    }
7. }

1.  public class B {
2.    public void method2( ) {
3.       C c = new C( );
4.       c.method3( );
5.       // more code here
6.    } 
7.  }

1.  public class C {
2.     public void method3( ) {
3.        // more code here
4.     }
5.  }

 A.  The application will crash.
B.  The code on line 29 will be executed.
C.  The code on line 5 of class A will execute.
D.  The code on line 5 of class B will execute.
E.  The exception will be propagated back to line 27.

Answer : B, E


SCJP Questions and Answers : 44

Question : Given :

23. int z = 5;
24.
25. public void stuff1(int x) {
26.  assert (x > 0);
27.  switch(x) {
28.    case 2: x = 3;
29.    default: assert false; } }
30.
31. private void stuff2(int y) { assert (y < 0); }
32.
33. private void stuff3() { assert (stuff4()); }
34.
35. private boolean stuff4() { z = 6; return false; }

 Which statement is true?

A.   All of the assert statements are used appropriately.
B.   Only the assert statement on line 31 is used appropriately.
C.   The assert statements on lines 29 and 31 are used appropriately.
D.   The assert statements on lines 26 and 29 are used appropriately.
E.   The assert statements on lines 29 and 33 are used appropriately.
F.   The assert statements on lines 29, 31, and 33 are used appropriately.
G.   The assert statements on lines 26, 29, and 31 are used appropriately.

Answer : C


OCJP Questions and Answers : 45

Question : Given :

25. int x = 12;
26. while (x < 10) {
27. x--;
28. }
29. System.out.print(x);

What is the result?

A.  0
B.  10
C.  12
D.  Line 29 will never be reached.

Answer : C


SCJP Questions and Answers : 46

Question : Given :

33. try {
34.  // some code here
35. } catch (NullPointerException e1) {
36.  System.out.print("a");
37. } catch (RuntimeException e2) {
38.  System.out.print("b");
39. } finally {
40.  System.out.print("c");
41. }

 What is the result if a NullPointerException occurs on line 34?

A.  c
B.  a
C.  ab
D.  ac
E.  bc
F.  abc

Answer : D


OCJP Questions and Answers : 47

Question : Given :

10. public class Foo {
11.   static int[] a;
12.   static { a[0]=2; }
13.   public static void main( String[] args ) {}
14. }

Which exception or error will be thrown when a programmer attempts to run this code?

A.  java.lang.StackOverflowError
B.  java.lang.IllegalStateException
C.  java.lang.ExceptionInInitializerError
D.  java.lang.ArrayIndexOutOfBoundsException

Answer : C


SCJP Questions and Answers : 48

Question : Given :

11. public static void main(String[] args) {
12.  try {
13.    args = null;
14.    args[0] = "test";
15.    System.out.println(args[0]);
16.  } catch (Exception ex) {
17.    System.out.println("Exception");
18.  } catch (NullPointerException npe) {
19.    System.out.println("NullPointerException");
20.  }
21. }

What is the result?

A.  test
B.  Exception
C.  Compilation fails.
D.  NullPointerException

Answer : C


OCJP Questions and Answers : 49

Question : Given :

12. Date date = new Date();
13. df.setLocale(Locale.ITALY);
14. String s = df.format(date);

The variable df is an object of type DateFormat that has been initialized in line 11.

What is the result if this code is run on December 14, 2000?

A.  The value of s is 14-dic-2004.
B.  The value of s is Dec 14, 2000.
C.  An exception is thrown at runtime.
D.  Compilation fails because of an error in line 13.

Answer : D


SCJP Questions and Answers : 50

Question : Given :

d is a valid, non-null Date object
df is a valid, non-null DateFormat object set to the current locale

What outputs the current locale's country name and the appropriate version of d's date?

???A. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d));
B. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d));
C. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));
D. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));

Answer : B


OCJP Questions and Answers : 51

Question : Given :

1. package util;
2. public class BitUtils {
3.  private static void process(byte[] b) {}
4. }
1. package app;
2. public class SomeApp {
3.  public static void main(String[] args) {
4.   byte[] bytes = new byte[256];
5.   // insert code here
6.  }
7. }

What is required at line 5 in class SomeApp to use the process method of BitUtils?

A.  process(bytes);
B.  BitUtils.process(bytes);
C.  app.BitUtils.process(bytes);
D.  util.BitUtils.process(bytes);
E.  import util.BitUtils.*; process(bytes);
F.  SomeApp cannot use the process method in BitUtils.

Answer : F


SCJP Questions and Answers : 52

Question : Given :

11. rbo = new ReallyBigObject();
12. // more code here
13. rbo = null;
14. /* insert code here */

 Which statement should be placed at line 14 to suggest that the virtual machine expend effort toward
recycling the memory used by the object rbo?

A.  System.gc();
B.  Runtime.gc();
C.  System.freeMemory();
D.  Runtime.getRuntime().growHeap();
E.  Runtime.getRuntime().freeMemory();

Answer : A


OCJP Questions and Answers : 53

Question : Given :

11. public static void test(String str) {
12.  int check = 4;
13.  if (check = str.length()) {
14.    System.out.print(str.charAt(check -= 1) +", ");
15.  } else {
16.    System.out.print(str.charAt(0) + ", ");
17.  }
18. }

and the invocation:

21. test("four");
22. test("tee");
23. test("to");

 What is the result?

A. r, t, t,
B. r, e, o,
C. Compilation fails.
D. An exception is thrown at runtime.

Answer : C


SCJP Questions and Answers : 54

Question : Given :

11. public class Commander {
12.  public static void main(String[] args) {
13.   String myProp = /* insert code here */
14.    System.out.println(myProp);
15.  }
16. }

and the command line:

java -Dprop.custom=gobstopper Commander

Which two, placed on line 13, will produce the output gobstopper? (Choose two.)

A.  System.load("prop.custom");
B.  System.getenv("prop.custom");
C.  System.property("prop.custom");
D.  System.getProperty("prop.custom");
E.  System.getProperties().getProperty("prop.custom");

Answer : D, E


OCJP Questions and Answers : 55

Question : Given :

11. class Snoochy {
12.  Boochy booch;
13.  public Snoochy() { booch = new Boochy(this); }
14. }
15.
16. class Boochy {
17.  Snoochy snooch;
18.  public Boochy(Snoochy s) { snooch = s; }
19. }

And the statements:

21. public static void main(String[] args) {
22.  Snoochy snoog = new Snoochy();
23.  snoog = null;
24.  // more code here
25. }

 Which statement is true about the objects referenced by snoog, snooch, and booch immediately after line 23 executes?

A.  None of these objects are eligible for garbage collection.
B.  Only the object referenced by booch is eligible for garbage collection.
C.  Only the object referenced by snoog is eligible for garbage collection.
D.  Only the object referenced by snooch is eligible for garbage collection
E.  The objects referenced by snooch and booch are eligible for garbage collection.

Answer : E


SCJP Questions and Answers : 56

Question : Given :

A developer is creating a class Book, that needs to access class Paper. The Paper class is deployed in a JAR named myLib.jar. Which three, taken independently, will allow the developer to use the Paper class while compiling the Book class? (Choose three.)

A. The JAR file is located at $JAVA_HOME/jre/classes/myLib.jar.
B. The JAR file is located at $JAVA_HOME/jre/lib/ext/myLib.jar..
C. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes
/foo/myLib.jar/Paper.class.
D. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes
/foo/myLib.jar.
E. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -cp /foo/myLib.jar/Paper
Book.java.
F. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -d /foo/myLib.jar
Book.java
G. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -classpath
/foo/myLib.jar Book.java

Answer : B, D, G


OCJP Questions and Answers : 57

Question : Given :

11. // insert code here
12.  private N min, max;
13.  public N getMin() { return min; }
14.  public N getMax() { return max; }
15.  public void add(N added) {
16.   if (min == null || added.doubleValue() < min.doubleValue()) 17. min = added;
18.   if (max == null || added.doubleValue() > max.doubleValue()) 19. max = added;
20.  }
21. }

Which two, inserted at line 11, will allow the code to compile? (Choose two.)

A.  public class MinMax<?> {
B.  public class MinMax<? extends Number> {
C.  public class MinMax<N extends Object> {
D.  public class MinMax<N extends Number> {
E.  public class MinMax<? extends Object> {
F.  public class MinMax<N extends Integer> {

Answer : D, F


SCJP Questions and Answers : 58

Question : Given :

11. public abstract class Shape {
12.  private int x;
13.  private int y;
14.  public abstract void draw();
15.  public void setAnchor(int x, int y) {
16.   this.x = x;
17.   this.y = y;
18.  }
19. }

Which two classes use the Shape class correctly? (Choose two.)

A. public class Circle implements Shape {
private int radius;
}
B. public abstract class Circle extends Shape {
private int radius;
}
C. public class Circle extends Shape {
private int radius;
public void draw();
}
D. public abstract class Circle implements Shape {
private int radius;
public void draw();
}
E. public class Circle extends Shape {
private int radius;
public void draw() {/* code here */}
F. public abstract class Circle implements Shape {
private int radius;
public void draw() { /* code here */ }

 Answer : B, E


OCJP Questions and Answers : 59

Question : Given :

10. class Foo {
11.   static void alpha() { /* more code here */ }
12.   void beta() { /* more code here */ }
13. }

Which two statements are true? (Choose two.)

A.  Foo.beta() is a valid invocation of beta().
B.  Foo.alpha() is a valid invocation of alpha().
C.  Method beta() can directly call method alpha().
D.  Method alpha() can directly call method beta().

Answer : B, C


SCJP Questions and Answers : 60

Question : Given :

10. class One {
11.   public One() { System.out.print(1); }
12. }
13. class Two extends One {
14.   public Two() { System.out.print(2); }
15. }
16. class Three extends Two {
17.   public Three() { System.out.print(3); }
18. }
19. public class Numbers{
20.   public static void main( String[] argv ) { new Three(); }
21. }

 What is the result when this code is executed?

A.  1
B.  3
C.  123
D.  321
E.  The code runs with no output.

Answer : C


OCJP Questions and Answers : 61

Question : Given :

10. class Nav{
11.   public enum Direction { NORTH, SOUTH, EAST, WEST }
12. }
13. public class Sprite{
14.   // insert code here
15. }

Which code, inserted at line 14, allows the Sprite class to compile?

A.  Direction d = NORTH;
B.  Nav.Direction d = NORTH;
C.  Direction d = Direction.NORTH;
D.  Nav.Direction d = Nav.Direction.NORTH;

Answer : D


SCJP Questions and Answers : 62

Question : Given :

10. class One {
11.   public One foo() { return this; }
12. }
13. class Two extends One {
14.   public One foo() { return this; }
15. }
16. class Three extends Two {
17.   // insert method here
18. }

Which two methods, inserted individually, correctly complete the Three class? (Choose two.)

A.  public void foo() {}
B.  public int foo() { return 3; }
C.  public Two foo() { return this; }
D.  public One foo() { return this; }
E.  public Object foo() { return this; }

Answer : C, D


OCJP Questions and Answers : 63

Question : Given :

A JavaBeans component has the following field:

11. private boolean enabled;

Which two pairs of method declarations follow the JavaBeans standard for accessing this field? (Choose two.)

A. public void setEnabled( boolean enabled )
public boolean getEnabled()
B. public void setEnabled( boolean enabled )
public void isEnabled()
C. public void setEnabled( boolean enabled )
public boolean isEnabled()
D. public boolean setEnabled( boolean enabled )
public boolean getEnabled()

Answer : A, C 


SCJP Questions and Answers : 64

Question : Given :

11. public static void main(String[] args) {
12.  Object obj = new int[] { 1, 2, 3 };
13.  int[] someArray = (int[])obj;
14.  for (int i : someArray) System.out.print(i + " ");
15. }

What is the result?

A.  1 2 3
B.  Compilation fails because of an error in line 12.
C.  Compilation fails because of an error in line 13.
D.  Compilation fails because of an error in line 14.
E.  A ClassCastException is thrown at runtime.

Answer : A


OCJP Questions and Answers : 65

Question : Given :

1. package test;
2.
3. class Target {
4.  public String name = "hello";
5. }

What can directly access and change the value of the variable name?

A.  any class
B.  only the Target class
C.  any class in the test package
D.  any class that extends Target

Answer : C 


SCJP Questions and Answers : 66

Question : Given :

Which three statements are true? (Choose three.)

A.  A final method in class X can be abstract if and only if X is abstract.
B.  A protected method in class X can be overridden by any subclass of X.
C.  A private static method can be called only within other static methods in class X.
D.  A non-static public final method in class X can be overridden in any subclass of X.
E.  A public static method in class X can be called by a subclass of X without explicitly referencing the class X.
F.  A method with the same signature as a private final method in class X can be implemented in a subclass of X.
G.  A protected method in class X can be overridden by a subclass of A only if the subclass is in the same package as X.

Answer : B, E, F


OCJP Questions and Answers : 67

Question : Given :

11. static void test() throws RuntimeException {
12.  try {
13.   System.out.print("test ");
14.   throw new RuntimeException();
15.  }
16.  catch (Exception ex) { System.out.print("exception "); }
17. }
18. public static void main(String[] args) {
19.   try { test(); }
20.   catch (RuntimeException ex) { System.out.print("runtime "); }
21.   System.out.print("end ");
22. }

 What is the result?

A.  test end
B.  Compilation fails.
C.  test runtime end
D.  test exception end
E.  A Throwable is thrown by main at runtime.

Answer : D


SCJP Questions and Answers : 68

Question : Given :

11. public class Test {
12.  public static void main(String [] args) {
13.   int x = 5;
14.   boolean b1 = true;
15.   boolean b2 = false;
16.
17.   if ((x == 4) && !b2 )
18.      System.out.print("1 ");
19.      System.out.print("2 ");
20.   if ((b2 = true) && b1 )
21.      System.out.print("3 ");
22.   }
23. }

 What is the result?

A.  2
B.  3
C.  1 2
D.  2 3
E.  1 2 3
F.  Compilation fails.
G.  An exception is thrown at runtime.

Answer : D


OCJP Questions and Answers : 69

Question : Given :

1. public class Blip {
2.  protected int blipvert(int x) { return 0; }
3. }
4. class Vert extends Blip {
5.  // insert code here
6. }

 Which five methods, inserted independently at line 5, will compile? (Choose five.)

A.  public int blipvert(int x) { return 0; }
B.  private int blipvert(int x) { return 0; }
C.  private int blipvert(long x) { return 0; }
D.  protected long blipvert(int x) { return 0; }
E.  protected int blipvert(long x) { return 0; }
F.  protected long blipvert(long x) { return 0; }
G.  protected long blipvert(int x, int y) { return 0; }

Answer : A, C, E, F, G


SCJP Questions and Answers : 70

Question : Given :

10. public class SuperCalc {
11.  protected static int multiply(int a, int b) { return a * b;}
12. }
and:
20. public class SubCalc extends SuperCalc{
21.  public static int multiply(int a, int b) {
22.   int c = super.multiply(a, b);
23.   return c;
24.  }
25. }
and:
30. SubCalc sc = new SubCalc ();
31. System.out.println(sc.multiply(3,4));
32. System.out.println(SubCalc.multiply(2,2));

What is the result?

A. 12
4
B. The code runs with no output.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 21.
E. Compilation fails because of an error in line 22.
F. Compilation fails because of an error in line 31.

Answer : E


OCJP Questions and Answers : 71

Question : Given :

16. Date d = new Date(0L);
17. String ds = "December 15, 2004";
18. // insert code here

 What updates d's value with the date represented by ds?

A. 18. d = df.parse(ds);
B. 18. d = df.getDate(ds);
C. 18. try {
19. d = df.parse(ds);
20. } catch(ParseException e) { };
D. 18. try {
19. d = df.getDate(ds);
20. } catch(ParseException e) { };

 Answer : C


SCJP Questions and Answers : 72

Question : Given :

1. public class MyLogger {
2.  private StringBuilder logger = new StringBuuilder();
3.  public void log(String message, String user) {
4.    logger.append(message);
5.    logger.append(user);
6.  }
7. }

The programmer must guarantee that a single MyLogger object works properly for a multi-threaded system.

How must this code be changed to be thread-safe?

A.  synchronize the log method
B.  replace StringBuilder with StringBuffer
C.  replace StringBuilder with just a String object and use the string concatenation (+=) within the log method
D.  No change is necessary, the current MyLogger code is already thread-safe.

Answer : A


OCJP Questions and Answers : 73

Question : Given :

11. double input = 314159.26;
12. NumberFormat nf = NumberFormat.getInstance(Locale.ITALIAN);
13. String b;
14. //insert code here

 Which code, inserted at line 14, sets the value of b to 314.159,26?

A. b = nf.parse( input );
B. b = nf.format( input );
C. b = nf.equals( input );
D. b = nf.parseObject( input );

Answer : B


SCJP Questions and Answers : 74

Question : Given :

11. public class Person {
12.  private String name;
13.  public Person(String name) {
14.   this.name = name;
15.  }
16.  public boolean equals(Object o) {
17.   if ( ! o instanceof Person ) return false;
18.   Person p = (Person) o;
19.   return p.name.equals(this.name);
20.  }
21. }

Which statement is true?

A.  Compilation fails because the hashCode method is not overridden.
B.  A HashSet could contain multiple Person objects with the same name.
C.  All Person objects will have the same hash code because the hashCode method is not overridden.
D.  If a HashSet contains more than one Person object with name="Fred", then removing another Person, also with name="Fred", will remove them all.

Answer : B 


OCJP Questions and Answers : 75

Question : Given :

Which two statements are true about the hashCode method? (Choose two.)

A.  The hashCode method for a given class can be used to test for object equality and object inequality for that class.
B.  The hashCode method is used by the java.util.SortedSet collection class to order the elements within that set.
C.  The hashCode method for a given class can be used to test for object inequality, but NOT object equality, for that class.
D.  The only important characteristic of the values returned by a hashCode method is that the distribution of values must follow a Gaussian distribution.
E.  The hashCode method is used by the java.util.HashSet collection class to group the elements within that set into hash buckets for swift retrieval.

Answer : C, E


SCJP Questions and Answers : 76

Question : Given :

1. package com.company.application;
2.
3. public class MainClass {
4.   public static void main(String[] args) {}
5. }

And MainClass exists in the /apps/com/company/application directory. Assume the CLASSPATH environment variable is set to "." (current directory).

Which two java commands entered at the command line will run MainClass? (Choose two.)

A. java MainClass if run from the /apps directory
B. java com.company.application.MainClass if run from the /apps directory
C. java -classpath /apps com.company.application.MainClass if run from any directory
D. java -classpath . MainClass if run from the /apps/com/company/application directory
E. java -classpath /apps/com/company/application:. MainClass if run from the /apps directory
F. java com.company.application.MainClass if run from the /apps/com/company/application directory

Answer : B, C


OCJP Questions and Answers : 77

Question : Given :

1. interface DoStuff2 {
2.   float getRange(int low, int high); }
3.
4. interface DoMore {
5.   float getAvg(int a, int b, int c); }
6.
7. abstract class DoAbstract implements DoStuff2, DoMore { }
8.
9. class DoStuff implements DoStuff2 {
10.   public float getRange(int x, int y) { return 3.14f; } }
11.
12. interface DoAll extends DoMore {
13.   float getAvg(int a, int b, int c, int d); }

 What is the result?

A.  The file will compile without error.
B.  Compilation fails. Only line 7 contains an error.
C.  Compilation fails. Only line 12 contains an error.
D.  Compilation fails. Only line 13 contains an error.
E.  Compilation fails. Only lines 7 and 12 contain errors.
F.  Compilation fails. Only lines 7 and 13 contain errors.
G.  Compilation fails. Lines 7, 12, and 13 contain errors.

Answer : A


SCJP Questions and Answers : 78

Question : Given :

1. package com.sun.sjcp;
2. public class Commander {
3.  public static void main(String[] args) {
4.   // more code here
5.  }
6. }

 Assume that the class file is located in /foo/com/sun/sjcp/, the current directory is /foo/, and that the classpath contains "." (current directory).

 Which command line correctly runs Commander?

A. java Commander
B. java com.sun.sjcp.Commander
C. java com/sun/sjcp/Commander
D. java -cp com.sun.sjcp Commander
E. java -cp com/sun/sjcp Commander

Answer : B


OCJP Questions and Answers : 79

Question : Given :

15. public class Pass2 {
16.  public void main(String [] args) {
17.    int x = 6;
18.    Pass2 p = new Pass2();
19.    p.doStuff(x);
20.    System.out.print(" main x = " + x);
21.  }
22.
23.  void doStuff(int x) {
24.    System.out.print(" doStuff x = " + x++);
25.  }
26. }

 What is the result?

A.  Compilation fails.
B.  An exception is thrown at runtime.
C.  doStuff x = 6 main x = 6
D.  doStuff x = 6 main x = 7
E.  doStuff x = 7 main x = 6
F.  doStuff x = 7 main x = 7

Answer : B


SCJP Questions and Answers : 80

Question : Given :

11. interface DeclareStuff {
12.  public static final int EASY = 3;
13.  void doStuff(int t); }
14. public class TestDeclare implements DeclareStuff {
15.  public static void main(String [] args) {
16.    int x = 5;
17.    new TestDeclare().doStuff(++x);
18.   }
19.  void doStuff(int s) {
20.    s += EASY + ++s;
21.    System.out.println("s " + s);
22.   }
23. }

 What is the result?

A. s 14
B. s 16
C. s 10
D. Compilation fails.
E. An exception is thrown at runtime.

Answer : D


OCJP Questions and Answers : 81

Question : Given :

11. class Cup { }
12. class PoisonCup extends Cup { }
...
21. public void takeCup(Cup c) {
22.   if (c instanceof PoisonCup) {
23.     System.out.println("Inconceivable!");
24.   } else if (c instanceof Cup) {
25.     System.out.println("Dizzying intellect!");
26.   } else {
27.     System.exit(0);
28.   }
29. }

And the execution of the statements:

Cup cup = new PoisonCup();
takeCup(cup);

 What is the output?

A.  Inconceivable!
B.  Dizzying intellect!
C.  The code runs with no output.
D.  An exception is thrown at runtime.
E.  Compilation fails because of an error in line 22.

Answer : A


SCJP Questions and Answers : 82

Question : Given :

A UNIX user named Bob wants to replace his chess program with a new one, but he is not sure where the old one is installed. Bob is currently able to run a Java chess program starting from his home directory /home/bob using the command:

java -classpath /test:/home/bob/downloads/*.jar games.Chess

Bob's CLASSPATH is set (at login time) to:

/usr/lib:/home/bob/classes:/opt/java/lib:/opt/java/lib/*.jar

 What is a possible location for the Chess.class file?

A. /test/Chess.class
B. /home/bob/Chess.class
C. /test/games/Chess.class
D. /usr/lib/games/Chess.class
E. /home/bob/games/Chess.class
F. inside jarfile /opt/java/lib/Games.jar (with a correct manifest)
G. inside jarfile /home/bob/downloads/Games.jar (with a correct manifest)

 Answer : C


OCJP Questions and Answers : 83

Question : Given :

1. class TestA {
2.   public void start() { System.out.println("TestA"); }
3. }
4. public class TestB extends TestA {
5.   public void start() { System.out.println("TestB"); }
6.   public static void main(String[] args) {
7.     ((TestA)new TestB()).start();
8.   }
9. }

What is the result?

A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.

Answer : B 


SCJP Questions and Answers : 84

Question : Given :

35. String #name = "Jane Doe";
36. int $age = 24;
37. Double _height = 123.5;
38. double ~temp = 37.5;

 Which two statements are true? (Choose two.)

A. Line 35 will not compile.
B. Line 36 will not compile.
C. Line 37 will not compile.
D. Line 38 will not compile.

Answer : A, D


OCJP Questions and Answers : 85

Question : Given :

1. interface TestA { String toString(); }
2. public class Test {
3.   public static void main(String[] args) {
4.     System.out.println(new TestA() {
5.       public String toString() { return "test"; }
6.    });
7.   }
8. }

What is the result?

A.  test
B.  null
C.  An exception is thrown at runtime.
D.  Compilation fails because of an error in line 1.
E.  Compilation fails because of an error in line 4.
F.  Compilation fails because of an error in line 5.

Answer : A


SCJP Questions and Answers : 86

Question : Given :

11. public interface Status {
12.   /* insert code here */ int MY_VALUE = 10;
13. }

 Which three are valid on line 12? (Choose three.)

A.  final
B.  static
C.  native
D.  public
E.  private
F.  abstract
G.  protected

Answer : A, B, D


OCJP Questions and Answers : 87

Question : Given :

55. int [] x = {1, 2, 3, 4, 5};
56. int y[] = x;
57. System.out.println(y[2]);

 Which statement is true?

A.  Line 57 will print the value 2.
B.  Line 57 will print the value 3.
C.  Compilation will fail because of an error in line 55.
D.  Compilation will fail because of an error in line 56.

Answer : B


SCJP Questions and Answers : 88

Question : Given :

A programmer needs to create a logging method that can accept an arbitrary number of arguments. For example, it may be called in these ways:

logIt("log message1");
logIt("log message2","log message3");
logIt("log message4","log message5","log message6");

 Which declaration satisfies this requirement?

A.  public void logIt(String * msgs)
B.  public void logIt(String [] msgs)
C.  public void logIt(String... msgs)
D.  public void logIt(String msg1, String msg2, String msg3)

Answer : C


 

You would also like to read:

OCJP OOP Questions
OCJP Java IO Questions
OCJP Collections Questions
OCJP Java JDBC Questions

Comments :

Name :
Email :
Comment :
Verify Text :
capch image, refresh page if not loaded somehow