Saturday 22 August 2015

Mock java Questions set - 2

MCQ  Set- 2
Total  MCQ – 20


1. What is the output?
package Java91.blogspot.in;

import java.util.*;

public class TestJava91 {
          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) {
          }
}

a) 10 20
b) ClassCastException
c) Compiler Error
d) Compiler Error

2. What is the output?

package Java91.blogspot.in;

import java.util.*;

import java.util.*;

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

a) [null = null]
b) NullPointerException
c) null
d) []
3. What is the output?

package Java91.blogspot.in;

import java.util.*;

public class TestJava91 {
          public static void main(String a[]) {
                   Map s = new WeakHashMap(10);

                   s.put(null, null);
                   System.out.println(s);
          }
}


a) [null = null]
b) NullPointerException
c) null
d) []

4. What is the output?

package Java91.blogspot.in;

import java.util.*;

public class TestJava91 {
          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);
          }
}

a) [1=one,3=three,2=two]
b) NullPointerException
c) [1=one,2=two,3=three]
d) []

5. What is the output?

package Java91.blogspot.in;
import java.util.*;

public class TestJava91 {
          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);
          }
}

a) [1=one,3=three,2=two]
b) [3=three,2=two,1=one]
c) cannot predict the order
d) []
6. What is the output?

package Java91.blogspot.in;

public class TestJava91 {
          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);
          }
}

a) Double Double Double Double Double
b) Float Float Float Float Float
c) Double Float Float Double Float
d)Double Float Float Double Double

7. What is the output?

package Java91.blogspot.in;

public class TestJava91 {
          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);
          }
}

a) Byte Int Int Byte
b) Byte Int Int Byte
c) Byte Int Byte Byte
d) Int Int Byte Byte

8. Which of the following are correct?
package Java91.blogspot.in;

public class TestJava91 {
          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
          }
}
a) Error at mark 1
b) Error at mark 2
c) Error at mark 3
d) Error at mark 4

9. Which of the following are correct?

package Java91.blogspot.in;

public class TestJava91 {
          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
          }
}

a) Error at mark 1
b) Error at mark 2
c) Error at mark 3
d) Error at mark 4

10. What is output?

package Java91.blogspot.in;

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


a) Sun
b) Java
c) SunJava
d) JavaSun
11. What is output?

package Java91.blogspot.in;
public class TestJava91 {
          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));
          }
}

a) false true true
b) true true true
c) false true false
d) Compiler Error

12. What is output?
package Java91.blogspot.in;

public class TestJava91 {
          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));
          }
}

a) false true
b) true false
c) true true
d) Compiler Error

13. What is output?

package Java91.blogspot.in;

public class TestJava91 {
          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);
          }
}

a) String Version
b) String Buffer Version
c) Runtime Exception
d) Compiler Error

14. What is output?

package Java91.blogspot.in;

public class TestJava91 {
          static String s = "Instance";

          public static void method(String s) {
                   s += "Add";
          }

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


a) Local Instance
b) Local New Instance
c) Loca Add New Instance
d) Compiler Error

15. What is output?

package Java91.blogspot.in;

public class TestJava91 {
          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);
          }
}

a) String Buffer
b) String Buffer Added
c) Hai
d) Compiler Error

16. What is output?

package Java91.blogspot.in;

public class TestJava91 {
          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);
          }
}


a) String Buffer
b) String Buffer Added
c) Hai
d) Compiler Error

17. What is output?

package Java91.blogspot.in;

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

a) 5
b) 10
c) 7
d)Compiler Error

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

a) 10
b) 16
c) 11
d) 20

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

a) 10
b) 16
c) 11
d) 20

20. Which of the following classes has synchronized methods?

a) ArrayList
b) Vector
c) HashTable
d) WeakHashMap
21. What is output?

package Java91.blogspot.in;

public class TestJava91 {
          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);
          }
}

a) false false true
b) true false true
c) false false false
d) false true true

22. What is output?

package Java91.blogspot.in;

public class TestJava91 {
          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));
          }
}

a) SunMicrosystems sun oSystem
b) SunMicrosystems unM Systems
c) StringIndexOutOfBoundsException
d) None Of the above

23. What is output?

package Java91.blogspot.in;

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

a) -1
b) 0
c) StringIndexOutOfBoundsException
d) ArrayIndexOutOfBoundsException

24. Which of the following are static methods in java.lang.String class?
a) valueOf
b) length
c) indexOf
d) All the above.
25. What is output?

package Java91.blogspot.in;

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

              System.out.println(sb.length());
       }

}

a) 8 10
b) 10 10
c) 18 10
d) 18 18

No comments:

Post a Comment