Corejava Objective Questions and Answers (SET - 6)
Please attempt at least 12 objective questions to see the test result.
1) Which are true? (Choose all that apply.) A. It is appropriate to use assertions to validate arguments to methods marked public B. It is appropriate to catch and handle assertion errors C. It is NOT appropriate to use assertions to validate command-line arguments D. It is appropriate to use assertions to generate alerts when you reach code that should not be reachable E. It is NOT appropriate for assertions to change a program’s state
2) Given: class Emu { static String s = "-"; public static void main(String[] args) { try { throw new Exception(); } catch (Exception e) { try { try { throw new Exception(); } catch (Exception ex) { s += "ic "; } throw new Exception(); } catch (Exception x) { s += "mc "; } finally { s += "mf "; } } finally { s += "of "; } System.out.println(s); } } What is the result?
3) Given: 3. class SubException extends Exception { } 4. class SubSubException extends SubException { } 5. 6. public class CC { void doStuff() throws SubException { } } 7. 8. class CC2 extends CC { void doStuff() throws SubSubException { } } 9. 10. class CC3 extends CC { void doStuff() throws Exception { } } 11. 12. class CC4 extends CC { void doStuff(int x) throws Exception { } } 13. 14. class CC5 extends CC { void doStuff() { } } What is the result?
4) Given: 3. public class Ebb { 4. static int x = 7; 5. public static void main(String[] args) { 6. String s = ""; 7. for(int y = 0; y < 3; y++) { 8. x++; 9. switch(x) { 10. case 8: s += "8 "; 11. case 9: s += "9 "; 12. case 10: { s+= "10 "; break; } 13. default: s += "d "; 14. case 13: s+= "13 "; 15. } 16. } 17. System.out.println(s); 18. } 19. static { x++; } 20. } What is the result?
5) Which three form part of correct array declarations? 1. public int a [ ] 2. static int [ ] a 3. public [ ] int a 4. private int a [3] 5. private int [3] a [ ] 6. public final int [ ] a
6) Which cause a compiler error?
7) Which class does not override the equals() and hashCode() methods, inheriting them directly from class Object?
8) Which interface does java.util.Hashtable implement?
9) /* Missing Statement ? */ public class foo { public static void main(String[]args)throws Exception { java.io.PrintWriter out = new java.io.PrintWriter(); new java.io.OutputStreamWriter(System.out,true); out.println("Hello"); } } What line of code should replace the missing statement to make this program compile?
10) Given: 3. public class Circles { 4. public static void main(String[] args) { 5. int[] ia = {1,3,5,7,9}; 6. for(int x : ia) { 7. for(int j = 0; j < 3; j++) { 8. if(x > 4 && x < 8) continue; 9. System.out.print(" " + x); 10. if(j == 1) break; 11. continue; 12. } 13. continue; 14. } 15. } 16. } What is the result?
11) Given: 3. public class Wind { 4. public static void main(String[] args) { 5. foreach: 6. for(int j=0; j<5; j++) { 7. for(int k=0; k< 3; k++) { 8. System.out.print(" " + j); 9. if(j==3 && k==1) break foreach; 10. if(j==0 || j==2) break; 11. } 12. } 13. } 14. } What is the result?
12) Given: 3. public class Clumsy { 4. public static void main(String[] args) { 5. int j = 7; 6. assert(++j > 7); 7. assert(++j > 8): "hi"; 8. assert(j > 10): j=12; 9. assert(j==12): doStuff(); 10. assert(j==12): new Clumsy(); 11. } 12. static void doStuff() { } 13. } Which are true?
13) Given: class Hexy { public static void main(String[] args) { Integer i = 42; String s = (i<40)?"life":(i>50)?"universe":"everything"; System.out.println(s); } } What is the result?
14) Given: class Fork { public static void main(String[] args) { if(args.length == 1 | args[1].equals("test")) { System.out.println("test case"); } else { System.out.println("production " + args[0]); } } } And the command-line invocation: java Fork live2 What is the result?
15) Given: 3. public class Twisty { 4. { index = 1; } 5. int index; 6. public static void main(String[] args) { 7. new Twisty().go(); 8. } 9. void go() { 10. int [][] dd = {{9,8,7}, {6,5,4}, {3,2,1,0}}; 11. System.out.println(dd[index++][index++]); 12. } 13. } What is the result?