Java Int Que Ans
Java Int Que Ans
com
2. Which one of these lists contains only Java programming language keywords?
A. class, if, void, long, Int, continue B. goto, instanceof, native, finally, default, throws C. try, virtual, throw, final, volatile, transient D. strictfp, constant, super, implements, do E. byte, break, assert, switch, include
6. Which three are legal array declarations? 1. int [] myScores []; 2. char [] myChars; 3. int [6] myScores; 4. Dog myDogs []; 5. Dog myDogs [7];
A. 1, 2, 4 C. 2, 3, 4 B. 2, 4, 5 D. All are correct.
7. public interface Foo { int k = 4; /* Line 3 */ } Which three piece of codes are equivalent to line 3? 1. final int k = 4; 2. public int k = 4; 3. static int k = 4; 4. abstract int k = 4; 5. volatile int k = 4; 6. protected int k = 4;
A. 1, 2 and 3 C. 3, 4 and 5 B. 2, 3 and 4 D. 4, 5 and 6
8. Which one of the following will declare an array and initialize it with five numbers?
A. Array a = new Array(5); B. int [] a = {23,22,21,20,19}; C. int a [] = new int[5]; D. int [5] array;
c1 c2 c3 c4 c5 c6
= = = = = =
B. 1, 3, 6 D. 5 only
12. Which three are valid declarations of a float? 1. float 2. float 3. float 4. float 5. float 6. float
A. 1, 2, 4 C. 1, 3, 6
f1 f2 f3 f4 f5 f6
= = = = = =
B. 2, 3, 5 D. 2, 4, 6
15. public void foo( boolean a, boolean b) { if( a ) { System.out.println("A"); /* Line 5 */ } else if(a && b) /* Line 7 */ { System.out.println( "A && B"); } else /* Line 11 */ { if ( !b ) { System.out.println( "notB") ; } else { System.out.println( "ELSE" ) ; } } }
A. If a is true and b is true then the output is "A && B" B. If a is true and b is false then the output is "notB" C. If a is false and b is true then the output is "ELSE" D. If a is false and b is false then the output is "ELSE"
17. public void test(int x) { int odd = 1; if(odd) /* Line 4 */ { System.out.println("odd"); } else { System.out.println("even"); } } Which statement is true?
A. Compilation fails. B. "odd" will always be output. C. "even" will always be output. D. "odd" will be output for odd values of x, and "even" for even values.
20. class Boo { Boo(String s) { } Boo() { } } class Bar extends Boo { Bar() { }
24. class Foo { class Bar{ } } class Test { public static void main (String [] args) { Foo f = new Foo(); /* Line 10: Missing statement ? */ } } which statement, inserted at line 10, creates an instance of Bar?
A. Foo.Bar b = new Foo.Bar(); B. Foo.Bar b = f.new Bar(); C. Bar b = new f.Bar(); D. Bar b = f.new Bar();
25. public class MyOuter { public static class MyInner { public static void foo() { } } } which statement, if placed in a class other than MyOuter or MyInner, instantiates an instance of the nested class?
A. MyOuter.MyInner m = new MyOuter.MyInner(); B.MyOuter.MyInner mi = new MyInner(); C. MyOuter m = new MyOuter(); MyOuter.MyInner mi = m.new MyOuter.MyInner();
26. What will be the output of the program? public class Test { public static void main(String[] args) { int x = 0; assert (x > 0) ? "assertion failed" : "assertion passed" ; System.out.println("finished"); } }
A. finished B. Compiliation fails. C. An AssertionError is thrown and finished is output. D. An AssertionError is thrown with the message "assertion failed."
27. public class Test { public void foo() { assert false; /* Line 5 */ assert false; /* Line 6 */ } public void bar() { while(true) { assert false; /* Line 12 */ } assert false; /* Line 14 */ } } What causes compilation to fail?
A. Line 5 C. Line 12 B. Line 6 D. Line 14
29. What will be the output of the program (when you run with the -ea option) ? public class Test { public static void main(String[] args) { int x = 0; assert (x > 0) : "assertion failed"; /* Line 6 */ System.out.println("finished"); } }
A. finished
10
30. public class Test2 { public static int x; public static int foo(int y) { return y * 2; } public static void main(String [] args) { int z = 5; assert z > 0; /* Line 11 */ assert z > 2: foo(z); /* Line 12 */ if ( z < 7 ) assert z > 4; /* Line 14 */ switch (z) { case 4: System.out.println("4 "); case 5: System.out.println("5 "); default: assert z < 10; } if ( z < 10 ) assert z > 4: z++; /* Line 22 */ System.out.println(z); } } which line is an example of an inappropriate use of assertions?
A. Line 11 C. Line 14 B. Line 12 D. Line 22
11
31. You want subclasses in any package to have access to members of a superclass. Which is the most
32. public class Outer { public void someOuterMethod() { //Line 5 } public class Inner { } public static void main(String[] argv) { Outer ot = new Outer(); //Line 10 } } Which of the following code fragments inserted, will allow to compile?
A. new Inner(); //At line 5 B. new Inner(); //At line 10 C. new ot.Inner(); //At line 10 D. new Outer.Inner(); //At line 10
33. interface Base { boolean m1 (); byte m2(short s); } which two code fragments will compile? 1. interface Base2 implements Base {} 2. abstract class Class2 extends Base { public boolean m1(){ return true; }} 3. abstract class Class2 implements Base {}
12
34. 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
A. 1, 3, 4 C. 1, 2, 6 B. 2, 4, 5 D. 2, 5, 6
35. public class Test { } What is the prototype of the default constructor?
A. Test( ) C. public Test( ) B. Test(void) D. public Test(void)
36. What is the most restrictive access modifier that will allow members of one class to have access to members of another class in the same package?
A. publicB. abstract D. synchronized C. protected E. default access
37. Which of the following is/are legal method declarations? 1. protected abstract void m1(); 2. static final void m1(){}
13
39. Which three are valid method signatures in an interface? 1. private int getArea(); 2. public float getVol(float x); 3. public void main(String [] args); 4. public static void main(String [] args); 5. boolean setFlag(Boolean [] test);
A. 1 and 2 C. 3, 4, and 5 B. 2, 3 and 5 D. 2 and 4
40. You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?
A. public C. protected B. private D. default access
14
42. class A { protected int method1(int a, int b) { return 0; } } Which is valid in a class that extends class A?
A. public int method1(int a, int b) {return 0; } B. private int method1(int a, int b) { return 0; } C. public short method1(int a, int b) { return 0; } D. static protected int method1(int a, int b) { return 0; }
44. Which two of the following are legal declarations for nonnested classes and interfaces?
15
1. final abstract class Test {} 2. public static interface Test {}
45. Which of the following class level (nonlocal) variable declarations will not compile?
A. protected int a; C. private synchronized int e; B. transient int b = 3; D. volatile int d;
46. Which two cause a compiler error? 1. float[ ] f = new float(3); 2. float f2[ ] = new float[ ]; 3. float[ ]f1 = new float[3]; 4. float f3[ ] = new float[3]; 5. float f5[ ] = {1.0f, 2.0f, 2.0f};
A. 2, 4 C. 4, 5 B. 3, 5 D. 1, 2
47. Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?
A. final C. private E. volatile B. static D. protected
16
B. protected short stop = 23; C. transient short stop = 23;
17
ANSWER
Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option Option B B D B A A A B B A C C A D C A A D C B B B D B A B D D C D C A C
2
Digitally signed by BHAVIN Reason: I am the author of this document Location: Date: 08/22/12 13:20:34