Explore topic-wise MCQs in Java Programming.

This section includes 40 Mcqs, each offering curated multiple-choice questions to sharpen your Java Programming knowledge and support exam preparation. Choose a topic below to get started.

1.

What is the output of below snippet?
import java.util.*;
public class Map_Example
{
public static void main(String[] args)
{
Map mapSample = new TreeMap();
mapSample.put(4, null);
mapSample.put(0, null);
mapSample.put(3, null);
mapSample.put(2, null);
mapSample.put(1, null);

System.out.println(mapSample);
}
}

A. {0=null, 1=null, 2=null, 3=null, 4=null}
B. Runtime Error
C. Compilation Error
D. {3=null, 0=null, 2=null, 1=null, 4=null}
E. None of these
Answer» B. Runtime Error
2.

What is the output of this program?
import java.util.*; 
public class TreeSet_Example
{
public static void main(String args[])
{
TreeSet object = new TreeSet();
object.add("7");
object.add("0");
object.add("9");
object.add("5");
object.add("6");
System.out.println(object);
}
}

A. Runtime Error
B. Compilation Error
C. [0, 5, 6, 7, 9]
D. [6, 7, 9]
E. [0, 5, 6, 7]
Answer» D. [6, 7, 9]
3.

What is the output of this program?
import java.util.*;
public class vector_Example
{
public static void main(String args[])
{
Vector object = new Vector(6,3);
object.addElement(new Integer(5));
object.addElement(new Integer(7));
object.addElement(new Integer(9));
System.out.println(object.elementAt(2));
}
}

A. 6, 3
B. 5
C. 7
D. 9
E. None of these
Answer» E. None of these
4.

What is the output of this program?
import java.util.*;
public class Map_Exmple
{
public static void main(String args[])
{
TreeMap object = new TreeMap();
object.put("I", new Integer(9));
object.put("L", new Integer(12));
object.put("U", new Integer(21));
System.out.println(object.entrySet());
}
}

A. 9, 12, 21
B. I, L, U
C. [9, 12, 21]
D. [I=9, L=12, U=21]
E. None of these
Answer» E. None of these
5.

What is the output of this program?
import java.util.*;
public class Map_Example
{
public static void main(String args[])
{
HashMap object = new HashMap();
object.put("I", new Integer(9));
object.put("L", new Integer(12));
object.put("U", new Integer(21));
System.out.println(object.get("L"));
}
}

A. 9
B. 12
C. 21
D. [I, L, U]
E. None of these
Answer» C. 21
6.

What is the output of this program?
import java.util.*;
public class Map_Example
{
public static void main(String args[])
{
HashMap object = new HashMap();
object.put("D", new Integer(4));
object.put("E", new Integer(5));
object.put("F", new Integer(6));
System.out.println(object.keySet());
}
}

A. {D=4, E=5, F=6}
B. [D, E, F]
C. [D, E, F] 3
D. [D=4, E=5, F=6]
E. None of these
Answer» C. [D, E, F] 3
7.

What is the output of this program?
import java.util.*;
public class Map_Example
{
public static void main(String args[])
{
HashMap object = new HashMap();
object.put("D", new Integer(4));
object.put("E", new Integer(5));
object.put("F", new Integer(6));
System.out.println(object);
}
}

A. {D=4, E=5, F=6}
B. {D, E, F}
C. {E=5, F=6}
D. Runtime Error
E. Compilation Error
Answer» B. {D, E, F}
8.

What is the output of this program?
import java.util.*;
public class hashtable_Example
{
public static void main(String args[])
{
Hashtable object = new Hashtable();
object.put("I", new Integer(10));
object.put("L", new Integer(60));
object.put("U", new Integer(25));
System.out.print(object.contains(new Integer(25)));
}
}

A. True
B. U
C. L
D. False
E. None of these
Answer» B. U
9.

What is the output of this program?
import java.util.*;
public class properties_Example
{
public static void main(String args[])
{
Properties object = new Properties();
object.put("PQ", new Integer(23));
object.put("SR", new Integer(46));
object.put("UV", new Integer(25));
System.out.print(object.keySet());
}
}

A. [PQ, UV, SR]
B. [PQ, UV]
C. [UV, SR]
D. [23, 46, 25]
E. None of these
Answer» B. [PQ, UV]
10.

What is the output of this program?
import java.util.*;
public class hashtable_Example
{
public static void main(String args[])
{
Hashtable object = new Hashtable();
object.put("P", new Integer(12));
object.put("Q", new Integer(21));
object.put("R", new Integer(18));
System.out.print(object.toString());
}
}

A. {R=18, Q=21}
B. {Q=21, P=12}
C. {R=18, P=12}
D. {R=18, Q=21, P=12}
E. {P=12, R=18}
Answer» E. {P=12, R=18}
11.

What is the output of this program?
import java.util.*;
public class hashtable
{
public static void main(String args[])
{
Hashtable object = new Hashtable();
object.put("D", new Integer(30));
object.put("E", new Integer(70));
object.put("F", new Integer(60));
object.remove(new String("E"));
System.out.print(object);
}
}

A. {F=60, D=30, E=70}
B. {D=30, E=70}
C. {F=60, D=30}
D. {D=30, E=70, F=60}
E. None of these
Answer» D. {D=30, E=70, F=60}
12.

What is the output of this program?
import java.util.*;
public class hashtable
{
public static void main(String args[])
{
Hashtable object = new Hashtable();
object.put("I", new Integer(25));
object.put("L", new Integer(60));
object.put("U", new Integer(20));
object.clear();
System.out.print(object.size());
}
}

A. 4
B. 3
C. 2
D. 1
Answer» F.
13.

What is the output of this program?
import java.util.*;
public class Bitset_Example
{
public static void main(String args[])
{
BitSet object = new BitSet(10);
for (int k = 0; k < 6; ++k)
object.set(k);
object.clear(2);
System.out.print(object);
}
}

A. {0, 1, 3, 4, 5}
B. {0, 1, 3, 4}
C. {1, 3, 4, 5}
D. {0, 1, 3, 4, 5, 6}
E. None of these
Answer» B. {0, 1, 3, 4}
14.

What is the output of this program?
 import java.util.*;
public class Bitset_Example
{
public static void main(String args[])
{
BitSet object0 = new BitSet(12);
BitSet object1 = new BitSet(15);
for (int k = 0; k < 7; ++k)
{
object0.set(k);
}
for (int k = 3; k < 15; ++k)
{
object1.set(k);
}
object0.and(object1);
System.out.print(object0);
}
}

A. {3, 4, 5}
B. {4, 5, 6}
C. {4, 5}
D. {3, 4, 5, 6}
E. None of these
Answer» E. None of these
15.

What is the output of this program?
import java.util.*;
public class date_Example
{
public static void main(String args[])
{
Date object = new Date();
System.out.print(object);
}
}

A. Any Garbage Value
B. Prints Present Time & Date
C. Runtime Error
D. Prints Present Date
E. None of these
Answer» C. Runtime Error
16.

What is the output of this program?
import java.util.*;
public class Bitset_Exmple
{
public static void main(String args[])
{
BitSet object = new BitSet(10);
for (int k = 0; k < 6; ++k)
object.set(k);
System.out.print(object.get(6));
}
}

A. 6
B. 10
C. True
D. False
E. None of these
Answer» E. None of these
17.

What is the output of this program?
import java.util.*;
public class Bitset_Example
{
public static void main(String args[])
{
BitSet object = new BitSet(10);
for (int K = 0; K < 6; ++K)
object.set(K);
object.clear(2);
System.out.print(object.length() + " " + object.size());
}
}

A. 6 60
B. 60 6
C. 6 64
D. 64 6
E. None of these
Answer» D. 64 6
18.

What is the output of this program?
import java.util.*;
public class Array_Examle
{
public static void main(String args[])
{
int p[] = new int [10];
for (int k = 7; k > 0 ; k--)
{
p[7-k] = k;
}
Arrays.fill(p, 1, 4, 8);
for (int k = 0; k < 5 ; k++)
{
System.out.print(p[k]);
}
}
}

A. 56885
B. 678867
C. 328832
D. 78883
E. 68883
Answer» E. 68883
19.

What is the output of this program?
import java.util.*;
public class Bitset_Example
{
public static void main(String args[])
{
BitSet object = new BitSet(8);
for (int k = 0; k < 6; ++k)
object.set(k);
object.clear(4);
System.out.print(object);
}
}

A. {0, 1, 3, 5}
B. {0, 1, 2, 5}
C. {0, 1, 2, 3, 5}
D. {0, 1, 2, 3, 4, 5}
E. None of these
Answer» D. {0, 1, 2, 3, 4, 5}
20.

What is the output of this program?
import java.util.*;
public class Collection_iterators_Example
{
public static void main(String args[])
{
LinkedList listobject = new LinkedList();
listobject.add(new Integer(10));
listobject.add(new Integer(9));
listobject.add(new Integer(6));
listobject.add(new Integer(11));
Iterator Itr = listobject.iterator();
Collections.reverse(listobject);
Collections.sort(listobject);
while(Itr.hasNext())
System.out.print(Itr.next() + " ");
}
}

A. 10 9 6 11
B. 10 9 6
C. 6 9 10 11
D. 11 10 9 6
E. None of these
Answer» D. 11 10 9 6
21.

What is the output of this program?
import java.util.*;
public class Collection_iterators_Example
{
public static void main(String args[])
{
LinkedList listObject = new LinkedList();
listObject.add(new Integer(3));
listObject.add(new Integer(9));
listObject.add(new Integer(7));
listObject.add(new Integer(2));
Iterator itr = listObject.iterator();
Collections.reverse(listObject);
while(itr.hasNext())
System.out.print(itr.next() + " ");
}
}

A. 2 7 9 3
B. 3 9 7 2
C. 2 3 7 9
D. 7 9 3 2
E. None of these
Answer» B. 3 9 7 2
22.

What is the output of this program?
import java.util.*;
public class Collection_iterators_Example
{
public static void main(String args[])
{
ListIterator object = List.istIterator();
if(object.previousIndex()!= -1)
while(object.hasNext())
System.out.print(object.next() + " ");
else
System.out.print("EMPTY");
}
}

A. Compilation Error
B. Runtime Error
C. EMPTY
D. empty
E. None of these
Answer» B. Runtime Error
23.

What is the output of this program?
import java.util.*;
public class Collection_AlgosExample
{
public static void main(String args[])
{
LinkedList listobject = new LinkedList();
listobject.add(new Integer(13));
listobject.add(new Integer(23));
listobject.add(new Integer(3));
listobject.add(new Integer(11));
Iterator Itr = listobject.iterator();
Collections.reverse(listobject);
Collections.sort(listobject);
while(Itr.hasNext())
System.out.print(Itr.next() + " ");
}
}

A. 3 11 13 23
B. 13 23 3 11
C. 3 11 13
D. 13 11 3
E. 23 13 11 3
Answer» B. 13 23 3 11
24.

What is the output of this program?
 import java.util.*;
public class Collection_AlgorithmsExample
{
public static void main(String args[])
{
LinkedList listObject = new LinkedList();
listObject.add(new Integer(22));
listObject.add(new Integer(28));
listObject.add(new Integer(25));
listObject.add(new Integer(21));
Iterator Itr = listObject.iterator();
Collections.reverse(listObject);
while(Itr.hasNext())
System.out.print(Itr.next() + " ");
}
}

A. 22 28 25 21
B. 21 25 28 22
C. 28 25 22 21
D. 21 22 25 28
E. None of these
Answer» C. 28 25 22 21
25.

What is the output of this program?
import java.util.*;
public class Collection_iterator_Example
{
public static void main(String args[])
{
LinkedList listobject = new LinkedList();
listobject.add(new Integer(10));
listobject.add(new Integer(3));
listobject.add(new Integer(15));
listobject.add(new Integer(12));
Iterator Itr = listobject.iterator();
Collections.reverse(listobject);
Collections.shuffle(listobject);
Itr.next();
Itr.remove();
while(Itr.hasNext())
System.out.print(Itr.next() + " ");
}
}

A. 12 10 15
B. 10 3 15 12
C. 3 15 12 10
D. 3 10 12 15
E. None of these
Answer» B. 10 3 15 12
26.

What is the output of this program?
import java.util.*;
public class stack_Example
{
public static void main(String args[])
{
Stack object = new Stack();
object.push(new Integer(20));
object.push(new Integer(12));
object.pop();
object.push(new Integer(60));
System.out.println(object);
}
}

A. [20, 60]
B. [20, 12, 60]
C. [20, 12]
D. [12, 60]
E. None of these
Answer» B. [20, 12, 60]
27.

What is the output of this program?
import java.util.*;
public class vector_Example
{
public static void main(String args[])
{
Vector object = new Vector(6,3);
object.addElement(new Integer(10));
object.addElement(new Integer(20));
object.addElement(new Integer(50));
object.removeAll(object);
System.out.println(object.isEmpty());
}
}

A. True
B. False
C. 0
D. 1
E. None of these
Answer» B. False
28.

What is the output of this program?
import java.util.*;
public class vector_Example
{
public static void main(String args[])
{
Vector object = new Vector(6,3);
object.addElement(new Integer(10));
object.addElement(new Integer(20));
object.addElement(new Integer(60));
object.insertElementAt(new Integer(80), 1);
System.out.println(object);
}
}

A. [10, 20, 60, 80]
B. [10, 20, 60] 4
C. [10, 20, 60]
D. [20, 60, 80]
E. [10, 80, 20, 60]
Answer» F.
29.

What is the output of this program?
import java.util.*;
public class vector
{
public static void main(String args[])
{
Vector object = new Vector(6,3);
object.addElement(new Integer(9));
object.addElement(new Integer(7));
object.addElement(new Integer(3));
System.out.println(object.capacity());
}
}

A. 3
B. 6
C. 9
D. 7
E. None of these
Answer» C. 9
30.

What is the output of this program?
import java.util.*;
public class HashSet_Example
{
public static void main(String args[])
{
HashSet object = new HashSet();
object.add("I");
object.add("L");
object.add("U");
System.out.println(object + " " + object.size());
}
}

A. [U, I, L]
B. [I, L, U] 3
C. [I, L, U]
D. [U, I, L] 3
E. None of these
Answer» E. None of these
31.

What is the output of this program?
import java.util.*;
public class Linkedlist_Example
{
public static void main(String args[])
{
LinkedList object = new LinkedList();
object.add("I");
object.add("L");
object.add("U");
object.removeFirst();
System.out.println(object);
}
}

A. [I, L, U]
B. [I, L]
C. [L, U]
D. [I, U]
E. None of these
Answer» D. [I, U]
32.

What is the output of this program?
import java.util.*;
public class Linkedlist_Example
{
public static void main(String args[])
{
LinkedList object = new LinkedList();
object.add("O");
object.add("V");
object.add("E");
object.addFirst("L");
System.out.println(object);
}
}

A. [L]
B. [L, O]
C. [L, O, V]
D. [L, O, V, E]
E. None of these
Answer» E. None of these
33.

What is the output of this program?
import java.util.*;
public class Output
{
public static void main(String args[])
{
ArrayList object = new ArrayList();
object.add("Y");
object.add("O");
object.add("U");
object.ensureCapacity(3);
object.trimToSize();
System.out.println(object.size());
}
}

A. 0
B. 1
C. 2
D. 3
E. None of these
Answer» E. None of these
34.

What is the output of this program?
import java.util.*;
public class Result
{
public static void main(String args[])
{
ArrayList object = new ArrayList();
object.add("Y");
object.add("O");
object.add("U");
object.ensureCapacity(5);
System.out.println(object.size());
}
}

A. 3
B. 2
C. 6
D. 0
E. Any Garbage Value
Answer» B. 2
35.

What is the output of this program?
 import java.util.*;
public class Result
{
public static void main(String args[])
{
ArrayList object = new ArrayList();
object.add("Y");
object.add("O");
object.add("U");
System.out.println(object.size());
}
}

A. Y
B. O
C. U
D. 3
E. None of these
Answer» E. None of these
36.

What is the output of this program?
import java.util.*;
public class Arraylist_Example
{
public static void main(String args[])
{
ArrayList object = new ArrayList();
object.add("L");
object.add("V");
object.add("E");
object.add(1, "O");
System.out.println(object);
}
}

A. [L, O]
B. [O]
C. [L, O, V, E]
D. [L, O, V]
E. None of these
Answer» D. [L, O, V]
37.

What is the output of this program?
import java.util.*;
public class Collection_AlgorithmsExample
{
public static void main(String args[])
{
LinkedList listObject = new LinkedList();
listObject.add(new Integer(2));
listObject.add(new Integer(8));
listObject.add(new Integer(5));
listObject.add(new Integer(1));
Iterator Itr = listObject.iterator();
while(Itr.hasNext())
System.out.print(Itr.next() + " ");
}
}

A. 25 28 52 31
B. 55 31 28 25
C. 31 55 28 25
D. 28 25 55 31
E. None of these
Answer» B. 55 31 28 25
38.

Which of these is the interface of legacy is implemented by Hashtable and Dictionary classes?

A. Hashtable
B. HashMap
C. Enumeration
D. Map
E. None of these
Answer» E. None of these
39.

Which of this interface is not a part of Java s collection framework?

A. SortedMap
B. List
C. SortedList
D. Set
E. None of these
Answer» D. Set
40.

Which of these classes is not part of Java s collection framework?

A. Stack
B. Queue
C. Array
D. Maps
E. None of these
Answer» E. None of these