JAVA MCQ SET


JAVA Multiple Choice Question Material
[1] public interface Foo { int k = 4; /* Line 3 */ } Which three piece of codes are equivalent to line 3?

(A) => final int k = 4;
(B) => public int k = 4;
(C) => static int k = 4;
Answer =>> final int k = 4;

[2] 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];
Answer =>> int [] a = {23,22,21,20,19};

[3] Which three are valid declarations of a char?
(A) => char c1 = 064770;
(B) => char c2 = 'face';
(C) => char c3 = 0xbeef;
Answer =>> char c2 = 'face';

[4] Which is the valid declarations within an interface definition?
(A) => public double methoda();
(B) => public final double methoda();
(C) => static void methoda(double d1);
Answer =>> public double methoda();

[5] Which one is a valid declaration of a boolean?
(A) => boolean b1 = 0;
(B) => boolean b2 = 'false';
(C) => boolean b3 = false;
Answer =>> boolean b3 = false;

[6] Which three are valid declarations of a float?
(A) => float f3 = 0x12345;
(B) => float f2 = 3.14;
(C) => float f1 = -343;
Answer =>> float f1 = -343;

[7] Which is a valid declarations of a String?
(A) => String s1 = null;
(B) => String s3 = (String) 'abc';
(C) => String s2 = 'null';
Answer =>> String s1 = null;

[8] What is the numerical range of a char?
(A) => -128 to 127
(B) => 0 to 65535
(C) => -(215) to (215) - 1
Answer =>> 0 to 65535

[9] What will be the output of the program?
 public class CommandArgsThree
 {
 public static void main(String [] args)
 {
 String [][] argCopy = new String[2][2];
 int x; argCopy[0] = args;
 x = argCopy[0].length;
 for (int y = 0; y < x; y++)
 {
 System.out.print(" " + argCopy[0][y]);
 }
 }
 }

 and the command-line invocation is > java CommandArgsThree 1 2 3
(A) => 00
(B) => 123
(C) => 12
Answer =>> 123

[10] What will be the output of the program?
 public class CommandArgs
 {
 public static void main(String [] args)
 {
 String s1 = args[1];
 String s2 = args[2];
 String s3 = args[3];
 String s4 = args[4];
 System.out.print(" args[2] = " + s2);
 }
 }
 and the command-line invocation is > java CommandArgs 1 2 3 4
(A) => args[2] = 2
(B) => args[2] = 3
(C) => An exception is thrown at runtime.
Answer =>> An exception is thrown at runtime.

[11] public class F0091 { public void main( String[] args ) { System.out.println( "Hello" + args[0] ); } } What will be the output of the program, if this code is executed with the command line: > java F0091 world
(A) => Hello
(B) => Hello Foo91
(C) => The code does not run.
Answer =>> The code does not run.

[12] What will be the output of the program? public class TestDogs { public static void main(String [] args) { Dog [][] theDogs = new Dog[3][]; System.out.println(theDogs[2][0].toString()); } } class Dog { }
(A) => An exception is thrown at runtime
(B) => theDogs
(C) => Compilation fails
Answer =>> An exception is thrown at runtime

[13] What will be the output of the program ? public class Test { public static void main(String [] args) { signed int x = 10; for (int y=0; y<5 data-blogger-escaped-b="" data-blogger-escaped-system.out.print="" data-blogger-escaped-x--="" data-blogger-escaped-x="" data-blogger-escaped-y="">
(A) => 10, 9, 8, 7, 6,
(B) => Compilation fails.
(C) => 9, 8, 7, 6, 5,
Answer =>> Compilation fails.

[14] What will be the output of the program? public class CommandArgsTwo { public static void main(String [] argh) { int x; x = argh.length; for (int y = 1; y <= x; y++) { System.out.print(" " + argh[y]); } } } and the command-line invocation is > java CommandArgsTwo 1 2 3
(A) => 012
(B) => 123
(C) => An exception is thrown at runtime
Answer =>> An exception is thrown at runtime

[15] In the given program, how many lines of output will be produced? public class Test { public static void main(String [] args) { int [] [] [] x = new int [3] [] []; int i, j; x[0] = new int[4][]; x[1] = new int[2][]; x[2] = new int[5][]; for (i = 0; i < x.length; i++) { for (j = 0; j < x[i].length; j++) { x[i][j] = new int [i + j + 1]; System.out.println("size = " + x[i][j].length); } } } }
(A) => 11
(B) => 9
(C) => 7
Answer =>> 11

[16] What will be the output of the program? public class X { public static void main(String [] args) { String names [] = new String[5]; for (int x=0; x < args.length; x++) names[x] = args[x]; System.out.println(names[2]); } } and the command line invocation is > java X a b
(A) => names
(B) => null
(C) => Compilation fails
Answer =>> null

[17] 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) => default access
(B) => protected
(C) => public
Answer =>> default access

[18] Which of the following is/are legal method declarations?
(A) => protected abstract void m1();
(B) => static final void m1(){}
(C) => private native void m1();
Answer =>> private native void m1();

[19] Which cause a compiler error?
(A) => int[ ] scores = {3, 5, 7};
(B) => int [ ][ ] scores = {2,7,6}, {9,3,45};
(C) => String cats[ ] = {"Fluffy", "Spot", "Zeus"};
Answer =>> int [ ][ ] scores = {2,7,6}, {9,3,45};

[20] Which three are valid method signatures in an interface?
(A) => private int getArea();
(B) => public float getVol(float x);
(C) => public void main(String [] args);
Answer =>> public float getVol(float x);

[21] 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
(B) => private
(C) => default access
Answer =>> default access

[22] What is the widest valid returnType for methodA in line 3? public class ReturnIt { returnType methodA(byte x, double y) /* Line 3 */ { return (long)x / y * 2; } }
(A) => int
(B) => double
(C) => long
Answer =>> double

[23] 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; }
Answer =>> public int method1(int a, int b) {return 0; }

[24] Which one creates an instance of an array?
(A) => int[ ] ia = new int[15];
(B) => float fa = new float[20];
(C) => char[ ] ca = "Some String";
Answer =>> int[ ] ia = new int[15];

[25] Which two of the following are legal declarations for nonnested classes and interfaces?
(A) => final abstract class Test {}
(B) => public static interface Test {}
(C) => final public class Test {}
Answer =>> final public class Test {}

[26] Which of the following class level (nonlocal) variable declarations will not compile?
(A) => private synchronized int e;
(B) => protected int a;
(C) => volatile int d;
Answer =>> private synchronized int e;

[27] Which two cause a compiler error?
(A) => float[ ] f = new float(3);
(B) => float f2[ ] = new float[ ];
(C) => float f5[ ] = {1.0f, 2.0f, 2.0f};
Answer =>> float f5[ ] = {1.0f, 2.0f, 2.0f};

[28] 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
(B) => static
(C) => private
Answer =>> private

[29] Which is a valid declaration within an interface?
(A) => public static short stop = 23;
(B) => protected short stop = 23;
(C) => transient short stop = 23;
Answer =>> public static short stop = 23;

[30] What will be the output of the program? class Test { public static void main(String [] args) { int x=20; String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge"; System.out.println(sup); } }
(A) => small
(B) => tiny
(C) => huge
Answer =>> tiny

[31] What will be the output of the program? class Test { public static void main(String [] args) { int x= 0; int y= 0; for (int z = 0; z < 5; z++) { if (( ++x > 2 ) && (++y > 2)) { x++; } } System.out.println(x + " " + y); } }
(A) => 5 2
(B) => 6 3
(C) => 5 3
Answer =>> 6 3

[32] What will be the output of the program? class Test { public static void main(String [] args) { int x= 0; int y= 0; for (int z = 0; z < 5; z++) { if (( ++x > 2 ) || (++y > 2)) { x++; } } System.out.println(x + " " + y); } }
(A) => 5 3
(B) => 8 2
(C) => 8 3
Answer =>> 8 2

[33] What will be the output of the program? class Bitwise { public static void main(String [] args) { int x = 11 & 9; int y = x ^ 3; System.out.println( y | 12 ); } }
(A) => 0
(B) => 8
(C) => 14
Answer =>> 14

[34] What will be the output of the program? class SSBool { public static void main(String [] args) { boolean b1 = true; boolean b2 = false; boolean b3 = true; if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */ System.out.print("ok "); if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/ System.out.println("dokey"); } }
(A) => ok
(B) => dokey
(C) => ok dokey
Answer =>> ok dokey

[35] What will be the output of the program? class SC2 { public static void main(String [] args) { SC2 s = new SC2(); s.start(); } void start() { int a = 3; int b = 4; System.out.print(" " + 7 + 2 + " "); System.out.print(a + b); System.out.print(" " + a + b + " "); System.out.print(foo() + a + b + " "); System.out.println(a + b + foo()); } String foo() { return "foo"; } }
(A) => 9 7 7 foo 7 7foo
(B) => 72 34 34 foo34 34foo
(C) => 72 7 34 foo34 7foo
Answer =>> 72 7 34 foo34 7foo

[36] What will be the output of the program? class Test { static int s; public static void main(String [] args) { Test p = new Test(); p.start(); System.out.println(s); } void start() { int x = 7; twice(x); System.out.print(x + " "); } void twice(int x) { x = x*2; s = x; } }
(A) => 7 7
(B) => 7 14
(C) => 14 0
Answer =>> 7 14

[37] What will be the output of the program? class Two { byte x; } class PassO { public static void main(String [] args) { PassO p = new PassO(); p.start(); } void start() { Two t = new Two(); System.out.print(t.x + " "); Two t2 = fix(t); System.out.println(t.x + " " + t2.x); } Two fix(Two tt) { tt.x = 42; return tt; } }
(A) => null null 42
(B) => 0 0 42
(C) => 0 42 42
Answer =>> 0 42 42

[38] What will be the output of the program? class BoolArray { boolean [] b = new boolean[3]; int count = 0; void set(boolean [] x, int i) { x[i] = true; ++count; } public static void main(String [] args) { BoolArray ba = new BoolArray(); ba.set(ba.b, 0); ba.set(ba.b, 2); ba.test(); } void test() { if ( b[0] && b[1] | b[2] ) count++; if ( b[1] && b[(++count - 2)] ) count += 7; System.out.println("count = " + count); } }
(A) => count = 0
(B) => count = 2
(C) => count = 3
Answer =>> count = 3

[39] What will be the output of the program? public class Test { public static void leftshift(int i, int j) { i <<= j; } public static void main(String args[]) { int i = 4, j = 2; leftshift(i, j); System.out.printIn(i); } }
(A) => 2
(B) => 4
(C) => 8
Answer =>> 4

[40] What will be the output of the program? int i = l, j = -1; switch (i) { case 0, 1: j = 1; /* Line 4 */ case 2: j = 2; default: j = 0; } System.out.println("j = " + j);
(A) => j = -1
(B) => Compilation fails.
(C) => j = 0
Answer =>> Compilation fails.

[41] What will be the output of the program? int i = 1, j = 10; do { if(i > j) { break; } j--; } while (++i < 5); System.out.println("i = " + i + " and j = " + j);
(A) => i = 6 and j = 5
(B) => i = 5 and j = 5
(C) => i = 5 and j = 6
Answer =>> i = 5 and j = 6

[42] What will be the output of the program? public class Switch2 { final static short x = 2; public static int y = 0; public static void main(String [] args) { for (int z=0; z < 3; z++) { switch (z) { case x: System.out.print("0 "); case x-1: System.out.print("1 "); case x-2: System.out.print("2 "); } } } }
(A) => 0 1 2
(B) => 0 1 2 1 2 2
(C) => 2 1 2 0 1 2
Answer =>> 2 1 2 0 1 2

[43] What will be the output of the program? public class SwitchTest { public static void main(String[] args) { System.out.println("value =" + switchIt(4)); } public static int switchIt(int x) { int j = 1; switch (x) { case l: j++; case 2: j++; case 3: j++; case 4: j++; case 5: j++; default: j++; } return j + x; } }
(A) => value = 2
(B) => value = 6
(C) => value = 8
Answer =>> value = 8

[44] What will be the output of the program? public class If2 { static boolean b1, b2; public static void main(String [] args) { int x = 0; if ( !b1 ) /* Line 7 */ { if ( !b2 ) /* Line 9 */ { b1 = true; x++; if ( 5 > 6 ) { x++; } if ( !b1 ) x = x + 10; else if ( b2 = true ) /* Line 19 */ x = x + 100; else if ( b1 | b2 ) /* Line 21 */ x = x + 1000; } } System.out.println(x); } }
(A) => 0
(B) => 1
(C) => 101
Answer =>> 101

[45] What will be the output of the program? public class Switch2 { final static short x = 2; public static int y = 0; public static void main(String [] args) { for (int z=0; z < 3; z++) { switch (z) { case y: System.out.print("0 "); /* Line 11 */ case x-1: System.out.print("1 "); /* Line 12 */ case x: System.out.print("2 "); /* Line 13 */ } } } }
(A) => 0 1 2
(B) => 0 1 2 1 2 2
(C) => Compilation fails at line 11.
Answer =>> Compilation fails at line 11.

[46] What will be the output of the program? public class If1 { static boolean b; public static void main(String [] args) { short hand = 42; if ( hand < 50 && !b ) /* Line 7 */ hand++; if ( hand > 50 ); /* Line 9 */ else if ( hand > 40 ) { hand += 7; hand++; } else --hand; System.out.println(hand); } }
(A) => 41
(B) => 42
(C) => 51
Answer =>> 51

[47] What will be the output of the program? public class Test { public static void main(String [] args) { int I = 1; do while ( I < 1 ) System.out.print("I is " + I); while ( I > 1 ) ; } }
(A) => I is 1
(B) => I is 1 I is 1
(C) => No output is produced.
Answer =>> No output is produced.

[48] What will be the output of the program? int x = l, y = 6; while (y--) { x++; } System.out.println("x = " + x +" y = " + y);
(A) => x = 6 y = 0
(B) => x = 6 y = -1
(C) => Compilation fails.
Answer =>> Compilation fails.

[49] What will be the output of the program? int I = 0; outer: while (true) { I++; inner: for (int j = 0; j < 10; j++) { I += j; if (j == 3) continue inner; break outer; } continue outer; } System.out.println(I);
(A) => 1
(B) => 2
(C) => 3
Answer =>> 1

[50] What will be the output of the program? for (int i = 0; i < 4; i += 2) { System.out.print(i + " "); } System.out.println(i); /* Line 5 */
(A) => 0 2 4
(B) => 0 2 4 5
(C) => Compilation fails.
Answer =>> Compilation fails.

[51] What will be the output of the program? int x = 3; int y = 1; if (x = y) /* Line 3 */ { System.out.println("x =" + x); }
(A) => x = 1
(B) => x = 3
(C) => Compilation fails.
Answer =>> Compilation fails.

[52] What will be the output of the program? Float f = new Float("12"); switch (f) { case 12: System.out.println("Twelve"); case 0: System.out.println("Zero"); default: System.out.println("Default"); }
(A) => Zero
(B) => Twelve
(C) => Compilation fails
Answer =>> Compilation fails

[53] What will be the output of the program? int i = O; while(1) { if(i == 4) { break; } ++i; } System.out.println("i = " + i);
(A) => Compilation fails.
(B) => i = 0
(C) => i = 3
Answer =>> Compilation fails.

[54] What will be the output of the program? public class Delta { static boolean foo(char c) { System.out.print(c); return true; } public static void main( String[] argv ) { int i = 0; for (foo('A'); foo('B') && (i < 2); foo('C')) { i++; foo('D'); } } }
(A) => ABDCBDCB
(B) => ABCDABCD
(C) => Compilation fails.
Answer =>> ABDCBDCB

[55] What will be the output of the program? for(int i = 0; i < 3; i++) { switch(i) { case 0: break; case 1: System.out.print("one "); case 2: System.out.print("two "); case 3: System.out.print("three "); } } System.out.println("done");
(A) => done
(B) => one two done
(C) => one two three two three done
Answer =>> one two three two three done

[56] What will be the output of the program? public class Test { public static void main(String args[]) { int i = 1, j = 0; switch(i) { case 2: j += 6; case 4: j += 1; default: j += 2; case 0: j += 4; } System.out.println("j = " + j); } }
(A) => 6
(B) => 4
(C) => 2
Answer =>> 6

[57] What will be the output of the program? boolean bool = true; if(bool = false) /* Line 2 */ { System.out.println("a"); } else if(bool) /* Line 6 */ { System.out.println("b"); } else if(!bool) /* Line 10 */ { System.out.println("c"); /* Line 12 */ } else { System.out.println("d"); }
(A) => c
(B) => b
(C) => a
Answer =>> c

[58] What will be the output of the program? public class Switch2 { final static short x = 2; public static int y = 0; public static void main(String [] args) { for (int z=0; z < 4; z++) { switch (z) { case x: System.out.print("0 "); default: System.out.print("def "); case x-1: System.out.print("1 "); break; case x-2: System.out.print("2 "); } } } }
(A) => 0 def 1
(B) => 2 1 0 def def
(C) => 2 1 0 def 1 def 1
Answer =>> 2 1 0 def 1 def 1

[59] What will be the output of the program? int i = 0, j = 5; tp: for (;;) { i++; for (;;) { if(i > --j) { break tp; } } System.out.println("i =" + i + ", j = " + j);
(A) => Compilation fails.
(B) => i = 3, j = 4
(C) => i = 1, j = 4
Answer =>> Compilation fails.

[60] What will be the output of the program? int I = 0; label: if (I < 2) { System.out.print("I is " + I); I++; continue label; }
(A) => I is 0
(B) => Compilation fails.
(C) => I is 0 I is 1
Answer =>> Compilation fails.

[61] What will be the output of the program? public class Test { public static void aMethod() throws Exception { try /* Line 5 */ { throw new Exception(); /* Line 7 */ } finally /* Line 9 */ { System.out.print("finally "); /* Line 11 */ } } public static void main(String args[]) { try { aMethod(); } catch (Exception e) /* Line 20 */ { System.out.print("exception "); } System.out.print("finished"); /* Line 24 */ } }
(A) => finally
(B) => exception finished
(C) => finally exception finished
Answer =>> finally exception finished

[62] What will be the output of the program? public class X { public static void main(String [] args) { try { badMethod(); System.out.print("A"); } catch (Exception ex) { System.out.print("B"); } finally { System.out.print("C"); } System.out.print("D"); } public static void badMethod() {} }
(A) => AC
(B) => BC
(C) => ACD
Answer =>> ACD

[63] What will be the output of the program? public class X { public static void main(String [] args) { try { badMethod(); /* Line 7 */ System.out.print("A"); } catch (Exception ex) /* Line 10 */ { System.out.print("B"); /* Line 12 */ } finally /* Line 14 */ { System.out.print("C"); /* Line 16 */ } System.out.print("D"); /* Line 18 */ } public static void badMethod() { throw new RuntimeException(); } }
(A) => AB
(B) => BCD
(C) => ABC
Answer =>> BCD

[64] What will be the output of the program? class Exc0 extends Exception { } class Exc1 extends Exc0 { } /* Line 2 */ public class Test { public static void main(String args[]) { try { throw new Exc1(); /* Line 9 */ } catch (Exc0 e0) /* Line 11 */ { System.out.println("Ex0 caught"); } catch (Exception e) { System.out.println("exception caught"); } } }
(A) => Ex0 caught
(B) => exception caught
(C) => Compilation fails because of an error at line 2.
Answer =>> Ex0 caught

[65] Which interface provides the capability to store objects using a key-value pair?
(A) => Java.util.Map
(B) => Java.util.Set
(C) => Java.util.List
Answer =>> Java.util.Map

[66] Which collection class allows you to associate its elements with key values, and allows you to retrieve objects in FIFO (first-in, first-out) sequence?
(A) => java.util.ArrayList
(B) => java.util.LinkedHashMap
(C) => java.util.HashMap
Answer =>> java.util.LinkedHashMap

[67] Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization?
(A) => java.util.SortedMap
(B) => java.util.Hashtable
(C) => java.util.TreeSet
Answer =>> java.util.Hashtable

[68] Which is valid declaration of a float?
(A) => float f = 1F;
(B) => float f = 1.0;
(C) => float f = "1";
Answer =>> float f = 1F;

[69] /* 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?
(A) => No statement required.
(B) => import java.io.*;
(C) => include java.io.*;
Answer =>> No statement required.

[70] What is the numerical range of char?
(A) => 0 to 32767
(B) => 0 to 65535
(C) => -256 to 255
Answer =>> 0 to 65535

[71] Which of the following are Java reserved words? 1.run 2.import 3.default 4.implement
(A) => 1 and 2
(B) => 2 and 3
(C) => 3 and 4
Answer =>> 2 and 3

[72] 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();
Answer =>> Foo.Bar b = f.new Bar();

[73] 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();
Answer =>> MyOuter.MyInner m = new MyOuter.MyInner();

[74] Which two of the following methods are defined in class Thread?
(A) => start()
(B) => wait()
(C) => notify()
Answer =>> start()

[75] Which three guarantee that a thread will leave the running state?
(A) => yield()
(B) => wait()
(C) => notify()
Answer =>> wait()

[76] Which of the following will directly stop the execution of a Thread?
(A) => wait()
(B) => notify()
(C) => notifyall()
Answer =>> wait()

[77] Which method must be defined by a class implementing the java.lang.Runnable interface?
(A) => void run()
(B) => public void run()
(C) => public void start()
Answer =>> public void run()

[78] Which will contain the body of the thread?
(A) => run();
(B) => start();
(C) => stop();
Answer =>> run();

[79] Which method registers a thread in a thread scheduler?
(A) => run();
(B) => construct();
(C) => start();
Answer =>> start();

[80] Assume the following method is properly synchronized and called from a thread A on an object B: wait(2000); After calling this method, when will the thread A become a candidate to get another turn at the CPU?
(A) => After thread A is notified, or after two seconds.
(B) => After the lock on B is released, or after two seconds.
(C) => Two seconds after thread A is notified.
Answer =>> After thread A is notified, or after two seconds.

[81] Which of the following will not directly cause a thread to stop?
(A) => notify()
(B) => wait()
(C) => InputStream access
Answer =>> notify()

[82] Which class or interface defines the wait(), notify(),and notifyAll() methods?
(A) => Object
(B) => Thread
(C) => Runnable
Answer =>> Object

[83] public class MyRunnable implements Runnable { public void run() { // some code here } } which of these will create and start this thread?
(A) => new Runnable(MyRunnable).start();
(B) => new Thread(MyRunnable).run();
(C) => new Thread(new MyRunnable()).start();
Answer =>> new Thread(new MyRunnable()).start();

[84] public Object m() { Object o = new Float(3.14F); Object [] oa = new Object[l]; oa[0] = o; /* Line 5 */ o = null; /* Line 6 */ oa[0] = null; /* Line 7 */ return o; /* Line 8 */ } When is the Float object, created in line 3, eligible for garbage collection?
(A) => just after line 5
(B) => just after line 6
(C) => just after line 7
Answer =>> just after line 7

[85] class X2 { public X2 x; public static void main(String [] args) { X2 x2 = new X2(); /* Line 6 */ X2 x3 = new X2(); /* Line 7 */ x2.x = x3; x3.x = x2; x2 = new X2(); x3 = x2; /* Line 11 */ doComplexStuff(); } } after line 11 runs, how many objects are eligible for garbage collection?
(A) => 0
(B) => 1
(C) => 2
Answer =>> 2

[86] What allows the programmer to destroy an object x?
(A) => x.delete()
(B) => Only the garbage collection system can destroy an object.
(C) => x.finalize()
Answer =>> Only the garbage collection system can destroy an object.

[87] Which of the following statements is true?
(A) => If assertions are compiled into a source file, and if no flags are included at runtime, assertions will execute by default.
(B) => As of Java version 1.4, assertion statements are compiled by default.
(C) => With the proper use of runtime arguments, it is possible to instruct the VM to disable assertions for a certain class, and to enable assertions for a certain package, at the same time.
Answer =>> With the proper use of runtime arguments, it is possible to instruct the VM to disable assertions for a certain class, and to enable assertions for a certain package, at the same time.

[88] Which statement is true?
(A) => Assertions can be enabled or disabled on a class-by-class basis.
(B) => Conditional compilation is used to allow tested classes to run at full speed.
(C) => Assertions are appropriate for checking the validity of arguments in a method.
Answer =>> Assertions can be enabled or disabled on a class-by-class basis.

[89] Which statement is true about assertions in the Java programming language?
(A) => Assertion expressions should not contain side effects.
(B) => Assertion expression values can be any primitive type.
(C) => Assertions should be used for enforcing preconditions on public methods.
Answer =>> Assertion expressions should not contain side effects.

[90] Which of the following statements is true?
(A) => It is sometimes good practice to throw an AssertionError explicitly.
(B) => Private getter() and setter() methods should not use assertions to verify arguments.
(C) => If an AssertionError is thrown in a try-catch block, the finally block will be bypassed.
Answer =>> It is sometimes good practice to throw an AssertionError explicitly.

[91] Which of the following statements is true?
(A) => In an assert statement, the expression after the colon ( : ) can be any Java expression.
(B) => If a switch block has no default, adding an assert default is considered appropriate.
(C) => It is appropriate to handle assertion failures using a catch clause.
Answer =>> If a switch block has no default, adding an assert default is considered appropriate.

[92] Which three statements are true?
(A) => Assertion checking is typically enabled when a program is deployed.
(B) => Assertion checking is typically enabled when a program is deployed.
(C) => Assertion checking is typically enabled during program development and testing.
Answer =>> Assertion checking is typically enabled when a program is deployed.

[93] You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?
(A) => public
(B) => protected
(C) => private
Answer =>> protected

[94] 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
Answer =>> new Inner(); //At line 5

[95] 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
(B) => 2, 4, 5
(C) => 1, 2, 6
Answer =>> 1, 2, 6

[96] public class Test { } What is the prototype of the default constructor?
(A) => Test( )
(B) => public Test( )
(C) => Test(void)
Answer =>> public Test( )

[97] What will be the output of the program? public class Foo { public static void main(String[] args) { try { return; } finally { System.out.println( "Finally" ); } } }
(A) => Finally
(B) => Compilation fails.
(C) => The code runs with no output.
Answer =>> Finally

[98] What will be the output of the program? try { int x = 0; int y = 5 / x; } catch (Exception e) { System.out.println("Exception"); } catch (ArithmeticException ae) { System.out.println(" Arithmetic Exception"); } System.out.println("finished");
(A) => finished
(B) => Exception
(C) => Compilation fails.
Answer =>> Compilation fails.

[99] What will be the output of the program? public class X { public static void main(String [] args) { try { badMethod(); System.out.print("A"); } catch (Exception ex) { System.out.print("B"); } finally { System.out.print("C"); } System.out.print("D"); } public static void badMethod() { throw new Error(); /* Line 22 */ } }
(A) => ABCD
(B) => Compilation fails.
(C) => C is printed before exiting with an error message.
Answer =>> C is printed before exiting with an error message.

[100] switch(x) { default: System.out.println("Hello"); } Which two are acceptable types for x? 1.byte 2.long 3.char 4.float 5.Short 6.Long
(A) => 1 and 3
(B) => 2 and 4
(C) => 3 and 5
Answer =>> 1 and 3

[101] void start() { A a = new A(); B b = new B(); a.s(b); b = null; /* Line 5 */ a = null; /* Line 6 */ System.out.println("start completed"); /* Line 7 */ } When is the B object, created in line 3, eligible for garbage collection?
(A) => after line 5
(B) => after line 6
(C) => There is no way to be absolutely certain.
Answer =>> There is no way to be absolutely certain.

[102] Which is true about an anonymous inner class?
(A) => It can extend exactly one class and implement exactly one interface.
(B) => It can extend exactly one class and can implement multiple interfaces.
(C) => It can extend exactly one class or implement exactly one interface.
Answer =>> It can extend exactly one class or implement exactly one interface.

[103] Which is true about a method-local inner class?
(A) => It must be marked final.
(B) => It can be marked abstract.
(C) => It can be marked public.
Answer =>> It can be marked abstract.

[104] Which statement is true about a static nested class?
(A) => You must have a reference to an instance of the enclosing class in order to instantiate it.
(B) => It does not have access to nonstatic members of the enclosing class.
(C) => It's variables and methods must be static.
Answer =>> It does not have access to nonstatic members of the enclosing class.

[105] Which constructs an anonymous inner class instance?
(A) => Runnable r = new Runnable() { };
(B) => Runnable r = new Runnable(public void run() { });
(C) => System.out.println(new Runnable() {public void run() { }});
Answer =>> System.out.println(new Runnable() {public void run() { }});

[106] What is the value of "d" after this line of code has been executed? double d = Math.round ( 2.5 + Math.random() );
(A) => 2
(B) => 3
(C) => 4
Answer =>> 3

[107] Which of the following would compile without error?
(A) => int a = Math.abs(-5);
(B) => int b = Math.abs(5.0);
(C) => int c = Math.abs(5.5F);
Answer =>> int a = Math.abs(-5);

[108] Which of the following are valid calls to Math.max?
(A) => Math.max(1,4)
(B) => Math.max(2.3, 5)
(C) => Math.max(1, 3, 5, 7)
Answer =>> Math.max(1,4)

[109] public class Myfile { public static void main (String[] args) { String biz = args[1]; String baz = args[2]; String rip = args[3]; System.out.println("Arg is " + rip); } } Select how you would start the program to cause it to print: Arg is 2
(A) => java Myfile 222
(B) => java Myfile 1 2 2 3 4
(C) => java Myfile 1 3 2 2
Answer =>> java Myfile 1 3 2 2

[110] Which four options describe the correct default values for array elements of the types indicated? 1.int -> 0 2.String -> "null" 3.Dog -> null 4.char -> '\u0000' 5.float -> 0.0f 6.boolean -> true
(A) => 1, 2, 3, 4
(B) => 1, 3, 4, 5
(C) => 2, 4, 5, 6
Answer =>> 1, 3, 4, 5

[111] 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
Answer =>> goto, instanceof, native, finally, default, throws

[112] Which will legally declare, construct, and initialize an array?
(A) => int [] myList = {"1", "2", "3"};
(B) => int myList [] = {4, 3, 7};
(C) => int [] myList = (5, 8, 2);
Answer =>> int myList [] = {4, 3, 7};

[113] Which is a reserved word in the Java programming language?
(A) => method
(B) => native
(C) => subclasses
Answer =>> native

[114] Which is a valid keyword in java?
(A) => interface
(B) => string
(C) => Float
Answer =>> interface

[115] Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?
(A) => TreeMap
(B) => HashMap
(C) => LinkedHashMap
Answer =>> LinkedHashMap

[116] Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?
(A) => TreeMap
(B) => HashMap
(C) => LinkedHashMap
Answer =>> LinkedHashMap

[117] Which class does not override the equals() and hashCode() methods, inheriting them directly from class Object?
(A) => java.lang.String
(B) => java.lang.Double
(C) => java.lang.StringBuffer
Answer =>> java.lang.StringBuffer

[118] Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?
(A) => java.util.HashSet
(B) => java.util.List
(C) => java.util.ArrayList
Answer =>> java.util.ArrayList

[119] You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability?
(A) => java.util.Map
(B) => java.util.Set
(C) => java.util.List
Answer =>> java.util.Set

[120] Which interface does java.util.Hashtable implement?
(A) => Java.util.Map
(B) => Java.util.List
(C) => Java.util.HashTable
Answer =>> Java.util.Map

[121] What is the name of the method used to start a thread execution?
(A) => init();
(B) => start();
(C) => run();
Answer =>> start();

[122] Which two are valid constructors for Thread? 1.Thread(Runnable r, String name) 2.Thread() 3.Thread(int priority) 4.Thread(Runnable r, ThreadGroup g) 5.Thread(Runnable r, int priority)
(A) => 1 and 3
(B) => 2 and 4
(C) => 1 and 2
Answer =>> 1 and 2

[123] Which three are methods of the Object class? 1.notify(); 2.notifyAll(); 3.isInterrupted(); 4.synchronized(); 5.interrupt(); 6.wait(long msecs); 7.sleep(long msecs); 8.yield();
(A) => 1, 2, 4
(B) => 2, 4, 5
(C) => 1, 2, 6
Answer =>> 1, 2, 6

[124] Which cannot directly cause a thread to stop executing?
(A) => Calling the SetPriority() method on a Thread object.
(B) => Calling the wait() method on an object.
(C) => Calling notify() method on an object.
Answer =>> Calling notify() method on an object.