

MCQOPTIONS
This section includes 1698 Mcqs, each offering curated multiple-choice questions to sharpen your General Awareness knowledge and support exam preparation. Choose a topic below to get started.
251. |
What is the output of this program?
import java.util.*; class Linkedlist { public static void main(String args[]) { LinkedList obj = new LinkedList(); obj.add("A"); obj.add("B"); obj.add("C"); obj.addFirst("D"); System.out.println(obj); } } |
A. | [A, B, C]. |
B. | [D, B, C]. |
C. | [A, B, C, D]. |
D. | [D, A, B, C]. |
Answer» E. | |
252. |
What is the output of this program?
import java.util.*; class Linkedlist { public static void main(String args[]) { LinkedList obj = new LinkedList(); obj.add("A"); obj.add("B"); obj.add("C"); obj.removeFirst(); System.out.println(obj); } } |
A. | [A, B]. |
B. | [B, C]. |
C. | [A, B, C, D]. |
D. | [A, B, C]. |
Answer» C. [A, B, C, D]. | |
253. |
What is the output of this program?
import java.util.*; class Bitset { public static void main(String args[]) { BitSet obj = new BitSet(5); for (int i = 0; i < 5; ++i) obj.set(i); obj.clear(2); System.out.print(obj.length() + " " + obj.size()); } } |
A. | 4 64 |
B. | 5 64 |
C. | 5 128 |
D. | 4 128 |
Answer» C. 5 128 | |
254. |
What is the output of this program?
import java.lang.reflect.*; class Additional_packages { public static void main(String args[]) { try { Class c = Class.forName("java.awt.Dimension"); Constructor constructors[] = c.getConstructors(); for (int i = 0; i < constructors.length; i++) System.out.println(constructors[i]); } catch (Exception e) { System.out.print("Exception"); } } } |
A. | Program prints all the constructors of java.awt.Dimension package |
B. | Program prints all the possible constructors of class Class |
C. | Program prints Exception |
D. | Runtime Error |
Answer» B. Program prints all the possible constructors of class Class | |
255. |
What is the output of this program?
import java.lang.reflect.*; class Additional_packages { public static void main(String args[]) { try { Class c = Class.forName("java.awt.Dimension"); Field fields[] = c.getFields(); for (int i = 0; i < fields.length; i++) System.out.println(fields[i]); } catch (Exception e) { System.out.print("Exception"); } } } |
A. | Program prints all the constructors of java.awt.Dimension package |
B. | Program prints all the methods of java.awt.Dimension package |
C. | Program prints all the data members of java.awt.Dimension package |
D. | program prints all the methods and data member of java.awt.Dimension package |
Answer» D. program prints all the methods and data member of java.awt.Dimension package | |
256. |
What is the output of this program?
import java.util.*; class vector { public static void main(String args[]) { Vector obj = new Vector(4,2); obj.addElement(new Integer(3)); obj.addElement(new Integer(2)); obj.addElement(new Integer(5)); System.out.println(obj.capacity()); } } |
A. | 2 |
B. | 3 |
C. | 4 |
D. | 6 |
Answer» D. 6 | |
257. |
What is the output of this program?
import java.util.*; class vector { public static void main(String args[]) { Vector obj = new Vector(4,2); obj.addElement(new Integer(3)); obj.addElement(new Integer(2)); obj.addElement(new Integer(5)); System.out.println(obj.elementAt(1)); } } |
A. | 0 |
B. | 1 |
C. | True |
D. | False |
Answer» E. | |
258. |
What is the output of this program?
import java.util.*; class hashtable { public static void main(String args[]) { Hashtable obj = new Hashtable(); obj.put("A", new Integer(3)); obj.put("B", new Integer(2)); obj.put("C", new Integer(8)); obj.clear(); System.out.print(obj.size()); } } |
A. | 0 |
B. | 1 |
C. | 2 |
D. | 3 |
Answer» B. 1 | |
259. |
What is the output of this program?
import java.util.*; class Maps { public static void main(String args[]) { HashMap obj = new HashMap(); obj.put("A", new Integer(1)); obj.put("B", new Integer(2)); obj.put("C", new Integer(3)); System.out.println(obj); } } |
A. | {A 1, B 1, C 1} |
B. | {A, B, C} |
C. | {A-1, B-1, C-1} |
D. | {A=1, B=2, C=3} |
Answer» E. | |
260. |
What is the output of this program?
import java.util.*; class Maps { public static void main(String args[]) { HashMap obj = new HashMap(); obj.put("A", new Integer(1)); obj.put("B", new Integer(2)); obj.put("C", new Integer(3)); System.out.println(obj.keySet()); } } |
A. | [A, B, C]. |
B. | {A, B, C} |
C. | {1, 2, 3} |
D. | [1, 2, 3]. |
Answer» B. {A, B, C} | |
261. |
Which two are valid constructors for Thread?1. Thread(Runnable r, String name) |
A. | 1 and 3 |
B. | 2 and 4 |
C. | 1 and 2 |
D. | 2 and 5 |
Answer» D. 2 and 5 | |
262. |
Which three are methods of the Object class?1. notify(); |
A. | 1, 2, 4 |
B. | 2, 4, 5 |
C. | 1, 2, 6 |
D. | 2, 3, 4 |
Answer» D. 2, 3, 4 | |
263. |
Which of the following line of code is suitable to start a thread ?
class X implements Runnable { public static void main(String args[]) { /* Missing code? */ } public void run() {} } |
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(); | |
264. |
What will be the output of the program?
public class Test107 implements Runnable { private int x; private int y; public static void main(String args[]) { Test107 that = new Test107(); (new Thread(that)).start(); (new Thread(that)).start(); } public synchronized void run() { for(int i = 0; i < 10; i++) { x++; y++; System.out.println("x = " + x + ", y = " + y); /* Line 17 */ } } } |
A. | Compilation error. |
B. | Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by both threads running simultaneously. |
C. | Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by first one thread then the other. This is guaranteed by the synchronised code. |
D. | Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8... |
Answer» D. Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8... | |
265. |
What will be the output of the program?
class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); Thread x = new Thread(t); x.start(); /* Line 7 */ } public void run() { for(int i = 0; i < 3; ++i) { System.out.print(i + ".."); } } } |
A. | Compilation fails. |
B. | 1..2..3.. |
C. | 0..1..2..3.. |
D. | 0..1..2.. |
Answer» E. | |
266. |
What will be the output of the program?
public class Test { public static void main (String [] args) { final Foo f = new Foo(); Thread t = new Thread(new Runnable() { public void run() { f.doStuff(); } }); Thread g = new Thread() { public void run() { f.doStuff(); } }; t.start(); g.start(); } } class Foo { int x = 5; public void doStuff() { if (x < 10) { // nothing to do try { wait(); } catch(InterruptedException ex) { } } else { System.out.println("x is " + x++); if (x >= 10) { notify(); } } } } |
A. | The code will not compile because of an error on notify(); of class Foo. |
B. | The code will not compile because of some other error in class Test. |
C. | An exception occurs at runtime. |
D. | It prints "x is 5 x is 6". |
Answer» D. It prints "x is 5 x is 6". | |
267. |
What will be the output of the program?
class MyThread extends Thread { MyThread() { System.out.print(" MyThread"); } public void run() { System.out.print(" bar"); } public void run(String s) { System.out.println(" baz"); } } public class TestThreads { public static void main (String [] args) { Thread t = new MyThread() { public void run() { System.out.println(" foo"); } }; t.start(); } } |
A. | Foo |
B. | MyThread foo |
C. | MyThread bar |
D. | Foo bar |
Answer» C. MyThread bar | |
268. |
What will be the output of the program?
class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); t.start(); System.out.print("one. "); t.start(); System.out.print("two. "); } public void run() { System.out.print("Thread "); } } |
A. | Compilation fails |
B. | An exception occurs at runtime. |
C. | It prints "Thread one. Thread two." |
D. | The output cannot be determined. |
Answer» C. It prints "Thread one. Thread two." | |
269. |
What will be the output of the program?
class MyThread extends Thread { MyThread() {} MyThread(Runnable r) {super(r); } public void run() { System.out.print("Inside Thread "); } } class MyRunnable implements Runnable { public void run() { System.out.print(" Inside Runnable"); } } class Test { public static void main(String[] args) { new MyThread().start(); new MyThread(new MyRunnable()).start(); } } |
A. | Prints "Inside Thread Inside Thread" |
B. | Prints "Inside Thread Inside Runnable" |
C. | Does not compile |
D. | Throws exception at runtime |
Answer» B. Prints "Inside Thread Inside Runnable" | |
270. |
What will be the output of the program?
class s1 implements Runnable { int x = 0, y = 0; int addX() {x++; return x;} int addY() {y++; return y;} public void run() { for(int i = 0; i < 10; i++) System.out.println(addX() + " " + addY()); } public static void main(String args[]) { s1 run1 = new s1(); s1 run2 = new s1(); Thread t1 = new Thread(run1); Thread t2 = new Thread(run2); t1.start(); t2.start(); } } |
A. | Compile time Error: There is no start() method |
B. | Will print in this order: 1 1 2 2 3 3 4 4 5 5... |
C. | Will print but not exactly in an order (e.g: 1 1 2 2 1 1 3 3...) |
D. | Will print in this order: 1 2 3 4 5 6... 1 2 3 4 5 6... |
Answer» D. Will print in this order: 1 2 3 4 5 6... 1 2 3 4 5 6... | |
271. |
What will be the output of the program?
public class Q126 implements Runnable { private int x; private int y; public static void main(String [] args) { Q126 that = new Q126(); (new Thread(that)).start( ); /* Line 8 */ (new Thread(that)).start( ); /* Line 9 */ } public synchronized void run( ) /* Line 11 */ { for (;;) /* Line 13 */ { x++; y++; System.out.println("x = " + x + "y = " + y); } } } |
A. | An error at line 11 causes compilation to fail |
B. | Errors at lines 8 and 9 cause compilation to fail. |
C. | The program prints pairs of values for x and y that might not always be the same on the same line (for example, "x=2, y=1") |
D. | The program prints pairs of values for x and y that are always the same on the same line (for example, "x=1, y=1". In addition, each value appears once (for example, "x=1, y=1" followed by "x=2, y=2") |
Answer» E. | |
272. |
What will be the output of the program?
class Happy extends Thread { final StringBuffer sb1 = new StringBuffer(); final StringBuffer sb2 = new StringBuffer(); public static void main(String args[]) { final Happy h = new Happy(); new Thread() { public void run() { synchronized(this) { h.sb1.append("A"); h.sb2.append("B"); System.out.println(h.sb1); System.out.println(h.sb2); } } }.start(); new Thread() { public void run() { synchronized(this) { h.sb1.append("D"); h.sb2.append("C"); System.out.println(h.sb2); System.out.println(h.sb1); } } }.start(); } } |
A. | ABBCAD |
B. | ABCBCAD |
C. | CDADACB |
D. | Output determined by the underlying platform. |
Answer» E. | |
273. |
The static method Thread.currentThread() returns a reference to the currently executing Thread object. What is the result of this code?
class Test { public static void main(String [] args) { printAll(args); } public static void printAll(String[] lines) { for(int i = 0; i < lines.length; i++) { System.out.println(lines[i]); Thread.currentThread().sleep(1000); } } } |
A. | Each String in the array lines will output, with a 1-second pause. |
B. | Each String in the array lines will output, with no pause in between because this method is not executed in a Thread. |
C. | Each String in the array lines will output, and there is no guarantee there will be a pause because currentThread() may not retrieve this thread. |
D. | This code will not compile. |
Answer» E. | |
274. |
What is the output of this program?
class newthread implements Runnable { Thread t; newthread() { t1 = new Thread(this,"Thread_1"); t2 = new Thread(this,"Thread_2"); t1.start(); t2.start(); } public void run() { t2.setPriority(Thread.MAX_PRIORITY); System.out.print(t1.equals(t2)); } } class multithreaded_programing { public static void main(String args[]) { new newthread(); } } |
A. | True |
B. | False |
C. | Truetrue |
D. | Falsefalse |
Answer» E. | |
275. |
What is the name of the thread in output of this program?
class multithreaded_programing { public static void main(String args[]) { Thread t = Thread.currentThread(); System.out.println(t.getPriority()); } } |
A. | 0 |
B. | 1 |
C. | 4 |
D. | 5 |
Answer» E. | |
276. |
What is the name of the thread in output of this program?
class multithreaded_programing { public static void main(String args[]) { Thread t = Thread.currentThread(); System.out.println(t.isAlive()); } } |
A. | 0 |
B. | 1 |
C. | True |
D. | False |
Answer» D. False | |
277. |
What is the output of this program?
class newthread extends Thread { Thread t; String name; newthread(String threadname) { name = threadname; t = new Thread(this,name); t.start(); } public void run() { } } class multithreaded_programing { public static void main(String args[]) { newthread obj1 = new newthread("one"); newthread obj2 = new newthread("two"); try { System.out.print(obj1.t.equals(obj2.t)); } catch(Exception e) { System.out.print("Main thread interrupted"); } } } |
A. | True |
B. | False |
C. | Main thread interrupted |
D. | None of the mentioned |
Answer» C. Main thread interrupted | |
278. |
What is the output of this program?
class newthread extends Thread { Thread t; newthread() { t1 = new Thread(this,"Thread_1"); t2 = new Thread(this,"Thread_2"); t1.start(); t2.start(); } public void run() { t2.setPriority(Thread.MAX_PRIORITY); System.out.print(t1.equals(t2)); } } class multithreaded_programing { public static void main(String args[]) { new newthread(); } } |
A. | True |
B. | False |
C. | Truetrue |
D. | Falsefalse |
Answer» E. | |
279. |
Which two of the following methods are defined in class Thread?1. start() |
A. | 1 and 4 |
B. | 2 and 3 |
C. | 3 and 4 |
D. | 2 and 4 |
Answer» B. 2 and 3 | |
280. |
Which three guarantee that a thread will leave the running state?1. yield() |
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 | |
281. |
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. |
D. | Two seconds after lock B is released. |
Answer» B. After the lock on B is released, or after two seconds. | |
282. |
What is the output of this program?
import java.util.*; class Array { public static void main(String args[]) { int array[] = new int [5]; for (int i = 5; i > 0; i--) array[5-i] = i; Arrays.fill(array, 1, 4, 8); for (int i = 0; i < 5 ; i++) System.out.print(array[i]); } } |
A. | 12885 |
B. | 12845 |
C. | 58881 |
D. | 54881 |
Answer» D. 54881 | |
283. |
What is the output of this program?
import java.util.*; class Bitset { public static void main(String args[]) { BitSet obj = new BitSet(5); for (int i = 0; i < 5; ++i) obj.set(i); obj.clear(2); System.out.print(obj); } } |
A. | {0, 1, 3, 4} |
B. | {0, 1, 2, 4} |
C. | {0, 1, 2, 3, 4} |
D. | {0, 0, 0, 3, 4} |
Answer» B. {0, 1, 2, 4} | |
284. |
What is the output of this program?
import java.util.*; class Output { public static void main(String args[]) { ArrayList obj = new ArrayList(); obj.add("A"); obj.ensureCapacity(3); System.out.println(obj.size()); } } |
A. | 1 |
B. | 2 |
C. | 3 |
D. | 4 |
Answer» B. 2 | |
285. |
What is the output of this program?
class Output { public static void main(String args[]) { ArrayList obj = new ArrayList(); obj.add("A"); obj.add("D"); obj.ensureCapacity(3); obj.trimToSize(); System.out.println(obj.size()); } } |
A. | 1 |
B. | 2 |
C. | 3 |
D. | 4 |
Answer» C. 3 | |
286. |
What is the output of this program?
import java.util.*; class Output { public static void main(String args[]) { HashSet obj = new HashSet(); obj.add("A"); obj.add("B"); obj.add("C"); System.out.println(obj + " " + obj.size()); } } |
A. | ABC 3 |
B. | [A, B, C] 3 |
C. | ABC 2 |
D. | [A, B, C] 2 |
Answer» C. ABC 2 | |
287. |
What is the output of this program?
import java.util.*; class Output { public static void main(String args[]) { TreeSet t = new TreeSet(); t.add("3"); t.add("9"); t.add("1"); t.add("4"); t.add("8"); System.out.println(t); } } |
A. | [1, 3, 5, 8, 9]. |
B. | [3, 4, 1, 8, 9]. |
C. | [9, 8, 4, 3, 1]. |
D. | [1, 3, 4, 8, 9]. |
Answer» E. | |
288. |
What is the output of this program?
import java.util.*; class Maps { public static void main(String args[]) { HashMap obj = new HashMap(); obj.put("A", new Integer(1)); obj.put("B", new Integer(2)); obj.put("C", new Integer(3)); System.out.println(obj.get("B")); } } |
A. | 1 |
B. | 2 |
C. | 3 |
D. | Null |
Answer» C. 3 | |
289. |
What is the output of this program?
import java.util.*; class Maps { public static void main(String args[]) { TreeMap obj = new TreeMap(); obj.put("A", new Integer(1)); obj.put("B", new Integer(2)); obj.put("C", new Integer(3)); System.out.println(obj.entrySet()); } } |
A. | [A, B, C]. |
B. | [1, 2, 3]. |
C. | {A=1, B=2, C=3} |
D. | [A=1, B=2, C=3]. |
Answer» E. | |
290. |
What is the output of this program?
import java.util.*; class vector { public static void main(String args[]) { Vector obj = new Vector(4,2); obj.addElement(new Integer(3)); obj.addElement(new Integer(2)); obj.addElement(new Integer(6)); obj.insertElementAt(new Integer(8), 2); System.out.println(obj); } } |
A. | [3, 2, 6]. |
B. | [3, 2, 8]. |
C. | [3, 2, 6, 8]. |
D. | [3, 2, 8, 6]. |
Answer» E. | |
291. |
What is the output of this program?
import java.util.*; class stack { public static void main(String args[]) { Stack obj = new Stack(); obj.push(new Integer(3)); obj.push(new Integer(2)); obj.pop(); obj.push(new Integer(5)); System.out.println(obj); } } |
A. | [3, 5]. |
B. | [3, 2]. |
C. | [3, 2, 5]. |
D. | [3, 5, 2]. |
Answer» B. [3, 2]. | |
292. |
What is the output of this program?
import java.util.*; class vector { public static void main(String args[]) { Vector obj = new Vector(4,2); obj.addElement(new Integer(3)); obj.addElement(new Integer(2)); obj.addElement(new Integer(5)); obj.removeAll(obj); System.out.println(obj.isEmpty()); } } |
A. | 0 |
B. | 1 |
C. | True |
D. | False |
Answer» D. False | |
293. |
What is the output of this program?
import java.util.*; class Arraylist { public static void main(String args[]) { ArrayList obj1 = new ArrayList(); ArrayList obj2 = new ArrayList(); obj1.add("A"); obj1.add("B"); obj2.add("A"); obj2.add(1, "B"); System.out.println(obj1.equals(obj2)); } } |
A. | 0 |
B. | 1 |
C. | True |
D. | False |
Answer» D. False | |
294. |
What is the output of this program?
import java.util.*; class Collection_Algos { public static void main(String args[]) { LinkedList list = new LinkedList(); list.add(new Integer(2)); list.add(new Integer(8)); list.add(new Integer(5)); list.add(new Integer(1)); Iterator i = list.iterator(); Collections.reverse(list); Collections.sort(list); while(i.hasNext()) System.out.print(i.next() + " "); } } |
A. | 2 8 5 1 |
B. | 1 5 8 2 |
C. | 1 2 5 8 |
D. | 2 1 8 5 |
Answer» D. 2 1 8 5 | |
295. |
What is the output of this program?
import java.util.*; class Array { public static void main(String args[]) { int array[] = new int [5]; for (int i = 5; i > 0; i--) array[5 - i] = i; Arrays.sort(array); for (int i = 0; i < 5; ++i) System.out.print(array[i]);; } } |
A. | 12345 |
B. | 54321 |
C. | 1234 |
D. | 5432 |
Answer» B. 54321 | |
296. |
What would be the output of following code snippet?int a = random.nextInt(15) + 1; |
A. | Random number between 1 to 15, including 1 and 15 |
B. | Random number between 1 to 15, excluding 15 |
C. | Random number between 1 to 15, excluding 1 |
D. | Random number between 1 to 15, excluding 1 and 15 |
Answer» B. Random number between 1 to 15, excluding 15 | |
297. |
What would be the output of following code snippet?int a = random.nextInt(7) + 4; |
A. | Random number between 4 to 7, including 4 and 7 |
B. | Random number between 4 to 7, excluding 4 and 7 |
C. | Random number between 4 to 10, excluding 4 and 10 |
D. | Random number between 4 to 10, including 4 and 10 |
Answer» E. | |
298. |
What is the output of this program?
import java.util.*; class Collection_iterators { public static void main(String args[]) { ListIterator a = list.listIterator(); if(a.previousIndex()! = -1) while(a.hasNext()) System.out.print(a.next() + " "); else System.out.print("EMPTY"); } } |
A. | 0 |
B. | 1 |
C. | -1 |
D. | EMPTY |
Answer» E. | |
299. |
What is the output of this program?
import java.util.*; class Collection_iterators { public static void main(String args[]) { LinkedList list = new LinkedList(); list.add(new Integer(2)); list.add(new Integer(8)); list.add(new Integer(5)); list.add(new Integer(1)); Iterator i = list.iterator(); Collections.reverse(list); while(i.hasNext()) System.out.print(i.next() + " "); } } |
A. | 2 8 5 1 |
B. | 1 5 8 2 |
C. | 2 |
D. | 2 1 8 5 |
Answer» C. 2 | |
300. |
What is the difference between Queue and Stack? |
A. | Stack is LIFO; Queue is FIFO |
B. | Queue is LIFO; Stack is FIFO |
C. | Stack and Queue is FIFO |
D. | Stack and Queue is LIFO |
Answer» B. Queue is LIFO; Stack is FIFO | |