Linked List Implementation
In the Java implementation of a singly linked list, what will happen if you forget to set 'newNode.next = null' when adding a new node to an empty list?
- A. The new node will still function correctly as the end of the list.
- B. The last node may incorrectly link to a previous node, creating a cycle.
- C. An exception will always be thrown at runtime.
- D. The next field of the new node will point to StackOverflow.
- E. The head will point to itself, causing an infinite loop immediately.
Array vs Linked List Access Times
Why does accessing an element by index in an array have O(1) time complexity, while the same operation in a singly linked list has O(n) time complexity?
- A. Arrays have contiguous memory allocation, enabling direct index calculation.
- B. Arrays allow random shuffling, while linked lists do not.
- C. Linked lists assign indices using hashcodes, reducing access efficiency.
- D. Linked lists store size information after every node.
- E. Arrays automatically cache all elements in the CPU.
Stack Overflow vs Stack Underflow
Given the following Stack implementation in Java, what will happen if you try to pop an element from an empty stack?
- A. The program will print 'Stack underflow!' and return -1.
- B. The program will print 'Stack overflow!' and return -1.
- C. The stack will automatically resize and return 0.
- D. The program throws NullPointerException.
- E. The pop method enters an infinite loop.
Recursive Factorial Base Case
In the recursive Java implementation of factorial, what is the significance of the base case 'if (n == 0) { return 1; }'?
- A. It defines the multiplication identity needed for recursion to terminate.
- B. It prevents stack overflow at all times.
- C. It initializes n to one for the calculation.
- D. It allows the function to run faster for odd numbers.
- E. Its absence causes integer division errors.
BST Structure and Insertion
When inserting an integer into a binary search tree (BST), what property ensures that searching is efficient?
- A. Every left child node's value is less than its parent node, and every right child's is greater.
- B. Each node holds a unique memory address.
- C. Nodes are always inserted at the root.
- D. The BST rotates itself after each insertion.
- E. Node values are checked using bubble sort before insertion.
Time vs Space Complexity
If an algorithm sorts an array of n elements by copying all the data into a new array, what will its space complexity be?
- A. O(n)
- B. O(1)
- C. O(log n)
- D. O(n^2)
- E. O(n log n)
BFS vs DFS Traversal
Suppose you use breadth-first search (BFS) to traverse a graph. Which order of node traversal does BFS guarantee compared to depth-first search (DFS)?
- A. BFS visits all neighboring nodes at each depth level before proceeding deeper.
- B. BFS visits nodes in reverse alphabetical order.
- C. BFS always visits the deepest node first.
- D. BFS uses a recursive call stack always.
- E. BFS and DFS guarantee identical traversal order.
Queue Data Structure in BFS in Java
In the provided Java BFS implementation for graphs, what role does the Queue play?
- A. It stores nodes that have been visited but whose neighbors have not yet been fully explored.
- B. It ensures the elements are sorted according to priority.
- C. It prevents cycles by keeping all nodes in memory.
- D. It is only used once at the beginning to store the source node.
- E. It computes the shortest path automatically.
Space Optimization in Linked List
What is a key advantage of using a singly linked list over an array in situations where frequent insertions and deletions occur, especially in large datasets?
- A. Linked lists do not require elements to be shifted after insertions or deletions.
- B. Linked lists have O(1) access time for any element.
- C. Arrays use less memory per element.
- D. Linked lists use contiguous memory for storage.
- E. Arrays automatically grow when needed.
Stack Implementation Error Detection
In the Java stack implementation given, what is the purpose of the condition 'if (top u003C maxSize - 1)' within the push method?
- A. To prevent pushing elements beyond the array's fixed capacity.
- B. To initialize elements with default values.
- C. To track the minimum element in the stack.
- D. To check for underflow conditions.
- E. To reverse the stack order automatically.