Advanced Topics MCQ

Code Tracing: Linked Lists

Practice mode shows one question at a time and lets students check each answer before moving on.

All AT CS Tests

For practice use only.

Code Tracing: Linked Lists

Code Tracing: Linked Lists with immediate answer checks and explanations for Advanced Topics in Computer Science.

Question 1 of 25

Answered 0 of 25

Choose one answer.

Trace the linked-list removal loop. What list is printed?

class Node { int val; Node next; Node(int v, Node n) { val = v; next = n; } }

public class LinkedRemoval
{
    private static String show(Node n)
    {
        String s = "";
        while (n != null) { s += n.val + " "; n = n.next; }
        return s.trim();
    }

    public static void main(String[] args)
    {
        int[] data = {7, 6, 11, 12, 5, 18};
        Node head = null;
        for (int i = data.length - 1; i >= 0; i--) head = new Node(data[i], head);
        Node cur = head;
        while (cur != null && cur.next != null)
        {
            if (cur.next.val % 3 == 0) cur.next = cur.next.next;
            else cur = cur.next;
        }
        System.out.println(show(head));
    }
}