Questions no -1 <br />What is the output for the below code ?<br />class A implements Runnable{<br />public void run(){<br />System.out.println(Thread.currentThread().getName());<br />}   <br />}<br />public class Test {<br />public static void main(String... args) {<br />A a = new A();<br />Thread t = new Thread(a);<br />Thread t1 = new Thread(a);<br />t.setName(\"
t\"
);<br />t1.setName(\"
t1\"
);<br />t.setPriority(10);<br />t1.setPriority(-3);<br />t.start();<br />t1.start();<br />}<br />}<br />Options are A.t t1B.t1 tC.t tD.Compilation succeed but Runtime ExceptionAnswer :D is the correct answer. <br />Thread priorities are set using a positive integer, usually between 1 and 10. t1.setPriority(-3); throws java.lang.IllegalArgumentException. <br />Questions no -2 <br />What is the output for the below code ?<br />class A implements Runnable{<br />public void run(){<br />System.out.println(Thread.currentThread().getName());<br />}   <br />}<br />1. public class Test {<br />2.public static void main(String... args) {<br />3.A a = new A();<br />4.Thread t = new Thread(a);<br />5.t.setName(\"
good\"
);<br />6.t.start();<br />7.}<br />8. }<br />Options are A.goodB.nullC.Compilation fails with an error at line 5D.Compilation succeed but Runtime ExceptionAnswer :A is the correct answer. <br />Thread.currentThread().getName() return name of the current thread. <br />Questions no -3 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String[] args) {<br />Boolean expr = true;<br />if (expr) {<br />System.out.println(\"
true\"
);<br />} else {<br />System.out.println(\"
false\"
);<br />}<br />}<br />}<br />options<br />A)true<br />B)Compile Error - can't use Boolean object in if().<br />C)false<br />D)Compile Properly but Runtime Exception.<br />Correct answer is : A<br />Explanations : In the if statement, condition can be Boolean object in jdk1.5 and jdk1.6.<br />In the previous version only boolean is allowed.<br />Questions no -4 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String[] args) {<br />List<Integer> list = new ArrayList<Integer>(); <br />list.add(0, 59);<br />int total = list.get(0);<br />System.out.println(total);<br />}<br />}<br />options<br />A)59<br />B)Compile time error, because you have to do int total = ((Integer)(list.get(0))).intValue();<br />C)Compile time error, because can't add primitive type in List.<br />D)Compile Properly but Runtime Exception.<br />Correct answer is : A<br />Explanations :Manual conversion between primitive types (such as an int) and wrapper classes <br />(such as Integer) is necessary when adding a primitive data type to a collection in jdk1.4 but<br />The new autoboxing/unboxing feature eliminates this manual conversion in jdk 1.5 and jdk 1.6.<br />Questions no -3 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String[] args) {<br />Integer i = null;<br />int j = i;<br />System.out.println(j);<br />}<br />}<br />options<br />A)0<br />B)Compile with error <br />C)null<br />D)NullPointerException<br />Correct answer is : D<br />Explanations :An Integer expression can have a null value. If your program tries to autounbox null, it will throw a NullPointerException.<br />Questions no -4 <br />What is the output for the below code ?<br />public class Outer {<br />private int a = 7;<br />   <br />   class Inner {<br />      public void displayValue() {<br />         System.out.println(\"
Value of a is \"
 + a);<br />      }<br />   }<br />}<br />public class Test {<br />public static void main(String... args) throws Exception {<br />Outer mo = new Outer();     <br />  Outer.Inner inner = mo.new Inner();<br />  inner.displayValue();<br />}<br />}<br />options<br />A)Value of a is 7<br />B)Compile Error - not able to access private member.<br />C)Runtime Exception<br />D)Value of a is 8<br />Correct answer is : A<br />Explanations : An inner class instance can never stand alone without a direct relationship to an instance of the outer class.<br />you can access the inner class is through a live instance of the outer class.<br />Inner class can access private member of the outer class.<br />Questions no -5 <br />What is the output for the below code ?<br />public class B {<br />public String getCountryName(){<br />return \"
USA\"
;<br />}<br />public StringBuffer getCountryName(){<br />StringBuffer sb = new StringBuffer();<br />sb.append(\"
UK\"
);<br />return sb;<br />}<br />public static void main(String[] args){<br />B b = new B();<br />System.out.println(b.getCountryName().toString());<br />}<br />}<br />options<br />A)Compile with error<br />B)USA<br />C)UK<br />D) Runtime Exception<br />Correct answer is : A<br />Explanations : You cannot have two methods in the same class with signatures that only differ by return type.<br />Questions no -6<br />What is the output for the below code ?<br />public class C {<br />}<br />public class D extends C{<br />}<br />public class A {<br />public C getOBJ(){<br />System.out.println(\"
class A - return C\"
);<br />return new C();<br />}<br />}<br />public class B extends A{<br />public D getOBJ(){<br />System.out.println(\"
class B - return D\"
);<br />return new D();<br />}<br />}<br />public class Test {<br />public static void main(String... args) {<br />     A a = new B();<br />     a.getOBJ();<br />     }<br />}<br />options<br />A)Compile with error - Not allowed to override the return type of a method with a subtype of the original type.<br />B)class A - return C<br />C)class B - return D<br />D) Runtime Exception<br />Correct answer is : C<br />Explanations : From J2SE 5.0 onwards. You are now allowed to override the return type of a method with a subtype of the original type.<br />Questions no -7<br />What is the output for the below code ?<br />public class A {<br />public String getName() throws ArrayIndexOutOfBoundsException{<br />return \"
Name-A\"
;<br />}<br />}<br />public class C extends A{<br />public String getName() throws Exception{<br />return \"
Name-C\"
;<br />}<br />}<br />public class Test {<br />public static void main(String... args) {<br />A a = new C();<br />a.getName();<br />}<br />}<br />options<br />A)Compile with error<br />B)Name-A<br />C)Name-C<br />D)Runtime Exception<br />Correct answer is : A<br />Explanations : Exception Exception is not compatible with throws clause in A.getName().<br />Overridden method should throw only same or sub class of the exception thrown by super class method.<br />Questions no -8 <br />What is the output for the below code ?<br />import java.util.regex.Matcher;<br />import java.util.regex.Pattern;<br />public class Test {<br />public static void main(String... args) {<br />            <br />Pattern p = Pattern.compile(\"
a*b\"
);<br />Matcher m = p.matcher(\"
b\"
);<br />boolean b = m.matches();<br />System.out.println(b);<br />}<br />}<br />options<br />A)true<br />B)Compile Error<br />C)false<br />D)b<br />Correct answer is : A<br />Explanations : a*b means \"
a\"
 may present zero or more time and \"
b\"
 should be present once.<br />Questions no -9 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) {<br />            <br />String input = \"
1 fish 2 fish red fish blue fish\"
;<br />Scanner s = new Scanner(input).useDelimiter(\"
\\s*fish\\s*\"
);<br />System.out.println(s.nextInt());<br />System.out.println(s.nextInt());<br />System.out.println(s.next());<br />System.out.println(s.next());<br />s.close(); <br />}<br />}<br />options<br />A)1 2 red blue<br />B)Compile Error - because Scanner is not defind in java.<br />C)1 fish 2 fish red fish blue fish<br />D)1 fish 2 fish red blue fish<br />Correct answer is : A<br />Explanations : java.util.Scanner is a simple text scanner which can parse primitive types and strings using regular expressions.<br />Questions no -10 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) {<br />            <br />Pattern p = Pattern.compile(\"
a{3}b?c*\"
);<br />Matcher m = p.matcher(\"
aaab\"
);<br />boolean b = m.matches();<br />System.out.println(b);<br />}<br />}<br />options<br />A)true<br />B)Compile Error <br />C)false<br />D)NullPointerException<br />Correct answer is : A<br />Explanations :<br />X?  X, once or not at all <br />X*  X, zero or more times <br />X+  X, one or more times <br />X{n}  X, exactly n times <br />X{n,}  X, at least n times <br />X{n,m}  X, at least n but not more than m times<br />Questions no -11 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) {<br />            <br />Pattern p = Pattern.compile(\"
a{1,3}b?c*\"
);<br />Matcher m = p.matcher(\"
aaab\"
);<br />boolean b = m.matches();<br />System.out.println(b);<br />}<br />}<br />options<br />A)true<br />B)Compile Error <br />C)false<br />D)NullPointerException<br />Correct answer is : A<br />Explanations :<br />X?  X, once or not at all <br />X*  X, zero or more times <br />X+  X, one or more times <br />X{n}  X, exactly n times <br />X{n,}  X, at least n times <br />X{n,m}  X, at least n but not more than m times<br />Questions no -12 <br />What is the output for the below code ?<br />public class A {<br />public A() {<br />        System.out.println(\"
A\"
);<br />    }<br />}<br />public class B extends A implements Serializable {<br />public B() {<br />        System.out.println(\"
B\"
);<br />    }<br />}<br />public class Test {<br />public static void main(String... args) throws Exception {<br />B b = new B();<br />       <br />        ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream(\"
datafile\"
));<br />        save.writeObject(b); <br />        save.flush(); <br />        <br />        ObjectInputStream restore = new ObjectInputStream(new FileInputStream(\"
datafile\"
));<br />        B z = (B) restore.readObject();<br />        <br />}<br />}<br />options<br />A)A B A<br />B)A B A B<br />C)B B<br />D)A B<br />Correct answer is : A<br />Explanations :On the time of deserialization , the Serializable object not create new object. So constructor of class B does not called. <br />A is not Serializable object so constructor is called.<br />Questions no -13 <br />What is the output for the below code ?<br />public class A {}<br />public class B implements Serializable  {<br />A a = new A();<br />public static void main(String... args){<br />B b = new B();<br />try{<br />FileOutputStream fs = new FileOutputStream(\"
b.ser\"
);<br />ObjectOutputStream os = new ObjectOutputStream(fs);<br />os.writeObject(b);<br />os.close();<br />}catch(Exception e){<br />e.printStackTrace();<br />}<br />}<br />}<br />options<br />A)Compilation Fail<br />B)java.io.NotSerializableException:  Because class A is not Serializable.<br />C)Run properly<br />D)Compilation Fail :  Because class A is not Serializable.<br />Correct answer is : B<br />Explanations :It throws java.io.NotSerializableException:A  Because class A is not Serializable.<br />When JVM tries to serialize object B it will try to serialize A also because (A a = new A()) is instance variable of Class B.<br />So thows NotSerializableException.<br />Questions no -14 <br />What is the output for the below code running in the same JVM?<br />public class A implements Serializable {<br />transient int a = 7;<br />  static int b = 9;<br />}<br />public class B implements Serializable  {<br />public static void main(String... args){<br />A a = new A();<br />try {<br />      ObjectOutputStream os = new ObjectOutputStream(<br />         new FileOutputStream(\"
test.ser\"
));<br />      os.writeObject(a);  <br />      os. close();<br />      System.out.print( + + a.b + \"
 \"
);<br />      ObjectInputStream is = new ObjectInputStream(new FileInputStream(\"
test.ser\"
));<br />      A s2 = (A)is.readObject();<br />      is.close();<br />      System.out.println(s2.a + \"
 \"
 + s2.b);<br />    } catch (Exception x) <br />    {<br />    x.printStackTrace();<br />    }<br />}<br />}<br />options<br />A)9 0 9<br />B)9 7 9<br />C)0 0 0<br />D)0 7 0<br />Correct answer is : A<br />Explanations :transient variables are not serialized when an object is serialized.<br />In the case of static variable you can get the values in the same JVM. <br />Questions no -15 <br />What is the output for the below code ?<br />public enum Test {<br />BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45);<br />private int hh;<br />private int mm;<br />Test(int hh, int mm) {<br />assert (hh >= 0 && hh <= 23) : \"
Illegal hour.\"
;<br />assert (mm >= 0 && mm <= 59) : \"
Illegal mins.\"
;<br />this.hh = hh;<br />this.mm = mm;<br />}<br />public int getHour() {<br />return hh;<br />}<br />public int getMins() {<br />return mm;<br />}<br />public static void main(String args[]){<br />Test t =  new BREAKFAST;<br />System.out.println(t.getHour() +\"
:\"
+t.getMins());<br />}<br />}<br />options<br />A)7:30<br />B)Compile Error - an enum cannot be instantiated using the new operator.<br />C)12:50<br />D)19:45<br />Correct answer is : B<br />Explanations : As an enum cannot be instantiated using the new operator, the constructors cannot be called explicitly.<br />You have to do like <br />Test t =  BREAKFAST;<br />Questions no -16 <br />What is the output for the below code ?<br />public class Test {<br />enum Day {<br />MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY<br />}<br />enum Month {<br />JAN, FEB<br />}<br />public static void main(String[] args) {<br />int[] freqArray = { 12, 34, 56, 23, 5, 13, 78 };<br />// Create a Map of frequencies<br />Map<Day, Integer> ordinaryMap = new HashMap<Day, Integer>();<br />for (Day day : Day.values()) {<br />ordinaryMap.put(day, freqArray[day.ordinal()]);<br />}<br />// Create an EnumMap of frequencies<br />EnumMap<Day, Integer> frequencyEnumMap = new EnumMap<Day, Integer>(ordinaryMap);<br />// Change some frequencies<br />frequencyEnumMap.put(null, 100);<br />System.out.println(\"
Frequency EnumMap: \"
 + frequencyEnumMap);<br />}<br />}<br />options<br />A)Frequency EnumMap: {MONDAY=12, TUESDAY=34, WEDNESDAY=56, THURSDAY=23, FRIDAY=5, SATURDAY=13, SUNDAY=78}<br />B)Compile Error <br />C)NullPointerException<br />D)Frequency EnumMap: {MONDAY=100, TUESDAY=34, WEDNESDAY=56, THURSDAY=23, FRIDAY=5, SATURDAY=13, SUNDAY=123}<br />Correct answer is : C<br />Explanations : The null reference as a key is NOT permitted.<br />Questions no -17 <br />public class EnumTypeDeclarations {<br />public void foo() {<br />enum SimpleMeal { <br />BREAKFAST, LUNCH, DINNER <br />}<br />}<br />}<br />Is the above code Compile without error ?<br />options<br />A)Compile without error<br />B)Compile with error<br />C)Compile without error but Runtime Exception<br />D)Compile without error but Enum Exception<br />Correct answer is : B<br />Explanations :<br />An enum declaration is a special kind of class declaration:<br />a) It can be declared at the top-level and as static enum declaration. <br />b) It is implicitly static, i.e. no outer object is associated with an enum constant. <br />c) It is implicitly final unless it contains constant-specific class bodies, but it can implement interfaces. <br />d) It cannot be declared abstract unless each abstract method is overridden in the constant-specific class body of every enum constant. <br />e) Local (inner) enum declaration is NOT OK! <br />Here in<br />public void foo() {<br />enum SimpleMeal { <br />BREAKFAST, LUNCH, DINNER <br />}<br />}<br />enum declaration is local within method so compile time error.<br />Questions no -18 <br />What is the output for the below code ?<br />public enum Test {<br />int t;<br />BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45);<br />private int hh;<br />private int mm;<br />Test(int hh, int mm) {<br />assert (hh >= 0 && hh <= 23) : \"
Illegal hour.\"
;<br />assert (mm >= 0 && mm <= 59) : \"
Illegal mins.\"
;<br />this.hh = hh;<br />this.mm = mm;<br />}<br />public int getHour() {<br />return hh;<br />}<br />public int getMins() {<br />return mm;<br />}<br />public static void main(String args[]){<br />Test t =  BREAKFAST;<br />System.out.println(t.getHour() +\"
:\"
+t.getMins());<br />}<br />}<br />options<br />A)7:30<br />B)Compile Error <br />C)12:15<br />D)19:45<br />Correct answer is : B<br />Explanations : The enum constants must be declared before any other declarations in an enum type.<br />In this case compile error because of declaration int t; before enum declaration.<br />Questions no -19 <br />What is the output for the below code ?<br />public class B extends Thread{<br />public static void main(String argv[]){<br />B b = new B();<br />b.run();<br />} <br />public void start(){<br />for (int i = 0; i < 10; i++){<br />System.out.println(\"
Value of i = \"
 + i); <br />}  <br />}<br />}<br />options<br />A)A compile time error indicating that no run method is defined for the Thread class<br />B)A run time error indicating that no run method is defined for the Thread class<br />C)Clean compile and at run time the values 0 to 9 are printed out<br />D)Clean compile but no output at runtime<br />Correct answer is : D<br />Explanations : This is a bit of a sneaky one as I have swapped around the names of the methods you need to define and call when running a thread. If the for loop were defined in a method called public void run() and the call in the main method had been to b.start() The list of values from 0 to 9 would have been output.<br />Questions no -20 <br />What is the output for the below code ?<br />public class Test extends Thread{<br />    static String sName = \"
good\"
;<br />    public static void main(String argv[]){<br />    Test t = new Test();<br />    t.nameTest(sName);<br />    System.out.println(sName);<br />    <br />    }<br />    public void nameTest(String sName){<br />            sName = sName + \"
 idea \"
;<br />             start();<br />    }<br />    public void run(){<br />    <br />    for(int i=0;i  <  4; i++){<br />            sName = sName + \"
 \"
 + i;<br />            <br />    }<br />    }<br />}<br />options<br />A)good<br />B)good idea<br />C)good idea good idea<br />D)good 0 good 0 1<br />Correct answer is : A<br />Explanations : Change value in local methods wouldn’t change in global in case of String ( because String object is immutable).<br />Questions no -21 <br />What is the output for the below code ?<br />public class Test{<br />public static void main(String argv[]){<br />Test1 pm1 = new Test1(\"
One\"
);<br />pm1.run();<br />Test1 pm2 = new Test1(\"
Two\"
);<br />pm2.run();<br />}<br />}<br />class Test1 extends Thread{<br />private String sTname=\"
\"
;<br />Test1(String s){<br />sTname = s;<br />}<br />public void run(){<br />for(int i =0; i < 2 ; i++){<br />try{<br /> sleep(1000);<br />}catch(InterruptedException e){}<br />yield();<br />System.out.println(sTname);<br />}<br />}<br />}<br />options<br />A)Compile error<br />B)One One Two Two<br />C)One Two One Two<br />D)One Two<br />Correct answer is : B<br />Explanations : If you call the run method directly it just acts as any other method and does not return to the calling code until it has finished. executing<br />Questions no -22 <br />What is the output for the below code ?<br />public class Test extends Thread{<br />    public static void main(String argv[]){<br />        Test  b = new Test();<br />        b.start();<br />    }<br />    public void run(){      <br />        System.out.println(\"
Running\"
);<br />    }<br />}<br />options<br />A)Compilation clean and run but no output<br />B)Compilation and run with the output \"
Running\"
<br />C)Compile time error with complaint of no Thread import<br />D)Compile time error with complaint of no access to Thread package<br />Correct answer is : B<br />Explanations :<br />The Thread class is part of the core java.lang package and does not need any explicit import statement.<br />Questions no -23 <br />What is the output for the below code ?<br />public class Tech {<br /> public void tech() {<br />      System.out.println(\"
Tech\"
);<br />   }<br />}<br />public class Atech {<br />Tech a = new Tech() {      <br />      public void tech() {<br />         System.out.println(\"
anonymous tech\"
);<br />      }<br />   };<br />   <br />   public void dothis() {<br />      a.tech();     <br />      <br />   }<br />public static void main(String... args){<br />Atech atech = new Atech();<br />atech.dothis();<br />}<br />options<br />A)anonymous tech<br />B)Compile Error <br />C)Tech<br />D)anonymous tech Tech<br />Correct answer is : A<br />Explanations : This is anonymous subclass of the specified class type.<br />Anonymous inner class ( anonymous subclass ) overriden the Tech super class of tech() method.<br />Therefore Subclass method will get called.<br />Questions no -24 <br />What is the output for the below code ?<br />public class Outer {<br /> private String x = \"
Outer variable\"
;<br />   void doStuff() {<br />     String z = \"
local variable\"
;<br />     class Inner {<br />       public void seeOuter() {<br />         System.out.println(\"
Outer x is \"
 + x);<br />         System.out.println(\"
Local variable z is \"
 + z);  <br />       } <br />     }   <br />   }  <br />   <br />  <br />}<br />options<br />A)Outer x is Outer variable.<br />B)Compile Error <br />C)Local variable z is local variable.<br />D)Outer x is Outer variable Local variable z is local variable<br />Correct answer is : B<br />Explanations : Cannot refer to a non-final variable z inside an inner class defined in a different method.<br />Questions no -25<br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) {<br />for(int i = 2; i < 4; i++)<br />  for(int j = 2; j < 4; j++)<br />     assert i!=j : i; <br />}<br />}<br />options<br />A)The class compiles and runs, but does not print anything.<br />B)The number 2 gets printed with AssertionError<br />C)The number 3 gets printed with AssertionError<br />D)compile error<br />Correct answer is : B<br />Explanations : When i and j are both 2, assert condition is false, and AssertionError gets generated. <br />Questions no -26<br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) {<br />for(int i = 2; i < 4; i++)<br />  for(int j = 2; j < 4; j++)<br />if(i < j)<br />     assert i!=j : i; <br />}<br />}<br />options<br />A)The class compiles and runs, but does not print anything.<br />B)The number 2 gets printed with AssertionError<br />C)The number 3 gets printed with AssertionError<br />D)compile error<br />Correct answer is : A<br />Explanations : When if condition returns true, the assert statement also returns true.<br />Hence AssertionError not generated.<br />Questions no -26 <br />What is the output for the below code ?<br />public class NameBean {<br />private String str;<br />NameBean(String str ){<br />this.str = str;<br />}<br />public String toString() {<br />return str;<br />}<br />}<br />import java.util.HashSet;<br />public class CollClient {<br />public static void main(String ... sss) {<br />HashSet myMap = new HashSet();<br />String s1 = new String(\"
das\"
);<br />String s2 = new String(\"
das\"
);<br />NameBean s3 = new NameBean(\"
abcdef\"
);<br />NameBean s4 = new NameBean(\"
abcdef\"
);<br />myMap.add(s1);<br />myMap.add(s2);<br />myMap.add(s3);<br />myMap.add(s4);<br />System.out.println(myMap);<br />}<br />}<br />options<br />A)das abcdef abcdef<br />B)das das abcdef abcdef<br />C)das abcdef<br />D)abcdef abcdef<br />Correct answer is : A<br />Explanations : Need to implement 'equals' and 'hashCode' methods to get unique Set for user defind objects(NameBean).<br />String object internally implements 'equals' and 'hashCode' methods therefore Set only stored one value.<br />Questions no -27 <br />Synchronized resizable-array implementation of the List interface is _____________?<br />options<br />A)Vector<br />B)ArrayList<br />C)Hashtable<br />D)HashMap<br />Correct answer is : A<br />Explanations : Vector implements List, RandomAccess - Synchronized resizable-array implementation of the List interface with additional \"
legacy methods.\"
<br />Questions no -28 <br />What is the output for the below code ?<br />public class Test {<br />    <br />    public static void main(String argv[]){<br />     <br />    ArrayList list = new ArrayList(); <br />    ArrayList<String> listStr = list; <br />    ArrayList<StringBuffer> listBuf = list; <br />    listStr.add(0, \"
Hello\"
); <br />    StringBuffer buff = listBuf.get(0); <br />       System.out.println(buff.toString());<br />    }<br />}<br />options<br />A)Hello<br />B)Compile error<br />C)java.lang.ClassCastException<br />D)null<br />Correct answer is : C<br />Explanations : java.lang.String cannot be cast to java.lang.StringBuffer at the code StringBuffer buff = listBuf.get(0); <br />So thows java.lang.ClassCastException.<br />Questions no -29 <br />What is the output for the below code ?<br />import java.util.LinkedList;<br />import java.util.Queue;<br />public class Test {<br />public static void main(String... args) {<br />        <br />Queue<String> q = new LinkedList<String>();<br />        q.add(\"
newyork\"
);<br />        q.add(\"
ca\"
);<br />        q.add(\"
texas\"
);<br />        show(q);<br />    }<br />    public static void show(Queue q) {<br />        q.add(new Integer(11));<br />        while (!q.isEmpty ( ) )<br />            System.out.print(q.poll() + \"
  \"
);<br />    }<br />}<br />options<br />A)Compile error : Integer can't add<br />B)newyork  ca  texas  11<br />C)newyork  ca  texas<br />D)newyork  ca <br />Correct answer is : B<br />Explanations : <br />  q was originally declared as Queue<String>,  But in show() method it is passed as an untyped Queue. nothing in the compiler or JVM prevents us from adding an Integer after that.<br />   If the show method signature is public static void show(Queue<String> q) than you can't add Integer, Only String allowed. But public static void show(Queue q) is untyped Queue so you can add Integer.<br />  poll() Retrieves and removes the head of this queue, or returns null if this queue is empty.<br />Questions no -30 <br />What is the output for the below code ?<br />public interface TestInf {<br /> int i =10;<br />}<br />public class Test {<br />public static void main(String... args) {<br />TestInf.i=12;<br />System.out.println(TestInf.i);<br />}<br />}<br />options<br />A)Compile with error<br />B)10<br />C)12<br />D) Runtime Exception<br />Correct answer is : A<br />Explanations : All the variables declared in interface is Implicitly static and final , therefore can't change the value.<br />Questions no -31 <br />What is the output for the below code ?<br />public class Test {<br />            static { int a = 5; }<br />            public static void main(String[] args){<br />            System.out.println(a);<br />            }<br />          }<br />options<br />A)Compile with error<br />B)5<br />C)0<br />D) Runtime Exception<br />Correct answer is : A<br />Explanations : A variable declared  in a static initialiser is not accessible outside its enclosing block.<br />Questions no -32 <br />What is the output for the below code ?<br />class A {<br />  { System.out.print(\"
b1 \"
); }<br />  public A() { System.out.print(\"
b2 \"
); }<br />}<br />class B extends A {<br />  static { System.out.print(\"
r1 \"
); }<br />  public B() { System.out.print(\"
r2 \"
); }<br />  { System.out.print(\"
r3 \"
); }<br />  static { System.out.print(\"
r4 \"
); }<br />}<br />class C extends B {<br />  public static void main(String[] args) {<br />    System.out.print(\"
pre \"
);<br />    new C();<br />    System.out.println(\"
post \"
);<br />  }<br />}<br />options<br />A)r1 r4 pre b1 b2 r3 r2 post<br />B)r1 r4 pre b1 b2  post<br />C)r1 r4 pre b1 b2  post r3 r2<br />D)pre r1 r4  b1 b2  r2 r3 post<br />Correct answer is : A<br />Explanations : All static blocks execute first then blocks and constructor. <br />Blocks and constructor executes (super class block then super class constructor, sub class block then sub class constructor).<br />Sequence for static blocks is super class first then sub class.<br />Sequence for blocks is super class first then sub class.<br />Questions no -33 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) throws Exception {<br />Integer i = 34;<br />int l = 34;<br />if(i.equals(l)){<br />System.out.println(true);<br />}else{<br />System.out.println(false);<br />}<br />}<br />}<br />options<br />A)true<br />B)false<br />C)Compile Error<br />D) Runtime Exception<br />Correct answer is : A<br />Explanations : equals() method for the integer wrappers will only return true if the two primitive types and the two values are equal.<br />Questions no -34 <br />Which statement is true about outer class?<br />options<br />A)outer class can only declare public , abstract and final<br />B)outer class may be private<br />C)outer class can't be abstract<br />D)outer class can be static<br />Correct answer is : A<br />Explanations : outer class can only declare public , abstract and final.<br />Questions no -35 <br />What is the output for the below code ?<br />static public class Test {<br />public static void main(String[] args) {<br />  char c = 'a';<br /> switch(c){<br />     case 65:<br />              System.out.println(\"
one\"
);break;<br />     case 'a':<br />              System.out.println(\"
two\"
);break;<br />     case 3:<br />              System.out.println(\"
three\"
);<br />}<br />}<br />}<br />options<br />A)one<br />B)two<br />C)Compile error - char can't be permitted in switch statement <br />D)Compile error - Illegal modifier for the class Test; only public, abstract & final are permitted.<br />Correct answer is : D<br />Explanations : outer class can only declare public , abstract and final.<br />Illegal modifier for the class Test; only public, abstract & final are permitted<br />Questions no -36<br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) {<br />ArrayList<Integer> list = new ArrayList<Integer>();<br />list.add(1);<br />list.add(2);<br />list.add(3);<br />for(int i:list)<br />System.out.println(i);<br />}<br />}<br />options<br />A)1 2 3<br />B)Compile error , can't add primitive type in ArrayList<br />C)Compile error on for(int i:list) , Incorrect Syntax<br />D)0 0 0<br />Correct answer is : A<br />Explanations : JDK 1.5, 1.6 allows add primitive type in ArrayList  and for(int i:list) syntax is also correct.<br />for(int i:list) is same as <br />for(int i=0; i < list.size();i++){<br />int a = list.get(i);<br />}<br />Questions no -37 <br />What is the output for the below code ?<br />public class SuperClass {<br />public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{<br />String signature = \"
(String, Integer[])\"
;<br />System.out.println(str + \"
  \"
 + signature);<br />return 1;<br />}<br />}<br />public class SubClass extends SuperClass{<br />public int doIt(String str, Integer... data) throws Exception<br />{<br />String signature = \"
(String, Integer[])\"
;<br />System.out.println(\"
Overridden: \"
 + str + \"
 \"
 + signature);<br />return 0;<br />}<br />public static void main(String... args)<br />{<br />SuperClass sb = new SubClass();<br />try{<br />sb.doIt(\"
hello\"
, 3);<br />}catch(Exception e){<br />}<br />}<br />}<br />options<br />A)Overridden: hello (String, Integer[])<br />B)hello (String, Integer[])<br />C)This code throws an Exception at Runtime<br />D)Compile with error<br />Correct answer is : D<br />Explanations : Exception Exception is not compatible with throws clause in SuperClass.doIt(String, Integer[]). <br />The same exception or subclass of that exception is allowed.<br />Questions no -38<br />What is the result of executing the following code, using the parameters 0 and 3 ?<br />public void divide(int a, int b) {<br />try {<br />int c = a / b;<br />} catch (Exception e) {<br />System.out.print(\"
Exception \"
);<br />} finally {<br />System.out.println(\"
Finally\"
);<br />}<br />options<br />A)Prints out: Exception Finally<br />B)Prints out: Finally<br />C)Prints out: Exception<br />D)Compile with error<br />Correct answer is : B<br />Explanations : finally block always executed whether exception occurs or not. <br />0/3 = 0 Does not throws exception.<br />Questions no -39<br />Which of the below statement is true about Error?<br />options<br />A)An Error is a subclass of Throwable<br />B)An Error is a subclass of Exception<br />C)Error indicates serious problems that a reasonable application should not try to catch.<br />D)An Error is a subclass of IOException<br />Correct answer is : A and C<br />Explanations : An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.<br />Questions no -40<br />Which of the following is type of RuntimeException?<br />options<br />A)IOException<br />B)ArrayIndexOutOfBoundsException<br />C)Exception<br />D)Error<br />Correct answer is : B<br />Explanations : Below is the tree.<br />java.lang.Object<br />  java.lang.Throwable<br />      java.lang.Exception<br />          java.lang.RuntimeException<br />              java.lang.IndexOutOfBoundsException<br />                  java.lang.ArrayIndexOutOfBoundsException<br />Questions no -41 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) throws Exception {<br /> File file = new File(\"
test.txt\"
);  <br /> System.out.println(file.exists());<br /> FileWriter fw = new FileWriter(file); <br /> System.out.println(file.exists());<br />}<br />}<br />options<br />A)true true<br />B)false false<br />C)false true<br />D)true false<br />Correct answer is : C<br />Explanations :Creating a new instance of the class File, you're not yet making an actual file, you're just creating a filename.<br />So file.exists() return false.<br />FileWriter fw = new FileWriter(file) do three things:<br />It created a FileWriter reference variable fw.<br />It created a FileWriter object, and assigned it to fw.<br />It created an actual empty file out on the disk.<br />So file.exists() return true.<br />Questions no -42 <br />When comparing java.io.BufferedWriter and java.io.FileWriter, which capability exist as a method in only one of two ?<br />options<br />A)closing the stream<br />B)flushing the stream<br />C)writting to the stream<br />D)writting a line separator to the stream<br />Correct answer is : D<br />Explanations :A newLine() method is provided in BufferedWriter which is not in FileWriter.<br />Questions no -43 <br />What is the output for the below code ?<br />public class Test{<br />public static void main(String[] args) {<br />  int i1=1;<br /> switch(i1){<br />     case 1:<br />              System.out.println(\"
one\"
);<br />     case 2:<br />              System.out.println(\"
two\"
);<br />     case 3:<br />              System.out.println(\"
three\"
);<br />}<br />}<br />}<br />options<br />A)one two three<br />B)one<br />C)one two<br />D)Compile error.<br />Correct answer is : A<br />Explanations : There is no break statement in case 1 so it causes the below case statements to execute regardless of their values.<br />Questions no -44 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String[] args) {<br />  char c = 'a';<br /> switch(c){<br />     case 65:<br />              System.out.println(\"
one\"
);break;<br />     case 'a':<br />              System.out.println(\"
two\"
);break;<br />     case 3:<br />              System.out.println(\"
three\"
);<br />}<br />}<br />}<br />options<br />A)one two three<br />B)one<br />C)two<br />D)Compile error - char can't be in switch statement.<br />Correct answer is : C<br />Explanations : Compile properly and print two.<br />Questions no -45 <br />What is the output for the below code ?<br />import java.util.NavigableMap;<br />import java.util.concurrent.ConcurrentSkipListMap;<br />public class Test {<br />public static void main(String... args) {<br />        <br />NavigableMap <Integer, String>navMap = new <br />        ConcurrentSkipListMap<Integer, String>();<br />navMap.put(4, \"
April\"
);<br />navMap.put(5, \"
May\"
);<br />navMap.put(6, \"
June\"
);<br />navMap.put(1, \"
January\"
);<br />navMap.put(2, \"
February\"
);<br />navMap.put(3, \"
March\"
);<br />        navMap.pollFirstEntry();<br />        navMap.pollLastEntry();<br />        navMap.pollFirstEntry();<br />        System.out.println(navMap.size());<br />           <br />}<br />}<br />options<br />A)Compile error : No method  name like pollFirstEntry() or pollLastEntry()<br />B)3<br />C)6<br />D)4<br />Correct answer is : B<br />Explanations : <br />  pollFirstEntry() Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.<br />  pollLastEntry() Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.<br />Questions no -46<br />What is the output for the below code ?<br />import java.util.NavigableMap;<br />import java.util.concurrent.ConcurrentSkipListMap;<br />public class Test {<br />public static void main(String... args) {<br />        <br />NavigableMap <Integer, String>navMap = new <br />        ConcurrentSkipListMap<Integer, String>();<br />        System.out.print(navMap.lastEntry());<br />           <br />}<br />}<br />options<br />A)Compile error : No method  name like lastEntry()<br />B)null<br />C)NullPointerException<br />D)0<br />Correct answer is : B<br />Explanations : lastEntry() Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.<br />Questions no -47<br />What is the output for the below code ?<br />import java.util.NavigableMap;<br />import java.util.concurrent.ConcurrentSkipListMap;<br />public class Test {<br />public static void main(String... args) {<br />        <br />NavigableMap<Integer, String>navMap = new <br />        ConcurrentSkipListMap<Integer, String>();<br />navMap.put(4, \"
April\"
);<br />navMap.put(5, \"
May\"
);<br />navMap.put(6, \"
June\"
);<br />navMap.put(1, \"
January\"
);<br />navMap.put(2, \"
February\"
);<br />        System.out.print(navMap.ceilingKey(3));<br />        <br />           <br />}<br />}<br />options<br />A)Compile error : No method  name like ceilingKey()<br />B)null<br />C)NullPointerException<br />D)4<br />Correct answer is : D<br />Explanations : Returns the least key greater than or equal to the given key, or null if there is no such key. <br />In the above case : 3 is not a key so return 4 (least key greater than or equal to the given key).<br />Questions no -48<br />What is the output for the below code ?<br />import java.util.NavigableMap;<br />import java.util.concurrent.ConcurrentSkipListMap;<br />public class Test {<br />public static void main(String... args) {<br />        <br />NavigableMap<Integer, String>navMap = new <br />        ConcurrentSkipListMap<Integer, String>();<br />navMap.put(4, \"
April\"
);<br />navMap.put(5, \"
May\"
);<br />navMap.put(6, \"
June\"
);<br />navMap.put(1, \"
January\"
);<br />navMap.put(2, \"
February\"
);<br />        System.out.print(navMap.floorKey(3));<br />        <br />           <br />}<br />}<br />options<br />A)Compile error : No method  name like floorKey()<br />B)null<br />C)NullPointerException<br />D)2<br />Correct answer is : D<br />Explanations : Returns the greatest key less than or equal to the given key, or null if there is no such key. <br />In the above case : 3 is not a key so return 2 (greatest key less than or equal to the given key).<br />Questions no -49 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) {<br />        <br /> List<Integer> lst = new ArrayList<Integer>();<br /> lst.add(34);<br /> lst.add(6);<br /> lst.add(6);<br /> lst.add(6);<br /> lst.add(6);<br /> lst.add(5);<br />         <br />        NavigableSet<Integer>  nvset = new TreeSet(lst);<br />        System.out.println(nvset.tailSet(6));<br />           <br />}<br />}<br />options<br />A)Compile error : No method  name like tailSet()<br />B)6 34<br />C)5<br />D)5 6 34<br />Correct answer is : B<br />Explanations : tailSet(6) Returns elements are greater than or equal to 6.<br />Questions no -50 <br />What is the output for the below code ?<br />import java.util.ArrayList;<br />import java.util.List;<br />import java.util.NavigableSet;<br />import java.util.TreeSet;<br />public class Test {<br />public static void main(String... args) {<br />        <br />List<Integer> lst = new ArrayList<Integer>();<br /> lst.add(34);<br /> lst.add(6);<br /> lst.add(6);<br /> lst.add(6);<br /> lst.add(6);<br />         <br />        NavigableSet<Integer>  nvset = new TreeSet(lst);<br />        nvset.pollFirst(); <br />        nvset.pollLast();<br />            System.out.println(nvset.size());<br />           <br />}<br />}<br />options<br />A)Compile error : No method  name like pollFirst() or pollLast()<br />B)0<br />C)3<br />D)5<br />Correct answer is : B<br />Explanations : <br />  pollFirst() Retrieves and removes the first (lowest) element, or returns null if this set is empty.<br />  pollLast() Retrieves and removes the last (highest) element, or returns null if this set is empty.<br />Questions no -51<br />What is the output for the below code ?<br />import java.util.ArrayList;<br />import java.util.List;<br />import java.util.NavigableSet;<br />import java.util.TreeSet;<br />public class Test {<br />public static void main(String... args) {<br />        <br /> List<Integer> lst = new ArrayList<Integer>();<br /> lst.add(34);<br /> lst.add(6);<br /> lst.add(2);<br /> lst.add(8);<br />     lst.add(7);<br />     lst.add(10);<br />        <br />        NavigableSet<Integer>  nvset = new TreeSet(lst);<br />        System.out.println(nvset.lower(6)+\"
 \"
+nvset.higher(6)+ \"
 \"
+ nvset.lower(2));<br />      <br />}<br />}<br />options<br />A)1 2 7 10 34 null<br />B)2 7 null<br />C)2 7 34<br />D)1 2 7 10 34<br />Correct answer is : B<br />Explanations : <br />  lower() Returns the greatest element in this set strictly less than the given element, or null if there is no such element.<br />  higher() Returns the least element in this set strictly greater than the given element, or null if there is no such element.<br />Questions no -52<br />What is the output for the below code ?<br />import java.util.ArrayList;<br />import java.util.Iterator;<br />import java.util.List;<br />import java.util.NavigableSet;<br />import java.util.TreeSet;<br />public class Test {<br />public static void main(String... args) {<br />        <br /> List<Integer> lst = new ArrayList<Integer>();<br /> lst.add(34);<br /> lst.add(6);<br /> lst.add(2);<br /> lst.add(8);<br />     lst.add(7);<br />     lst.add(10);<br />        <br />        NavigableSet<Integer>  nvset = new TreeSet(lst);<br />         System.out.println(nvset.headSet(10));<br />      <br />}<br />}<br />options<br />A)Compile error : No method  name like headSet()<br />B)2, 6, 7, 8, 10<br />C)2, 6, 7, 8<br />D)34<br />Correct answer is : C<br />Explanations : <br />  headSet(10) Returns the elements elements are strictly less than 10.<br />  headSet(10,false) Returns the elements elements are strictly less than 10.<br />  headSet(10,true) Returns the elements elements are strictly less than or equal to 10.<br />Questions no -53 <br />What is the output for the below code ?<br />import java.io.Console;<br />public class Test {<br />public static void main(String... args) {<br />        <br />Console con = System.console();<br />  boolean auth = false;<br /> <br />  if (con != null)<br />  {<br />    int count = 0;<br />    do<br />    {<br />      String uname = con.readLine(null);<br />      char[] pwd = con.readPassword(\"
Enter %s's password: \"
, uname);<br />      <br />      con.writer().write(\"
\n\n\"
);  <br />    } while (!auth && ++count < 3);<br />  }<br />           <br />}<br />}<br />options<br />A)NullPointerException<br />B)It works properly<br />C)Compile Error : No readPassword() method in Console class.<br />D)null<br />Correct answer is : A<br />Explanations : passing a null argument to any method in Console class will cause a NullPointerException to be thrown.<br />
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7

7

  • 1.
    Questions no -1<br />What is the output for the below code ?<br />class A implements Runnable{<br />public void run(){<br />System.out.println(Thread.currentThread().getName());<br />} <br />}<br />public class Test {<br />public static void main(String... args) {<br />A a = new A();<br />Thread t = new Thread(a);<br />Thread t1 = new Thread(a);<br />t.setName(\" t\" );<br />t1.setName(\" t1\" );<br />t.setPriority(10);<br />t1.setPriority(-3);<br />t.start();<br />t1.start();<br />}<br />}<br />Options are A.t t1B.t1 tC.t tD.Compilation succeed but Runtime ExceptionAnswer :D is the correct answer. <br />Thread priorities are set using a positive integer, usually between 1 and 10. t1.setPriority(-3); throws java.lang.IllegalArgumentException. <br />Questions no -2 <br />What is the output for the below code ?<br />class A implements Runnable{<br />public void run(){<br />System.out.println(Thread.currentThread().getName());<br />} <br />}<br />1. public class Test {<br />2.public static void main(String... args) {<br />3.A a = new A();<br />4.Thread t = new Thread(a);<br />5.t.setName(\" good\" );<br />6.t.start();<br />7.}<br />8. }<br />Options are A.goodB.nullC.Compilation fails with an error at line 5D.Compilation succeed but Runtime ExceptionAnswer :A is the correct answer. <br />Thread.currentThread().getName() return name of the current thread. <br />Questions no -3 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String[] args) {<br />Boolean expr = true;<br />if (expr) {<br />System.out.println(\" true\" );<br />} else {<br />System.out.println(\" false\" );<br />}<br />}<br />}<br />options<br />A)true<br />B)Compile Error - can't use Boolean object in if().<br />C)false<br />D)Compile Properly but Runtime Exception.<br />Correct answer is : A<br />Explanations : In the if statement, condition can be Boolean object in jdk1.5 and jdk1.6.<br />In the previous version only boolean is allowed.<br />Questions no -4 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String[] args) {<br />List<Integer> list = new ArrayList<Integer>(); <br />list.add(0, 59);<br />int total = list.get(0);<br />System.out.println(total);<br />}<br />}<br />options<br />A)59<br />B)Compile time error, because you have to do int total = ((Integer)(list.get(0))).intValue();<br />C)Compile time error, because can't add primitive type in List.<br />D)Compile Properly but Runtime Exception.<br />Correct answer is : A<br />Explanations :Manual conversion between primitive types (such as an int) and wrapper classes <br />(such as Integer) is necessary when adding a primitive data type to a collection in jdk1.4 but<br />The new autoboxing/unboxing feature eliminates this manual conversion in jdk 1.5 and jdk 1.6.<br />Questions no -3 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String[] args) {<br />Integer i = null;<br />int j = i;<br />System.out.println(j);<br />}<br />}<br />options<br />A)0<br />B)Compile with error <br />C)null<br />D)NullPointerException<br />Correct answer is : D<br />Explanations :An Integer expression can have a null value. If your program tries to autounbox null, it will throw a NullPointerException.<br />Questions no -4 <br />What is the output for the below code ?<br />public class Outer {<br />private int a = 7;<br /> <br /> class Inner {<br /> public void displayValue() {<br /> System.out.println(\" Value of a is \" + a);<br /> }<br /> }<br />}<br />public class Test {<br />public static void main(String... args) throws Exception {<br />Outer mo = new Outer(); <br /> Outer.Inner inner = mo.new Inner();<br /> inner.displayValue();<br />}<br />}<br />options<br />A)Value of a is 7<br />B)Compile Error - not able to access private member.<br />C)Runtime Exception<br />D)Value of a is 8<br />Correct answer is : A<br />Explanations : An inner class instance can never stand alone without a direct relationship to an instance of the outer class.<br />you can access the inner class is through a live instance of the outer class.<br />Inner class can access private member of the outer class.<br />Questions no -5 <br />What is the output for the below code ?<br />public class B {<br />public String getCountryName(){<br />return \" USA\" ;<br />}<br />public StringBuffer getCountryName(){<br />StringBuffer sb = new StringBuffer();<br />sb.append(\" UK\" );<br />return sb;<br />}<br />public static void main(String[] args){<br />B b = new B();<br />System.out.println(b.getCountryName().toString());<br />}<br />}<br />options<br />A)Compile with error<br />B)USA<br />C)UK<br />D) Runtime Exception<br />Correct answer is : A<br />Explanations : You cannot have two methods in the same class with signatures that only differ by return type.<br />Questions no -6<br />What is the output for the below code ?<br />public class C {<br />}<br />public class D extends C{<br />}<br />public class A {<br />public C getOBJ(){<br />System.out.println(\" class A - return C\" );<br />return new C();<br />}<br />}<br />public class B extends A{<br />public D getOBJ(){<br />System.out.println(\" class B - return D\" );<br />return new D();<br />}<br />}<br />public class Test {<br />public static void main(String... args) {<br /> A a = new B();<br /> a.getOBJ();<br /> }<br />}<br />options<br />A)Compile with error - Not allowed to override the return type of a method with a subtype of the original type.<br />B)class A - return C<br />C)class B - return D<br />D) Runtime Exception<br />Correct answer is : C<br />Explanations : From J2SE 5.0 onwards. You are now allowed to override the return type of a method with a subtype of the original type.<br />Questions no -7<br />What is the output for the below code ?<br />public class A {<br />public String getName() throws ArrayIndexOutOfBoundsException{<br />return \" Name-A\" ;<br />}<br />}<br />public class C extends A{<br />public String getName() throws Exception{<br />return \" Name-C\" ;<br />}<br />}<br />public class Test {<br />public static void main(String... args) {<br />A a = new C();<br />a.getName();<br />}<br />}<br />options<br />A)Compile with error<br />B)Name-A<br />C)Name-C<br />D)Runtime Exception<br />Correct answer is : A<br />Explanations : Exception Exception is not compatible with throws clause in A.getName().<br />Overridden method should throw only same or sub class of the exception thrown by super class method.<br />Questions no -8 <br />What is the output for the below code ?<br />import java.util.regex.Matcher;<br />import java.util.regex.Pattern;<br />public class Test {<br />public static void main(String... args) {<br /> <br />Pattern p = Pattern.compile(\" a*b\" );<br />Matcher m = p.matcher(\" b\" );<br />boolean b = m.matches();<br />System.out.println(b);<br />}<br />}<br />options<br />A)true<br />B)Compile Error<br />C)false<br />D)b<br />Correct answer is : A<br />Explanations : a*b means \" a\" may present zero or more time and \" b\" should be present once.<br />Questions no -9 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) {<br /> <br />String input = \" 1 fish 2 fish red fish blue fish\" ;<br />Scanner s = new Scanner(input).useDelimiter(\" \\s*fish\\s*\" );<br />System.out.println(s.nextInt());<br />System.out.println(s.nextInt());<br />System.out.println(s.next());<br />System.out.println(s.next());<br />s.close(); <br />}<br />}<br />options<br />A)1 2 red blue<br />B)Compile Error - because Scanner is not defind in java.<br />C)1 fish 2 fish red fish blue fish<br />D)1 fish 2 fish red blue fish<br />Correct answer is : A<br />Explanations : java.util.Scanner is a simple text scanner which can parse primitive types and strings using regular expressions.<br />Questions no -10 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) {<br /> <br />Pattern p = Pattern.compile(\" a{3}b?c*\" );<br />Matcher m = p.matcher(\" aaab\" );<br />boolean b = m.matches();<br />System.out.println(b);<br />}<br />}<br />options<br />A)true<br />B)Compile Error <br />C)false<br />D)NullPointerException<br />Correct answer is : A<br />Explanations :<br />X? X, once or not at all <br />X* X, zero or more times <br />X+ X, one or more times <br />X{n} X, exactly n times <br />X{n,} X, at least n times <br />X{n,m} X, at least n but not more than m times<br />Questions no -11 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) {<br /> <br />Pattern p = Pattern.compile(\" a{1,3}b?c*\" );<br />Matcher m = p.matcher(\" aaab\" );<br />boolean b = m.matches();<br />System.out.println(b);<br />}<br />}<br />options<br />A)true<br />B)Compile Error <br />C)false<br />D)NullPointerException<br />Correct answer is : A<br />Explanations :<br />X? X, once or not at all <br />X* X, zero or more times <br />X+ X, one or more times <br />X{n} X, exactly n times <br />X{n,} X, at least n times <br />X{n,m} X, at least n but not more than m times<br />Questions no -12 <br />What is the output for the below code ?<br />public class A {<br />public A() {<br /> System.out.println(\" A\" );<br /> }<br />}<br />public class B extends A implements Serializable {<br />public B() {<br /> System.out.println(\" B\" );<br /> }<br />}<br />public class Test {<br />public static void main(String... args) throws Exception {<br />B b = new B();<br /> <br /> ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream(\" datafile\" ));<br /> save.writeObject(b); <br /> save.flush(); <br /> <br /> ObjectInputStream restore = new ObjectInputStream(new FileInputStream(\" datafile\" ));<br /> B z = (B) restore.readObject();<br /> <br />}<br />}<br />options<br />A)A B A<br />B)A B A B<br />C)B B<br />D)A B<br />Correct answer is : A<br />Explanations :On the time of deserialization , the Serializable object not create new object. So constructor of class B does not called. <br />A is not Serializable object so constructor is called.<br />Questions no -13 <br />What is the output for the below code ?<br />public class A {}<br />public class B implements Serializable {<br />A a = new A();<br />public static void main(String... args){<br />B b = new B();<br />try{<br />FileOutputStream fs = new FileOutputStream(\" b.ser\" );<br />ObjectOutputStream os = new ObjectOutputStream(fs);<br />os.writeObject(b);<br />os.close();<br />}catch(Exception e){<br />e.printStackTrace();<br />}<br />}<br />}<br />options<br />A)Compilation Fail<br />B)java.io.NotSerializableException: Because class A is not Serializable.<br />C)Run properly<br />D)Compilation Fail : Because class A is not Serializable.<br />Correct answer is : B<br />Explanations :It throws java.io.NotSerializableException:A Because class A is not Serializable.<br />When JVM tries to serialize object B it will try to serialize A also because (A a = new A()) is instance variable of Class B.<br />So thows NotSerializableException.<br />Questions no -14 <br />What is the output for the below code running in the same JVM?<br />public class A implements Serializable {<br />transient int a = 7;<br /> static int b = 9;<br />}<br />public class B implements Serializable {<br />public static void main(String... args){<br />A a = new A();<br />try {<br /> ObjectOutputStream os = new ObjectOutputStream(<br /> new FileOutputStream(\" test.ser\" ));<br /> os.writeObject(a); <br /> os. close();<br /> System.out.print( + + a.b + \" \" );<br /> ObjectInputStream is = new ObjectInputStream(new FileInputStream(\" test.ser\" ));<br /> A s2 = (A)is.readObject();<br /> is.close();<br /> System.out.println(s2.a + \" \" + s2.b);<br /> } catch (Exception x) <br /> {<br /> x.printStackTrace();<br /> }<br />}<br />}<br />options<br />A)9 0 9<br />B)9 7 9<br />C)0 0 0<br />D)0 7 0<br />Correct answer is : A<br />Explanations :transient variables are not serialized when an object is serialized.<br />In the case of static variable you can get the values in the same JVM. <br />Questions no -15 <br />What is the output for the below code ?<br />public enum Test {<br />BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45);<br />private int hh;<br />private int mm;<br />Test(int hh, int mm) {<br />assert (hh >= 0 && hh <= 23) : \" Illegal hour.\" ;<br />assert (mm >= 0 && mm <= 59) : \" Illegal mins.\" ;<br />this.hh = hh;<br />this.mm = mm;<br />}<br />public int getHour() {<br />return hh;<br />}<br />public int getMins() {<br />return mm;<br />}<br />public static void main(String args[]){<br />Test t = new BREAKFAST;<br />System.out.println(t.getHour() +\" :\" +t.getMins());<br />}<br />}<br />options<br />A)7:30<br />B)Compile Error - an enum cannot be instantiated using the new operator.<br />C)12:50<br />D)19:45<br />Correct answer is : B<br />Explanations : As an enum cannot be instantiated using the new operator, the constructors cannot be called explicitly.<br />You have to do like <br />Test t = BREAKFAST;<br />Questions no -16 <br />What is the output for the below code ?<br />public class Test {<br />enum Day {<br />MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY<br />}<br />enum Month {<br />JAN, FEB<br />}<br />public static void main(String[] args) {<br />int[] freqArray = { 12, 34, 56, 23, 5, 13, 78 };<br />// Create a Map of frequencies<br />Map<Day, Integer> ordinaryMap = new HashMap<Day, Integer>();<br />for (Day day : Day.values()) {<br />ordinaryMap.put(day, freqArray[day.ordinal()]);<br />}<br />// Create an EnumMap of frequencies<br />EnumMap<Day, Integer> frequencyEnumMap = new EnumMap<Day, Integer>(ordinaryMap);<br />// Change some frequencies<br />frequencyEnumMap.put(null, 100);<br />System.out.println(\" Frequency EnumMap: \" + frequencyEnumMap);<br />}<br />}<br />options<br />A)Frequency EnumMap: {MONDAY=12, TUESDAY=34, WEDNESDAY=56, THURSDAY=23, FRIDAY=5, SATURDAY=13, SUNDAY=78}<br />B)Compile Error <br />C)NullPointerException<br />D)Frequency EnumMap: {MONDAY=100, TUESDAY=34, WEDNESDAY=56, THURSDAY=23, FRIDAY=5, SATURDAY=13, SUNDAY=123}<br />Correct answer is : C<br />Explanations : The null reference as a key is NOT permitted.<br />Questions no -17 <br />public class EnumTypeDeclarations {<br />public void foo() {<br />enum SimpleMeal { <br />BREAKFAST, LUNCH, DINNER <br />}<br />}<br />}<br />Is the above code Compile without error ?<br />options<br />A)Compile without error<br />B)Compile with error<br />C)Compile without error but Runtime Exception<br />D)Compile without error but Enum Exception<br />Correct answer is : B<br />Explanations :<br />An enum declaration is a special kind of class declaration:<br />a) It can be declared at the top-level and as static enum declaration. <br />b) It is implicitly static, i.e. no outer object is associated with an enum constant. <br />c) It is implicitly final unless it contains constant-specific class bodies, but it can implement interfaces. <br />d) It cannot be declared abstract unless each abstract method is overridden in the constant-specific class body of every enum constant. <br />e) Local (inner) enum declaration is NOT OK! <br />Here in<br />public void foo() {<br />enum SimpleMeal { <br />BREAKFAST, LUNCH, DINNER <br />}<br />}<br />enum declaration is local within method so compile time error.<br />Questions no -18 <br />What is the output for the below code ?<br />public enum Test {<br />int t;<br />BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45);<br />private int hh;<br />private int mm;<br />Test(int hh, int mm) {<br />assert (hh >= 0 && hh <= 23) : \" Illegal hour.\" ;<br />assert (mm >= 0 && mm <= 59) : \" Illegal mins.\" ;<br />this.hh = hh;<br />this.mm = mm;<br />}<br />public int getHour() {<br />return hh;<br />}<br />public int getMins() {<br />return mm;<br />}<br />public static void main(String args[]){<br />Test t = BREAKFAST;<br />System.out.println(t.getHour() +\" :\" +t.getMins());<br />}<br />}<br />options<br />A)7:30<br />B)Compile Error <br />C)12:15<br />D)19:45<br />Correct answer is : B<br />Explanations : The enum constants must be declared before any other declarations in an enum type.<br />In this case compile error because of declaration int t; before enum declaration.<br />Questions no -19 <br />What is the output for the below code ?<br />public class B extends Thread{<br />public static void main(String argv[]){<br />B b = new B();<br />b.run();<br />} <br />public void start(){<br />for (int i = 0; i < 10; i++){<br />System.out.println(\" Value of i = \" + i); <br />} <br />}<br />}<br />options<br />A)A compile time error indicating that no run method is defined for the Thread class<br />B)A run time error indicating that no run method is defined for the Thread class<br />C)Clean compile and at run time the values 0 to 9 are printed out<br />D)Clean compile but no output at runtime<br />Correct answer is : D<br />Explanations : This is a bit of a sneaky one as I have swapped around the names of the methods you need to define and call when running a thread. If the for loop were defined in a method called public void run() and the call in the main method had been to b.start() The list of values from 0 to 9 would have been output.<br />Questions no -20 <br />What is the output for the below code ?<br />public class Test extends Thread{<br /> static String sName = \" good\" ;<br /> public static void main(String argv[]){<br /> Test t = new Test();<br /> t.nameTest(sName);<br /> System.out.println(sName);<br /> <br /> }<br /> public void nameTest(String sName){<br /> sName = sName + \" idea \" ;<br /> start();<br /> }<br /> public void run(){<br /> <br /> for(int i=0;i < 4; i++){<br /> sName = sName + \" \" + i;<br /> <br /> }<br /> }<br />}<br />options<br />A)good<br />B)good idea<br />C)good idea good idea<br />D)good 0 good 0 1<br />Correct answer is : A<br />Explanations : Change value in local methods wouldn’t change in global in case of String ( because String object is immutable).<br />Questions no -21 <br />What is the output for the below code ?<br />public class Test{<br />public static void main(String argv[]){<br />Test1 pm1 = new Test1(\" One\" );<br />pm1.run();<br />Test1 pm2 = new Test1(\" Two\" );<br />pm2.run();<br />}<br />}<br />class Test1 extends Thread{<br />private String sTname=\" \" ;<br />Test1(String s){<br />sTname = s;<br />}<br />public void run(){<br />for(int i =0; i < 2 ; i++){<br />try{<br /> sleep(1000);<br />}catch(InterruptedException e){}<br />yield();<br />System.out.println(sTname);<br />}<br />}<br />}<br />options<br />A)Compile error<br />B)One One Two Two<br />C)One Two One Two<br />D)One Two<br />Correct answer is : B<br />Explanations : If you call the run method directly it just acts as any other method and does not return to the calling code until it has finished. executing<br />Questions no -22 <br />What is the output for the below code ?<br />public class Test extends Thread{<br /> public static void main(String argv[]){<br /> Test b = new Test();<br /> b.start();<br /> }<br /> public void run(){ <br /> System.out.println(\" Running\" );<br /> }<br />}<br />options<br />A)Compilation clean and run but no output<br />B)Compilation and run with the output \" Running\" <br />C)Compile time error with complaint of no Thread import<br />D)Compile time error with complaint of no access to Thread package<br />Correct answer is : B<br />Explanations :<br />The Thread class is part of the core java.lang package and does not need any explicit import statement.<br />Questions no -23 <br />What is the output for the below code ?<br />public class Tech {<br /> public void tech() {<br /> System.out.println(\" Tech\" );<br /> }<br />}<br />public class Atech {<br />Tech a = new Tech() { <br /> public void tech() {<br /> System.out.println(\" anonymous tech\" );<br /> }<br /> };<br /> <br /> public void dothis() {<br /> a.tech(); <br /> <br /> }<br />public static void main(String... args){<br />Atech atech = new Atech();<br />atech.dothis();<br />}<br />options<br />A)anonymous tech<br />B)Compile Error <br />C)Tech<br />D)anonymous tech Tech<br />Correct answer is : A<br />Explanations : This is anonymous subclass of the specified class type.<br />Anonymous inner class ( anonymous subclass ) overriden the Tech super class of tech() method.<br />Therefore Subclass method will get called.<br />Questions no -24 <br />What is the output for the below code ?<br />public class Outer {<br /> private String x = \" Outer variable\" ;<br /> void doStuff() {<br /> String z = \" local variable\" ;<br /> class Inner {<br /> public void seeOuter() {<br /> System.out.println(\" Outer x is \" + x);<br /> System.out.println(\" Local variable z is \" + z); <br /> } <br /> } <br /> } <br /> <br /> <br />}<br />options<br />A)Outer x is Outer variable.<br />B)Compile Error <br />C)Local variable z is local variable.<br />D)Outer x is Outer variable Local variable z is local variable<br />Correct answer is : B<br />Explanations : Cannot refer to a non-final variable z inside an inner class defined in a different method.<br />Questions no -25<br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) {<br />for(int i = 2; i < 4; i++)<br /> for(int j = 2; j < 4; j++)<br /> assert i!=j : i; <br />}<br />}<br />options<br />A)The class compiles and runs, but does not print anything.<br />B)The number 2 gets printed with AssertionError<br />C)The number 3 gets printed with AssertionError<br />D)compile error<br />Correct answer is : B<br />Explanations : When i and j are both 2, assert condition is false, and AssertionError gets generated. <br />Questions no -26<br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) {<br />for(int i = 2; i < 4; i++)<br /> for(int j = 2; j < 4; j++)<br />if(i < j)<br /> assert i!=j : i; <br />}<br />}<br />options<br />A)The class compiles and runs, but does not print anything.<br />B)The number 2 gets printed with AssertionError<br />C)The number 3 gets printed with AssertionError<br />D)compile error<br />Correct answer is : A<br />Explanations : When if condition returns true, the assert statement also returns true.<br />Hence AssertionError not generated.<br />Questions no -26 <br />What is the output for the below code ?<br />public class NameBean {<br />private String str;<br />NameBean(String str ){<br />this.str = str;<br />}<br />public String toString() {<br />return str;<br />}<br />}<br />import java.util.HashSet;<br />public class CollClient {<br />public static void main(String ... sss) {<br />HashSet myMap = new HashSet();<br />String s1 = new String(\" das\" );<br />String s2 = new String(\" das\" );<br />NameBean s3 = new NameBean(\" abcdef\" );<br />NameBean s4 = new NameBean(\" abcdef\" );<br />myMap.add(s1);<br />myMap.add(s2);<br />myMap.add(s3);<br />myMap.add(s4);<br />System.out.println(myMap);<br />}<br />}<br />options<br />A)das abcdef abcdef<br />B)das das abcdef abcdef<br />C)das abcdef<br />D)abcdef abcdef<br />Correct answer is : A<br />Explanations : Need to implement 'equals' and 'hashCode' methods to get unique Set for user defind objects(NameBean).<br />String object internally implements 'equals' and 'hashCode' methods therefore Set only stored one value.<br />Questions no -27 <br />Synchronized resizable-array implementation of the List interface is _____________?<br />options<br />A)Vector<br />B)ArrayList<br />C)Hashtable<br />D)HashMap<br />Correct answer is : A<br />Explanations : Vector implements List, RandomAccess - Synchronized resizable-array implementation of the List interface with additional \" legacy methods.\" <br />Questions no -28 <br />What is the output for the below code ?<br />public class Test {<br /> <br /> public static void main(String argv[]){<br /> <br /> ArrayList list = new ArrayList(); <br /> ArrayList<String> listStr = list; <br /> ArrayList<StringBuffer> listBuf = list; <br /> listStr.add(0, \" Hello\" ); <br /> StringBuffer buff = listBuf.get(0); <br /> System.out.println(buff.toString());<br /> }<br />}<br />options<br />A)Hello<br />B)Compile error<br />C)java.lang.ClassCastException<br />D)null<br />Correct answer is : C<br />Explanations : java.lang.String cannot be cast to java.lang.StringBuffer at the code StringBuffer buff = listBuf.get(0); <br />So thows java.lang.ClassCastException.<br />Questions no -29 <br />What is the output for the below code ?<br />import java.util.LinkedList;<br />import java.util.Queue;<br />public class Test {<br />public static void main(String... args) {<br /> <br />Queue<String> q = new LinkedList<String>();<br /> q.add(\" newyork\" );<br /> q.add(\" ca\" );<br /> q.add(\" texas\" );<br /> show(q);<br /> }<br /> public static void show(Queue q) {<br /> q.add(new Integer(11));<br /> while (!q.isEmpty ( ) )<br /> System.out.print(q.poll() + \" \" );<br /> }<br />}<br />options<br />A)Compile error : Integer can't add<br />B)newyork ca texas 11<br />C)newyork ca texas<br />D)newyork ca <br />Correct answer is : B<br />Explanations : <br /> q was originally declared as Queue<String>, But in show() method it is passed as an untyped Queue. nothing in the compiler or JVM prevents us from adding an Integer after that.<br /> If the show method signature is public static void show(Queue<String> q) than you can't add Integer, Only String allowed. But public static void show(Queue q) is untyped Queue so you can add Integer.<br /> poll() Retrieves and removes the head of this queue, or returns null if this queue is empty.<br />Questions no -30 <br />What is the output for the below code ?<br />public interface TestInf {<br /> int i =10;<br />}<br />public class Test {<br />public static void main(String... args) {<br />TestInf.i=12;<br />System.out.println(TestInf.i);<br />}<br />}<br />options<br />A)Compile with error<br />B)10<br />C)12<br />D) Runtime Exception<br />Correct answer is : A<br />Explanations : All the variables declared in interface is Implicitly static and final , therefore can't change the value.<br />Questions no -31 <br />What is the output for the below code ?<br />public class Test {<br /> static { int a = 5; }<br /> public static void main(String[] args){<br /> System.out.println(a);<br /> }<br /> }<br />options<br />A)Compile with error<br />B)5<br />C)0<br />D) Runtime Exception<br />Correct answer is : A<br />Explanations : A variable declared in a static initialiser is not accessible outside its enclosing block.<br />Questions no -32 <br />What is the output for the below code ?<br />class A {<br /> { System.out.print(\" b1 \" ); }<br /> public A() { System.out.print(\" b2 \" ); }<br />}<br />class B extends A {<br /> static { System.out.print(\" r1 \" ); }<br /> public B() { System.out.print(\" r2 \" ); }<br /> { System.out.print(\" r3 \" ); }<br /> static { System.out.print(\" r4 \" ); }<br />}<br />class C extends B {<br /> public static void main(String[] args) {<br /> System.out.print(\" pre \" );<br /> new C();<br /> System.out.println(\" post \" );<br /> }<br />}<br />options<br />A)r1 r4 pre b1 b2 r3 r2 post<br />B)r1 r4 pre b1 b2 post<br />C)r1 r4 pre b1 b2 post r3 r2<br />D)pre r1 r4 b1 b2 r2 r3 post<br />Correct answer is : A<br />Explanations : All static blocks execute first then blocks and constructor. <br />Blocks and constructor executes (super class block then super class constructor, sub class block then sub class constructor).<br />Sequence for static blocks is super class first then sub class.<br />Sequence for blocks is super class first then sub class.<br />Questions no -33 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) throws Exception {<br />Integer i = 34;<br />int l = 34;<br />if(i.equals(l)){<br />System.out.println(true);<br />}else{<br />System.out.println(false);<br />}<br />}<br />}<br />options<br />A)true<br />B)false<br />C)Compile Error<br />D) Runtime Exception<br />Correct answer is : A<br />Explanations : equals() method for the integer wrappers will only return true if the two primitive types and the two values are equal.<br />Questions no -34 <br />Which statement is true about outer class?<br />options<br />A)outer class can only declare public , abstract and final<br />B)outer class may be private<br />C)outer class can't be abstract<br />D)outer class can be static<br />Correct answer is : A<br />Explanations : outer class can only declare public , abstract and final.<br />Questions no -35 <br />What is the output for the below code ?<br />static public class Test {<br />public static void main(String[] args) {<br /> char c = 'a';<br /> switch(c){<br /> case 65:<br /> System.out.println(\" one\" );break;<br /> case 'a':<br /> System.out.println(\" two\" );break;<br /> case 3:<br /> System.out.println(\" three\" );<br />}<br />}<br />}<br />options<br />A)one<br />B)two<br />C)Compile error - char can't be permitted in switch statement <br />D)Compile error - Illegal modifier for the class Test; only public, abstract & final are permitted.<br />Correct answer is : D<br />Explanations : outer class can only declare public , abstract and final.<br />Illegal modifier for the class Test; only public, abstract & final are permitted<br />Questions no -36<br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) {<br />ArrayList<Integer> list = new ArrayList<Integer>();<br />list.add(1);<br />list.add(2);<br />list.add(3);<br />for(int i:list)<br />System.out.println(i);<br />}<br />}<br />options<br />A)1 2 3<br />B)Compile error , can't add primitive type in ArrayList<br />C)Compile error on for(int i:list) , Incorrect Syntax<br />D)0 0 0<br />Correct answer is : A<br />Explanations : JDK 1.5, 1.6 allows add primitive type in ArrayList and for(int i:list) syntax is also correct.<br />for(int i:list) is same as <br />for(int i=0; i < list.size();i++){<br />int a = list.get(i);<br />}<br />Questions no -37 <br />What is the output for the below code ?<br />public class SuperClass {<br />public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{<br />String signature = \" (String, Integer[])\" ;<br />System.out.println(str + \" \" + signature);<br />return 1;<br />}<br />}<br />public class SubClass extends SuperClass{<br />public int doIt(String str, Integer... data) throws Exception<br />{<br />String signature = \" (String, Integer[])\" ;<br />System.out.println(\" Overridden: \" + str + \" \" + signature);<br />return 0;<br />}<br />public static void main(String... args)<br />{<br />SuperClass sb = new SubClass();<br />try{<br />sb.doIt(\" hello\" , 3);<br />}catch(Exception e){<br />}<br />}<br />}<br />options<br />A)Overridden: hello (String, Integer[])<br />B)hello (String, Integer[])<br />C)This code throws an Exception at Runtime<br />D)Compile with error<br />Correct answer is : D<br />Explanations : Exception Exception is not compatible with throws clause in SuperClass.doIt(String, Integer[]). <br />The same exception or subclass of that exception is allowed.<br />Questions no -38<br />What is the result of executing the following code, using the parameters 0 and 3 ?<br />public void divide(int a, int b) {<br />try {<br />int c = a / b;<br />} catch (Exception e) {<br />System.out.print(\" Exception \" );<br />} finally {<br />System.out.println(\" Finally\" );<br />}<br />options<br />A)Prints out: Exception Finally<br />B)Prints out: Finally<br />C)Prints out: Exception<br />D)Compile with error<br />Correct answer is : B<br />Explanations : finally block always executed whether exception occurs or not. <br />0/3 = 0 Does not throws exception.<br />Questions no -39<br />Which of the below statement is true about Error?<br />options<br />A)An Error is a subclass of Throwable<br />B)An Error is a subclass of Exception<br />C)Error indicates serious problems that a reasonable application should not try to catch.<br />D)An Error is a subclass of IOException<br />Correct answer is : A and C<br />Explanations : An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.<br />Questions no -40<br />Which of the following is type of RuntimeException?<br />options<br />A)IOException<br />B)ArrayIndexOutOfBoundsException<br />C)Exception<br />D)Error<br />Correct answer is : B<br />Explanations : Below is the tree.<br />java.lang.Object<br /> java.lang.Throwable<br /> java.lang.Exception<br /> java.lang.RuntimeException<br /> java.lang.IndexOutOfBoundsException<br /> java.lang.ArrayIndexOutOfBoundsException<br />Questions no -41 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) throws Exception {<br /> File file = new File(\" test.txt\" ); <br /> System.out.println(file.exists());<br /> FileWriter fw = new FileWriter(file); <br /> System.out.println(file.exists());<br />}<br />}<br />options<br />A)true true<br />B)false false<br />C)false true<br />D)true false<br />Correct answer is : C<br />Explanations :Creating a new instance of the class File, you're not yet making an actual file, you're just creating a filename.<br />So file.exists() return false.<br />FileWriter fw = new FileWriter(file) do three things:<br />It created a FileWriter reference variable fw.<br />It created a FileWriter object, and assigned it to fw.<br />It created an actual empty file out on the disk.<br />So file.exists() return true.<br />Questions no -42 <br />When comparing java.io.BufferedWriter and java.io.FileWriter, which capability exist as a method in only one of two ?<br />options<br />A)closing the stream<br />B)flushing the stream<br />C)writting to the stream<br />D)writting a line separator to the stream<br />Correct answer is : D<br />Explanations :A newLine() method is provided in BufferedWriter which is not in FileWriter.<br />Questions no -43 <br />What is the output for the below code ?<br />public class Test{<br />public static void main(String[] args) {<br /> int i1=1;<br /> switch(i1){<br /> case 1:<br /> System.out.println(\" one\" );<br /> case 2:<br /> System.out.println(\" two\" );<br /> case 3:<br /> System.out.println(\" three\" );<br />}<br />}<br />}<br />options<br />A)one two three<br />B)one<br />C)one two<br />D)Compile error.<br />Correct answer is : A<br />Explanations : There is no break statement in case 1 so it causes the below case statements to execute regardless of their values.<br />Questions no -44 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String[] args) {<br /> char c = 'a';<br /> switch(c){<br /> case 65:<br /> System.out.println(\" one\" );break;<br /> case 'a':<br /> System.out.println(\" two\" );break;<br /> case 3:<br /> System.out.println(\" three\" );<br />}<br />}<br />}<br />options<br />A)one two three<br />B)one<br />C)two<br />D)Compile error - char can't be in switch statement.<br />Correct answer is : C<br />Explanations : Compile properly and print two.<br />Questions no -45 <br />What is the output for the below code ?<br />import java.util.NavigableMap;<br />import java.util.concurrent.ConcurrentSkipListMap;<br />public class Test {<br />public static void main(String... args) {<br /> <br />NavigableMap <Integer, String>navMap = new <br /> ConcurrentSkipListMap<Integer, String>();<br />navMap.put(4, \" April\" );<br />navMap.put(5, \" May\" );<br />navMap.put(6, \" June\" );<br />navMap.put(1, \" January\" );<br />navMap.put(2, \" February\" );<br />navMap.put(3, \" March\" );<br /> navMap.pollFirstEntry();<br /> navMap.pollLastEntry();<br /> navMap.pollFirstEntry();<br /> System.out.println(navMap.size());<br /> <br />}<br />}<br />options<br />A)Compile error : No method name like pollFirstEntry() or pollLastEntry()<br />B)3<br />C)6<br />D)4<br />Correct answer is : B<br />Explanations : <br /> pollFirstEntry() Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.<br /> pollLastEntry() Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.<br />Questions no -46<br />What is the output for the below code ?<br />import java.util.NavigableMap;<br />import java.util.concurrent.ConcurrentSkipListMap;<br />public class Test {<br />public static void main(String... args) {<br /> <br />NavigableMap <Integer, String>navMap = new <br /> ConcurrentSkipListMap<Integer, String>();<br /> System.out.print(navMap.lastEntry());<br /> <br />}<br />}<br />options<br />A)Compile error : No method name like lastEntry()<br />B)null<br />C)NullPointerException<br />D)0<br />Correct answer is : B<br />Explanations : lastEntry() Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.<br />Questions no -47<br />What is the output for the below code ?<br />import java.util.NavigableMap;<br />import java.util.concurrent.ConcurrentSkipListMap;<br />public class Test {<br />public static void main(String... args) {<br /> <br />NavigableMap<Integer, String>navMap = new <br /> ConcurrentSkipListMap<Integer, String>();<br />navMap.put(4, \" April\" );<br />navMap.put(5, \" May\" );<br />navMap.put(6, \" June\" );<br />navMap.put(1, \" January\" );<br />navMap.put(2, \" February\" );<br /> System.out.print(navMap.ceilingKey(3));<br /> <br /> <br />}<br />}<br />options<br />A)Compile error : No method name like ceilingKey()<br />B)null<br />C)NullPointerException<br />D)4<br />Correct answer is : D<br />Explanations : Returns the least key greater than or equal to the given key, or null if there is no such key. <br />In the above case : 3 is not a key so return 4 (least key greater than or equal to the given key).<br />Questions no -48<br />What is the output for the below code ?<br />import java.util.NavigableMap;<br />import java.util.concurrent.ConcurrentSkipListMap;<br />public class Test {<br />public static void main(String... args) {<br /> <br />NavigableMap<Integer, String>navMap = new <br /> ConcurrentSkipListMap<Integer, String>();<br />navMap.put(4, \" April\" );<br />navMap.put(5, \" May\" );<br />navMap.put(6, \" June\" );<br />navMap.put(1, \" January\" );<br />navMap.put(2, \" February\" );<br /> System.out.print(navMap.floorKey(3));<br /> <br /> <br />}<br />}<br />options<br />A)Compile error : No method name like floorKey()<br />B)null<br />C)NullPointerException<br />D)2<br />Correct answer is : D<br />Explanations : Returns the greatest key less than or equal to the given key, or null if there is no such key. <br />In the above case : 3 is not a key so return 2 (greatest key less than or equal to the given key).<br />Questions no -49 <br />What is the output for the below code ?<br />public class Test {<br />public static void main(String... args) {<br /> <br /> List<Integer> lst = new ArrayList<Integer>();<br /> lst.add(34);<br /> lst.add(6);<br /> lst.add(6);<br /> lst.add(6);<br /> lst.add(6);<br /> lst.add(5);<br /> <br /> NavigableSet<Integer> nvset = new TreeSet(lst);<br /> System.out.println(nvset.tailSet(6));<br /> <br />}<br />}<br />options<br />A)Compile error : No method name like tailSet()<br />B)6 34<br />C)5<br />D)5 6 34<br />Correct answer is : B<br />Explanations : tailSet(6) Returns elements are greater than or equal to 6.<br />Questions no -50 <br />What is the output for the below code ?<br />import java.util.ArrayList;<br />import java.util.List;<br />import java.util.NavigableSet;<br />import java.util.TreeSet;<br />public class Test {<br />public static void main(String... args) {<br /> <br />List<Integer> lst = new ArrayList<Integer>();<br /> lst.add(34);<br /> lst.add(6);<br /> lst.add(6);<br /> lst.add(6);<br /> lst.add(6);<br /> <br /> NavigableSet<Integer> nvset = new TreeSet(lst);<br /> nvset.pollFirst(); <br /> nvset.pollLast();<br /> System.out.println(nvset.size());<br /> <br />}<br />}<br />options<br />A)Compile error : No method name like pollFirst() or pollLast()<br />B)0<br />C)3<br />D)5<br />Correct answer is : B<br />Explanations : <br /> pollFirst() Retrieves and removes the first (lowest) element, or returns null if this set is empty.<br /> pollLast() Retrieves and removes the last (highest) element, or returns null if this set is empty.<br />Questions no -51<br />What is the output for the below code ?<br />import java.util.ArrayList;<br />import java.util.List;<br />import java.util.NavigableSet;<br />import java.util.TreeSet;<br />public class Test {<br />public static void main(String... args) {<br /> <br /> List<Integer> lst = new ArrayList<Integer>();<br /> lst.add(34);<br /> lst.add(6);<br /> lst.add(2);<br /> lst.add(8);<br /> lst.add(7);<br /> lst.add(10);<br /> <br /> NavigableSet<Integer> nvset = new TreeSet(lst);<br /> System.out.println(nvset.lower(6)+\" \" +nvset.higher(6)+ \" \" + nvset.lower(2));<br /> <br />}<br />}<br />options<br />A)1 2 7 10 34 null<br />B)2 7 null<br />C)2 7 34<br />D)1 2 7 10 34<br />Correct answer is : B<br />Explanations : <br /> lower() Returns the greatest element in this set strictly less than the given element, or null if there is no such element.<br /> higher() Returns the least element in this set strictly greater than the given element, or null if there is no such element.<br />Questions no -52<br />What is the output for the below code ?<br />import java.util.ArrayList;<br />import java.util.Iterator;<br />import java.util.List;<br />import java.util.NavigableSet;<br />import java.util.TreeSet;<br />public class Test {<br />public static void main(String... args) {<br /> <br /> List<Integer> lst = new ArrayList<Integer>();<br /> lst.add(34);<br /> lst.add(6);<br /> lst.add(2);<br /> lst.add(8);<br /> lst.add(7);<br /> lst.add(10);<br /> <br /> NavigableSet<Integer> nvset = new TreeSet(lst);<br /> System.out.println(nvset.headSet(10));<br /> <br />}<br />}<br />options<br />A)Compile error : No method name like headSet()<br />B)2, 6, 7, 8, 10<br />C)2, 6, 7, 8<br />D)34<br />Correct answer is : C<br />Explanations : <br /> headSet(10) Returns the elements elements are strictly less than 10.<br /> headSet(10,false) Returns the elements elements are strictly less than 10.<br /> headSet(10,true) Returns the elements elements are strictly less than or equal to 10.<br />Questions no -53 <br />What is the output for the below code ?<br />import java.io.Console;<br />public class Test {<br />public static void main(String... args) {<br /> <br />Console con = System.console();<br /> boolean auth = false;<br /> <br /> if (con != null)<br /> {<br /> int count = 0;<br /> do<br /> {<br /> String uname = con.readLine(null);<br /> char[] pwd = con.readPassword(\" Enter %s's password: \" , uname);<br /> <br /> con.writer().write(\" \n\n\" ); <br /> } while (!auth && ++count < 3);<br /> }<br /> <br />}<br />}<br />options<br />A)NullPointerException<br />B)It works properly<br />C)Compile Error : No readPassword() method in Console class.<br />D)null<br />Correct answer is : A<br />Explanations : passing a null argument to any method in Console class will cause a NullPointerException to be thrown.<br />