Core Java Questions

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 11

CORE JAVA QUESTIONS

Q1.What will be the result of compiling and running the given program? public class Q1 { public static void main(String[] args) { boolean t1 = true, t2 = false, t3 = t1; boolean t = false; t &&= (t1 || ( t2 && t3)); //line no. 5 System.out.println("boolean t = "+t); } } 1. Compile time error at the line no. 5 as illegal start of expression as well as this is not a statement 2. Program compiles correctly and print "boolean t = false" when executed. 3. Compile time error at the line no. 7. 4. Program compiles correctly and print "boolean t = true" when executed. Q2.What will be the result of compiling and running the given program? public class Q2 { public static void main(String[] args) { int[] a; a = new int[0]; call(a); System.out.println(a.length+" , "+a[0]); } static void call(int[] a){ int[] b = {4,5,6}; a = b; a = new int[6]; } } 1. Compile time error. 2. Run time error. 3. Program compiles correctly and print "3,4" when executed. 4. Program compiles correctly and print "6,0" when executed. 5. Program compiles correctly and print "0,0" when executed. Q3.Which of the given choices are true?Select any two. public class Q3 { public static void main(String[] args) { long l = 2; int l1 = 2; long i = l1 >> 33; int i1 = l1 >> 33; long i2 = l >> 33; if ( i = = i2) System.out.println("equal"); else System.out.println("not equal"); if ( i = = i1) System.out.println("equal"); else System.out.println("not equal"); } } 1. "i = = i2" will print not equal. 2. "i = = i2" will print equal. 3. "i = = i1" will print equal. 4. "i = = i1" will print not equal. Q4.What will be the result of compiling and running the given program? public class Q4 { public static void main(String[] args) { double d = Integer.MAX_VALUE; float f= Integer.MAX_VALUE; if (f == d) System.out.println("equal"); else System.out.println(" not equal ");

} } 1. Compile time error. 2. Program compiles correctly and print "equal" when executed. 3. Program compiles correctly and print "not equal" when executed. 4. Run time error. Q5.What will be the result of compiling and running the given program?Select any two. public class Q5 { public static void main(String[] args) { int a[] = {4,5,9,8,6,6,0}; int i=1; a[i] = i+= a[i++]; System.out.println(a[i]); // line number 8. System.out.println(i); } } 1. Line number 8 will print 6. 2. Line number 8 will print 0. 3. Line number 9 will print 9. 4. Line number 9 will print 6. Q6.What will be the result of compiling and running the given program? public class Q6 { public static void main(String[] args) { int a=1,b=2,c=3; a=b=c; // line number 6. boolean bool; bool = a = = b = = c; // line number 8 System.out.println(a+b+c+" , "+bool); } } 1. 9 , false 2. 9 , true 3. Compiler Error at line 8 4. Compiler Error at line 6 Q7.What will be the result of compiling and running the given program? public class Q7 { public static void main(String[] args) { int i=5; System.out.println(i%-0.0); } } 1. NaN 2. Infinity 3. -Infinity 4. 0.0 5. Runtime error at line number 6. Q8.Which of the following are not legal declarations in a class?. 1. synchronized native void method(); 2. final native void method(); 3. abstract native void method(); 4. transient volatile final int i; Q9.Which one of the following are valid declaration of switch statements?Select any two. 1. int x; switch( x = 5){ } 2. switch(int x = 5){ } 3. int x; switch( x = 5){ break; } 4. int x; switch (x=5){ default: } Q10.What will be the result of compiling and running the given program? public class Q10 { public static void main(String[] args) { int i=3; System.out.println(i*=2 + i++); } } 1. Program compiles correctly and prints 7 when executed. 2. Program compiles correctly and prints 11 when executed. 3. Program compiles correctly and prints 15 when executed. 4. None of above. Q11.What will be the result of compiling and running the given program? public class Q11 { static int i=4; Q11() { i=i++; }

void Q11(){ this(); System.out.println(i); } public static void main(String args){ Q11 q1 = new Q11(); q1.Q11(); } } 1. Program compiles correctly and prints 5 when executed. 2. Program compiles correctly and prints 6 when executed. 3. Compile time error at line number 10. 4. Compile time error at line number 8. Q12.What will be the result of compiling and running the given program? public class Q12 { public static void main(String[] args) { char x='1'; switch (x) { default: System.out.println("default"); case 1: System.out.println("case 1"); case 2: System.out.println("case 2"); break; } } } 1. Compile time error as explicit cast needed to convert case constants from int to char. 2. Run time erroras explicit cast needed to convert case constants from int to char. 3. Program compiles correctly and prints "case 1" and "case 2" when executed. 4. Program compiles correctly and prints "case 1" when executed. 5. None of above. Q13.What will be the result of compiling and running the given program? public class Q13 { static int call(int x) { try { System.out.println(x---x/0); return x--; } catch(Exception e){ System.out.println(--x-x%0); return x--; } finally{ return x--; } }//end of call method public static void main(String[] args){ System.out.println(" value = "+ call(5)); } } 1. Compile time error. 2. Run time error. 3. Program compiles correctly and prints 4 when executed. 4. Program compiles correctly and prints 3 when executed. Q14.What will be the result of compiling and running the given program? public class Q14 { public static void main(String[] args)throws Exception { RuntimeException re = null; try { throw re; // line number 8 }

catch(Exception e) { throw e; // line number 12 } finally { throw re; // line number 16 } } } 1. java.lang.NullPointerException will be thrown. 2. java.lang.RuntimeException will be thrown.. 3. Compile time error at line number 8. 4. Compile time error at line number 12. 5. Compile time error at line number 16. Q15.What will happen if I save,compile and run the bellow code as Q15.java? class superclass { public static void main(String[] args) { System.out.println("Hello"); } } class Q15 extends superclass { } 1. Program compiles correctly and prints Hello when executed. 2. Program compiles correctly and give no output. 3. Program compiles correctly but give run time error. 4. Compile time error at line 1. 5. Compile time error at line 11. Q16.What will be the result of compiling and running the given program? interface I1 {} interface I2 {} class A implements I2{} class Q16 extends A implements I1 { public static void main(String[] args) { A a = new Q16(); I2 i2 = a; // line number 9 I1 i1 = (I1)a; // line no. 10 } } 1. Compile time error at line number 9. 2. Runtime time error at line number 9. 3. Runtime error at line number 10. 4. Program compiles and runs without any error. Q17.What will be the result of compiling and running the given program? interface I1 {} interface I2 extends I1{} class A implements I2{} class Q17 implements I2 extends A { public static void main(String[] args) { Q17 q =new Q17(); I1 i1 = q; // line no.9 } } 1. Compile time error at line number 9. 2. Runtime error at line number 9. 3. Program will compile and run correctly. 4. None of above. Q18.What will be the result of compiling and running the given program? class superclass { void method1(int i){} // line no.3 void method2(final int j){} // line no.4 void method3(){} } class Q18 extends superclass implements Runnable { void method1(final int i){} // line no.9 void method2(int j) throws ArithmeticException{} // line no.10 void method3() throws InterruptedException{} // line no.11

synchronized void run() throws NullPointerException{} // line no.12 } 1. Compile time errors at line number 11 and 12. 2. Compile time errors at line number 4 and 10. 3. Compile time errors at line number 3 and 9. 4. Program will compile correctly. Q19.What will be the result of compiling and running the given program? class Q19 { private static int increase(int i) { return i++; } public Q19() { i = increase(1); // line no.9 } { i = increase(1); // line no.12 } static int i = increase(1); public static void main(String[] args) { System.out.println(i=increase(i)); // line no.17 } } 1. Compile time error at line 9 as forward referencing is not allowed. 2. Compile time error at line 12 instant intializer block cannot access a static method. 3. Program will compile correctly and print 1 when executed. 4. Program will compile correctly and print 4 when executed. 5. Compile time error at line 17 as you can not use = in the print statement. Q20.Which of the given choices can make the program compiles and run without any error?Select two interface I1 {} class A {} class Q20 extends A implements I1 { public static void main(String[] args) { Q20 q = new Q20(); A a =new A();// line no.8 I1 i1 = (I1)a; //This line(9) is giving ClassCastException. } } 1. Replace the statement at line number 9 with the statement "I1 i1 = a". 2. Replace the statement at line number 8 with the statement "A a = q;". 3. Replace the statement at line number 8 with the statement "A a = new Q20();". 4. Completly remove the statement at line number 8. Q21.What will be the result of compiling and running the given program? interface I1 {} class A {} class Q21 extends A implements I1 { public static void main(String[] args) { Q21 q = new Q21(); A a =null; // line no.8 I1 i1 = (I1)a; // line no.9 } } 1. Program compiles and run without any error. 2. Program compiles correctly but give NullPointerException when excuted. 3. Compile time error at line number 8. 4. Compile time error at line number 9. Q22.Which method is used to make the thread eligible for running? 1. run() 2. start() 3. notify() 4. resume() Q23.What is the default priority of a thread?Select one correct answer. 1. Depends upon Operating System. 2. The default priority is 1. 3. The default priority is 5. 4. The default priority is 10. Q24.What will be the result of compiling and running the given program? class Q24 { public static void main(String[] args) { StringBuffer str =new StringBuffer("Hello");

String str1 = new String(str); if (str.equals(str1)) // line number 7. System.out.println("true"); else System.out.println("false"); } } 1. Program compiles correctly and prints true when executed. 2. Program compiles correctly and prints false when executed. 3. Compile time error at line number 7. 4. Runtime error at line number 7. Q25.What will be the result of compiling and running the given program? class Q25 { public static void main(String[] args) { Boolean bool1 = new Boolean("Wrong"); // line number 5. Boolean bool2 = new Boolean("True"); System.out.println(bool1.toString()); System.out.println(bool2.toString()); } } 1. Program compiles correctly and prints false and true when executed. 2. Program compiles correctly and prints Wrong and True when executed. 3. Compile time error at line number 5. 4. Runtime error at line number 5. Q26.What will be the value of st after line no.14?Enter your answer in the given field. Don't use any extra signs like ("")quotes or any string like "st=".For example, if your answer is the string "XYZ" you just write XYZ class Q26 { public static void main(String[] args) { String str = new String("Hello"),st=""; if (str.substring(0) == str) st+="A"; else st+="B"; if (str.replace('k','j') == str) st+="C"; else st+="D"; System.out.println(st); } } (Note:This is the same way of doing entry as it is in real exam, you have to take care of above assumption while doing entry in a text field) Q27.Which of the following regarding garbage collection are true? 1. Given,b1 = b2. If b2 is eligible for garbage collection, then b1 is also eligible for garbage collection. 2. An object will be garbage collected immediately after the last reference to the object is removed. 3. Once an object has become eligible for garbage collection, it will remain so until it is destroyed. 4. You cannot force garbage collection. Q28.What will be the result of compiling and running the given program? class Q28 { public static void main(String[] args){ System.out.println(Math.ceil(-0.5)+"/"+Math.floor(-0.5)); } } 1. Program compiles correctly and prints -0.0/-1.0 when executed. 2. Program compiles correctly and prints -1.0/0.0 when executed. 3. Program compiles correctly and prints -1.0/-0.0 when executed. 4. Program compiles correctly and prints 0.0/-1.0 when executed. Q29.Which of the following collection implementations are thread safe? Select any two. 1. HashMap 2. Vector 3. Hashtable 4. LinkedList

Q30.What is the default number of rows visible in a List? Select one correct answer. 1. 1 2. 2 3. 3 4. 4 Q31.Which interface is implemented by the MouseMotionAdapter class? 1. MouseListener 2. MouseMotionListener 3. MouseEvent 4. MouseMotionEvent Q32.What will be the result of compiling and running the given program? class Q32 { public static void main(String[] args){ Frame f = new Frame("My frame"); f.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); f.add(new Button("B1")); f.add(new Button("B2")); f.setSize(400,400); f.setVisible(true); } } 1. Only the button B2 will appear covering the whole frame. 2. Both the button B1 and B2 will appear side by side covering the whole frame. 3. Both the button B1 and B2 will appear with thier actual size placed in the top left area of the frame. 4. Both the button B1 and B2 will appear with thier actual size placed in the center of the frame. 5. None of above. Q33.Name the component whose prefered size is honoured by BorderLayout? 1. Choice 2. TextArea 3. Checkbox 4. It is not possible to do so using BorderLayout.Only either one dimension can be honoured. Q34.Which of the following are valid constructors of IO stream classes? 1. DataInputStream dis = new DataInputStream(new File("test.txt")); 2. DataOutputStream dos = new OutputStream(new FileInputStream("test.txt")); 3. OutputStream os = new BufferedOutputStream(new FileOutputStream("test.txt")); 4. FileInputStream fs = new FileInputStream("C:\test","test.txt"); Q35.Which of the following are valid constructors of IO classes? 1. PrintWriter pw = new PrintWriter(new File("test.txt"),true); 2. InputStreamReader isr = new InputStreamReader(new File("test.txt")); 3. BufferedWriterr br = new BufferedWriter(new PrintInputStream(System.out)); 4. BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Q36.What will be the result of compiling and running the given program? class Q36 extends Thread { public void run() { System.out.println("Hello"); } public static void main(String[] args) { Runnable r = new Q36(); // line number 9. Thread t = new Thread(r); t.run(); } } 1. Compile time error at line number 9. 2. Program compiles correctly and prints Hello when executed. 3. Runtime error at line number 9 4. None of above. Q37.What will be the result of compiling and running the given program? class Q37{ public static void main(String[] args) { String s1 = null; String s2 = null; if (s1 == s2) System.out.print("A"); else System.out.print("B"); if (s1 .equals(s2)) System.out.print("C");

else System.out.print("D"); } 1. 2. 3. 4. } Program will compile correctly and print A and D when executed. Program will compile correctly and print A and C when executed. Program will compile correctly and print B and D when executed. Compile time error. 5. Runtime error.

Q38.Which of the following about inner classes are true?Select any two. 1. When an anonymous class implements an interface it extends Object class implicitly. 2. An anonymous class can implement any number of interface. 3. An anonymous class cannot have constructors 4. An anonymous class can be both an explicit subclass and implement an interface. Q39.What will be the result of compiling and running the given program? public class Q39 { public static void main(String args[]) { int x = 10; final int y; // line no.6 if(x++ > 10) y = 1; // line no.8 else y = 2; // line no.10 System.out.println("y is " + y); } } 1. Compile time error at line number 6 as y is final and must be initialized within the statement. 2. Compile time error at line number 8 as you cannot initialize a final variable again. 3. Compile time error at line number 10 as you cannot initialize a final variable again. 4. Program will compile correctly and print y is 2 when executed. 5. Program will compile correctly and print y is 1 when executed. Q40.Which of the following are methods of ComponentListener?Select any two. 1. componentHidden(....) 2. componentAdded(....) 3. componentShown(....) 4. componentRemoved(....) Q41.Whether the following code compile or not? class Q41{ public static void main(String arg[]){ final int y; } } 1. It will not compile. 2. It will compile. 3. It will compile if we initalize the y in the statement at line number 5. Q42.What will be the result of compiling and running the given program? class Q42 { public static void main(String args[]) { int b='A'; // line no.5 System.out.write(b); // line no.6 } } 1. Compile time error at line number 5 as you cannot assign a char value to an int variable. 2. Compile time error at line number 6 as there is no write() method in class System.out 3. Program compiles correctly and prints A when executed. 4. Program compiles correctly and give no output when executed. Q43.What will be the result of compiling and running the given program? class Q43 { int y=1; Q43() { this(y); } Q43(int x) { y=x++ + ++x; }

public static void main(String [] args) { Q43 t = new Q43(); System.out.println(t.y); } }//end of class 1. Program compiles correctly and prints 1 when executed. 2. Program compiles correctly and prints 4 when executed. 3. Compile time error. 4. None of above. Q44.What will be the result of compiling and running the given program? class Q44 { public static void main(String[] args) { int intNumber = 123456789; float floatNumber = intNumber; int x = (int)floatNumber; System.out.println(intNumber - x); System.out.println(intNumber == x ); }// This brace is for main }//this brace is for classQ44 1. Prints 0 and true. 2. Prints 0 and false. 3. Prints some number (other than 0) and true. 4. Prints some number (other than 0) and false. Q45.What will be the result of compiling and running the given program? Select one correct answer. public class Q45 { static { System.out.print("Java".startsWith("")); } static void processorB() { System.out.println(" is a boolean Operator."); } static Q45 processorA() { Q45 q=null; if (("java").startsWith("null")) return q; else return null; } public static void main(String[] args) { processorA().processorB(); // line number 22. } } 1. Compile time error at line number 22 as you can't call processorB() like this. 2. NullPointerException at line number 22 as processorA() will return null. 3. Program will compile correctly and print false is a boolean operator when executed. 4. Program will compile correctly and print true is a boolean operator when executed. Q46.What will be the result of compiling and running the given program? class Q46 { public static void main(String [] args) { String str = "Java"; str += "Beans"; if(str.equals("JavaBeans")) System.out.println("TRUE" ); else System.out.println("FALSE" ); if(str == "JavaBeans") System.out.println("TRUE" ); else System.out.println("FALSE" ); } } 1. Program will compile correctly and print TRUE , TRUE when executed. 2. Program will compile correctly and print FALSE , FALSE when executed. 3. Program will compile correctly and print FALSE , TRUE when executed. 4. Program will compile correctly and print TRUE , FALSE when executed.

Q47.Which of the following is not valid adapter class?Select one correct answer. 1. MouseMotionAdapter 2. KeyAdapter 3. AdjustmentAdapter 4. WindowAdapter Q48.What will be the result of compiling and running the given program? class Except extends Exception{} public class Q48 { public static void main(String[]args) { try { System.out.println(10/0); // line number 8 } catch(Except e){ // line number 10. System.out.println("catch block"); } } } 1. Program will throw ArithmeticException which is not handled. 2. Compile time error at line number 10. 3. Compile time error at line number 8 as ArithmeticException must be caught. 4. None of above. Q49.Which of these are legal ways of accessing a File named "file.txt" for 1. FileWriter fr = new FileWriter("file.txt"); 2. FileOutputStream fr = new FileOutputStream("file.txt","UTF8"); 3. FileWriter fr = new FileWriter("file.txt", "UTF8"); 4. OutputStreamWriter out = new OutputStreamWriter("file.txt"); Q50.What will be the result of compiling and running the given program? public class Q50 { public static void main(String argv[]) { int x=31; System.out.println( x >>> 1 + x); System.out.println( -x >>> 1 + x); } } 1. Program will compile correctly and print 31,31 when executed. 2. Program will compile correctly and print 31,-31 when executed. 3. Program will compile correctly and print 46,2147483632 when executed. 4. Compile time error at line number 6 and 7. Q51.What is the name of collection interface used to maintain unique elements in order? 1. Set 2. Map 3. SortedSet 4. ArrayList 5. List Q52.What is the name of interface used to store data as key-value pairs? 1. Map 2. Set 3. List 4. ArrayList 5. SortedSet Q53.What will be the result of compiling and running the given program? class Q53 { public static void main(String [] args) { java.lang.StringBuffer sb; sb = new java.lang.StringBuffer().append("Hello").append("World");// line number 6 System.out.println(sb. substring(5,10).toString()); // line number 7 } } 1. Compile time error at line number 6. 2. Runtime error at line number 6. 3. Program will compile correctly and print World when executed. 4. StringIndexOutOfBoundsException at line number 7. Q54.Which of the following methods can force a thread to leave the Lock(monitor)? Select any two. 1. yield() 2. wait() 3. sleep() 4. start() Q55.What will be the result of compiling and running the given program? class Q55 { public static void main(String[] args) { int[] a={11,12,13,14}; int[] b={0,1,2,3}; System.out.println(a[(a=b)[3]]); // line number 7 } } 1. Compile time error at line number 7. 2. Runtime error at line number 7.

3. Program will compile correctly and print 3 when executed. 4. Program will compile correctly and print 14 when executed. Q56.What will be the result of compiling and running the given program? public class Q56 { static boolean x; public static void main(String args[]) { int a; if(x) // line number 7 a = x ? 1: 2; else a = x ? 3: 4; System.out.println(a);// line number 11. } } 1. Compile time error at line number 7 as x is not intialized. 2. Compile time error at line number 11 as a may not be intialized. 3. Program will compile correctly and print 1 when executed. 4. Program will compile correctly and print 4 when executed. Q57.What will be the result of compiling and running the given program?Select one correct answer. class Q57 { public static double[] d ; public static void main(String [] args) { Q57 x = new Q57(); x.methodA(d); // line number 7 } void methodA(double[] d1) { System.out.println(d1[0]); // line number 11. } } 1. Compile time error at line number 7 as d is not intialized. 2. Program compiles correctly and print 0.0 when executed. 3. ArrayIndexOutOfBoundsException at line number 11. 4. None of above. Q58.Which of the following statement is true?Select one correct answer. class Q58 { int x=0; final int y=0; private int z=0; void xyz() { int a=0; final int b=0; class abcd { abcd() { System.out.println(x);// line number 14. System.out.println(b);// line number 15. System.out.println(a);// line number 16. System.out.println(y);// line number 17. System.out.println(z);// line number 18. }/ for abcd constructor }//for abcd class } //for xyz method }//For Q58 1. Compile time error at line number 14. 2. Compile time error at line number 15. 3. Compile time error at line number 16. 4. Compile time error at line number 17. 5. Compile time error at line number 18. Q59.Which of the following statement can be placed at line number 8, without causing any error? public class OuterTest { class Inner { } public static void main(String args[]) { //Here } } 1. Inner i = new Inner(); 2. OuterTest ob = new Inner(); 3. OuterTest ob = null; ob.new Inner(); 4. OuterTest ob = new OuterTest(); new ob.Inner();

You might also like