

MCQOPTIONS
Saved Bookmarks
This section includes 219 Mcqs, each offering curated multiple-choice questions to sharpen your Engineering knowledge and support exam preparation. Choose a topic below to get started.
101. |
What will be the output of the program? public class Test { private static float[] f = new float[2]; public static void main (String[] args) { System.out.println("f[0] = " + f[0]); } } |
A. | f[0] = 0 |
B. | f[0] = 0.0 |
C. | Compile Error |
D. | Runtime Exception |
Answer» C. Compile Error | |
102. |
What will be the output of the program? import java.util.*; class H { public static void main (String[] args) { Object x = new Vector().elements(); System.out.print((x instanceof Enumeration)+","); System.out.print((x instanceof Iterator)+","); System.out.print(x instanceof ListIterator); } } |
A. | Prints: false,false,false |
B. | Prints: false,false,true |
C. | Prints: false,true,false |
D. | Prints: true,false,false |
Answer» E. | |
103. |
What will be the output of the program? TreeSet map = new TreeSet(); map.add("one"); map.add("two"); map.add("three"); map.add("four"); map.add("one"); Iterator it = map.iterator(); while (it.hasNext() ) { System.out.print( it.next() + " " ); } |
A. | one two three four |
B. | four three two one |
C. | four one three two |
D. | one two three four one |
Answer» D. one two three four one | |
104. |
What will be the output of the program? public static void main(String[] args) { Object obj = new Object() { public int hashCode() { return 42; } }; System.out.println(obj.hashCode()); } |
A. | 42 |
B. | Runtime Exception |
C. | Compile Error at line 2 |
D. | Compile Error at line 5 |
Answer» B. Runtime Exception | |
105. |
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. | I is 0 I is 1 |
C. | Compilation fails. |
D. | None of the above |
Answer» D. None of the above | |
106. |
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. | j = 0 |
C. | j = 1 |
D. | Compilation fails. |
Answer» E. | |
107. |
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 = 4 |
C. | value = 6 |
D. | value = 8 |
Answer» E. | |
108. |
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 |
D. | 111 |
Answer» D. 111 | |
109. |
What will be the output of the program? public class Test { private static int[] x; public static void main(String[] args) { System.out.println(x[0]); } } |
A. | 0 |
B. | null |
C. | Compile Error |
D. | <i class="java-code">NullPointerException</i> |
E. | at runtime |
Answer» E. at runtime | |
110. |
What will be the output of the program? import java.util.*; class I { public static void main (String[] args) { Object i = new ArrayList().iterator(); System.out.print((i instanceof List)+","); System.out.print((i instanceof Iterator)+","); System.out.print(i instanceof ListIterator); } } |
A. | Prints: false, false, false |
B. | Prints: false, false, true |
C. | Prints: false, true, false |
D. | Prints: false, true, true |
Answer» D. Prints: false, true, true | |
111. |
What will be the output of the program? package foo; import java.util.Vector; /* Line 2 */ private class MyVector extends Vector { int i = 1; /* Line 5 */ public MyVector() { i = 2; } } public class MyNewVector extends MyVector { public MyNewVector () { i = 4; /* Line 15 */ } public static void main (String args []) { MyVector v = new MyNewVector(); /* Line 19 */ } } |
A. | Compilation will succeed. |
B. | Compilation will fail at line 3. |
C. | Compilation will fail at line 5. |
D. | Compilation will fail at line 15. |
Answer» C. Compilation will fail at line 5. | |
112. |
What will be the output of the program? public class Test { public static void main (String[] args) { String foo = args[1]; String bar = args[2]; String baz = args[3]; System.out.println("baz = " + baz); /* Line 8 */ } } And the command line invocation: > java Test red green blue |
A. | baz = |
B. | baz = null |
C. | baz = blue |
D. | Runtime Exception |
Answer» E. | |
113. |
What will be the output of the program? public class Test { public static void main (String args[]) { String str = NULL; System.out.println(str); } } |
A. | NULL |
B. | Compile Error |
C. | Code runs but no output |
D. | Runtime Exception |
Answer» C. Code runs but no output | |
114. |
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 0 1 0 0 |
D. | 2 1 2 0 1 2 |
Answer» E. | |
115. |
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 = 6 and j = 4 |
D. | i = 5 and j = 6 |
Answer» E. | |
116. |
Which two are valid constructors for Thread? Thread(Runnable r, String name) Thread() Thread(int priority) Thread(Runnable r, ThreadGroup g) Thread(Runnable r, int priority) |
A. | 1 and 3 |
B. | 2 and 4 |
C. | 1 and 2 |
D. | 2 and 5 |
Answer» D. 2 and 5 | |
117. |
class X implements Runnable { public static void main(String args[]) { /* Missing code? */ } public void run() {} } Which of the following line of code is suitable to start a thread ? |
A. | Thread t = new Thread(X); |
B. | Thread t = new Thread(X); t.start(); |
C. | X run = new X(); Thread t = new Thread(run); t.start(); |
D. | Thread t = new Thread(); x.run(); |
Answer» D. Thread t = new Thread(); x.run(); | |
118. |
Which three are methods of the Object class? notify(); notifyAll(); isInterrupted(); synchronized(); interrupt(); wait(long msecs); sleep(long msecs); yield(); |
A. | 1, 2, 4 |
B. | 2, 4, 5 |
C. | 1, 2, 6 |
D. | 2, 3, 4 |
Answer» D. 2, 3, 4 | |
119. |
switch(x) { default: System.out.println("Hello"); } Which two are acceptable types for x? byte long char float Short Long |
A. | 1 and 3 |
B. | 2 and 4 |
C. | 3 and 5 |
D. | 4 and 6 |
Answer» B. 2 and 4 | |
120. |
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. |
Answer» B. "odd" will always be output. | |
121. |
public class While { public void loop() { int x= 0; while ( 1 ) /* Line 6 */ { System.out.print("x plus one is " + (x + 1)); /* Line 8 */ } } } Which statement is true? |
A. | There is a syntax error on line 1. |
B. | There are syntax errors on lines 1 and 6. |
C. | There are syntax errors on lines 1, 6, and 8. |
D. | There is a syntax error on line 6. |
Answer» E. | |
122. |
Which three guarantee that a thread will leave the running state? yield() wait() notify() notifyAll() sleep(1000) aLiveThread.join() Thread.killThread() |
A. | 1, 2 and 4 |
B. | 2, 5 and 6 |
C. | 3, 4 and 7 |
D. | 4, 5 and 7 |
Answer» C. 3, 4 and 7 | |
123. |
Which two of the following methods are defined in class Thread? start() wait() notify() run() terminate() |
A. | 1 and 4 |
B. | 2 and 3 |
C. | 3 and 4 |
D. | 2 and 4 |
Answer» B. 2 and 3 | |
124. |
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() |
D. | void run(int priority) |
Answer» C. public void start() | |
125. |
Which of the following will directly stop the execution of a Thread? |
A. | wait() |
B. | notify() |
C. | notifyall() |
D. | exits synchronized code |
Answer» B. notify() | |
126. |
Which will contain the body of the thread? |
A. | run(); |
B. | start(); |
C. | stop(); |
D. | main(); |
Answer» B. start(); | |
127. |
Which method registers a thread in a thread scheduler? |
A. | run(); |
B. | construct(); |
C. | start(); |
D. | register(); |
Answer» D. register(); | |
128. |
Which of the following will not directly cause a thread to stop? |
A. | notify() |
B. | wait() |
C. | InputStream access |
D. | sleep() |
Answer» B. wait() | |
129. |
Which class or interface defines the wait(), notify(),and notifyAll() methods? |
A. | Object |
B. | Thread |
C. | Runnable |
D. | Class |
Answer» B. Thread | |
130. |
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(); |
D. | new MyRunnable().start(); |
Answer» D. new MyRunnable().start(); | |
131. |
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 |
B. | Line 6 |
C. | Line 12 |
D. | Line 14 |
Answer» E. | |
132. |
What will be the output of the program? public class Test { public static int y; public static void foo(int x) { System.out.print("foo "); y = x; } public static int bar(int z) { System.out.print("bar "); return y = z; } public static void main(String [] args ) { int t = 0; assert t > 0 : bar(7); assert t > 1 : foo(8); /* Line 18 */ System.out.println("done "); } } |
A. | bar |
B. | bar done |
C. | foo done |
D. | Compilation fails |
Answer» E. | |
133. |
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 |
B. | Compilation fails. |
C. | An AssertionError is thrown. |
D. | An AssertionError is thrown and finished is output. |
Answer» D. An AssertionError is thrown and finished is output. | |
134. |
Which two cause a compiler error? float[ ] f = new float(3); float f2[ ] = new float[ ]; float[ ]f1 = new float[3]; float f3[ ] = new float[3]; float f5[ ] = {1.0f, 2.0f, 2.0f}; |
A. | 2, 4 |
B. | 3, 5 |
C. | 4, 5 |
D. | 1, 2 |
Answer» E. | |
135. |
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 |
D. | protected |
E. | volatile |
Answer» D. protected | |
136. |
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 |
B. | Line 12 |
C. | Line 14 |
D. | Line 22 |
Answer» E. | |
137. |
Which is a valid declaration within an interface? |
A. | public static short stop = 23; |
B. | protected short stop = 23; |
C. | transient short stop = 23; |
D. | final void madness(short stop); |
Answer» B. protected short stop = 23; | |
138. |
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. | byte |
C. | long |
D. | double |
Answer» E. | |
139. |
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; } |
Answer» B. private int method1(int a, int b) { return 0; } | |
140. |
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"; |
D. | int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 }; |
Answer» B. float fa = new float[20]; | |
141. |
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. | public |
B. | abstract |
C. | protected |
D. | synchronized |
E. | default access |
Answer» F. | |
142. |
Which two of the following are legal declarations for nonnested classes and interfaces? final abstract class Test {} public static interface Test {} final public class Test {} protected abstract class Test {} protected interface Test {} abstract public class Test {} |
A. | 1 and 4 |
B. | 2 and 5 |
C. | 3 and 6 |
D. | 4 and 6 |
Answer» D. 4 and 6 | |
143. |
Which of the following class level (nonlocal) variable declarations will not compile? |
A. | protected int a; |
B. | transient int b = 3; |
C. | private synchronized int e; |
D. | volatile int d; |
Answer» D. volatile int d; | |
144. |
Which of the following is/are legal method declarations? protected abstract void m1(); static final void m1(){} synchronized public final void m1() {} private native void m1(); |
A. | 1 and 3 |
B. | 2 and 4 |
C. | 1 only |
D. | All of them are legal declarations. |
Answer» E. | |
145. |
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"}; |
D. | boolean results[ ] = new boolean [] {true, false, true}; |
E. | Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)}; |
Answer» C. String cats[ ] = {"Fluffy", "Spot", "Zeus"}; | |
146. |
What will be the output of the program? class A { final public int GetResult(int a, int b) { return 0; } } class B extends A { public int GetResult(int a, int b) {return 1; } } public class Test { public static void main(String args[]) { B b = new B(); System.out.println("x = " + b.GetResult(0, 1)); } } |
A. | x = 0 |
B. | x = 1 |
C. | Compilation fails. |
D. | An exception is thrown at runtime. |
Answer» D. An exception is thrown at runtime. | |
147. |
What will be the output of the program? public class A { void A() /* Line 3 */ { System.out.println("Class A"); } public static void main(String[] args) { new A(); } } |
A. | Class A |
B. | Compilation fails. |
C. | An exception is thrown at line 3. |
D. | The code executes with no output. |
Answer» E. | |
148. |
What will be the output of the program? class Super { public int i = 0; public Super(String text) /* Line 4 */ { i = 1; } } class Sub extends Super { public Sub(String text) { i = 2; } public static void main(String args[]) { Sub sub = new Sub("Hello"); System.out.println(sub.i); } } |
A. | 0 |
B. | 1 |
C. | 2 |
D. | Compilation fails. |
Answer» E. | |
149. |
What will be the output of the program? public class Test { public int aMethod() { static int i = 0; i++; return i; } public static void main(String args[]) { Test test = new Test(); test.aMethod(); int j = test.aMethod(); System.out.println(j); } } |
A. | 0 |
B. | 1 |
C. | 2 |
D. | Compilation fails. |
Answer» E. | |
150. |
Which statement is true for the class java.util.HashSet? |
A. | The elements in the collection are ordered. |
B. | The collection is guaranteed to be immutable. |
C. | The elements in the collection are guaranteed to be unique. |
D. | The elements in the collection are accessed using a unique key. |
Answer» D. The elements in the collection are accessed using a unique key. | |