| Mock Exam – 2 |
| Q1 |
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){} } What is the output? |
| A1 | 10 20 |
| A2 | ClassCastException |
| A3 | Compiler Error |
| A4 | Compiler Error |
| Q2 |
import java.util.*; public class Test2{ public static void main(String a[]){ Map s = new Hashtable(); s.put(null,null); System.out.println(s); } } What is the output?
|
| A1 |
[null = null]
|
| A2 | NullPointerException |
| A3 | null |
| A4 | [] |
| Q3 |
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); } } What is the output? |
| A1 | [null = null] |
| A2 | NullPointerException |
| A3 | null |
| A4 | [] |
| Q4 |
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); } } What is the output? |
| A1 | [1=one,3=three,2=two] |
| A2 | NullPointerException |
| A3 | [1=one,2=two,3=three] |
| A4 | [] |
| Q5 |
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); } } What is the output? |
| A1 | [1=one,3=three,2=two] |
| A2 | [3=three,2=two,1=one] |
| A3 | cannot predict the order |
| A4 | [] |
| Q6 |
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); } } What is the output?
|
| A1 | Double Double Double Double Double |
| A2 | Float Float Float Float Float |
| A3 | Double Float Float Double Float |
| A4 | Double Float Float Double Double |
| Q7 |
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); } } What is the output? |
| A1 | Byte Int Int Byte |
| A2 | Byte Int Int Byte |
| A3 | Byte Int Byte Byte |
| A4 | Int Int Byte Byte |
| Q8 |
public class Test8{ public static void main(String a[]){ byte b = 1; char c = 2; short s = 3; int i = 4; c = b; // 1 |
| A1 | Error at mark 1 |
| A2 | Error at mark 2 |
| A3 | Error at mark 3 |
| A4 | Error at mark 4 |
| Q9 |
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
|
| A1 | Error at mark 1 |
| A2 | Error at mark 2 |
| A3 | Error at mark 3 |
| A4 | Error at mark 4 |
| Q10 |
public class Test10{ public static void main(String a[]){ String s1 = “Sun”; String s2 = “Java”; s1.concat(s2); System.out.println(s1); } } What is output?
|
| A1 | Sun |
| A2 | Java |
| A3 | SunJava |
| A4 | JavaSun |
| Q11 |
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)); } } What is output?
|
| A1 | false true true |
| A2 | true true true |
| A3 | false true false |
| A4 | Compiler Error |
| Q12 |
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)); } } What is output?
|
| A1 | false true |
| A2 | true false |
| A3 | true true |
| A4 | Compiler Error |
| Q13 |
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); } } What is output?
|
| A1 | String Version |
| A2 | String Buffer Version |
| A3 | Runtime Exception |
| A4 | Compiler Error |
| Q14 |
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); } } What is output?
|
| A1 | Local Instance |
| A2 | Local New Instance |
| A3 | Loca Add New Instance |
| A4 | Compiler Error |
| Q15 |
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); } } What is output?
|
| A1 | String Buffer |
| A2 | String Buffer Added |
| A3 | Hai |
| A4 | Compiler Error |
| Q16 |
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); } } What is output?
|
| A1 | String Buffer |
| A2 | String Buffer Added |
| A3 | Hai |
| A4 | Compiler Error |
| Q17 |
import java.util.*; public class Test17{ public static void main(String a[]){ Map m = new Hashtable(10,0.75f); System.out.println(m.size()); } } What is output?
|
| A1 | |
| A2 | 10 |
| A3 | 7 |
| A4 | cOMPILER eRROR |
| Q18 |
What is the default capacity of java.util.Hashtable? |
| A1 | 10 |
| A2 | 16 |
| A3 | 11 |
| A4 | 20 |
| Q19 |
What is the default capacity of java.util.HashMap? |
| A1 | 10 |
| A2 | 16 |
| A3 | 11 |
| A4 | 20 |
| Q20 |
Which of the following classes has synchronized methods? |
| A1 | ArrayList |
| A2 | Vector |
| A3 | HashTable |
| A4 | WeakHashMap |
| Q21 |
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); } } What is output? |
| A1 | false false true |
| A2 | true false true |
| A3 | false false false |
| A4 | false true true |
| Q22 |
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)); } } What is output?
|
| A1 | SunMicrosystems sun oSystem |
| A2 | SunMicrosystems unM Systems |
| A3 | StringIndexOutOfBoundsException |
| A4 | None Of the above |
| Q23 |
public class Test23{ public static void main(String a[]){ String s1 = “Sun”; System.out.println(s1.substring(5)); } } What is output? |
| A1 | -1 |
| A2 | 0 |
| A3 | StringIndexOutOfBoundsException |
| A4 | ArrayIndexOutOfBoundsException |
| Q24 |
Which of the following are static methods in java.lang.String class? |
| A1 | valueOf |
| A2 | length |
| A3 | indexOf |
| A4 | All the above. |
| Q25 |
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()); } }
What
|
| A1 | 8 10 |
| A2 | 10 10 |
| A3 | 18 10 |
| A4 | 18 18 |
| Answers |
| 1 | ClassCastException |
| 2 | NullPointerException |
| 3 | [null = null] |
| 4 | [1=one,3=three,2=two] |
| 5 |
cannot predict the order. |
| 6 |
Double Float Float Double Float |
| 7 | Int Int Byte Byte |
| 8 |
Error at mark 1 Error at mark 4 |
| 9 | Error at mark 4 |
| 10 | Sun |
| 11 | false true false |
| 12 | Compiler Error |
| 13 | Compiler Error |
| 14 | Local New Instance |
| 15 | String Buffer Added |
| 16 | String Buffer |
| 17 | 0 |
| 18 | 11 |
| 19 | 16 |
| 20 |
Vector HashTable |
| 21 | false false true |
| 22 |
SunMicrosystems unM Systems |
| 23 | StringIndexOutOfBoundsException |
| 24 | valueOf |
| 25 | 18 10 |