0% found this document useful (0 votes)
11 views10 pages

Collection Question Mcqs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Collection Question Mcqs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1. What will be the output of the program?

import java.util.*;

class I

public static void main (String[] args)

Object i = new ArrayList().iterator();

System.out.print((i instanceof List)+",");

System.out.print((i instanceof Iterator)+",");

System.out.print(i instanceof ListIterator);

a.Prints: false, false, false

b.Prints: false, false, true

c.Prints: false, true, false

d.Prints: false, true, true

Ans: c

XX) What will be the output of the program?

import java.util.*;

class H

public static void main (String[] args)

Object x = new Vector().elements();

System.out.print((x instanceof Enumeration)+",");

System.out.print((x instanceof Iterator)+",");

System.out.print(x instanceof ListIterator);


}

a.Prints: false,false,false

b.Prints: false,false,true

c.Prints: false,true,false

d.Prints: true,false,false

Ans: d

2. Which of the following are true statements?

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

3. What will be the output of the program?

TreeSet map = new TreeSet();

map.add("one");

map.add("two");

map.add("three");

map.add("four");

map.add("one");

Iterator it = map.iterator();

while (it.hasNext() )

System.out.print( it.next() + " " );


}

a. one two three four


b. four three two one
c. four one three two
d. one two three four one
Ans: c

4. What will be the output of the program?

public static void main(String[] args)


{
Object obj = new Object()
{
public int hashCode()
{
return 42;
}
};
System.out.println(obj.hashCode());
}

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

7. What is the output of below snippet?

public class Demo

public static void main(String[] args)

Map<Integer, Object> sampleMap = new TreeMap<Integer, Object>();

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}

Ans a

8. What is the output of this 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}

Ans: d

9. What is the output of this 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.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

1. Java program to multiply corresponding elements of two lists


Here, we are implementing a java program that will multiply corresponding elements of two
array lists.

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

Program to multiply corresponding elements of two array lists in java

import java.util.Arrays;

public class ExArrayMultiplyCorresElem


{
public static void main(String[] args)
{
// take a default string array you wants.
String result = "";
int[] left_array = {2, -5, 4, -2};
int[] right_array = {6, 4, -5, -2};

// print both the string array first.


System.out.print("\nArray1: "+Arrays.toString(left_array));
System.out.println("\nArray2: "+Arrays.toString(right_array));
for (int i = 0; i < left_array.length; i++)
{
int num1 = left_array[i];
int num2 = right_array[i];

// this will calculate the product of the string array.


result += Integer.toString(num1 * num2) + " ";
}
// print the result.
System.out.println("\nResult: "+result);
}
}

Output

Array1: [2, -5, 4, -2]


Array2: [6, 4, -5, -2]

Result: 12 -20 -20 4

2. Write a Java program to compare two sets and retain elements which are same on both sets.

Sample Output:

Frist HashSet content: [Red, White, Black, Green]


Second HashSet content: [Red, Pink, Black, Orange]
HashSet content:
[Red, Black]

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);
}
}

3. Program to determine whether a singly linked list is the palindrome


Sample output
Nodes of singly linked list:
12321
Given singly linked list is a palindrome

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

public class Palindrome {

//Represent a node of the singly linked list


class Node{
int data;
Node next;

public Node(int data) {


this.data = data;
this.next = null;
}
}

public int size;


//Represent the head and tail of the singly linked list
public Node head = null;
public Node tail = null;

//addNode() will add a new node to the list


public void addNode(int data) {
//Create a new node
Node newNode = new Node(data);

//Checks if the list is empty


if(head == null) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
//Size will count the number of nodes present in the list
size++;
}

//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;
}

//isPalindrome() will determine whether given list is palindrome or not.


public void isPalindrome(){
Node current = head;
boolean flag = true;

//Store the mid position of the list


int mid = (size%2 == 0)? (size/2) : ((size+1)/2);

//Finds the middle node in given singly linked list


for(int i=1; i<mid; i++){
current = current.next;
}

//Reverse the list after middle node to end


Node revHead = reverseList(current.next);

//Compare nodes of first half and second half of list


while(head != null && revHead != null){
if(head.data != revHead.data){
flag = false;
break;
}
head = head.next;
revHead = revHead.next;
}

if(flag)
System.out.println("Given singly linked list is a palindrome");
else
System.out.println("Given singly linked list is not a palindrome");
}

//display() will display all the nodes present in the list


public void display() {
//Node current will point to head
Node current = head;

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();
}

public static void main(String[] args) {

Palindrome sList = new Palindrome();

//Add nodes to the list


sList.addNode(1);
sList.addNode(2);
sList.addNode(3);
sList.addNode(2);
sList.addNode(1);

sList.display();

//Checks whether given list is palindrome or not


sList.isPalindrome();
}
}

Output:

Nodes of singly linked list:


12321
Given singly linked list is a palindrome

You might also like