Object Oriented Programming using Java Questions and Answers – Collection Classes

This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Collection Classes”.

1. What will be the output of the following Java code?

import java.util.*;
public class Collection
{
    public static void main(String args[])
    {
        String a[] = {"Sa","nf","ou","nd","ry"};
        Arrays.sort(a);
        for(int i=0;i<5;i++)
        {
            System.out.print(a[i]);
        }
    }
}

a) Sandnfoury
b) Sanfoundry
c) ndnfouSary
d) Compilation Error
View Answer

Answer: a
Explanation: The sort() function of the Arrays class sorts the elements of the array in alphabetic order, giving more preference to upper case letters first. As the string “Sa” begins with an upper case letter, it is given more preference.
Output:

$ javac Collection.java
$ java Collection
Sandnfoury

2. What will be the output of the following Java code?

advertisement
advertisement
import java.util.*;
public class Collection2
{
    public static void main(String args[])
    {
        LinkedList l = new LinkedList();
        l.add("Sanfoundry");
        l.add(10);
        l.add(20);
        l.add(30);
        Collections.shuffle(l);
        Iterator i = l.iterator();
        while(i.hasNext())
        System.out.println(i.next());
    }
}

a)

10
20
30
Sanfoundry

b)

advertisement
20
30
10
Sanfoundry

c)

30
Sanfoundry 
10 
20

d) Random arrangement of the 4 items
View Answer

Answer: d
Explanation: The shuffle() function of the collections class shuffles the given linked list completely, therefore it is difficult to predict the final list after shuffling. There is no particular order for the shuffling and may result in many cases which are near impossible to compute.
Output:

advertisement
$ javac Collection2.java
$ java Collection2
30
Sanfoundry
20
10

3. What will be the output of the following Java code?

import java.util.*;
public class MainCall
{
    public static void main(String args[])
    {
        LinkedList l = new LinkedList();
        l.add("S");
        l.add("a");
        l.add("n");
        l.add("f");
        l.add("o");
        l.add("u");
        l.add("n");
        l.add("d");
        l.add("r");
        l.add("y");
        Collections.reverse(l);
        Iterator i = l.iterator();
        while(i.hasNext())
        System.out.print(i.next());
    }
}

a) Sanfoundry
b) yrdnuofnaS
c)

S
a
n
f
o
u
n
d
r
y

d)

y
r
d
n
u
o
f
n
a
S
View Answer
Answer: b
Explanation: The reverse() function of the Collections class reverses the elements of the given collection class(in this case, it is a linked list). Hence, the given string “Sanfoundry” stored in a linked list object ‘l’ appears in reverse order.
Output:

$ javac MainCall.java
$ java MainCall
yrdnuofnaS

4. What will be the output of the following Java code?

import java.util.*;
public class MainExecute
{
    public static void main(String args[])
    {
        LinkedList l = new LinkedList();
        String s = "Sanfoundry";
        for(int i=0;i<s.length();i++)
        {
            l.add(s.charAt(i));
        }
        System.out.println(l);
    }
}

a) Sanfoundry
b) “Sanfoundry”
c) [S, a, n, f, o, u, n, d, r, y]
d) Compilation Error
View Answer

Answer: c
Explanation: A string variable is converted into a linked list object ‘l’ in the above program. When a collection item is printed, it returns the collection in the form of an array by default if no further instructions are given by the user.
Output:

$ javac MainExecute.java
$ java MainExecute
[S, a, n, f, o, u, n, d, r, y]

5. What will be the output of the following Java code?

import java.util.*;
public class LinkedList
{
    public static void main(String args[])
    {
        LinkedList l = new LinkedList();
        l.add(1);
        l.add(2);
        l.add(4);
        l.add(5);
        l.add(3);
        l.remove(3);
        System.out.println(l);
    }
}

a) [1, 2, 5, 3]
b) [1, 2, 4, 3]
c) [1, 2, 4, 5]
d) Compilation Error
View Answer

Answer: b
Explanation: The remove() function of the Collection class removes the element at the specified index from the collection. In the above program, the index 3 is specified inside the remove() method, hence the number 3 which is a part of the linked list is erased from the linked list.
Output:

$ javac LinkedList.java
$ java LinkedList
[1, 2, 4, 3]

6. What will be the output of the following Java code?

import java.util.*;
public class retainAll
{
    public static void main(String args[])
    {
        LinkedList l = new LinkedList();
        String s = "Sanfoundry";
        for(int i=0;i<s.length();i++)
        {
            l.add(s.charAt(i));
        }
        LinkedList a = new LinkedList();
        a.add('S');
        a.add('f');
        a.add('n');
        a.add('o');
        l.retainAll(a);
        System.out.println(l);
    }
}

a) [S, n, f, o, n]
b) [a, u, d, r, y]
c) [S, n, f, o]
d) Error
View Answer

Answer: a
Explanation: The retainAll() function retains all the values of the given collection. If the function is called, it matches the given class or object in the collection which is passed as a parameter to the function in the given block of code.
Output:

$ javac retainAll.java
$ java retainAll
[S, n, f, o, n]

7. What will be the output of the following Java code?

import java.util.*;
public class charAt
{
    public static void main(String args[])
    {
        LinkedList l = new LinkedList();
        String s = "Sanfoundry";
        for(int i=0;i<s.length();i++)
        {
            l.add(s.charAt(i));
        }
        System.out.println(l.contains('S'));
    }
}

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

Answer: c
Explanation: The contains() function of Collections class is used to check if the element specified is present in the collection class. If it is present, it returns true, otherwise it returns false.
Output:

$ javac charAt.java
$ java charAt
true

8. What will be the output of the following Java code?

import java.util.*;
public class Find
{
     public static void main(String args[])
    {
        LinkedList l = new LinkedList();
        l.add("S");
        l.add("a");
        l.add("n");
        l.add("f");
        l.add("o");
        l.add("u");
        l.add("n");
        l.add("d");
        l.add("r");
        l.add("y");
        Iterator i = l.iterator();
        while(i.hasNext())
        {
            if(i.next().equals("n"))
            System.out.println("Found 'n'");
        }
    }
}

a) No output
b) Found ‘n’
c)

Found 'n'
Found 'n'

d) Compilation Error
View Answer

Answer: c
Explanation: The iterator i iterates through the LinkedList and next function gives the value of the element. The if checks if the element of the list is equal to “n” or not. If it is equal to the given string “n”, then it prints Found ‘n’.
Output:

$ javac Find.java
$ java Find
Found 'n'
Found 'n'

9. What will be the output of the following Java code?

import java.util.*;
public class Size
{
     public static void main(String args[])
    {
        LinkedList l = new LinkedList();
        for(int i=0;i<10;i++)
        {
            l.add(i);
        }
        System.out.println(l.size());
        l.clear();
        System.out.println(l.size());
    }
}

a)

9
9

b)

9
0

c)

10
0

d)

10
10
View Answer
Answer: c
Explanation: The size() function of the collections class returns the size of the collection calling it, and the clear() function clears all the items in the collection calling it. In the above code, the size() function is used twice, once for the original linked list and second time for the empty linked list.
Output:

$ javac Size.java
$ java Size
10
0

10. What will be the output of the following Java code?

import java.util.*;
public class hashCode
{
     public static void main(String args[])
    {
        LinkedList l = new LinkedList();
        l.add("Sanfoundry");
        System.out.println(l.hashCode());
        l.clear();
        System.out.println(l.hashCode());
        l.add("SANFOUNDRY");
        System.out.println(l.hashCode());
        l.clear();
        System.out.println(l.hashCode());
    }
}

a)

-599207512
1
-2107580024
1

b)

-599207512
-599207512
-599207512
-599207512

c)

1
1
1
1

d)

-599207512
1
-599207512
1
View Answer
Answer: a
Explanation: The hashCode() function of the collections class returns a specific hashcode for a collection which changes based on the content of the collection. For an empty collection, the hashcode is always 1. Hashcode generated is very complicated and tough to predict for a user.
Output:

$ javac hashCode.java
$ java hashCode
-599207512
1
-2107580024
1

11. What will be the output of the following Java code?

import java.util.*;  
public class Stack
{  
    public static void main(String args[])
    {  
        Stack<String> stack = new Stack<String>();  
        stack.push("A");  
        stack.push("B");  
        stack.pop();  
        Iterator<String> itr=stack.iterator();  
        while(itr.hasNext())
        {  
            System.out.println(itr.next());  
        }  
    }  
}

a) A
b) B
c)

B
A 

d)

A 
B
View Answer
Answer: a
Explanation: Stack is a first-in-first-out(FIFO) oriented data structure. Stack collection class has two features – push and pop. Push mechanism inserts an element at the top of the stack while pop removes the top element.
Output:

$ javac Stack.java
$ java Stack
A

12. What will be the output of the following Java code?

import java.util.*;  
class TestJavaCollection1
{  
    public static void main(String args[])
    {  
        ArrayList<String> list=new ArrayList<String>();
        list.add("A");
        Iterator itr=list.iterator();  
        while(itr.hasNext())
        {      
            System.out.println(itr.next());  
        }  
    }  
}

a) A
b) B
c) Null
d) Compilation error
View Answer

Answer: a
Explanation: The ArrayList class is a resizable dynamic array, which can be found in the java.util package. It is a collection class which used to store values in an array, dynamically at the runtime by the compiler.
Output:

$ javac TestJavaCollection1.java
$ java TestJavaCollection1
A

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.