Sail E0 Webinar

MCQs

Total Questions : 97 | Page 8 of 10 pages
Question 71.

Which of these interface must contain a unique element?


  1.    Set
  2.    List
  3.    Array
  4.    Collection
 Discuss Question
Answer: Option A. -> Set

Set interface extends collection interface to handle sets, which must contain 

unique elements.


Question 72.

Which of these methods can convert an object into a List?


  1.    SetList()
  2.    ConvertList()
  3.    singletonList()
  4.    CopyList()
 Discuss Question
Answer: Option C. -> singletonList()

singletonList() returns the object as an immutable List. This is a easy way to convert 

Question 73.

You need to store elements in a collection that guarantees that no duplicates are stored and 

all elements can be accessed in natural  order . Which interface provides that capability?


  1.    java.util.Map
  2.    java.util.Set
  3.    java.util.List
  4.    java.util.Collection
 Discuss Question
Answer: Option B. -> java.util.Set

Option B is correct. A set is a collection that contains no duplicate elements. The iterator 

returns the elements in no particular order (unless this set is an instance of some class 

that provides a guarantee). A map cannot contain duplicate keys but it may contain duplicate values. List and Collection allow duplicate elements.

Option A is wrong. A map is an object that maps keys to values. A map cannot contain duplicate 

keys; each key can map to at most one value. The Map interface provides three collection views,

 which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value 

mappings. The order of a map is defined as the order in which the iterators on the map's collection 

views return their elements. Some map implementations, like the TreeMap class, make specific

 guarantees as to their order (ascending key order); others, like the HashMap class, do not (does

 not guarantee that the order will remain constant over time).

Option C is wrong. A list is an ordered collection (also known as a sequence). The user of this 

interface has precise control over where in the list each element is inserted. The user can access

 elements by their integer index (position in the list), and search for elements in the list. Unlike sets,

 lists typically allow duplicate elements.

Option D is wrong. A collection is also known as a sequence. The user of this interface has precise

 control over where in the list each element is inserted. The user can access elements by their integer 

index (position in the list), and search for elements in the list. Unlike sets, lists typically allow duplicate 

elements.


Question 74.
class Bar { }
class Test
{
Bar doBar()
{
Bar b = new Bar(); /* Line 6 */
return b; /* Line 7 */
}
public static void main (String args[])
{
Test t = new Test(); /* Line 11 */
Bar newBar = t.doBar(); /* Line 12 */
System.out.println("newBar");
newBar = new Bar(); /* Line 14 */
System.out.println("finishing"); /* Line 15 */
}
}


At what point is the Bar object, created on line 6, eligible for garbage collection?


  1.    after line 12
  2.    after line 14
  3.    after line 7, when doBar() completes
  4.    after line 15, when main() completes
 Discuss Question
Answer: Option B. -> after line 14

Option B is correct. All references to the Bar object created on line 6 are destroyed when a 

new reference to a new Bar object is assigned to the variable newBar on line 14. Therefore 

the Bar object, created on line 6, is eligible for garbage collection after line 14.

Option A is wrong. This actually protects the object from garbage collection.

Option C is wrong. Because the reference in the doBar() method is returned on line 7 and is stored 

in newBar on line 12. This preserver the object created on line 6.

Option D is wrong. Not applicable because the object is eligible for garbage collection after line 14.


Question 75.

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


  1.    List
  2.    Set
  3.    SortedMap
  4.    SortedList
 Discuss Question
Answer: Option D. -> SortedList

SortedList is not a part of collection framework.


Question 76.

Which of these method of Object class is used to obtain class of an object at run time?


  1.    get()
  2.    void getclass()
  3.    Class getclass()
  4.    None of the mentioned
 Discuss Question
Answer: Option C. -> Class getclass()

None.


Question 77.

Which collection class allows you to grow or shrink its size and provides indexed access to its 

elements, but whose methods are not synchronized?


  1.    java.util.HashSet
  2.    java.util.LinkedHashSet
  3.    java.util.List
  4.    java.util.ArrayList
 Discuss Question
Answer: Option D. -> java.util.ArrayList

All of the collection classes allow you to grow or shrink the size of your collection. ArrayList 

provides an index to its elements. The newer collection classes tend not to have synchronized

 methods. Vector is an older implementation of ArrayList functionality and has synchronized

 methods; it is slower than ArrayList.


Question 78.

Which of the following is a method having same name as that of its class?


  1.    finalize
  2.    delete
  3.    class
  4.    constructor
 Discuss Question
Answer: Option D. -> constructor

A constructor is a method that initializes an object immediately upon creation. It has the same 

name as that of class in which it resides.


Question 79.

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


  1.    List
  2.    Set
  3.    SortedMap
  4.    SortedList
 Discuss Question
Answer: Option D. -> SortedList

SortedList is not a part of collection framework.


Question 80.

Which of the following is a valid declaration of an object of class Box?


  1.    Box obj = new Box();
  2.    Box obj = new Box;
  3.    obj = new Box();
  4.    new Box obj;
 Discuss Question
Answer: Option A. -> Box obj = new Box();

None.


Latest Videos

Latest Test Papers