Search Tutorials


Java LinkedList MCQ Questions and Answers | JavaInUse

Java LinkedList MCQ Questions and Answers

Q. What is a linked list in Java?

A. A linear data structure with fixed-size elements
B. A non-linear data structure with dynamic size elements
C. A linear data structure with dynamic size elements
D. A hierarchical data structure with fixed-size elements

Q. What is a node in a linked list?

A. A fixed-size data container
B. A dynamic data container with a value and a reference
C. A hierarchical data structure
D. A functional interface

Q. What is the time complexity of inserting a node at the beginning of a linked list?

A. O(1)
B. O(n)
C. O(log n)
D. O(n^2)

Q. What is the time complexity of searching for a node in a linked list?

A. O(1)
B. O(n)
C. O(log n)
D. O(n^2)

Q. What is the output of this code snippet?
LinkedList<String> list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
list.addFirst("Mango");
System.out.println(list);

A. [Mango, Apple, Banana]
B. [Apple, Mango, Banana]
C. [Mango, Banana, Apple]
D. [Apple, Banana]

Q. What is the purpose of the ListIterator in a linked list?

A. To iterate through the list in reverse order
B. To add elements to the list
C. To remove elements from the list
D. To iterate through the list and modify elements

Q. What is the output of this code snippet?
LinkedList<Integer> list = new LinkedList<>();
list.add(10);
list.add(20);
list.add(30);
System.out.println(list.removeFirst());
System.out.println(list);

A. 10
[20, 30]
B. 20
[10, 30]
C. 30
[10, 20]
D. 10
[30, 20]

Q. How do you check if a linked list is empty?

A. list.isEmpty()
B. list.size() == 0
C. list.length() == 0
D. list.count() == 0

Q. What is the output of this code snippet?
LinkedList<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
list.removeLast();
System.out.println(list);

A. [1, 2]
B. [2, 3]
C. [1, 3]
D. [1]

Q. What is the time complexity of deleting a node from the middle of a linked list?

A. O(1)
B. O(n)
C. O(log n)
D. O(n^2)





Q. What will be the output of the following code?

LinkedList<String> list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.remove("Banana");


System.out.println(list);
A.
[Apple, Cherry]
B.
[Banana, Cherry]
C.
[Apple, Banana]
D.
The code will throw an exception

Q. How to check if a LinkedList is empty in Java?

A.
if (list.isEmpty()) {
    // Code to handle empty list
}
B.
if (list.size() == 0) {
    // Code to handle empty list
}
C.
if (list.first() == null) {
    // Code to handle empty list
}
D.
A and B are both correct

Q. What will be the output of the following code?

LinkedList<Integer> list = new LinkedList<>();
list.add(3);
list.add(5);
list.add(8);
list.set(1, 10);


System.out.println(list);
A.
[3, 5, 8]
B.
[3, 10, 8]
C.
[3, 8, 10]
D.
[3, 5, 10]

Q. What will be the output of the following code?

LinkedList<String> list = new LinkedList<>();
list.add("Red");
list.add("Green");
list.add("Blue");
Iterator<String> iterator = list.iterator();


while (iterator.hasNext()) {
System.out.print(iterator.next());
iterator.remove();
}
A.
RedGreenBlue
The list remains unchanged
B.
RedGreen
C.
Red
D.
GreenBlue

Q. What will be the output of the following code?

LinkedList<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
list.addAll(1, Arrays.asList(4, 5, 6));


System.out.println(list);
A.
[1, 4, 5, 6, 2, 3]
B.
[1, 4, 5, 6, 2]
C.
[4, 5, 6, 1, 2, 3]
D.
[1, 2, 3, 4, 5, 6]

Q. What will be the output of the following code?

LinkedList<String> list = new LinkedList<>();
list.add("One");
list.add("Two");
list.add("Three");
list.removeFirst();
list.removeLast();


System.out.println(list);
A.
[Two]
B.
The code will throw an exception
C.
[]
D.
[One, Three]

Q. What will be the output of the following code?

LinkedList<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
list.offer(4);
list.offerFirst(5);


System.out.println(list);
A.
[1, 2, 3, 4, 5]
B.
[5, 1, 2, 3, 4]
C.
[5, 1, 2, 3]
D.
[1, 5, 2, 3, 4]

Q. What will be the output of the following code?

LinkedList<String> list = new LinkedList<>();
list.add("Hello");
list.add("World");
list.poll();
list.pollLast();


System.out.println(list);
A.
[Hello]
B.
[World]
C.
[]
D.
The code will throw an exception

Q. What will be the output of the following code?

LinkedList<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
list.push(4);
list.push(5);


System.out.println(list);
A.
[5, 4, 1, 2, 3]
B.
[5, 4, 1, 2]
C.
[1, 2, 3, 4, 5]
D.
[4, 5, 1, 2, 3]

Q. What will be the output of the following code?

LinkedList<String> list = new LinkedList<>();
list.add("Cat");
list.add("Dog");
list.add("Bird");
list.listIterator(1).add("Rabbit");


System.out.println(list);
A.
[Cat, Rabbit, Dog, Bird]
B.
[Cat, Dog, Rabbit, Bird]
C.
[Cat, Dog, Bird, Rabbit]
D.
[Rabbit, Cat, Dog, Bird]