Java MCQ Number 01076

Java MCQ Set 1

1. Which of these object stores association between keys and values?
a) Hash table
b) Map
c) Array
d) String

Answer

Answer: b [Reason:] None.

2. Which of these classes provide implementation of map interface?
a) ArrayList
b) HashMap
c) LinkedList
d) DynamicList

Answer

Answer: b [Reason:] AbstractMap, WeakHashMap, HashMap and TreeMap provide implementation of map interface.

3. Which of these method is used to remove all keys/values pair from the invoking map?
a) delete()
b) remove()
c) clear()
d) removeAll()

Answer

Answer: b [Reason:] None.

4. Which of these method Map class is used to obtain an element in the map having specified key?
a) search()
b) get()
c) set()
d) look()

Answer

Answer: b [Reason:] None.

5. Which of these methods can be used to obtain set of all keys in a map?
a) getAll()
b) getKeys()
c) keyall()
d) keySet()

Answer

Answer: d [Reason:] keySet() methods is used to get a set containing all the keys used in a map. This method provides set view of the keys in the invoking map.

6. Which of these method is used add an element and corresponding key to a map?
a) put()
b) set()
c) redo()
d) add()

Answer

Answer: a [Reason:] Maps revolve around two basic operations – get() and put(). to put a value into a map, use put(), specifying the key and the value. To obtain a value, call get() , passing the key as an argument. The value is returned.

7. What is the output of this program?

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

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

Answer: d [Reason:] None.
Output:
$ javac Maps.java
$ java Maps
{A=1, B=2, C=3}

8. What is the output of this program?

  1.     import java.util.*;
  2.     class Maps 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             HashMap obj = new HashMap();
  7.             obj.put("A", new Integer(1));
  8.             obj.put("B", new Integer(2));
  9.             obj.put("C", new Integer(3));
  10.             System.out.println(obj.keySet());
  11.         }
  12.     }

a) [A, B, C].
b) {A, B, C}
c) {1, 2, 3}
d) [1, 2, 3].

Answer

Answer: a [Reason:] keySet() method returns a set containing all the keys used in the invoking map. Here keys are characters A, B & C. 1, 2, 3 are the values given to these keys.
Output:
$ javac Maps.java
$ java Maps
[A, B, C].

9. What is the output of this program?

  1.     import java.util.*;
  2.     class Maps 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             HashMap obj = new HashMap();
  7.             obj.put("A", new Integer(1));
  8.             obj.put("B", new Integer(2));
  9.             obj.put("C", new Integer(3));
  10.             System.out.println(obj.get("B"));
  11.         }
  12.     }

a) 1
b) 2
c) 3
d) null

Answer

Answer: b [Reason:] obj.get(“B”) method is used to obtain the value associated with key “B”, which is 2.
Output:
$ javac Maps.java
$ java Maps
2

10. What is the output of this program?

  1.     import java.util.*;
  2.     class Maps 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             TreeMap obj = new TreeMap();
  7.             obj.put("A", new Integer(1));
  8.             obj.put("B", new Integer(2));
  9.             obj.put("C", new Integer(3));
  10.             System.out.println(obj.entrySet());
  11.         }
  12.     }

a) [A, B, C].
b) [1, 2, 3].
c) {A=1, B=2, C=3}
d) [A=1, B=2, C=3].

Answer

Answer: d [Reason:] obj.entrySet() method is used to obtain a set that contains the entries in the map. This method provides set view of the invoking map.
Output:
$ javac Maps.java
$ java Maps
[A=1, B=2, C=3].

Java MCQ Set 2

1. What is multithreaded programming?
a) It’s a process in which two different processes run simultaneously
b) It’s a process in which two or more parts of same process run simultaneously
c) Its a process in which many different process are able to access same information
d) Its a process in which a single process can access information from many sources

Answer

Answer: b [Reason:] Multithreaded programming a process in which two or more parts of same process run simultaneously.

2. Which of these are types of multitasking?
a) Process based
b) Thread based
c) Process and Thread based
d) None of the mentioned

Answer

Answer: c [Reason:] There are two types of multitasking: Process based multitasking and Thread based multitasking.

3. Which of these packages contain all the Java’s built in exceptions?
a) java.io
b) java.util
c) java.lang
d) java.net

Answer

Answer: c [Reason:] None.

4. Thread priority in Java is?
a) Integer
b) Float
c) double
d) long

Answer

Answer: a [Reason:] Java assigns to each thread a priority that determines hoe that thread should be treated with respect to others. Thread priority are integers that specify relative priority of one thread to another.

5. What will happen if two thread of same priority are called to be processed simultaneously?
a) Any one will be executed first lexographically
b) Both of them will be executed simultaneously
c) None of them will be executed
d) It is dependent on the operating system

Answer

Answer: d [Reason:] In cases where two or more thread with same priority are competing for CPU cycles, different operating system handle this situation differently. Some execute them in time sliced manner some depending on the thread they call.

6. Which of these statements is incorrect?
a) By multithreading CPU’s idle time is minimized, and we can take maximum use of it
b) By multitasking CPU’s idle time is minimized, and we can take maximum use of it
c) Two thread in Java can have same priority
d) A thread can exist only in two states, running and blocked

Answer

Answer: d [Reason:] Thread exist in several states, a thread can be running, suspended, blocked, terminated & ready to run.

7. What is the output of this program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t);        
  7.         }
  8.     }

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

Answer

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

8. What is the priority of the thread in output of this program?

  1.     class multithreaded_programing 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t);        
  7.         }
  8.     }

a) 4
b) 5
c) 0
d) 1

Answer

Answer: b [Reason:] The output of program is Thread[main,5,main], in this priority assigned to the thread is 5. Its the default value. Since we have not named the thread they are named by the group to they belong i:e main method.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[main,5,main]

9. What is the name of the thread in output of this program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t);        
  7.         }
  8.     }

a) main
b) Thread
c) System
d) None of the mentioned

Answer

Answer: a [Reason:] The output of program is Thread[main,5,main], Since we have not explicitly named the thread they are named by the group to they belong i:e main method. Hence they are named ‘main’.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[main,5,main]

10. What is the name of the thread in output of this program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t.isAlive());        
  7.         }
  8.     }

a) 0
b) 1
c) true
d) false

Answer

Answer: c [Reason:] Thread t is seeded to currently program, hence when you run the program the thread becomes active & code ‘t.isAlive’ returns true.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
true

Java MCQ Set 3

1. Which of these is a bundle of information passed between machines?
a) Mime
b) Cache
c) Datagrams
d) DatagramSocket

Answer

Answer: c [Reason:] The Datagrams are the bundle of information passed between machines.

2. Which of these class is necessary to implement datagrams?
a) DatagramPacket
b) DatagramSocket
c) All of the mentioned
d) None of the mentioned

Answer

Answer: c [Reason:] None.

3. Which of these method of DatagramPacket is used to find the port number?
a) port()
b) getPort()
c) findPort()
d) recievePort()

Answer

Answer: b [Reason:] None.

4. Which of these method of DatagramPacket is used to obtain the byte array of data contained in a datagram?
a) getData()
b) getBytes()
c) getArray()
d) recieveBytes()

Answer

Answer: a [Reason:] None.

5. Which of these method of DatagramPacket is used to find the length of byte array?
a) getnumber()
b) length()
c) Length()
d) getLength()

Answer

Answer: d [Reason:] getLength returns the length of the valid data contained in the byte array that would be returned from the getData () method. This typically is not equal to length of whole byte array.

6. Which of these class must be used to send a datatgram packets over a connection?
a) InetAdress
b) DatagramPacket
c) DatagramSocket
d) All of the mentioned

Answer

Answer: d [Reason:] By using 5 classes we can send and receive data between client and server, these are InetAddress, Socket, ServerSocket, DatagramSocket, and DatagramPacket.

7. Which of these method of DatagramPacket class is used to find the destination address?
a) findAddress()
b) getAddress()
c) Address()
d) whois()

Answer

Answer: b [Reason:] None.

8. Which of these is a return type of getAddress() method of DatagramPacket class?
a) DatagramPacket
b) DatagramSocket
c) InetAddress
d) ServerSocket

Answer

Answer: c [Reason:] None.

9. Which API gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.
a) getSocketAddress()
b) getAddress()
c) address()
d) none of the mentioned

Answer

Answer: a [Reason:] getSocketAddress() is used to get the socket address

Java MCQ Set 4

1. Which of these keywords is used to define packages in Java?
a) pkg
b) Pkg
c) package
d) Package

Answer

Answer: c [Reason:] None.

2. Which of these is a mechanism for naming and visibility control of a class and its content?
a) Object
b) Packages
c) Interfaces
d) None of the Mentioned.

Answer

Answer: b [Reason:] Packages are both naming and visibility control mechanism. We can define a class inside a package which is not accessible by code outside the package.

3. Which of this access specifies can be used for a class so that its members can be accessed by a different class in the same package?
a) Public
b) Protected
c) No Modifier
d) All of the mentioned

Answer

Answer: d [Reason:] Either we can use public, protected or we can name the class without any specifier.

4. Which of these access specifiers can be used for a class so that it’s members can be accessed by a different class in the different package?
a) Public
b) Protected
c) Private
d) No Modifier

Answer

Answer: a [Reason:] None.

5. Which of the following is correct way of importing an entire package ‘pkg’?
a) import pkg.
b) Import pkg.
c) import pkg.*
d) Import pkg.*

Answer

Answer: c [Reason:] Operator * is used to import the entire package.

6. Which of the following is incorrect statement about packages?
a) Package defines a namespace in which classes are stored
b) A package can contain other package within it
c) Java uses file system directories to store packages
d) A package can be renamed without renaming the directory in which the classes are stored

Answer

Answer: d [Reason:] A package can be renamed only after renaming the directory in which the classes are stored.

7. Which of the following package stores all the standard java classes?
a) lang
b) java
c) util
d) java.packages

Answer

Answer: b [Reason:] None.

8. What is the output of this program?

  1.     package pkg;
  2.     class display 
  3.     {
  4.         int x;
  5.         void show() 
  6.         {
  7.             if (x > 1)
  8.                 System.out.print(x + " ");
  9.         }
  10.     }
  11.     class packages 
  12.     {
  13.         public static void main(String args[]) 
  14.         {
  15.             display[] arr=new display[3];
  16.             for(int i=0;i<3;i++)
  17.                 arr[i]=new display();
  18.             arr[0].x = 0;      
  19.             arr[1].x = 1;
  20.             arr[2].x = 2;
  21.             for (int i = 0; i < 3; ++i)
  22.                 arr[i].show();
  23.          }
  24.     }

Note : packages.class file is in directory pkg;
a) 0
b) 1
c) 2
d) 0 1 2

Answer

Answer: c [Reason:] None.
Output:
$ javac packages.java
$ java packages
2

9. What is the output of this program?

  1.     package pkg;
  2.     class output 
  3.     {
  4.         public static void main(String args[])
  5.         { 
  6.             StringBuffer s1 = new StringBuffer("Hello");
  7.             s1.setCharAt(1, x);
  8.             System.out.println(s1);
  9.         }
  10.     }

a) xello
b) xxxxx
c) Hxllo
d) Hexlo

Answer

Answer: c [Reason:] None.
Output:
$ javac output.java
$ java output
Hxllo

10. What is the output of this program?

  1.     package pkg;
  2.     class output 
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             StringBuffer s1 = new StringBuffer("Hello World");
  7.             s1.insert(6 , "Good ");
  8.             System.out.println(s1);
  9.         }
  10.    }

Note : Output.class file is not in directory pkg.
a) HelloGoodWorld
b) HellGoodoWorld
c) Compilation error
d) Runtime error

Answer

Answer: d [Reason:] Since output.class file is not in the directory pkg in which class output is defined, program will not be able to run.
output:
$ javac output.java
$ java output
can not find file output.class

Java MCQ Set 5

1. Which of these class produce objects with respect to geographical locations?
a) TimeZone
b) Locale
c) Date
d) SimpleTimeZone

Answer

Answer: b [Reason:] The Locale class isinstantiated to produce objects that each describe a geographical or cultural region.

2. Which of these methods is not a Locale class?
a) UK
b) US
c) INDIA
d) KOREA

Answer

Answer: c [Reason:] INDIA is not a Locale class.

3. Which of these class can generate pseudorandom numbers?
a) Locale
b) Rand
c) Random
d) None of the mentioned

Answer

Answer: c [Reason:] None.

4. Which of these method of Locale class can be used to obtain country of operation?
a) getCountry()
b) whichCountry()
c) DisplayCountry()
d) getDisplayCountry()

Answer

Answer: d [Reason:] None.

5. Which of these is a method can generate a boolean output?
a) retbool()
b) getBool()
c) nextBool()
d) nextBoolean()

Answer

Answer: d [Reason:] None.

6. Which of these exceptions is thrown by remover() method?
a) IOException
b) SystemException
c) ObjectNotFoundExeception
d) IllegalStateException

Answer

Answer: d [Reason:] None.

7. What is the output of this program?

  1.     import java.util.*;
  2.     class LOCALE_CLASS
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             Locale obj = new Locale("INDIA") ;
  7.             System.out.print(obj.getCountry());
  8.         }
  9.     }

a) India
b) INDIA
c) Compilation Error
d) Nothing is displayed

Answer

Answer: d [Reason:] None.
Output:
$ javac LOCALE_CLASS.java
$ java LOCALE_CLASS

8. What is the output of this program?

  1.     import java.util.*;
  2.     class LOCALE_CLASS
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             Locale obj = new Locale("HINDI", "INDIA") ;
  7.             System.out.print(obj.getCountry());
  8.         }
  9.     }

a) India
b) INDIA
c) Compilation Error
d) Nothing is displayed

Answer

Answer: b [Reason:] None.
Output:
$ javac LOCALE_CLASS.java
$ java LOCALE_CLASS
INDIA

9. What is the output of this program?

  1.     import java.util.*;
  2.     class LOCALE_CLASS
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             Locale obj = new Locale("HINDI") ;
  7.             System.out.print(obj.getDisplayLanguage());
  8.         }
  9.     }

a) India
b) INDIA
c) HINDI
d) Nothing is displayed

Answer

Answer: c [Reason:] None.
Output:
$ javac LOCALE_CLASS.java
$ java LOCALE_CLASS
HINDI

10. What is the output of this program?

  1.     import java.util.*;
  2.     class LOCALE_CLASS
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             Locale obj = new Locale("HINDI", "INDIA") ;
  7.             System.out.print(obj.getDisplayLanguage());
  8.         }
  9.     }

a) India
b) INDIA
c) HINDI
d) Nothing is displayed

Answer

Answer: c [Reason:] None.
Output:
$ javac LOCALE_CLASS.java
$ java LOCALE_CLASS
HINDI

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.