What will be the output of the following Java code snippet?

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]

1 thought on “What will be the output of the following Java code snippet?”

  1. d
    Explanation: obj.addFirst(ā€œDā€) method is used to add ā€˜Dā€™ to the start of a LinkedList object obj.
    Output:
    $ javac Linkedlist.java
    $ java Linkedlist
    [D, A, B, C].

Leave a Comment