Java Collections Framework Past Paper PDF

Document Details

LighterResilience692

Uploaded by LighterResilience692

Tags

java programming collections framework data structures computer science

Summary

This document is a past paper containing questions and answers related to the Java Collections Framework. Topics covered include Lists, Sets, Maps, and Queues. The document is suitable for undergraduate-level computer science students.

Full Transcript

Q:-1 What is the purpose of the Collections Framework? A.To provide a unified architecture for representing and manipulating collections B.To provide a way to create arrays C.To define primitive data types D.To handle exceptions Ans:-A. 2:-Which interface in the Java Collections Framework represent...

Q:-1 What is the purpose of the Collections Framework? A.To provide a unified architecture for representing and manipulating collections B.To provide a way to create arrays C.To define primitive data types D.To handle exceptions Ans:-A. 2:-Which interface in the Java Collections Framework represents an ordered collection of elements that can contain duplicate values? A.`Collection` B.`Map` C.`Set` D.`List` Ans:-D Q:-3 What is the primary difference between a `List` and a `Set` in the Java Collections Framework? A. A `Set` allows duplicate elements, while a `List` does not B. A `List` is unmodifiable, while a `Set` is modifiable C. A `List` allows duplicate elements, while a `Set` does not D. A `Set` is indexed, while a `List` is not Answer: Option C Q:-4 Which class is typically used to implement a `List` in the Java Collections Framework? A. `HashMap` B. `ArrayList` C. `LinkedList` D. None of These Answer: Option B Q:-5 In the Java Collections Framework, which interface extends the `List` interface to provide more efficient insertions and deletions in the middle of the list? A. `Map` B. `Set` C. `Deque` D. `Queue` Answer: Option C Q:-6 What is the purpose of the `Map` interface in the Java Collections Framework? A. To store a collection of objects B. To provide a stack data structure C. To store unique elements D. To store key-value pairs Answer: Option D Q:-7 Which class is typically used to implement a `Map` in the Java Collections Framework? A. `HashMap` B. `ArrayList` C. `HashSet` D. `LinkedList` Answer: Option A Q:- 8 What is the primary difference between a `HashMap` and a `TreeMap` in the Java Collections Framework? A. A `TreeMap` uses a linked list to store elements B. A `HashMap` is synchronized, while a `TreeMap` is not C. A `TreeMap` allows duplicate keys, while a `HashMap` does not D. A `HashMap` does not maintain order, while a `TreeMap` does Answer: Option D Q:-9 In the Java Collections Framework, which interface represents a collection of unique elements with no duplicates? A. `List` B. `Map` C. `Set` D. `Collection` Answer: Option C Q:-10 Which class is typically used to implement a `Set` in the Java Collections Framework? A. `HashMap` B. `HashSet` C. `LinkedList` D. None of These Answer: Option B Q:-11 What is the primary difference between a `HashSet` and a `LinkedHashSet` in the Java Collections Framework? A. A `HashSet` uses a tree structure to store elements B. A `LinkedHashSet` is synchronized, while a `HashSet` is not C. A `LinkedHashSet` maintains insertion order, while a `HashSet` does not D. A `HashSet` allows duplicate elements, while a `LinkedHashSet` does not Answer: Option C Q:-12 Which interface in the Java Collections Framework represents a collection of elements with no specific order? A. `List` B. `Map` C. `Collection` D. `Set` Answer: Option D Q:-13 What is the primary difference between a `Queue` and a `Deque` in the Java Collections Framework? A. A `Queue` represents a single-ended queue, while a `Deque` represents a double-ended queue B. A `Queue` allows duplicate elements, while a `Deque` does not C. A `Deque` is synchronized, while a `Queue` is not D. A `Queue` is indexed, while a `Deque` is not Answer: Option A Q:-14 Which class is typically used to implement a `Deque` in the Java Collections Framework? A. `LinkedList` B. `HashMap` C. `HashSet` D. `ArrayDeque` Answer: Option D Q:-15 In the Java Collections Framework, which class allows you to create a synchronized (thread-safe) collection? A. `ArrayList` B. `HashMap` C. `Collections` D. `HashSet` Answer: Option C Q:- 16 Which interface in the Java Collections Framework extends the `Set` interface and represents a collection of elements stored in a sorted order? A. `Map` B. `SortedSet` C. `Queue` D. None of These Answer: Option B Q:-17 What is the purpose of the `Comparator` interface in Java Collections Framework? A. It represents a generic collection B. It provides methods for hashing elements C. It defines a comparison function for ordering elements D. It represents a synchronized collection Answer: Option C Q:-18 Which class is typically used to implement a `SortedSet` in the Java Collections Framework? A. `HashSet` B. `LinkedHashSet` C. `ArrayList` D. `TreeSet` Answer: Option D Q:-19 In the Java Collections Framework, which interface represents a last-in, first-out (LIFO) stack of elements? A. `Stack` B. `Queue` C. `List` D. `Deque` Answer: Option A Q:-20 What is the primary difference between a `Stack` and a `Queue` in the Java Collections Framework? A. A `Queue` allows duplicate elements, while a `Stack` does not B. A `Stack` allows duplicate elements, while a `Queue` does not C. A `Queue` uses a LIFO order, while a `Stack` uses a FIFO order D. A `Stack` uses a LIFO (Last-In-First-Out) order, while a `Queue` uses a FIFO (First-In-First-Out) order Answer: Option D Q:-21 In Java, which method is used to add an element to a collection in the Java Collections Framework? A. `insert()` B. `put()` C. `add()` D. `append()` Answer: Option C Q:-22 What is the purpose of the `toArray()` method in the Java Collections Framework? A. It sorts the collection B. It converts a collection to an array C. It reverses the collection D. None of These Answer: Option B Q:-23 In the Java Collections Framework, which class provides methods to manipulate the size of a list and its elements? A. `Set` B. `ArrayList` C. `Collections` D. `List` Answer: Option C Q:-24 What is the purpose of the `Iterator` interface in the Java Collections Framework? A. It defines a comparison function for ordering elements B. It represents a synchronized collection C. It provides methods for hashing elements D. It provides a way to iterate over a collection's elements Answer: Option D Q:-25 Which method is used to remove all elements from a collection in the Java Collections Framework? A. `clear()` B. `removeAll()` C. `erase()` D. `purge()` Answer: Option A Q:-26 What is the purpose of the `Map.Entry` interface in the Java Collections Framework? A. It provides methods for hashing elements B. It represents a synchronized collection C. It defines a comparison function for ordering elements D. It represents a key-value pair in a map Answer: Option D Q:-27 In the Java Collections Framework, which method is used to obtain a synchronized (thread-safe) version of a collection? A. `synchronize()` B. `synchronizedList()` C. `Collections.synchronizedCollection()` D. `makeSynchronized()` Answer: Option C Q:-28 What is the primary difference between a `HashSet` and a `LinkedHashSet` in the Java Collections Framework? A. A `LinkedHashSet` allows duplicate elements, while a `HashSet` does not B. A `LinkedHashSet` maintains the insertion order of elements, while a `HashSet` does not C. A `HashSet` allows duplicate elements, while a `LinkedHashSet` does not D. None of These Answer: Option B Q:-29 In Java, which method is used to obtain a synchronized (thread- safe) map from an existing map in the Java Collections Framework? A. `makeSynchronizedMap()` B. `synchronized()` C. `Collections.synchronizedMap()` D. `synchronizeMap()` Answer: Option C Q:-30 What is the purpose of the `Collections.reverse()` method in the Java Collections Framework? A. It removes all elements from a list B. It sorts a list in ascending order C. It adds elements to a list D. It reverses the order of elements in a list Answer: Option D Q:-31 Which of these method can be used to increase the capacity of ArrayList object manually? A. Capacity() B. increaseCapacity() C. increasecapacity() D. ensureCapacity() Answer: Option D Q:-32 What will be the output of the following Java code snippet? 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.addFirst("D"); System.out.println(obj); } } A. [A, B, C] B. [D, B, C] C. [A, B, C, D] D. [D, A, B, C] Answer: Option D Q:-33 What is Collection in Java? A. A group of objects B. A group of classes C. A group of interfaces D. None of the mentioned Answer: Option A Q:-34 Which of these methods can convert an object into a List? A. SetList() B. ConvertList() C. singletonList() D. CopyList() Answer: Option C Q:-35 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.get("B")); } } A. 1 B. 2 C. 3 D. null Answer: Option B Q:-36 What is the premise of equality for IdentityHashMap? A. Reference equality B. Name equality C. Hashcode equality D. Length equality Answer: Option A Q:-37 Which of these is static variable defined in Collections? A. EMPTY_SET B. EMPTY_LIST C. EMPTY_MAP D. All of the mentioned Answer: Option D Q:-38 Which of these methods is used to add elements in vector at specific location? A. add() B. set() C. AddElement() D. addElement() Answer: Option D Q:-39 What will be the output of the following Java program? 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.sort(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. 2 1 8 5 Answer: Option C Q:-40 What is the difference between Queue and Stack? A. Stack is LIFO; Queue is FIFO B. Queue is LIFO; Stack is FIFO C. Stack and Queue is FIFO D. Stack and Queue is LIFO Answer: Option A Q:-41 What will be the output of the following Java code? import java.util.*; class Bitset { public static void main(String args[]) { BitSet obj1 = new BitSet(5); BitSet obj2 = new BitSet(10); for (int i = 0; i < 5; ++i) obj1.set(i); for (int i = 3; i < 13; ++i) obj2.set(i); obj1.and(obj2); System.out.print(obj1); } } A. {0, 1} B. {2, 4} C. {3, 4} D. {3, 4, 5} Answer: Option C Q:-42 What are the use of front and rear pointers in CircularQueue implementation? A. Front pointer points to first element; rear pointer points to the last element B. Rear pointer points to first element; front pointer points to the last element C. Front and read pointers point to the first element D. Front pointer points to the first element; rear pointer points to null object Answer: Option C Q:-43 Which of the below is not a subinterface of Queue? A. BlockingQueue B. BlockingEnque C. TransferQueue D. None of These Answer: Option B Q:-44 Which of these method is used to reduce the capacity of an ArrayList object? A. trim() B. trimSize() C. trimTosize() D. trimToSize() Answer: Option D Q:-45 How to sort elements of ArrayList? A. Collection.sort(listObj); B. Collections.sort(listObj); C. listObj.sort(); D. Sorter.sortAsc(listObj); Answer: Option B Q:-46 Which of these packages contain all the collection classes? A. java.lang B. java.util C. java.net D. java.awt Answer: Option B Q:-47 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: Option D Q:-48 Which of these methods are member of Remote class? A. checkIP() B. addLocation() C. AddServer() D. None of the mentioned Answer: Option D Q:-49 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: Option B Q:-50 What is the name of a data member of class Vector which is used to store a number of elements in the vector? A. length B. elements C. elementCount D. capacity Answer: Option C Q:-51 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.keySet()); } } A. [A, B, C] B. {A, B, C} C. {1, 2, 3} D. [1, 2, 3] Answer: Option A Q:-52 What will be the output of the following Java code? import java.util.*; class date { public static void main(String args[]) { Date obj = new Date(); System.out.print(obj); } } A. Prints Present Date B. Runtime Error C. Any Garbage Value D. Prints Present Time & Date Answer: Option D Q:-53 What is the worst case complexity of accessing an element in ArrayList? A. O(n) B. O(1) C. O(nlogn) D. O(2) Answer: Option B Q:-54 Which of these standard collection classes implements a dynamic array? A. AbstractList B. LinkedList C. ArrayList D. AbstractSet Answer: Option C Q:-55 What is the correct method used to insert and delete items from the queue? A. push and pop B. enqueue and dequeue C. enqueue and peek D. add and remove Answer: Option B Q:-56 Which of these standard collection classes implements all the standard functions on list data structure? A. Array B. LinkedList C. HashSet D. AbstractSet Answer: Option A Q:-57 What happens if two threads simultaneously modify TreeSet? A. ConcurrentModificationException is thrown B. Both threads can perform action successfully C. FailFastException is thrown D. IteratorModificationException is thrown Answer: Option A Q:-59 What will be the output of the following Java code snippet? public class Test { public static void main(String[] args) { Set s = new HashSet(); s.add(new Long(10)); s.add(new Integer(10)); for(Object object : s) { System.out.println("test - "+object); } } } A. Test - 10 Test - 10 B. Test - 10 C. Runtime Exception D. Compilation Failure Answer: Option A Q:-60 Which of these is the interface of legacy? A. Map B. Enumeration C. HashMap D. Hashtable Answer: Option B Q:-61 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)); System.out.print(obj.contains(new Integer(5))); } } A. 0 B. 1 C. true D. false Answer: Option D Which of the below is not an implementation of List interface? A. RoleUnresolvedList B. Stack C. AttibuteList D. SessionList Answer: Option D Q:-62 While finding the correct location for saving key value pair, how many times the key is hashed? A. 1 B. 2 C. 3 D. unlimited till bucket is found Answer: Option B Q:-63 Which of these is a Basic interface that all other interface inherits? A. Set B. Array C. List D. Collection Answer: Option D Q:-64 Which of these is a method of ListIterator used to obtain index of previous element? A. previous() B. previousIndex() C. back() D. goBack() Answer: Option B Q:-65 What will be the output of the following Java program? 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 Q:-67 Which of these method is used to calculate number of bits required to hold the BitSet object? A. size() B. length() C. indexes() D. numberofBits() Answer: Option B Q:-68 Which of these Exceptions is thrown by remote method? A. RemoteException B. InputOutputException C. RemoteAccessException D. RemoteInputOutputException Answer: Option A Q:-69 Which of these class object has an architecture similar to that of array? A. Bitset B. Map C. Hashtable D. All of the mentioned Answer: Option A Q:-70 Which of these method of Array class is used sort an array or its subset? A. binarysort() B. bubblesort() C. sort() D. insert() Answer: Option C Q:-71 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)); 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 Q:-72 Which of these is the interface of legacy is implemented by Hashtable and Dictionary classes? A. Map B. Enumeration C. HashMap D. Hashtable Answer: Option A Q:-73 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 ; 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 Q:-74 What is the unique feature of LinkedHashSet? A. It is not a valid class B. It maintains the insertion order and guarantees uniqueness C. It provides a way to store key values with uniqueness D. The elements in the collection are linked to each other Answer: Option B Q:-75 What will be the output of the following Java program? 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 Q:-78 Which of these class object can be used to form a dynamic array? A. ArrayList B. Map C. Vector D. ArrayList & Vector Answer: Option D Q:-79 What will be the output of the following Java program? 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 Q:-80 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(5)); System.out.println(obj.elementAt(1)); } } A. 0 B. 3 C. 2 D. 5 Answer: Option C Q:-81 What will be the output of the following Java code? 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 Q:-82 Which of these methods can randomize all elements in a list? A. rand() B. randomize() C. shuffle() D. ambiguous() Answer: Option C Q:-83 Which of these methods can be used to obtain a static array from an ArrayList object? A. Array() B. covertArray() C. toArray() D. covertoArray() Answer: Option C Q:-84 Which of these method is used to insert value and its key? A. put() B. set() C. insertElement() D. addElement() Answer: Option A Q:-85 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: Option B Q:-86 What will be the output of the following Java program? 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 Q:-87 What will be the output of the following Java program? 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 Q:-88 What happens if we put a key object in a HashMap which exists? A. The new object replaces the older object B. The new object is discarded C. The old object is removed from the map D. It throws an exception as the key already exists in the map Answer: Option A Q:-89 Which of these interface handle sequences? A. Set B. List C. Comparator D. Collection Answer: Option B Q:-90 How to externally synchronize hashmap? A. HashMap.synchronize(HashMap a); B. HashMap a = new HashMap(); a.synchronize(); C. Collections.synchronizedMap(new HashMap()); D. Collections.synchronize(new HashMap()); Answer: Option C Q:-91 Set has contains(Object o) method. A. True B. False Answer: Option A Q:-92 What is the relation between hashset and hashmap? A. HashSet internally implements HashMap B. HashMap internally implements HashSet C. HashMap is the interface; HashSet is the concrete class D. HashSet is the interface; HashMap is the concrete class Answer: Option A Q:-93 Which of these return type of hasNext() method of an iterator? A. Integer B. Double C. Boolean D. Collections Object Answer: Option C Q:-94 Which of these is an incorrect form of using method max() to obtain a maximum element? A. max(Collection c) B. max(Collection c, Comparator comp) C. max(Comparator comp) D. max(List c) Answer: Option C Q:-95 Which of these methods is used to obtain an iterator to the start of collection? A. start() B. begin() C. iteratorSet() D. iterator() Answer: Option D Q:-96 What will be the output of the following Java program? 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 Q:-97 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 ; 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:-98 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 Q:-99 Which of these methods is used to retrieve the elements in properties object at specific location? A. get() B. Elementat() C. ElementAt() D. getProperty() Answer: Option D Q:-100 Set has contains(Object o) method. A. True B. False Answer: Option A Q:-101 What is the relation between hashset and hashmap? A. HashSet internally implements HashMap B. HashMap internally implements HashSet C. HashMap is the interface; HashSet is the concrete class D. HashSet is the interface; HashMap is the concrete class Answer: Option A Q:-102 Which of these return type of hasNext() method of an iterator? A. Integer B. Double C. Boolean D. Collections Object Answer: Option C Q:-103 Which of these methods is used to obtain an iterator to the start of collection? A. start() B. begin() C. iteratorSet() D. iterator() Answer: Option D Q:-104 What will be the output of the following Java program? 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 Q:-105 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 ; 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 Q:-107 Which of these methods is used to retrieve the elements in properties object at specific location? A. get() B. Elementat() C. ElementAt() D. getProperty() Answer: Option D Q:-108 Which of these method is used add an element and corresponding key to a map? A. put() B. set() C. redo() D. add() Answer: Option A Q:-109 How to remove duplicates from List? A. HashSet listToSet = new HashSet(duplicateList); B. HashSet listToSet = duplicateList.toSet(); C. HashSet listToSet = Collections.convertToSet(duplicateList); D. HashSet listToSet = duplicateList.getSet(); Answer: Option A 110 Which of these iterators can be used only with List? A. Setiterator B. ListIterator C. Literator D. None of the mentioned Answer: Option B 111 Is hashmap an ordered collection. A. True B. False Answer: Option B 112 How is Arrays.asList() different than the standard way of initialising List? A. Both are same B. Arrays.asList() throws compilation error C. Arrays.asList() returns a fixed length list and doesn't allow to add or remove elements D. We cannot access the list returned using Arrays.asList() Answer: Option C 113 What is difference between dequeue() and peek() function of java? A. dequeue() and peek() remove and return the next time in line B. dequeue() and peek() return the next item in line C. dequeue() removes and returns the next item in line while peek() returns the next item in line D. peek() removes and returns the next item in line while dequeue() returns the next item in line Answer: Option C 114 Which of these method of ArrayList class is used to obtain present size of an object? A. size() B. length() C. index() D. capacity() Answer: Option A 115 Which of these are legacy classes? A. Stack B. Hashtable C. Vector D. All of the mentioned Answer: Option D 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 117 What will be the output of the following Java program? 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 118 What will be the output of the following Java program? 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 119 Do we have get(Object o) method in HashSet. A. True B. False Answer: Option B 120 Which of these standard collection classes implements a linked list data structure? A. AbstractList B. LinkedList C. HashSet D. AbstractSet Answer: Option B 121 Which of these methods deletes all the elements from invoking collection? A. clear() B. reset() C. delete() D. refresh() Answer: Option A 122 What does Collections.emptySet() return? A. Immutable Set B. Mutable Set C. The type of Set depends on the parameter passed to the emptySet() method D. Null object Answer: Option A 123 What will be the output of the following Java code snippet? public class Demo { public static void main(String[] args) { Map sampleMap = new TreeMap(); sampleMap.put(1, null); sampleMap.put(5, null); sampleMap.put(3, null); sampleMap.put(2, null); sampleMap.put(4, null); 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 124 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(5)); System.out.println(obj.capacity()); } } A. 2 B. 3 C. 4 D. 6 Answer: Option C 125 Which of these object stores association between keys and values? A. Hash table B. Map C. Array D. String Answer: Option B 126 What will be the output of the following Java program? 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); 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 127 Which of these methods can be used to search an element in a list? A. find() B. sort() C. get() D. binaryserach() Answer: Option D 128 How can we remove an object from ArrayList? A. remove() method B. using Iterator C. remove() method and using Iterator D. delete() method Answer: Option C 129 What will be the output of the following Java program? 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 ; 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 131 Which of these methods can be used to move to next element in a collection? A. next() B. move() C. shuffle() D. hasNext() Answer: Option A 134 Which of this method is used to change an element in a LinkedList Object? A. change() B. set() C. redo() D. add() Answer: Option B 135 What will be the output of the following Java program? 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 136 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(5)); obj.removeAll(obj); System.out.println(obj.isEmpty()); } } A. 0 B. 1 C. true D. false Answer: Option C 137 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.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 138 Which of these classes implements Set interface? A. ArrayList B. HashSet C. LinkedList D. DynamicList Answer: Option B 139 Which of this interface is not a part of Java's collection framework? A. List B. Set C. SortedMap D. SortedList Answer: Option D 140 Which of this method is used to make all elements of an equal to specified value? A. add() B. fill() C. all() D. set() Answer: Option B 141 Which of these interface declares core method that all collections will have? A. set B. EventListner C. Comparator D. Collection Answer: Option D 142 What is the difference between TreeSet and SortedSet? A. TreeSet is more efficient than SortedSet B. SortedSet is more efficient than TreeSet C. TreeSet is an interface; SortedSet is a concrete class D. SortedSet is an interface; TreeSet is a concrete class Answer: Option D 143 Map implements collection interface? A. True B. False Answer: Option B 144 What will be the output of the following Java program? 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 146 Which of these methods sets every element of a List to a specified object? A. set() B. fill() C. Complete() D. add() Answer: Option B 147 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: Option C 148 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: Option D 149 What will be the output of the following Java code? 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 151 Which of these classes provide implementation of map interface? A. ArrayList B. HashMap C. LinkedList D. DynamicList Answer: Option B 152 Which of these methods is used to retrieve elements in BitSet object at specific location? A. get() B. Elementat() C. ElementAt() D. getProperty() Answer: Option A 153 Which of these exceptions is thrown by remover() method? A. IOException B. SystemException C. ObjectNotFoundExeception D. IllegalStateException Answer: Option D 154 Which of these method of HashSet class is used to add elements to its object? A. add() B. Add() C. addFirst() D. insert() Answer: Option A 155 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 ; 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 156 What will be the output of the following Java program? 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 157 Which of this interface must contain a unique element? A. Set B. List C. Array D. Collection Answer: Option A 158 What is the difference between length() and size() of ArrayList? A. length() and size() return the same value B. length() is not defined in ArrayList C. size() is not defined in ArrayList D. length() returns the capacity of ArrayList and size() returns the actual number of elements stored in the list Answer: Option D 159 If the size of the array used to implement a circular queue is MAX_SIZE. How rear moves to traverse inorder to insert an element in the queue? A. rear=(rear%1)+MAX_SIZE B. rear=(rear+1)%MAX_SIZE C. rear=rear+(1%MAX_SIZE) D. rear=rear%(MAX_SIZE+1) Answer: Option B 160 What will be the output of the following Java program? 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 PriorityQueue is thread safe. A. True B. False Answer: Option A 161 Which of these method is used to make a bit zero specified by the index? A. put() B. set() C. remove() D. clear() Answer: Option D 162 Which of these is a method of class Date which is used to search whether object contains a date before the specified date? A. after() B. contains() C. before() D. compareTo() Answer: Option C 163 Which of these is a class which uses String as a key to store the value in object? A. Array B. ArrayList C. Dictionary D. Properties Answer: Option D 164 When two threads access the same ArrayList object what is the outcome of the program? A. Both are able to access the object B. ConcurrentModificationException is thrown C. One thread is able to access the object and second thread gets Null Pointer exception D. One thread is able to access the object and second thread will wait till control is passed to the second one Answer: Option B 165 1What will be the output of the following Java program? 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 166 Which data structure is used in Breadth First Traversal of a graph? A. Stack B. Queue C. Array D. Tree Answer: Option B 167 Which of these classes is not part of Java's collection framework? A. Maps B. Array C. Stack D. Queue Answer: Option A MCQ on Packages 168. What is a package in Java?  a) A collec on of classes  b) A collec on of interfaces  c) A collec on of classes and interfaces  d) A method of error handling Answer: c) A collec on of classes and interfaces 169. Which keyword is used to define a package in Java?  a) import  b) package  c) namespace  d) module Answer: b) package 170. What is the purpose of a package in Java?  a) To organize classes and interfaces  b) To provide access control  c) To avoid name conflicts  d) All of the above Answer: d) All of the above 171. How do you import a package in Java?  a) include packageName;  b) using packageName;  c) import packageName;  d) use packageName; Answer: c) import packageName; 172. Can a package contain sub-packages in Java?  a) Yes  b) No Answer: a) Yes 173. Which package is automa cally imported in all Java programs?  a) java.u l  b) java.io  c) java.lang  d) java.math Answer: c) java.lang 174. How do you import all classes of a package?  a) import packageName;  b) import packageName.*;  c) use packageName.*;  d) include packageName.all; Answer: b) import packageName.*; 175. What is the fully qualified name of a class?  a) The class name only  b) The package name followed by the class name  c) The import statement  d) None of the above Answer: b) The package name followed by the class name 176. Which package contains the Scanner class?  a) java.io  b) java.u l  c) java.lang  d) java.net Answer: b) java.u l 177. Which package contains classes for input and output opera ons?  a) java.io  b) java.u l  c) java.lang  d) java.math Answer: a) java.io 178. What is the syntax to create a package in Java?  a) package packageName;  b) create 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.u l  b) java.io  c) java.graphics  d) java.lang Answer: c) java.graphics 181. How do you use a class from another package without impor ng it?  a) Use the class name directly  b) Use the fully qualified name of the class  c) Use the import keyword  d) You cannot use it Answer: b) Use the fully qualified name of the class 182. What is the java.sql package used for?  a) Handling math opera ons  b) Handling database opera ons  c) Handling file opera ons  d) Handling networking Answer: b) Handling database opera ons 183. Can a package have private classes?  a) Yes  b) No Answer: a) Yes 184. What is the use of CLASSPATH in Java?  a) It specifies the directory for Java libraries  b) It specifies the loca on of compiled class files  c) It specifies the loca on of package files  d) All of the above Answer: d) All of the above 185. Which package contains classes for networking in Java?  a) java.u l  b) java.io  c) java.net  d) java.nio Answer: c) java.net 186. What is the use of the java.u l package?  a) It contains u lity classes like Scanner and ArrayList  b) It is used for mathema cal opera ons  c) It provides input and output func onality  d) It is for database connec vity Answer: a) It contains u lity classes like Scanner and ArrayList Mul threading MCQ 187. Which method is used to start a thread in Java?  a) run()  b) execute()  c) start()  d) begin() Answer: c) start() 188. What is the default priority of a thread in Java?  a) 0  b) 1  c) 5  d) 10 Answer: c) 5 189. Which interface must a class implement to create a thread?  a) Cloneable  b) Runnable  c) Serializable  d) Comparable Answer: b) Runnable 190. Which of these is not a state of a thread?  a) Running  b) Waiting  c) Blocked  d) Ready Answer: c) Blocked 191. How can we stop a thread in Java?  a) By using the stop() method (deprecated)  b) By calling the thread's destroy() method  c) By using System.exit()  d) None of the above Answer: a) By using the stop() method (deprecated) 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) A new thread will be created  b) It will throw an exception  c) The code will run in the current thread  d) It will execute parallelly Answer: c) The code will run in the current thread 194. What does the synchronized keyword do in Java?  a) Allows multiple threads to access a method at once  b) Prevents thread interference and memory consistency errors  c) Speeds up thread execution  d) Stops all threads Answer: b) Prevents thread interference and memory consistency errors 195. Which class is used to create a thread by extending it?  a) Thread  b) Runnable  c) Executor  d) Future Answer: a) Thread 196. What happens if a thread goes to sleep by calling Thread.sleep()?  a) The thread releases all locks  b) The thread releases CPU but holds any locks it has  c) The thread remains running  d) The thread stops permanently Answer: b) The thread releases CPU but holds any locks it has 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() 198. Which method checks whether a thread is alive?  a) isActive()  b) isRunning()  c) isAlive()  d) isWorking() Answer: c) isAlive() 199. What is the purpose of the yield() method in Java threads?  a) It stops the thread permanently  b) It pauses the thread to allow other threads of the same priority to execute  c) It terminates the thread  d) It resumes a paused thread Answer: b) It pauses the thread to allow other threads of the same priority to execute 200. Which exception is thrown if the sleep() method is interrupted?  a) RuntimeException  b) InterruptedException  c) IllegalThreadStateException  d) None of the above Answer: b) InterruptedException 201. What does the notifyAll() method do?  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 202. Which of the following is used to create a thread pool in Java?  a) Executors  b) Runnable  c) Thread  d) Callable Answer: a) Executors 203. What is the main advantage of using thread pools?  a) Faster thread creation  b) Better thread control and resource management  c) Reduced code complexity  d) Avoids deadlocks automatically Answer: b) Better thread control and resource management 204. What is a daemon thread in Java?  a) A thread with high priority  b) A thread running in the background for supporting tasks  c) A thread created using new Thread()  d) A thread that cannot be stopped Answer: b) A thread running in the background for supporting tasks 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() 206. Which class in Java supports the concept of a monitor?  a) Thread  b) Object  c) Runnable  d) Executors Answer: b) Object

Use Quizgecko on...
Browser
Browser