Java MCQ Number 01114

Java MCQ Set 1

1. Which of the following is not a segment of memory in java?
a) Stack Segment
b) Heap Segment
c) Code Segment
d) Register Segment

Answer

Answer: d [Reason:] There are only 3 type of memory segment. Stack Segment, Heap Segment and Code Segment.

2. Does code Segment loads the java code?
a) True
b) False

Answer

Answer: a [Reason:] Code Segment loads compiled java bytecode.Byte code is platform independent.

3. What is JVM?
a) Bootstrap
b) Interpretor
c) Extension
d) Compiler

Answer

Answer: b [Reason:] JVM is interpretor. It reads .class files which is the byte code generated by compiler line by line and converts it into native OS code.

4. Which one of the following is a class loader?
a) Bootstrap
b) Compiler
c) Heap
d) Interpretor

Answer

Answer: a [Reason:] Bootstrap is a class loader. It loads the classes into memory.

5. Which class loader loads jar files from JDK directory?
a) Bootstrap
b) Extension
c) System
d) Heap

Answer

Answer: b [Reason:] Extension loads jar files from lib/ext directory of the JRE.This gives the basic functionality available.

6. Which of the following is not a memory classification in java?
a) Young
b) Old
c) Permanent
d) Temporary

Answer

Answer: d [Reason:] Young generation is further classified into Eden space and Survivor space. Old generation is also the tenured space. Permanent generation is the non heap space.

7. What is the java 8 update of PermGen?
a) Code Cache
b) Tenured Space
c) Metaspace
d) Eden space

Answer

Answer: c [Reason:] Metaspace is the replacement of PermGen in java 8. It is very similar to PermGen except that it resizes itself dynamically. Thus, it is unbounded.

8. Classes and Methods are stored in which space?
a) Eden space
b) Survivor space
c) Tenured space
d) Permanent space

Answer

Answer: d [Reason:] The permanent generation holds objects which JVM finds convenient to have the garbage collector. Objects describing classes and methods, as well as the classes and methods themselves are a part of Permanent generation.

9. Where is String Pool stored?
a) Java Stack
b) Java Heap
c) Permanent Generation
d) Metaspace

Answer

Answer: b [Reason:] When a string is created ; if the string already exists in the pool, the reference of the existing string will be returned, else a new object is created and its reference is returned.

10. The same import package/class be called twice in java?
a) True
b) False

Answer

Answer: a [Reason:] We can import the same package or same class multiple times. Neither compiler nor JVM complains will complain about it. JVM will internally load the class only once no matter how many times we import the same class or package.

Java MCQ Set 2

1. Which of these standard collection classes implements a linked list data structure?
a) AbstractList
b) LinkedList
c) HashSet
d) AbstractSet

Answer

Answer: b [Reason:] None.

2. Which of these classes implements Set interface?
a) ArrayList
b) HashSet
c) LinkedList
d) DynamicList

Answer

Answer: b [Reason:] HashSet and TreeSet implements Set interface where as LinkedList and ArrayList implements List interface.

3. Which of these method is used to add an element to the start of a LinkedList object?
a) add()
b) first()
c) AddFirst()
d) addFirst()

Answer

Answer: d [Reason:] None.

4. Which of these method of HashSet class is used to add elements to its object?
a) add()
b) Add()
c) addFirst()
d) insert()

Answer

Answer: a [Reason:] None.

5. Which of these methods can be used to delete the last element in a LinkedList object?
a) remove()
b) delete()
c) removeLast()
d) deleteLast()

Answer

Answer: c [Reason:] removeLast() and removeFirst() methods are used to remove elements in end and beginning of a linked list.

6. Which of these method is used to change an element in a LinkedList Object?
a) change()
b) set()
c) redo()
d) add()

Answer

Answer: b [Reason:] An element in a LinkedList object can be changed by first using get() to obtain the index or location of that object and the passing that location to method set() along with its new value.

7. What is the output of this program?

  1.     import java.util.*;
  2.     class Linkedlist 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList obj = new LinkedList();
  7.             obj.add("A");
  8.             obj.add("B");
  9.             obj.add("C");
  10.             obj.addFirst("D");
  11.             System.out.println(obj);
  12.         }
  13.     }

a) [A, B, C].
b) [D, B, C].
c) [A, B, C, D].
d) [D, A, B, C].

Answer

Answer: d [Reason:] obj.addFirst(“D”) method is used to add ‘D’ to the start of a LinkedList object obj.
Output:
$ javac Linkedlist.java
$ java Linkedlist
[D, A, B, C].

8. What is the output of this program?

  1.     import java.util.*;
  2.     class Linkedlist 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList obj = new LinkedList();
  7.             obj.add("A");
  8.             obj.add("B");
  9.             obj.add("C");
  10.             obj.removeFirst();
  11.             System.out.println(obj);
  12.         }
  13.     }

a) [A, B].
b) [B, C].
c) [A, B, C, D].
d) [A, B, C].

Answer

Answer: b [Reason:] None.
Output:
$ javac Linkedlist.java
$ java Linkedlist
[B, C]

9. What is the output of this program?

  1.     import java.util.*;
  2.     class Output 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             HashSet obj = new HashSet();
  7.             obj.add("A");
  8.             obj.add("B");
  9.             obj.add("C");
  10.             System.out.println(obj + " " + obj.size());
  11.         }
  12.     }

a) ABC 3
b) [A, B, C] 3
c) ABC 2
d) [A, B, C] 2

Answer

Answer: b [Reason:] HashSet obj creates an hash object which implements Set interface, obj.size() gives the number of elements stored in the object obj which in this case is 3.
Output:
$ javac Output.java
$ java Output
[A, B, C] 3

10. What is the output of this program?

  1.     import java.util.*; 
  2.     class Output 
  3.     {
  4.         public static void main(String args[]) 
  5.         { 
  6.             TreeSet t = new TreeSet();
  7.             t.add("3");
  8.             t.add("9");
  9.             t.add("1");
  10.             t.add("4");
  11.             t.add("8"); 
  12.             System.out.println(t);
  13.         }
  14.     }

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

Answer: d [Reason:]TreeSet class uses set to store the values added by function add in ascending order using tree for storage
Output:
$ javac Output.java
$ java Output
[1, 3, 4, 8, 9].

Java MCQ Set 3

1. Which of interface contains all the methods used for handling thread related operations in Java?
a) Runnable interface
b) Math interface
c) System interface
d) ThreadHandling interface

Answer

Answer: a [Reason:] Runnable interface defines all the methods for handling thread operations in Java.

2. Which of these class is used to make a thread?
a) String
b) System
c) Thread
d) Runnable

Answer

Answer: c [Reason:] Thread class is used to make threads in java, Thread encapsulates a thread of execution. To create a new thread the program will either extend Thread or implement the Runnable interface.

3. Which of these interface is implemented by Thread class?
a) Runnable
b) Connections
c) Set
d) MapConnections

Answer

Answer: a [Reason:] None.

4. Which of these method of Thread class is used to suspend a thread for a period of time?
a) sleep()
b) terminate()
c) suspend()
d) stop()

Answer

Answer: a [Reason:] None.

5. toRadian() and toDegree() methods were added by which version of Java?
a) Java 1.0
b) Java 1.5
c) Java 2.0
d) Java 3.0

Answer

Answer: c [Reason:] toRadian() and toDegree() methods were added by Java 2.0 before that there was no method which could directly convert degree into radians and vice versa.

6. What is the output of this program?

  1.     class newthread implements Runnable 
  2.     {
  3. Thread t1,t2;
  4.         newthread() 
  5.         {
  6.             t1 = new Thread(this,"Thread_1");
  7.             t2 = new Thread(this,"Thread_2");
  8.             t1.start();
  9.             t2.start();
  10. }
  11. public void run() 
  12.         {
  13.             t2.setPriority(Thread.MAX_PRIORITY);
  14. 	    System.out.print(t1.equals(t2));
  15.         }    
  16.     }
  17.     class multithreaded_programing 
  18.     {
  19.         public static void main(String args[]) 
  20.         {
  21.             new newthread();        
  22.         }
  23.     }

a) true
b) false
c) truetrue
d) falsefalse

Answer

Answer: d [Reason:] Threads t1 & t2 are created by class newthread that is implementing runnable interface, hence both the threads are provided their own run() method specifying the actions to be taken. When constructor of newthread class is called first the run() method of t1 executes than the run method of t2 printing 2 times “false” as both the threads are not equal one is having different priority than other, hence falsefalse is printed.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse

7. What is the output of this program?

  1.     class newthread implements Runnable 
  2.     {
  3. Thread t;
  4.         newthread() 
  5.         {
  6.             t = new Thread(this,"New Thread");
  7.             t.start();
  8. }
  9. public void run()
  10.         {
  11.             t.setPriority(Thread.MAX_PRIORITY);
  12.             System.out.println(t);
  13. }
  14.     }
  15.     class multithreaded_programing 
  16.     {
  17.         public static void main(String args[]) 
  18.         {
  19.             new newthread();        
  20.         }
  21.     }

a) Thread[New Thread,0,main].
b) Thread[New Thread,1,main].
c) Thread[New Thread,5,main].
d) Thread[New Thread,10,main].

Answer

Answer: d [Reason:] Thread t has been made with default priority value 5 but in run method the priority has been explicitly changed to MAX_PRIORITY of class thread, that is 10 by code ‘t.setPriority(Thread.MAX_PRIORITY);’ using the setPriority function of thread t.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[New Thread,10,main]

8. What is the output of this program?

  1.     class newthread implements Runnable 
  2.     {
  3. Thread t;
  4.         newthread() 
  5.         {
  6.             t = new Thread(this,"My Thread");
  7.             t.start();
  8. }
  9.     }
  10.     class multithreaded_programing 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             new newthread();        
  15.         }
  16.     }

a) My Thread
b) Thread[My Thread,5,main].
c) Compilation Error
d) Runtime Error

Answer

Answer: c [Reason:] Thread t has been made by using Runnable interface, hence it is necessary to use inherited abstract method run() method to specify instructions to be implemented on the thread, since no run() method is used it gives a compilation error.
Output:
$ javac multithreaded_programing.java
The type newthread must implement the inherited abstract method Runnable.run()

9. What is the output of this program?

  1.     class newthread implements Runnable 
  2.     {
  3. Thread t;
  4.         newthread() 
  5.         {
  6.             t = new Thread(this,"My Thread");
  7.             t.start();
  8. }
  9. public void run() 
  10.         {
  11. 	    System.out.println(t.getName());
  12. }
  13.     }
  14.     class multithreaded_programing 
  15.     {
  16.         public static void main(String args[]) 
  17.         {
  18.             new newthread();        
  19.         }
  20.     }

a) My Thread
b) Thread[My Thread,5,main].
c) Compilation Error
d) Runtime Error

Answer

Answer: a [Reason:] None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
My Thread

10. What is the output of this program?

  1.     class newthread implements Runnable 
  2.     {
  3. Thread t;
  4.         newthread() 
  5.         {
  6.             t = new Thread(this,"My Thread");
  7.             t.start();
  8. }
  9. public void run() 
  10.         {
  11. 	    System.out.println(t);
  12. }
  13.     }
  14.     class multithreaded_programing 
  15.     {
  16.         public static void main(String args[]) 
  17.         {
  18.             new newthread();        
  19.         }
  20.     }

a) My Thread
b) Thread[My Thread,5,main].
c) Compilation Error
d) Runtime Error

Answer

Answer: b [Reason:] None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[My Thread,5,main]

Java MCQ Set 4

1. What requires less resources?
a) Thread
b) Process
c) Thread and Process
d) Neither Thread nor Process

Answer

Answer: a [Reason:] Thread is a lightweight and requires less resources to create and exist in the process. Thread shares the process resources.

2. What does not prevent JVM from terminating?
a) Process
b) Daemon Thread
c) User Thread
d) JVM Thread

Answer

Answer: b [Reason:] Daemon thread runs in the background and does not prevent JVM from terminating. Child of daemon thread is also daemon thread.

3. What decides thread priority?
a) Process
b) Process schdduler
c) Thread
d) Thread scheduler

Answer

Answer: d [Reason:] Thread scheduler decides the priority of the thread execution. This cannot guarantee that higher priority thread will be executed first, it depends on thread scheduler implementation that is OS dependent.

4. What is true about time slicing?
a) Time slicing is OS service that allocates CPU time to available runnable thread
b) Time slicing is the process to divide the available CPU time to available runnable thread
c) Time slicing depends on its implementation in OS
d) Time slicing allocates more resources to thread

Answer

Answer: b [Reason:] Time slicing is the process to divide the available CPU time to available runnable thread.

5. Deadlock is a situation when thread is waiting for other thread to release acquired object?
a) True
b) False

Answer

Answer: a [Reason:] Deadlock is java programming situation where one thread waits for an object lock that is acquired by other thread and vice-versa.

6. What should not be done to avoid deadlock?
a) Avoid using multiple threads
b) Avoid hold several locks at once
c) Execute foreign code while holding a lock
d) Use interruptible locks

Answer

Answer: c [Reason:] To avoid deadlock situation in Java programming do not execute foreign code while holding a lock.

7. What is true about threading?
a) run() method calls start() method and runs the code
b) run() method creates new thread
c) run() method can be called directly without start() method being called
d) start() method creates new thread and calls code written in run() metho

Answer

Answer: d [Reason:] start() eventually calls run() method. Start() method creates thread and calls the code written inside run method.

8. Which of the following is correct constructor for thread?
a) Thread(Runnable a, String str)
b) Thread(int priority)
c) Thread(Runnable a, int priority)
d) Thread(Runnable a, ThreadGroup t)

Answer

Answer: a [Reason:] Thread(Runnable a, String str) is a valid constructor for thread. Thread() is also a valid constructor.

9. Which of the following stops execution of a thread?
a) Calling SetPriority() method on a Thread object
b) Calling notify() method on an object
c) Calling wait() method on an object
d) Calling read() method on an InputStream object

Answer

Answer: b [Reason:] notify() wakes up a single thread which is waiting for this object.

10. Which of the following will ensure the thread will be in running state?
a) yield()
b) notify()
c) wait()
d) Thread.killThread()

Answer

Answer: c [Reason:] wait() always causes the current thread to go into the object’s wait pool. Hence, using this in a thread will keep it in running state.

Java MCQ Set 5

1. Which class is used to generate random number?
a) java.lang.Object
b) java.util.randomNumber
c) java.util.Random
d) java.util.Object

Answer

Answer: c [Reason:] java.util.random class is used to generate random numbers in java program.

2. Which method is used to generate boolean random values in java?
a) nextBoolean()
b) randomBoolean()
c) previousBoolean()
d) generateBoolean()

Answer

Answer: a [Reason:] nextBoolean() method of java.util.Random class is used to generate random numbers.

3. What is the return type of Math.random() method?
a) Integer
b) Double
c) String
d) Boolean

Answer

Answer: b [Reason:] Math.random() method returns floating point number or precisely a double.

4. Random is a final class?
a) True
b) False

Answer

Answer: b [Reason:] Random is not a final class and can be extended to implement algorithm as per requirement.

5. What is the range of numbers returned by Math.random() method?
a) -1.0 to 1.0
b) -1 to 1
c) 0 to 100
d) 0.0 to 1.0

Answer

Answer: d [Reason:] Math.random() returns only double value greater than or equal to 0.0 and less than 1.0.

6. How many bits are used for generating random numbers?
a) 32
b) 64
c) 48
d) 8

Answer

Answer: c [Reason:] Random number can accept 64 bits but it only uses 48 bits for generating random numbers.

7. 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

Answer: a [Reason:] random.nextInt(15) + 1; returns random numbers between 1 to 15 including 1 and 15.

8. 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

Answer: d [Reason:] random.nextInd(7) + 4; returns random numbers between 4 to 10 including 4 and 10. it follows “nextInt(max – min +1) + min” formula.

9. Math.random() guarantees uniqueness?
a) True
b) False

Answer

Answer: b [Reason:] Math.random() doesn’t guarantee uniqueness. To guarantee uniqueness we must store the generated value in database and compare against already generated values.

10. What is the signature of Math.random() method?
a) public static double random()
b) public void double random()
c) public static int random()
d) public void int random()

Answer

Answer: a [Reason:] public static double random() is the utility method provided by Math class which returns double.

ed010d383e1f191bdb025d5985cc03fc?s=120&d=mm&r=g

DistPub Team

Distance Publisher (DistPub.com) provide project writing help from year 2007 and provide writing and editing help to hundreds student every year.