Java LinkedList MCQ

Java LinkedList MCQ | In Java, collection framework classes are used to store elements in value or key-value format without size limitation. We have listed some Java LinkedList MCQ and programming questions, answer them. See:- Java Collections Quiz-1

Also see- Java Collection Framework OverviewJava Collection InterfaceList Interface in Java

Q1) In Java, the underlying data structure for the LinkedList class is?

a) Singly linked list
b) Doubly linked list
c) Circular linked list
d) None of these

View Answer Answer:- b) Doubly linked list

Q2) What will be the output for the below statements?

List list = new LinkedList();
System.out.println(list instanceof RandomAccess);

a) true
b) false
c) both
d) None of these

View Answer Answer:- b) false

LinkedList class doesn’t implement RandomAccess interface therefore it gives “false” as output.

Q3) LinkedList class implements?

a) List interface
b) Deque interface
c) Both (a) and (b)
d) None of these

View Answer Answer:- c) Both (a) and (b)

LinkedList class implements both List(I) and Deque(I) interface.

Q4) Find the output of the below LinkedList program?

import java.util.LinkedList;
import java.util.List;

public class Test {
   public static void main(String[] args) {

      List<Integer> ll = new LinkedList<Integer>();
      ll.add(100);
      ll.add(220);
      ll.add(50);
      ll.add(null);
      ll.add(100);
      ll.add(50);
      ll.add(100);
      ll.add(null);

      System.out.println(ll);
   }
}

a) [100, 220, 50, null, 100, 50, 100, null]
b) [100, 220, 50, 100, 50, 100]
c) [100, 220, 50, null]
d) None of these

View Answer Answer:- a) [100, 220, 50, null, 100, 50, 100, null]

LinkedList allows storing the duplicates and null values without any restriction.

Q5) Find the output of the below Java LinkedList program?

import java.util.LinkedList;

public class Test {
   public static void main(String[] args) {

      LinkedList<Integer> ll = new LinkedList<Integer>();
      ll.add(100);
      ll.add(220);
      ll.add(50);
      ll.add(100);
      ll.add(50);
      
      ll.addFirst(999);
      ll.addLast(888);
      System.out.println(ll);
   }
}

a) [100, 220, 50, 100, 50]
b) [999, 100, 220, 50, 888]
c) [999, 100, 220, 50, 100, 50, 888]
d) None of these

View Answer Answer:- a) [999, 100, 220, 50, 100, 50, 888]

The addFirst(object) and addLast(object) method add element first and last index respectively.

Q6) Find the output of the below Java LinkedList program?

import java.util.LinkedList;

public class Test {
   public static void main(String[] args) {

      LinkedList<Integer> ll = new LinkedList<Integer>();
      ll.add(100);
      ll.add(50);
      ll.add(220);
      ll.add(100);
      ll.add(50);

      System.out.println(ll.getFirst() 
                 + " " + ll.getLast());
   }
}

a) 100 50
b) 100 220
c) 50 50
d) None of these

View Answer Answer:- a) 100 50

The getFirst() and getLast() methods fetch the elements at the first and last index in the list and return to the called method.

Q7) Find the output of the below Java LinkedList program?

import java.util.LinkedList;

public class Test {
   public static void main(String[] args) {

      LinkedList<Integer> ll = new LinkedList<Integer>();
      ll.add(100);
      ll.add(50);
      ll.add(220);
      ll.add(100);
      ll.add(50);

      System.out.println(ll.removeFirst() 
                 + " " + ll.removeLast() 
                + "; " + ll); 
   }
}

a) true true; [50, 220, 100]
b) true true; [100, 50, 220, 100, 50]
c) 100 50; [50, 220, 100]
d) 100 50; [100, 50, 220, 100, 50]

View Answer Answer:- c) 100 50; [50, 220, 100]

The removeFirst() and removeLast() method removes the first and last element in the list and returns the corresponding elements.

Q8) Which of the following statement is true?

a) In LinkedList elements are stored in consecutive memory locations.
b) In LinkedList elements are not stored in consecutive memory locations.

View Answer Answer:- b) In LinkedList elements are not stored in consecutive memory locations.

In LinkedList, the elements will not be stored in consecutive memory locations and hence retrieval operation becomes complex. Hence it is not recommended to use LinkedList if our primary task is to perform retrieval operation, in that case, ArrayList is the best choice.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *