Saturday, December 22, 2012

OCJP QUIZ QUESTIONS

Given the code. What is the output?
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);
    }
}


0/p :   5 
------------------------------------------------------------------

Given the code. What is the result?
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class HashTest {
   
    private String str;
   
    public HashTest(String str) {
        this.str = str;
    }
   
    @Override
    public String toString() {
        return this.str;
    }
       
    public static void main(String args[]) {
        HashTest h1 = new HashTest("2");       
        String s1 = new String("1");       
       
        List<Object> list = new LinkedList<Object>();
        list.add(h1);
        list.add(s1);
       
        Collections.sort(list);
        for (Object o : list) {
            System.out.print(o + " ");
        }
    }
}
  A) "2 1" is printed.
 B) "1 2" is printed.
 C) Compilation fails.
 D) An exception is thrown at runtime
o/p :
-----------------------------------------------------------------

Given the code. What is the result?
    public static void main(String args[]) {
                String str = "null";
                if (str == null) {
                        System.out.print("1");
                } else if (str.length() == 0) {
                        System.out.print("2");
                } else {
                        System.out.print("3");
                }
        }
       
        A) Compilation fails.
 B) "1" is printed.
 C) "2" is printed.
 D) "3" is printed.
 E) An exception is thrown at runtime.

 --------------------------------------------------------------
 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)
BigObject a = MyFactory.createBigObject();
a.doStuff();
a = null;
 A) System.gc()
 B) Runtime.gc()
 C) System.freeMemory()
 D) Runtime.getRuntime().freeMemory()
 E) Runtime.getRuntime().gc()

 --------------------------------------------------------------
 class Hotel {
    public void book() throws Exception {
        throw new Exception();
    }
}

public class SuperHotel extends Hotel  {
    public void book() {
        System.out.print("booked");
    }
   
    public static void main(String args[]) {
        Hotel h = new SuperHotel();
        h.book();
    }  
}
 A) An exception is thrown at runtime.
 B) Compilation fails.
 C) The code runs without any output.
 D) "boked" is printed.
 ------------------------------------------------------------
 class Hotel {
    public void book() throws Exception {
        throw new Exception();
    }
}

public class SuperHotel extends Hotel  {
    public void book() {
        System.out.print("booked");
    }
   
    public static void main(String args[]) {
        Hotel h = new SuperHotel();
        h.book();
    }  
}

-----------------------------------------------------------
Given the code. What is the result?
public class Test {
    private static void doStuff(String str) {
        int var = 4;
        if (var == str.length()) {
            System.out.print(str.charAt(var--) + " ");
        }
        else {
            System.out.print(str.charAt(0) + " ");
        }
    }
    public static void main(String args[]) {
        doStuff("abcd");
        doStuff("efg");
        doStuff("hi");
    }
}
 A) Compilation fails.
 B) An exception is thrown at runtime.
 C) d e h
 D) d f i
 E) c f i
 F) c e h
 --------------------------------------------------------
 Given the code. Which statements are true? (Select two)
public class Hotel {        

        public static void book() {
                //some code goes here
        }
       
        public void cancelBooking() {
                //some code goes here
        }      
}     
 A) Method book() can directly call method cancelBooking()
 B) Method cancelBooking() can directly call method book()
 C) Hotel.book() is a valid invocation of book()
 D) Hotel.cancelBooking() is a valid invocation of cancelBooking()

 expalnation :

                public class StaticEx1 {        

        public static void book() {
              // cancelBooking();         
        }
       
        public void cancelBooking() {
                //some code goes here
   
        }      
}  
 if we call method cancelBooking in book() method we will get the following output:
StaticEx1.java:4: non-static method cancelBooking() cannot be referenced from a
static context
               cancelBooking();
               ^
1 error

so Correct answer is

    public class StaticEx1 {        

        public static void book() {
                
        }
       
        public void cancelBooking() {
                //some code goes here
                book();
        }      
}  
 i.e we can call a static method in non static context. But not a non-static method in static context.


 class StaticEx {        

        public static void book() {
              // cancelBooking();
        System.out.println(" Hi! I am a static method ");

        }
       
        public void cancelBooking() {
                //some code goes here
        book();
        System.out.println(" Hi! I am a non-static method ");
        }      
}     
class StaticEx1
{
    public static void main(String[] args)
    {
        StaticEx.book();
    //    StaticEx.cancelBooking(); //a non-static method cannot be referenced from a static context.
    }

}
 ------------------------------------------------------------------

Given the code. What is the result?
public class Cruiser implements Runnable {
    public static void main(String[] args) {
        Thread a = new Thread(new Cruiser());
        a.start();
       
        System.out.print("Begin");
        a.join();
        System.out.print("End");
    }
   
    public void run() {
        System.out.print("Run");
    }
}
 A) Compilation fails.
 B) An exception is thrown at runtime.
 C) "BeginRunEnd" is printed.
 D) "BeginEndRun" is printed.
 E) "BeginEnd" is printed.
 -----------------------------------------------------------------

 public class TryMe {
   
    public static void printB(String str) {
         System.out.print(Boolean.valueOf(str) ? "true" : "false");
    }
   
    public static void main(String args[]) {
        printB("tRuE");
        printB("false");
    }
}
-------------------------------------------------------------------
Given the code. What is the result?
    public static void main(String args[]) {
        String str = null;
        if (str == null) {
            System.out.print("1");
        } else (str.length() == 0) {
            System.out.print("2");
        } else {
            System.out.print("3");
        }
    }
 A) Compilation fails.
 B) "1" is printed.
 C) "2" is printed.
 D) "3" is printed.
 E) An exception is thrown at runtime.
-----------------------------------------------------------------
Given the code. What is the result?
public class Hotel  {
   
    private static void book() {
        System.out.print("book");
    }
   
    public static void main(String[] args) {
        Thread.sleep(1);
        book();
    }
}
 A) Compilation fails.
 B) An exception is thrown at runtime.
 C) "book" is printed.
 D) The code executes normally, but nothing is printed.
----------------------------------------------------------------
import java.util.HashSet;

public class HashTest {
   
    private String str;
   
    public HashTest(String str) {
        this.str = str;
    }
       
    @Override
    public int hashCode() {            
        return this.str.hashCode();
    }
   
    @Override
    public boolean equals(Object obj) {
        return this.str.equals(obj);
    }
   
    public static void main(String args[]) {
        HashTest h1 = new HashTest("1");
        HashTest h2 = new HashTest("1");
        String s1 = new String("2");
        String s2 = new String("2");
       
        HashSet<Object> hs = new HashSet<Object>();
        hs.add(h1);
        hs.add(h2);
        hs.add(s1);
        hs.add(s2);
       
        System.out.print(hs.size());
    }
}
)
 A)"4" is printed.
 B) "3" is printed.
 C) "2" is printed.
 D) Compilation fails.
 E) An exception is thrown at runtime.
        
------------------------------------------------------------------------

Given the code. What is the result?
    public static void main(String args[]) {
        try {
            String arr[] = new String[10];
            arr = null;
            arr[0] = "one";
            System.out.print(arr[0]);
        } catch(NullPointerException nex) {
            System.out.print("null pointer exception");
        } catch(Exception ex) {
            System.out.print("exception");
        }
    }
 A) "one" is printed.
 B) "exception" is printed.
 C) "null pointer exception" is printed. // correct answer
 D) Compilation fails.
-----------------------------------------------------------------------

.

Which of the following lines will print false?

1.public class MyClass
2.{
3.     static String s1 = "I am unique!";
4.     public static void main(String[] args)
5.     {
6.          String s2 = "I am unique!";
7.          String s3 = new String(s1);
8.          System.out.println(s1 == s2);
9.          System.out.println(s1.equals(s2));
10.         System.out.println(s3 == s1);
11.         System.out.println(s3.equals(s1));
12.     }
13.}
     a.  Line 8   
     b.  Line 9   
     c.  Line 10   
     d.  Line 11   
     e.  None of these
     -------------------------------------------------------------------
     Given the code. What is the result?
import java.util.Collections;
import java.util.LinkedList;

public class TryMe {
    public static void main(String args[]) {
        LinkedList<String> list = new LinkedList<String>();
        list.add("BbB1");
        list.add("bBb2");
        list.add("bbB3");
        list.add("BBb4");
        Collections.sort(list);
        for (String str : list) {
            System.out.print(str + ":");
        }
    }
}
 A) "BbB1:bBb2:bbB3:BBb4:" is printed.
 B) "BBb4:bbB3:bBb2:BbB1:" is printed.
 C) "BBb4:BbB1:bBb2:bbB3:" is printed. // correct answer
 D) "bbB3:bBb2:BbB1:BBb4:" is printed
 E) Compilation fails.
 F) An exception is thrown at runtime.
 --------------------------------------------------------------------
 Give the code. What is the result?
class Hotel {
    public int bookings;
    public void book() {
        bookings++;
    }
}

public class SuperHotel extends Hotel {
    public void book() {
        bookings--;
    }
   
    public void book(int size) {
        book();
        super.book();
        bookings += size;
    }
   
    public static void main(String args[]) {
        SuperHotel hotel = new SuperHotel();
        hotel.book(2);
        System.out.print(hotel.bookings);
    }
}
 A) Compilation fails.
 B) An exception is thrown at runtime.
 C) 0
 D) 1
 E) 2
 F) -1
 -----------------------------------------------------------------------------
 Given the code. What is the result?
public class Cruiser implements Runnable {
   
    public void run() {
        System.out.print("go");
    }
   
    public static void main(String arg[]) {
        Thread t = new Thread(new Cruiser());
        t.run();
        t.run();
        t.start();
    }
}
 A) Compilation fails.
 B) An exception is thrown at runtime.
 C) "go" is printed
 D) "gogogo" is printed   // cORRECT ANSWER
 E) "gogo" is printed
 ================================================================================
 Given the code. What is the result?
class Small {
    public Small() {
        System.out.print("a ");
    }
}

class Small2  extends Small {
    public Small2() {
        System.out.print("b ");
    }
}

class Small3 extends Small2 {
    public Small3() {
        System.out.print("c ");
    }
}

public class Test {    
    public static void main(String args[]) {
        new Small3();
    }
}
 A) a
 B) c
 C) a b c
 D) c b a
 E) The code runs without output.
---------------------------------------------------------------------------------

class Test2{
public static void main(String args[]) {
      Object myObj = new String[]{"one", "two", "three"};
 {
          for (String s : (String[])myObj) System.out.print(s + ".");
      }
  }
}

o/p : Error at line 3  or one.two.three if declaration is ended with semicoaln in line 3.
----------------------------------------------------------------------------------
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class TryMe3 {
    public static void main(String args[]) {
        List list = new LinkedList<String>();
        list.add("one");
        list.add("two");
        list.add("three");
       
        Collections.reverse(list);
        Iterator iter = list.iterator();
       
        for (Object o : list) {
            System.out.print(o + " ");
        }
//    System.out.println(iter);

    }
}
 three two one is output
 -------------------------------------------------------------------------------
 Given the code. What is the result?
import java.io.*;

public class Hotel implements Serializable {
    private transient Room room = new Room();
   
    public static void main(String[] args) {
        Hotel h = new Hotel();
        try {
            FileOutputStream fos = new FileOutputStream("Hotel.dat");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(h);
            oos.close();
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

class Room {
}
 A) Compilation fails.
 B) An exception is thrown at runtime.
 C) An instance of Hotel is serialized.
 D) An instance of Hotel and an instance of Room are both serialized.
 -----------------------------------------------------------------------------
 Give a piece of code. Which two are possible results? (Select two)
public class Cruiser {
    private int a = 0;
   
    public void foo() {
        Runnable r = new LittleCruiser();
        new Thread(r).start();
        new Thread(r).start();
    }
   
    public static void main(String arg[]) {
        Cruiser c = new Cruiser();
        c.foo();
    }
   
    public class LittleCruiser implements Runnable {
        public void run() {
            int current = 0;
            for (int i = 0; i < 4; i++) {
                current = a;
                System.out.print(current + ", ");
                a = current + 2;
            }
        }
    }
}
 A) 0, 2, 4, 0, 2, 4, 6, 6,
 B) 0, 2, 4, 6, 8, 10, 12, 14,
 C) 0, 2, 4, 6, 8, 10, 2, 4,
 D) 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
 E) 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
 ===============================================================================
 package com.mycompany;

public class Hotel  {
    public int roomNr = 100;
}
 A) Only the Hotel class.
 B) Any class.
 C) Any class in com.mycompany package.
 D) Any class that extends Hotel.
 --------------------------------------------------------------------------------
 Given the exhibit. What is the result?
public class Hotel {
   
    public static void book(short a) {
        System.out.print("short ");
    }
   
    public static void book(Short a) {
        System.out.print("SHORT ");
    }
   
    public static void book(Long a) {
        System.out.print("LONG ");
    }
   
    public static void main(String[] args) {
        short shortRoom = 1;
        int intRoom = 2;
       
        book(shortRoom);
        book(intRoom);
    }
}
 A) SHORT LONG
 B) short LONG
 C) Compilation fails
 D) An exception is thrown at runtime
 -----------------------------------------------------------------------------
 Given the code. What is the output?
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() + " ");
    }
}
 A) 100 1 1 1 1
 B) 100 100 1 1 1
 C) 100 100 100 1 1
 D) 100 100 100 100 1
 E) 100 100 100 100 100
-------------------------------------------------------------------------
Given the code. What is the result?
public class Test {
    private static void doStuff(String str) {
        int var = 4;
        if (var == str.length()) {
            System.out.print(str.charAt(--var) + " ");
        }
        else {
            System.out.print(str.charAt(0) + " ");
        }
    }
    public static void main(String args[]) {
        doStuff("abcd");
        doStuff("efg");
        doStuff("hi");
    }
}
 A) Compilation fails.
 B) An exception is thrown at runtime.
 C) d e h
 D) d f i
 E) c f i
 F) c e h
 -----------------------------------------------------------------------
 The code below is executed by "java -Dmyprop=myprop Test". Which two, placed instead of "//some code goes here", will produce the output "myprop"? (Choose two)
public class Test {
    public static void main(String args[]) {
        String prop = //some code goes here//
        System.out.print(prop);
    }
}
 A) System.getEnv("myprop");
 B) System.load("myprop");
 C) System.property("myprop");
 D) System.getProperty("myprop");
 E) System.get("myprop");
 F) System.getProperties().getProperty("myprop");
 ---------------------------------------------------------------------------
 Given the code. Select correct calls of methods. (Select all that apply)
import java.util.*;

class Empty {  
}

class Extended extends Empty { 
}

public class TryMe {   
        public static void doStuff1(List<Empty> list) {
                // some code
        }
        public static void doStuff2(List list) {       
                // some code
        }
        public static void doStuff3(List<? extends Empty> list) {
                // some code           
        }
       
        public static void main(String args[]) {
                List<Empty> list1 = new LinkedList<Empty>();
                List<Extended> list2 = new LinkedList<Extended>();
               
                // more code here
        }
}
 A) doStuff1(list1);
 B) doStuff2(list2);
 C) doStuff2(list1);
 D) doStuff3(list1);
 E) doStuff3(list2);
 F) doStuff1(list2);
---------------------------------------------------------------------------------
Given the code. What is the result after the class TryMe execution?
class A {
    public void doA() {
        B b = new B();
        b.dobB();
        System.out.print("doA");
    }
}

class B {
    public void dobB() {
        C c = new C();
        c.doC();
        System.out.print("doB");
    }
}

class C {
    public void doC() {
        if (true)
            throw new NullPointerException();
        System.out.print("doC");
    }
}

public class TryMe {

    public static void main(String args[]) {
        try {
            A a = new A();
            a.doA();
        } catch (Exception ex) {
            System.out.print("error");
        }
    }
}
 A) "doCdoBdoA" is printed
 B) "doAdoBdoC" is printed
 C) "doBdoAerror" is printed
 D) "error" is printed
 E) nothing is printed
 ---------------------------------------------------------------------
 Given:
System.out.printf("Pi = %f and E = %b", Math.PI, Math.E).
Place values where they would appear in the output
Pi = 3.141593 and E = 2.718282
3 2 true false

--------------------------------------------------------------------
Given the code. What is the result?
public class SomeClass {
    private int value = 1;
   
    public int getValue() {
        return value;
    }
   
    public void changeVal(int value) {
        value = value;
    }

    public static void main(String args[]) {
        int a = 2;
        SomeClass c = new SomeClass();
        c.changeVal(a);
        System.out.print(c.getValue());
    }
}
 A) "1" is printed // correct answer
 B) "2" is printed
 C) Compilation fails
 D) An exception is thrown at runtime
 -------------------------------------------------------------------
Which of the following lines will print false?

1.public class MyClass
2.{
3.     static String s1 = "I am unique!";
4.     public static void main(String[] args)
5.     {
6.          String s2 = "I am unique!";
7.          String s3 = new String(s1);
8.          System.out.println(s1 == s2);
9.          System.out.println(s1.equals(s2));
10.         System.out.println(s3 == s1);
11.         System.out.println(s3.equals(s1));
12.     }
13.}
     a.  Line 8   
     b.  Line 9   
     c.  Line 10// correct answer   
     d.  Line 11   
     e.  None of these   
-------------------------------------------------------------------
What is the result of compiling and running the following code?

public class IntegerTest
{
     public static void main(String[] argv)
     {
          Integer i1 = new Integer("1");
          Integer i2 = new Integer("2");
          Integer i3 = Integer.valueOf("3");
          int i4 = i1 + i2 + i3;
          System.out.println(i4);
     }
}
     a.  Compiler error: Integer class has no constructors which take a String argument   
     b.  Compiler error: Incorrect argument passed to the valueOf method   
     c.  Compiler error: Integer objects cannot work with + operator   
     d.  Prints 6    // CORRECT ANSWER
     e.  Prints 123

No comments:

Post a Comment