3. Java Exam Preparation Practice Test - MCQ 1. Consider the following code , what is the output provided a valid file name is given on the command prompt? class Q006{ public static void main(String args[]){ boolean flag = false; if ( flag = true) { import java.io.*; class Q032{ public static void main(String args[]) throws Exception{ FileInputStream fin; int c = 0; try { fin = new FileInputStream(args[0]); while ((c = fin.read()) != -1) { System.out.print((char)c); } } catch (Exception e) { System.out.println(e); } fin.close(); } } System.out.println(“true”); } if ( flag == false) { System.out.println(“false”); } } } Options : a. true b. false c. Compile-time error " Q006.java: Incompatible type for declaration. Can't convert boolean to java.lang.Boolean. " d. true false Options : a. Compile-time error " Variable fin may not have been initialized." b. Run-time exception " java.lang.ArrayIndexOutOfBoundsException: 0" c. Run's fine displaying the contents of the file . d. Compile-time error " Undefined variable: args" 2. What is the output ? 4. What is the output ? class A{ A() { System.out.print("1"); } What is the output for the following: } class A{ class Q005 extends A{ Q005() { System.out.print("2"); } public static void main(String args[]) { Q005 obj = new Q005(); System.out.println("3"); } } static int i; A(){ ++i; } private int get(){ return ++i; } } class B extends A{ B(){ i++; } int get(){ return ( i + 3); } } Options : a. 123 b. 23 c. 3 d. 1 , 2 , 3 each on a separate line 6. Predict the output: class Q008{ public static void main(String args[]){ int i = -1; System.out.println((i<0)?-i:i); } } class Q028 extends B{ public static void main(String ka[]){ Q028 obj = new Q028(); A ob_a = new A(); ob_a = (A)obj; System.out.println(ob_a.get()); } } Options : a. 2 b. Compile error " No method matching get() found in class Q026." c. 5 d. NullPointerException thrown at run-time . Options : a. 1 b. -1 c. Compile error d. Run-time error 1 7. 8. Will the following compile? If yes what is the output? 10. class Q009{ public static void main(String args[]){ System.out.println( (Math.abs(Integer.MIN_VALUE))); //For Your Information, //Integer.MIN_VALUE = -2147483648 } } class Test{ static boolean flag; public static void main(String args[]){ if (flag) { System.out.println(flag); } } } Options : a. 2147483648 b. Compile Error c. -2147483648 d. NegativeArraySizeException thrown at runtime Options : a. Compiler error, boolean flag: variable flag may not have been initialized b. Compiler error, System.out.println (flag): can't convert boolean to String c. true d. false e. Compiles & runs fine with no output generated Will the following code compile? If yes , what is the output? class Q012 { Q012(int i) { System.out.println(i); this.i = i; } 11. What is the output when Test01 class is run? class A { A() { //some initialization } void do_something(){ System.out.println("I'm in A"); } public static void main(String ka[]) { Q012 obj = new Q012(10); } } } class B extends A { B() { //some initialization } void do_something() { System.out.println("I'm in B"); } } class Test01{ public static void main(String args[]){ A a = new B(); a.do_something(); } } Options : a. 0 b. 10 c. null d. Compile error "Q012.java:10: No variable i defined in class Q012. " 9. What is the output for this piece? What is the output of this code? class Q011{ int i; Q011(int i){ System.out.println(i); this.i = i; } Options : a. I'm in A b. I'm in B c. Compiler error , A a = new B ( ) : Explicit cast needed to convert A to B d. ClassCastException thrown at runtime public static void main(String ka[]){ Q011 obj = new Q011(10); System.out.println(obj.i); } } 12. Options : a. 10 10 b. Compile error " Q012.java:16: Variable i may not have been initialized. " c. 0 10 d. 10 0 What is the output for the following code? class Q014{ public String get(int i){return null;} public static void main(String ka[]){ System.out.println((new Q014()).get(1)); } } Options : a. 0 b. NullPointerException at run-time c. Compile error d. null 2 13. What is the output? 16. class Q018 { static Object obj; static String get() { return null; } What is the output? class Test02 { public String fetch() { return null; } public static void main(String args[]){ System.out.println( (Q018.get()) == (Q018.obj)); } public static void main(String args[]) { Test02 obj = new Test02(); String str = obj.fetch(); System.out.println((str+"").toString()); } } } Options : a. Compile error b. Run-time error c. true d. false 14. Options : a. null b. Compiler error , can't invoke a method on null c. NullPointerException thrown at runtime d. ClassCastException thrown at runtime What is the output? 17. class Q015{ public String get(int i){ return null; } class Q022 { public static void main(String ka[]) { while(false) { System.out.println("Great"); } } } public static void main(String ka[]){ Q015 obj = new Q015(); Object i = obj.get(10); System.out.println(i.toString()); } Options : a. Run's with no output b. Run-time error "Statement not reached." c. Great d. Compiler error, System.out.println ( "Great"): statement not reached } Options : a. null b. NullPointerException at run-time c. Compile error d. 0 15. What is the output? 18. What is the output? What is the output? class A{ static int i; private int get(){ return 1; } class Q013{ int i; Q013(int i){ System.out.println(i); } } class B extends A{ private int get(){ return 2; } } class Q027 extends B{ public static void main(String ka[]) { Q027 obj = new Q027(); System.out.println(obj.get()); } } public static void main(String ka[]) { Q013 obj = new Q013(10); System.out.println(obj.i); } } Options : a. 10 10 b. Compile error , System.out.println (obj.i): variable i may not have been initialized c. 0 10 d. 10 0 Options : a. 2 b. Compile error , System.out.println (obj.get ( )): no method matching get( ) found in class Q027 c. 1 d. 2 e. NullPointerException thrown at run-time . 3 19. What is the output? 23. Will the output from the following two code snippets be any different? Snippet 1 : for ( int i = 0 ; i < 5 ; i++ ) { System.out.println(i); } Snippet 2 : for ( int i = 0 ; i < 5 ; ++i ) { System.out.println(i); } Options : a. yes b. no 24. What is the output of the following code? class Q029{ public static void main(String args[]){ boolean b[] = new boolean[2]; System.out.println(b[1]); } } Options : a. Compile error " Variable b may not have been initialized." b. Run-time error " Variable b may not have been initialized." c. false d. true 21. class Q009{ public static void main(String args[]) { float f = 1/2; System.out.println(f); } } Will the following code compile? If yes choose the most correct two options. class Q011 { public static void main(String args[]) { char a = 'a' + 2; System.out.println(a); int i = 2; char c = 'a' + i; //---1 System.out.println(c); //---2 } } Options : a. Compile-time error float f = 1/2 : explicit cast needed to convert int to float b. 0.0 c. 0.5 d. ClassCastException thrown at run-time Options : a. Compile-time error "Explicit cast needed to convert int to char" b. The code can be made to compile by commenting out lines marked as 1 & 2 c. The code can be made to compile by commenting out the line "char a = 'a' + 2;" d. The output is: 67 67 22. 25. What is the output for the following code? class Torment{ int i = 2; public static void main(String args[]) { int i = 12; System.out.println(i); } } Options : a. Compiler error, System.out.println(i) : can't make a static reference to a non static variable i b. Output = 2 c. Output = 12 d. Compiler error, i = 12 : variable i is already defined in this class What is the output ? FYI ( int ) ' A ' = 65 class Q1{ public static void main(String args[]){ byte b = 65; switch (b) { case 'A': System.out.println("A"); break; case 65: System.out.println("65"); break; default: System.out.println("default"); } } } 29. Options : a. A b. 65 c. Compile time error, case 65: duplicate case label d. Runtime error "Duplicate case label: 65" e. default 4 Will the following class compile ? If yes, what is the output ? class Envy{ static int i; Envy(){ ++i; } public static void main(String args[]) { System.out.println(i); } } Options : a. Yes , it compiles but NullPointerException thrown at runtime b. Compiler error, System.out.println(i) : variable i may not have been initialized c. Yes it compiles & output = 0 d. Yes it compiles & the output = 1 e. Compiler error, ++ i : can't make a non-static reference to a static variable , i 27. Consider the following class . Which of the marked lines need to be commented out for the class to compile correctly? 33. class Evolve { static int i = 1; static int j = 2; int x = 3; static int y = 6; class Test02{ public static void main(String args[]){ String str = args[0]; switch ( str.equals("just")) { case 1: System.out.println("case 1"); break; case 2: System.out.println("case 2"); break; default: break; } } } public static void main(String args[]) { System.out.println(i + j); //---1 System.out.println(x + i); //---2 System.out.println(i + y); //---3 System.out.println(x + j); //---4 } } Options : a. 1 & 2 b. 1, 2 & 4 c. 3 & 4 d. 2 & 4 e. 1, 2 & 3 30. 32. Options : a. Output = case 1 b. Output = case 2 c. No output generated d. Compiler error, switch(str.equals("just")) : Can't convert boolean to int. e. Compiler error, case 1 : can't convert boolean to int Choose all the valid declarations for the main method from the list below. a. public void main(String args[]) b. static void main(String args[]) c. public static void main(String[] ka) d. static public void main(String[] args) e. public static void main(String ka[]) 34. What is the output of the following code fragment when run with the following command line - java 1 ? class Test03{ public static void main(String args[]) { int i = Integer.parseInt(args[0]); switch (i) { case 1: System.out.println("case 1"); case 2: System.out.println("case 2"); default: System.out.println("default"); break; } } } What is the compiler error generated by the following piece of code or what is the output if any? class Test01{ public static void main(String args[]){ int i = 1; if ( i = 2 ) { System.out.println("In if"); } else System.out.println("In else"); } } Options : a. Output = In If b. Compiles but no output is generated c. Output is = In else d. Compiler error, if (i=2) : can't convert int to boolean 37. Will the code given below compile? If run with the following command line - java just - what is the output? Options : a. case 1 case2 default b. case 1 c. Runtime exception thrown NumberFormatException, Integer.parseInt(args[0] ): Can't convert String to int d. The code won't compile because there is no method parseInt( ) defined in the Integer class . What is the output for this? class Test06{ public static void main(String args[]){ for (int i = 0 ; i < 5 ; i++) { System.out.println(i); break; } } } Options : a. 0 to 5, each on a separate line b. 1 to 5, each on a new line c. 1 to 4 each on a new line d. 0 e. 0 to 4, each on a new line 5 35. Predict the output for the following piece of code. 40. class Test04{ public static void main(String args[]){ for ( int i = 1 ; i <= 5 ; i++ ) { if ( i < 5 ) { continue; } else System.out.println( i ); } } } class Test09 { static int tomb(char a) { throw new NumberFormatException(); } public static void main(String args[]) { try { tomb('a'); } catch (Exception e) { System.out.println("Done"); } } Options : a. 1 to 5 are printed on a new line each b. 1 to 4 are printed on a new line each c. Only 5 is printed d. 2 to 5 are printed on a new line each e. 2 to 4 are printed on a new line each f. No output is generated because continue terminates the loop 36. } Options: a. NumberFormatException thrown at runtime b. Compiler error , throw new NumberFormatException() : Checked Exceptions must be declared in the throws clause or must be caught in the corresponding catch block c. Compiler error, System.out.println("Done") : statement not reached d. Output = Done Will the following code block compile? If yes , what is the output? class Test05{ public static void main(String args[]){ try { throw new Exception(); System.out.println("try"); } catch (Exception e) { System.out.println("catch"); } finally { System.out.println("finally"); } } } Options : a. try catch finally b. catch finally c. finally d. Exception must be declared in the throws clause e. Compiler error, System.out.println("try"): statement not reached 38. Does the following piece of code compile? If yes , what is the output? Will the following code generate a compiler error? If not , what is the output? class Test07{ public static void main(String args[]){ for (int i = 0 ; i < 5 ; i++) { break; System.out.println(i); } } } Options : a. 0 to 5 , each on a separate line b. 1 to 5 , each on a new line c. 1 to 4 each on a new line d. 0 e. Compiler error , System.out.println ( i ) : statement not reached f. 0 to 4 , each on a new line 6 41. What will happen when you invoke the following method? void infiniteLoop ( ) { byte b = 1 ; while (++b > 0) ; System.out.println ("Welcome to Java") ; } Options : a. The loop never ends ( infinite loop ) . b. Prints " Welcome to Java " to the console c. Compilation error at line 5. ++ operator should not be used for byte type variables. d. Prints nothing but terminates after some time 42. What is the output of the following code ? class A{ static int i; A(){ ++i; } int get(){ return ++i; } } class B extends A{ private B(){ i++; } int get(){ return ( i + 3); } } class Q028 extends B{ public static void main(String ka[]) { Q028 obj = new Q028(); A ob = new A(); ob = (A)obj; System.out.println(ob.get()); } } Options: a. 2 b. Compile error " No method matching get( ) found in class Q026." c. 5 d. NullPointerException thrown at run-time . Answers: 1. a 2. b 3. a 4. a 5. c 6. a 7. c 8. d 9. a 10. e 11. b 12. d 13. c 14. b 15. d 16. a 17. d 18. b 19. c 20. e 21. a & b 22. c 23. b 24. b 25. c 26. b 27. d 28. e 29. c 30. c , d & e 31. d 32. d 33. d 34. a 35. c 36. e 37. d 38. e 39. c 40. d 41. b 42. c 7