OCJP Questions

Q1.- Which code, inserted at line labeled "//some code goes her", allows the class Test to be compiled?

    1. class Util {
          public enum State{ACTIVE, DELETED, INACTIVE}
      }
      
      public class Test {     
          public static void main(String args[]) {
              //some code goes here       
          }
      }
      
    2. State state = State.INACTIVE;
    3. State state = INACTIVE;
    4. Util.State state = Util.State.INACTIVE;
    5. State state = Util.INACTIVE;

3

Q2.- Given the code. What is the result?

    1. import java.util.Collections;
      import java.util.LinkedList;
      
      public class TryMe {
          public static void main(String args[]) {
              LinkedList list = new LinkedList();
              list.add("BbB1");
              list.add("bBb2");
              list.add("bbB3");
              list.add("BBb4");
              Collections.sort(list);
              for (String str : list) {
                  System.out.print(str + ":");
              }
          }
      }
      
      
    2. BbB1:bBb2:bbB3:BBb4:" is printed.
    3. "BBb4:bbB3:bBb2:BbB1:" is printed.
    4. "BBb4:BbB1:bBb2:bbB3:" is printed.
    5. "bbB3:bBb2:BbB1:BBb4:" is printed.
    6. Compilation fails.
    7. An exception is thrown at runtime.

3

Q3.- Given the code. What is the result?

    1.       public void waitForSomething() {
            SomeClass o = new SomeClass();
              synchronized (o) {
                  o.wait();
                  o.notify();         
              }
          }
      
      
      
    2. This code may throw an InterruptedException
    3. This code may throw an IllegalStateException
    4. This code may throw a TimeOutException
    5. Reversing the ofrer of o.wait() and o.notify() will cause this method to complete

1

Q4.- What is true? (Choose three)

    1. A method with the same signature as a private final method in class Z can be implemented in a subclass of Z.
    2. A final method in class Z can be abstract if and only if Z is abstract.
    3. A protected method in class Z can be overriden by any subclass of Z.
    4. A private static method can be called only within other static methods in class Z.
    5. A non-static public final method in class Z can be overriden in any subclass of Z.
    6. A public static method in class Z can be called by a subclass of Z without explicitly referencing the class Z.

1,3,5

Q5.- Given the code. What is the result?

    1.               class Vehicle {
          public void printSound() {
              System.out.print("vehicle");
              }
          }
      
      class Car extends Vehicle {
          public void printSound() {
              System.out.print("car");
          }
       }
      
      class Bike extends Vehicle {
          public void printSound() {
              System.out.print("bike");
          }
      }
      
      public class Test {
          public static void main(String[] args) {
              Vehicle v = new Car();
              Car c = (Car) v;
              v.printSound();
              c.printSound();
          }   
      }
      
      
    2. Compilation fails.
    3. An exception is thrown at runtime.
    4. "vehiclecar" is printed.
    5. "vehiclebike" is printed.
    6. "carcar" is printed.
    7. "bikebike" is printed

1,3,5

Q6.- What should be called after the code below to suggest that the JVM expend effort toward recycling the memory used by the object a? (Select two)

    1.   BigObject a = MyFactory.createBigObject();
      a.doStuff();
      a = null;
    2. System.gc()
    3. Runtime.gc()
    4. System.freeMemory()
    5. Runtime.getRuntime().freeMemory()
    6. Runtime.getRuntime().gc()

1,5

Q7.- Given the code. Select correct calls of methods. (Select all that apply) import java.util.*;

    1. class Empty {   
      }
      
      class Extended extends Empty {  
      }
      
      public class TryMe {    
              public static void doStuff1(List list) {
                      // some code
              }
              public static void doStuff2(List list) {        
                      // some code
              }
              public static void doStuff3(List list) {
                      // some code            
              }
              
              public static void main(String args[]) {
                      List list1 = new LinkedList();
                      List list2 = new LinkedList();
                      
                      // more code here
              }
      }
      
      
    2. doStuff1(list1);
    3. doStuff2(list2);
    4. doStuff2(list1);
    5. doStuff3(list1);
    6. doStuff3(list2);
    7. doStuff1(list2);

1,2,3,4,5

Q8.- Given the code. What is the output?

    1. public class Hotel {
          private int roomNr;
          
          public Hotel(int roomNr) {
              this.roomNr = roomNr;
          }
          
          public int getRoomNr() {
              return this.roomNr;
          }
          
          static Hotel doStuff(Hotel hotel) {
              hotel = new Hotel(1);
              return hotel;
          }
          
          public static void main(String args[]) {
              Hotel h1 = new Hotel(100);
              System.out.print(h1.getRoomNr() + " ");
              Hotel h2 = doStuff(h1);
              System.out.print(h1.getRoomNr() + " ");
              System.out.print(h2.getRoomNr() + " ");
              h1 = doStuff(h2);
              System.out.print(h1.getRoomNr() + " ");
              System.out.print(h2.getRoomNr() + " ");
          }
      }
      
    2. 100 1 1 1 1
    3. 100 100 1 1 1
    4. 100 100 100 1 1
    5. 100 100 100 100 1
    6. 100 100 100 100 100

2

Q9.- Given the code. What is the output?

    1. public class Test {     
          int a = 10;
          public void doStuff(int a) {
              a += 1;
              System.out.println(++a);
          }
          public static void main(String args[]) {
              Test t = new Test();
              t.doStuff(3);
          }
      }
      
      
    2. 4
    3. 5
    4. 12
    5. 11

2

Q9.- Which are possible outputs? (Choose all that apply.)

    1. public static void main(String[] args) 
      { 
             try
       { 
             if(args.length == 0) throw new Exception(); 
       } 
       catch (Exception e) 
      { 
       System.out.print("done "); 
      doStuff(); // assume this method compiles 
      } 
      finally { 
      System.out.println("finally ");
       }  
      }
      
      
      
    2. done "
    3. "finally "
    4. "done finally "
    5. Compilation fails.
    6. No output is produced.

1,2,3