Collection Question Mcqs
Collection Question Mcqs
import java.util.*;
class I
Ans: c
import java.util.*;
class H
a.Prints: false,false,false
b.Prints: false,false,true
c.Prints: false,true,false
d.Prints: true,false,false
Ans: d
1. The Iterator interface declares only three methods: hasNext, next and remove.
2. The ListIterator interface extends both the List and Iterator interfaces.
3. The ListIterator interface provides forward and backward iteration capabilities.
4. The ListIterator interface provides the ability to modify the List during iteration.
5. The ListIterator interface provides the ability to determine its position in the List.
a.2, 3, 4 and 5
b.1, 3, 4 and 5
c.3, 4 and 5
d.1, 2 and 3
Ans: b
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it = map.iterator();
while (it.hasNext() )
a. 42
b. Runtime Exception
c. Compile Error at line 2
d. Compile Error at line 5
Ans: a
5. Which of these interface declares core method that all collections will have?
A. set
B. EventListner
C. Comparator
D. Collection
Ans: d
6. Which two statements are true about comparing two instances of the same class, given
that the equals() and hashCode() methods have been properly overridden?
1. If the equals() method returns true, the hashCode() comparison == must return true.
2. If the equals() method returns false, the hashCode() comparison != must return true.
3. If the hashCode() comparison == returns true, the equals() method must return true.
4. If the hashCode() comparison == returns true, the equals() method might return true.
a. 1 and 4
b. 2 and 3
c. 3 and 4
d. 1 and 3
Ans: a
sampleMap.put(1, null);
sampleMap.put(5, null);
sampleMap.put(3, null);
sampleMap.put(2, null);
sampleMap.put(4, null);
System.out.println(sampleMap);
b) {5=null}
c) Exception is thrown
Ans a
import java.util.*;
class Maps
System.out.println(obj);
a) {A 1, B 1, C 1}
b) {A, B, C}
Ans: d
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].
Ans: d
Programs
Given two array lists and we have to multiply its corresponding elements using java program.
Example:
Input:
Array1: [2, -5, 4, -2]
Array2: [6, 4, -5, -2]
Output:
Result: 12 -20 -20 4
Solution
import java.util.Arrays;
Output
2. Write a Java program to compare two sets and retain elements which are same on both sets.
Sample Output:
Solution
import java.util.*;
public class Exercise11 {
public static void main(String[] args) {
// Create a empty hash set
HashSet<String> h_set1 = new HashSet<String>();
// use add() method to add values in the hash set
h_set1.add("Red");
h_set1.add("Green");
h_set1.add("Black");
h_set1.add("White");
System.out.println("Frist HashSet content: "+h_set1);
HashSet<String>h_set2 = new HashSet<String>();
h_set2.add("Red");
h_set2.add("Pink");
h_set2.add("Black");
h_set2.add("Orange");
System.out.println("Second HashSet content: "+h_set2);
h_set1.retainAll(h_set2);
System.out.println("HashSet content:");
System.out.println(h_set1);
}
}
Solution Explanation
In this program, we need to check whether given singly linked list is a palindrome or not. A
palindromic list is the one which is equivalent to the reverse of itself.
Program to determine whether a singly linked list is the palindrome
The list given in the above figure is a palindrome since it is equivalent to its reverse list, i.e., 1, 2,
3, 2, 1. To check whether a list is a palindrome, we traverse the list and check if any element
from the starting half doesn't match with any element from the ending half, then we set the
variable flag to false and break the loop.
In the last, if the flag is false, then the list is palindrome otherwise not. The algorithm to check
whether a list is a palindrome or not is given below
//reverseList() will reverse the singly linked list and return the head of the list
public Node reverseList(Node temp){
Node current = temp;
Node prevNode = null, nextNode = null;
//Swap the previous and next nodes of each node to reverse the direction of the list
while(current != null){
nextNode = current.next;
current.next = prevNode;
prevNode = current;
current = nextNode;
}
return prevNode;
}
if(flag)
System.out.println("Given singly linked list is a palindrome");
else
System.out.println("Given singly linked list is not a palindrome");
}
if(head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Nodes of singly linked list: ");
while(current != null) {
//Prints each node by incrementing pointer
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
sList.display();
Output: