OCPJP 6 Mock Exam – 2

SHARE & COMMENT :

Q 1 : What is the output?

import java.util.*;
public class Test1{
    public static void main(String a[]){
       Set s = new TreeSet();
       s.add(new Person(20));
       s.add(new Person(10));
       System.out.println(s);
    }
}
class Person{
   Person(int i){}
}

Choose the one below:

  1. 10 20
  2. ClassCastException
  3. Compiler Error
  4. Compiler Error

Q 2 : What is the output?

import java.util.*;
public class Test2{
    public static void main(String a[]){
        Map s = new Hashtable();
        s.put(null,null);
        System.out.println(s);
    }
}

Choose the one below:

  1. [null = null]
  2. NullPointerException
  3. null
  4. []

Q 3 : What is the output?

import java.util.*;
public class Test3{
   public static void main(String a[]){
      Map s = new WeakHashMap(10);
      s.put(null,null);
      System.out.println(s);
   }
}

Choose the one below:

  1. [null = null]
  2. NullPointerException
  3. null
  4. []

Q 4 : What is the output?

import java.util.*;
public class Test4{
    public static void main(String a[]){
       Map s = new LinkedHashMap();
       s.put("1","one");
       s.put("3","three");
       s.put("2","two");
       System.out.println(s);
    }
}

Choose the one below:

  1. [1=one,3=three,2=two]
  2. NullPointerException
  3. [1=one,2=two,3=three]
  4. []

Q 5 : What is the output?

import java.util.*;
public class Test5{
    public static void main(String a[]){
       Map s = new HashMap();
       s.put("1","one");
       s.put("3","three");
       s.put("2","two");
       System.out.println(s);
    }
}

Choose the one below:

  1. [1=one,3=three,2=two]
  2. [3=three,2=two,1=one]
  3. cannot predict the order
  4. []

Q 6 : What is the output?

public class Test6{
    public static void method(float f){
       System.out.println("Float");
    }
    public static void method(double f){
       System.out.println("Double");
    }
    public static void main(String a[]){
       float f1 = 2.0f;
       float f2 = 2.0f;
       method(1.0);
       method(1.0f);
       method(1.0f*2.0f);
       method(1.0f*2.0);
       method(f1*f2);
    }
}

Choose the one below:

  1. Double Double Double Double Double
  2. Float Float Float Float Float
  3. Double Float Float Double Float
  4. Double Float Float Double Double

Q 7 : What is the output?

public class Test7{
   public static void method(byte b){
      System.out.println("Byte");
   }
   public static void method(int i){
      System.out.println("Int");
   }
   public static void main(String a[]){
      byte b = 1;
      method(1);
      method(128);
      method((byte)128);
      method(b);
   }
}

Choose the one below:

  1. Byte Int Int Byte
  2. Byte Int Int Byte
  3. Byte Int Byte Byte
  4. Int Int Byte Byte

Q 8 : Which of the following are correct?

public class Test8{
    public static void main(String a[]){
       byte b = 1;
       char c = 2;
       short s = 3;
       int i = 4;

       c = b; // 1
       s = b; // 2
       i = b; //3
       s = c * b; //4
    }
}

Choose the one below:

  1. Error at mark 1
  2. Error at mark 2
  3. Error at mark 3
  4. Error at mark 4

Q 9 : Which of the following are correct?

public class Test9{
public static void main(String a[]){
final byte b = 1;
char c = 2;
short s = 3;
int i = 4;

c = b; // 1
s = b; // 2
i = b; //3
s = c * b; //4
}
}

Choose the one below:

  1. Error at mark 1
  2. Error at mark 2
  3. Error at mark 3
  4. Error at mark 4

Q 10 : What is output?

public class Test10{
     public static void main(String a[]){
        String s1 = "Sun";
        String s2 = "Java";
        s1.concat(s2);
        System.out.println(s1);
     }
}

Choose the one below:

  1. Sun
  2. Java
  3. SunJava
  4. JavaSun

Q 11 : What is output?

public class Test11{
    public static void main(String a[]){
       Integer i1 = new Integer(127);
       Integer i2 = new Integer(127);
       Long l = new Long(127);
       System.out.println(i1 == i2);
       System.out.println(i1.equals(i2));
       System.out.println(i1.equals(l));
    }
}

Choose the one below:

  1. false true true
  2. true true true
  3. false true false
  4. Compiler Error

Q 12 : What is output?

public class Test12{
   public static void main(String a[]){
       byte b = 100;
       Byte b1= new Byte(100);
       Byte b2 = new Byte(b);
       System.out.println(b1 == b2);
       System.out.println(b1.equals(b2));
   }
}

Choose the one below:

  1. false true
  2. true false
  3. true true
  4. Compiler Error

Q 13 : What is output?

public class Test13{
   public static void method(String s){
      System.out.println("String Version");
   }
   public static void method(StringBuffer sb){
      System.out.println("String Buffer Version");
   }
   public static void main(String a[]){
      method(null);
   }
}

Choose the one below:

  1. String Version
  2. String Buffer Version
  3. Runtime Exception
  4. Compiler Error

Q 14 : What is output?

public class Test14{
    static String s ="Instance";
    public static void method(String s){
        s+="Add";
    }
    public static void main(String a[]){
        Test14 t = new Test14();
        s = "New Instance";
        String s = "Local";
        method(s);
        System.out.println(s);
        System.out.println(t.s);
    }
}

Choose the one below:

  1. Local Instance
  2. Local New Instance
  3. Local Add New Instance
  4. Compiler Error

Q 15 : What is output?

public class Test15{
    public static void method(StringBuffer sb){
        sb.append(" Added");
        sb = new StringBuffer("Hai");
    }
    public static void main(String a[]){
        StringBuffer sb = new StringBuffer("String Buffer");
        method(sb);
        System.out.println(sb);
    }
}

Choose the one below:

  1. String Buffer
  2. String Buffer Added
  3. Hai
  4. Compiler Error

Q 16 : What is output?

public class Test16{
    public static void method(StringBuffer sb){
        sb = new StringBuffer("Hai");
        sb.append(" Added");
    }
    public static void main(String a[]){
        StringBuffer sb = new StringBuffer("String Buffer");
        method(sb);
        System.out.println(sb);
    }
}

Choose the one below:

  1. String Buffer
  2. String Buffer Added
  3. Hai
  4. Compiler Error

Q 17 : What is output?

import java.util.*;
public class Test17{
    public static void main(String a[]){
        Map m = new Hashtable(10,0.75f);
        System.out.println(m.size());
    }
}

Choose the one below:

  1. 10
  2. 7
  3. cOMPILER eRROR

Q 18 : What is the default capacity of java.util.Hashtable?

  1. 10
  2. 16
  3. 11
  4. 20

Q 19 : What is the default capacity of java.util.HashMap?

  1. 10
  2. 16
  3. 11
  4. 20

Q 20 : Which of the following classes has synchronized methods?

  1. ArrayList
  2. Vector
  3. HashTable
  4. WeakHashMap

Q 21 : What is output?

public class Test21{
    public static void main(String a[]){
       String s1 = new String("Hai");
       String s2 = "Hai";
       String s3 = new String("Hai").intern();
       System.out.println(s1 == s2);
       System.out.println(s1 == s3);
       System.out.println(s2 == s3);
    }
}

Choose the one below:

  1. false false true
  2. true false true
  3. false false false
  4. false true true

Q 22 : What is output?

public class Test22{
    public static void main(String a[]){
       String s1 = "SunMicroSystems";
       System.out.println(s1.substring(0));
       System.out.println(s1.substring(1,4));
       System.out.println(s1.substring(8));
    }
}

Choose the one below:

  1. SunMicrosystems sun oSystem
  2. SunMicrosystems unM Systems
  3. StringIndexOutOfBoundsException
  4. None Of the above

Q 23 : What is output?

public class Test23{
    public static void main(String a[]){
        String s1 = "Sun";
        System.out.println(s1.substring(5));
    }
}

Choose the one below:

  1. -1
  2. 0
  3. StringIndexOutOfBoundsException
  4. ArrayIndexOutOfBoundsException

Q 24 : Which of the following are static methods in java.lang.String class?

  1. valueOf
  2. length
  3. indexOf
  4. All the above.

Q 25 : What is output?

public class Test25{
   public static void main(String a[]){
      StringBuffer sb = new StringBuffer(8);
      sb.append("TestString");
      System.out.println(sb.capacity());
      System.out.println(sb.length());
   }
}

Choose the one below:

  1. 8 10
  2. 10 10
  3. 18 10
  4. 18 18

Answers

Q 1 : 2 is correct. ClassCastException.

Q 2 : 2 is correct. NullPointerException.

Q 3 : 1 is correct. [null = null].

Q 4 : 1 is correct. [1=one,3=three,2=two].

Q 5 : 3 is correct. cannot predict the order.

Q 6 : 3 is correct. Double Float Float Double Float.

Q 7 : 4 is correct. Int Int Byte Byte.

Q 8 : 1 is correct. Error at mark 1.
4 is correct. Error at mark 4.

Q 9 : 4 is correct. Error at mark 4.

Q 10 : 1 is correct. Sun.

Q 11 : 3 is correct. false true false.

Q 12 : 4 is correct. Compiler Error.

Q 13 : 4 is correct. Compiler Error.

Q 14 : 2 is correct. Local New Instance.

Q 15 : 2 is correct. String Buffer Added.

Q 16 : 1 is correct. String Buffer.

Q 17 : 1 is correct. 0.

Q 18 : 3 is correct. 11.

Q 19 : 2 is correct. 16.

Q 20 : 2 is correct. Vector.
3 is correct. HashTable.

Q 21 : 1 is correct. false false true.

Q 22 : 2 is correct. SunMicrosystems unM Systems.

Q 23 : 3 is correct. StringIndexOutOfBoundsException.

Q 24 : 1 is correct. valueOf.

Q 25 : 3 is correct. 18 10.

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

*

Close
Please support the site
By clicking any of these buttons you help our site to get better