Final MCQ For Exam
Final MCQ For Exam
Answer: Option A
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));
System.out.print(obj.contains(new Integer(5)));
}
}
A. 0
B. 1
C. true
D. false
Answer: Option D
import java.util.*;
class Arraylist
{
public static void main(String args[])
{
ArrayList obj = new ArrayList();
obj.add("A");
obj.add("B");
obj.add("C");
obj.add(1, "D");
System.out.println(obj);
}
}
A. [A, B, C, D]
B. [A, D, B, C]
C. [A, D, C]
D. [A, B, C]
Answer: Option B
Q:-66 What will be the output of the following Java 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: Option D
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));
System.out.print(obj.toString());
}
}
A. {C=8, B=2}
B. [C=8, B=2]
C. {A=3, C=8, B=2}
D. [A=3, C=8, B=2]
Answer: Option C
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: Option A
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: Option C
Q:-76 What will be the output of the following Java 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: Option D
Q:-77 Which of these class object uses the key to store value?
A. Dictionary
B. Map
C. Hashtable
D. All of the mentioned
Answer: Option D
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);
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: Option B
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. 3
C. 2
D. 5
Answer: Option C
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: Option A
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: Option D
import java.lang.reflect.*;
class Additional_packages
{
public static void main(String args[])
{
try
{
Class c = Class.forName("java.awt.Dimension");
Method methods[] = c.getMethods();
for (int i = 0; i < methods.length; i++)
System.out.println(methods[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: Option B
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: Option D
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: Option C
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: Option A
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: Option D
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: Option C
Q:-106 What will be the output of the following Java code?
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: Option A
116 What are the initial capacity and load factor of HashSet?
A. 10, 1.0
B. 32, 0.75
C. 16, 0.75
D. 32, 1.0
Answer: Option C
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();
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: Option A
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: Option B
System.out.println(sampleMap);
}
}
A. {1=null, 2=null, 3=null, 4=null, 5=null}
B. {5=null}
C. Exception is thrown
D. {1=null, 5=null, 3=null, 2=null, 4=null}
Answer: Option A
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: Option C
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: Option A
130 What will be the output of the following Java 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: Option A
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: Option B
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: Option C
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.remove(new String("A"));
System.out.print(obj);
}
}
A. {C=8, B=2}
B. [C=8, B=2]
C. {A=3, C=8, B=2}
D. [A=3, C=8, B=2]
Answer: Option A
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.shuffle(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. Any random order
Answer: Option D
145 What will be the output of the following Java code?
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: Option D
import java.util.*;
class properties
{
public static void main(String args[])
{
Properties obj = new Properties();
obj.put("AB", new Integer(3));
obj.put("BC", new Integer(2));
obj.put("CD", new Integer(8));
System.out.print(obj.keySet());
}
}
A. {AB, BC, CD}
B. [AB, BC, CD]
C. [3, 2, 8]
D. {3, 2, 8}
Answer: Option B
150 Which of the below does not implement Map interface?
A. HashMap
B. Hashtable
C. EnumMap
D. Vector
Answer: Option D
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);
System.out.print(Arrays.binarySearch(array, 4));
}
}
A. 2
B. 3
C. 4
D. 5
Answer: Option B
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: Option B
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);
Collections.shuffle(list);
i.next();
i.remove();
while(i.hasNext())
System.out.print(i.next() + " ");
}
}
A. 2 8 5
B. 2 1 8
C. 2 5 8
D. 8 5 1
Answer: Option B
import java.util.*;
class Output
{
public static void main(String args[])
{
ArrayList obj = new ArrayList();
obj.add("A");
obj.add(0, "B");
System.out.println(obj.size());
}
}
A. 0
B. 1
C. 2
D. Any Garbage Value
Answer: Option C
MCQ on Packages
a) A collection of classes
b) A collection of interfaces
a) import
b) package
c) namespace
d) module
Answer: b) package
a) include packageName;
b) using packageName;
c) import packageName;
d) use packageName;
Answer: c) import packageName;
a) Yes
b) No
Answer: a) Yes
a) java.util
b) java.io
c) java.lang
d) java.math
Answer: c) java.lang
a) import packageName;
b) import packageName.*;
c) use packageName.*;
d) include packageName.all;
Answer: b) import packageName.*;
a) java.io
b) java.util
c) java.lang
d) java.net
Answer: b) java.util
177. Which package contains classes for input and output operations?
a) java.io
b) java.util
c) java.lang
d) java.math
Answer: a) java.io
a) package packageName;
c) namespace packageName;
d) module packageName;
Answer: a) package packageName;
179. Can two packages have classes with the same name?
a) Yes
b) No
Answer: a) Yes
180. Which of the following is not part of the Java standard library?
a) java.util
b) java.io
c) java.graphics
d) java.lang
Answer: c) java.graphics
181. How do you use a class from another package without importing it?
d) Handling networking
Answer: b) Handling database operations
a) Yes
b) No
Answer: a) Yes
a) java.util
b) java.io
c) java.net
d) java.nio
Answer: c) java.net
Multithreading MCQ
a) run()
b) execute()
c) start()
d) begin()
Answer: c) start()
a) 0
b) 1
c) 5
d) 10
Answer: c) 5
a) Cloneable
b) Runnable
c) Serializable
d) Comparable
Answer: b) Runnable
a) Running
b) Waiting
c) Blocked
d) Ready
Answer: c) Blocked
192. Which of these methods is used to make a thread wait until it is notified?
a) sleep()
b) join()
c) wait()
d) notify()
Answer: c) wait()
193. What will happen if we call the run() method directly instead of start()?
a) Thread
b) Runnable
c) Executor
d) Future
Answer: a) Thread
197. How do you force one thread to wait for another thread to finish in Java?
a) join()
b) sleep()
c) notify()
d) wait()
Answer: a) join()
a) isActive()
b) isRunning()
c) isAlive()
d) isWorking()
Answer: c) isAlive()
a) RuntimeException
b) InterruptedException
c) IllegalThreadStateException
d) None of the above
Answer: b) InterruptedException
a) Notifies all waiting threads but only one can acquire the lock
b) Wakes up all threads waiting on the object's monitor
c) Stops all running threads
d) Resumes the current thread
Answer: b) Wakes up all threads waiting on the object's monitor
a) Executors
b) Runnable
c) Thread
d) Callable
Answer: a) Executors
205. Which method is used to check if the current thread is a daemon thread?
a) isDaemon()
b) isAlive()
c) isRunning()
d) isThread()
Answer: a) isDaemon()
a) Thread
b) Object
c) Runnable
d) Executors
Answer: b) Object