Quiz about Python Linked List Quiz

Python Linked List Quiz

Last Updated :
Discuss
Comments

Question 1

What is a linked list?

  • A

    A collection of elements that can only be accessed sequentially.

  • B

    A collection of nodes where each node contains data and a reference to the next node.

  • C

    A collection of elements that are all of the same type.

  • D

    A collection of elements stored in contiguous memory locations.

Question 2

What is the main difference between an array and a linked list?

  • A

    An array supports dynamic insertion and deletion, while a linked list does not.

  • B

    In an array, elements are stored in contiguous memory locations, while in a linked list, nodes can be scattered in memory.

  • C

    An array allows dynamic memory allocation, while a linked list has fixed memory allocation.

  • D

    An array is a linear data structure, while a linked list is non-linear.

Question 3

Which of the following is true about linked lists?

  • A

    Linked lists can store elements of different data types.

  • B

    Linked lists can only store elements of the same data type.

  • C

    In a linked list, each node must contain only data, not references.

  • D

    Linked lists are always faster than arrays for accessing elements.

Question 4

In a singly linked list, what does the last node’s next pointer point to?

  • A

    A random node

  • B

    The first node

  • C

    A NULL value

  • D

    The second node

Question 5

In a singly linked list, how do you insert a new node at the beginning?

  • A

    Change the next pointer of the last node to the new node.

  • B

    Add the new node at the end of the list.

  • C

    Move the head pointer to the new node.

  • D

    Create a new node and set its next pointer to head.

Question 6

What does it mean when we say a linked list is "doubly linked"?

  • A

    It means the list can contain a maximum of two nodes.

  • B

    It means each node has two pointers: one pointing to the next node and the other pointing to the previous node.

  • C

    It means the list is circular.

  • D

    It means the list can store two data items in each node.

Question 7

Which of the following is not a valid way to traverse a singly linked list?

  • A

    Start from the head and follow the next pointers

  • B

    Use recursion to traverse each node

  • C

    Start from the tail and follow the prev pointers

  • D

    Start from the head and use a while loop

Question 8

What is the output of the following python code?

Python
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)

current = head
while current.next and current.next.next:
    current = current.next

current.next = None


  • A

    The list becomes 1 -> 2 -> 3.

  • B

    The list becomes 1 -> 2.

  • C

    The list becomes 2 -> 3.

  • D

    The list becomes 1.

Question 9

What does the following function do for a given Linked List with first node as head?

Python
def fun1(head):
  if head is None:
    return
  
  fun1(head.next)
  print(head.data, end='  ')


  • A

    Prints all nodes of linked list in reverse order

  • B

    Prints all nodes of linked list

  • C

    Prints alternate nodes in reverse order

  • D

    Prints alternate nodes of Linked List

Question 10

Consider the following function that takes reference to head of a Doubly Linked List as parameter. Assume that a node of doubly linked list has previous pointer as prev and next pointer as next. 

Python
def fun(head_ref):
    temp = None
    current = head_ref[0]

    while current is not None:
        temp = current.prev
        current.prev = current.next
        current.next = temp
        current = current.prev

    if temp is not None:
        head_ref[0] = temp.prev

What action will be performed by the following code?

  • A

    Set the value of head_ref to next;

  • B

    Set the value of head_ref to NULL;

  • C

    Set the value of head_ref to current;

  • D

    Set the value of head_ref to prev;

There are 15 questions to complete.

Take a part in the ongoing discussion