VNSGU BCA Sem 4: Java Programming (403) Practical Solutions - April 2025 Set AΒΆ
Paper Details
- Subject: Java Programming Language
- Subject Code: 403
- Set: A
- Semester: 4
- Month/Year: April 2025
- Max Marks: 25
- Time Recommendation: 45 Minutes
- Paper: View Paper | Download PDF
Questions & SolutionsΒΆ
All questions are compulsoryΒΆ
Q1: Singly Linked List ImplementationΒΆ
Max Marks: 20
Implement a Singly Linked List in Java with the following operations:
- Insert at the end
- Display all elements
1. Node Class StructureΒΆ
Create the Node class to represent each element in the linked list.
Hint
A Node contains data and a reference to the next node. Use a class with two fields: data (int) and next (Node reference).
flowchart TD
A["Node Class"] --> B["data: int"]
A --> C["next: Node"]
B --> D["Stores value"]
C --> E["Points to next"]
View Solution & Output
// Node class representing each element
class Node {
int data;
Node next;
// Constructor
Node(int data) {
this.data = data;
this.next = null;
}
}
Output:
Step-by-Step Explanation:
1. Initialization: Define a Node class with two fields - data to store the integer value and next to reference the next node
2. Logic Flow: Create a constructor that initializes the data field and sets next to null
3. Completion: Each new node starts as a standalone element with no connection to other nodes
2. LinkedList Class with Insert OperationΒΆ
Create the LinkedList class with insert at end functionality.
Hint
Maintain a head reference to track the first node. For insertion at end, traverse to the last node and append the new node.
flowchart TD
A["Insert at End"] --> B["List Empty?"]
B -->|Yes| C["Set head = newNode"]
B -->|No| D["Traverse to last"]
D --> E["last.next = newNode"]
C --> F["Done"]
E --> F
View Solution & Output
// LinkedList class with operations
class LinkedList {
Node head; // Reference to first node
// Insert at end operation
void insertAtEnd(int data) {
Node newNode = new Node(data);
// Case 1: Empty list
if (head == null) {
head = newNode;
return;
}
// Case 2: Traverse to last node
Node current = head;
while (current.next != null) {
current = current.next;
}
// Link new node at the end
current.next = newNode;
}
}
Output:
Step-by-Step Explanation: 1. Initialization: Create a new node with the given data 2. Logic Flow: Check if list is empty (head is null). If yes, set head to new node. Otherwise, traverse to the last node using a while loop 3. Completion: Set the next reference of the last node to the new node, completing the insertion
3. Display OperationΒΆ
Implement the display method to print all elements.
Hint
Traverse from head to end, printing each node's data. Use a temporary reference to avoid modifying head.
flowchart TD
A["Display"] --> B["current = head"]
B --> C{"current != null?"}
C -->|Yes| D["Print current.data"]
D --> E["current = current.next"]
E --> C
C -->|No| F["End"]
View Solution & Output
// Display all elements
void display() {
// Check if list is empty
if (head == null) {
System.out.println("List is empty!");
return;
}
Node current = head;
System.out.print("Linked List: ");
// Traverse and print
while (current != null) {
System.out.print(current.data);
if (current.next != null) {
System.out.print(" -> ");
}
current = current.next;
}
System.out.println(" -> null");
}
Output:
Step-by-Step Explanation: 1. Initialization: Check if list is empty. If yes, print message and return 2. Logic Flow: Start from head and traverse using a while loop until current becomes null. Print each node's data with an arrow separator 3. Completion: When current reaches null, all nodes have been printed. Add "-> null" to show end of list
4. Complete Main ProgramΒΆ
Putting it all together with user input and menu-driven interface.
View Solution & Output
import java.util.Scanner;
// Complete Linked List Implementation
public class LinkedListDemo {
// Node class
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
// LinkedList class
static class LinkedList {
Node head;
// Insert at end
void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
System.out.println("Inserted: " + data);
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
System.out.println("Inserted: " + data);
}
// Display list
void display() {
if (head == null) {
System.out.println("List is empty!");
return;
}
Node current = head;
System.out.print("Linked List: ");
while (current != null) {
System.out.print(current.data);
if (current.next != null) {
System.out.print(" -> ");
}
current = current.next;
}
System.out.println(" -> null");
}
}
// Main method
public static void main(String[] args) {
LinkedList list = new LinkedList();
Scanner sc = new Scanner(System.in);
int choice, data;
do {
System.out.println("\n===== Linked List Menu =====");
System.out.println("1. Insert at End");
System.out.println("2. Display All Elements");
System.out.println("3. Exit");
System.out.print("Enter choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter data to insert: ");
data = sc.nextInt();
list.insertAtEnd(data);
break;
case 2:
list.display();
break;
case 3:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice!");
}
} while (choice != 3);
sc.close();
}
}
Output:
===== Linked List Menu =====
1. Insert at End
2. Display All Elements
3. Exit
Enter choice: 1
Enter data to insert: 10
Inserted: 10
===== Linked List Menu =====
1. Insert at End
2. Display All Elements
3. Exit
Enter choice: 1
Enter data to insert: 20
Inserted: 20
===== Linked List Menu =====
1. Insert at End
2. Display All Elements
3. Exit
Enter choice: 1
Enter data to insert: 30
Inserted: 30
===== Linked List Menu =====
1. Insert at End
2. Display All Elements
3. Exit
Enter choice: 2
Linked List: 10 -> 20 -> 30 -> null
===== Linked List Menu =====
1. Insert at End
2. Display All Elements
3. Exit
Enter choice: 3
Exiting...
Step-by-Step Explanation: 1. Initialization: Create LinkedList object and Scanner for input. Display menu options 2. Logic Flow: Use do-while loop for menu. Switch case handles: Insert (calls insertAtEnd), Display (calls display), Exit (terminates) 3. Completion: Close scanner on exit. The linked list maintains all inserted nodes in memory during program execution
Concept Deep Dive: Linked List vs Array
Linked List Advantages: - Dynamic size (grows as needed) - Easy insertion/deletion at any position - No memory waste (allocates as needed)
Array Advantages: - Direct access by index (O(1)) - Better cache performance - Less memory overhead per element
Use linked lists when frequent insertions/deletions are needed. Use arrays when random access is priority.
Q2: Viva PreparationΒΆ
Max Marks: 5
Potential Viva Questions
- Q: What is a Linked List?
- A: A linear data structure where elements (nodes) are connected via references/pointers. Each node contains data and a reference to the next node.
- Q: What is the difference between Singly and Doubly Linked List?
- A: Singly has one reference (next), allowing traversal in one direction. Doubly has two references (next and prev), allowing bidirectional traversal.
- Q: What is the time complexity of inserting at the end?
- A: O(n) because we need to traverse from head to the last node.
- Q: How can we improve insertion time to O(1)?
- A: By maintaining a tail reference that always points to the last node.
- Q: What happens if we lose the head reference?
- A: The entire list becomes inaccessible (memory leak in unmanaged languages). Always preserve head.
- Q: Why use null for the last node's next?
- A: null marks the end of list. It helps in traversal (while current != null) and prevents infinite loops.
- Q: Can we create a linked list of objects instead of primitives?
- A: Yes, the data field can be any type (custom objects, strings, etc.). Just change the data type.
- Q: What is a null pointer exception in linked lists?
- A: Occurs when trying to access a field of a null node. Always check if node is null before accessing.
Common Pitfalls
- Forgetting null check: Always check if head is null before operations
- Losing head reference: Never modify head directly in traversal; use temporary variable
- Infinite loops: Ensure loop condition properly advances (current = current.next)
- Memory leaks: In languages without garbage collection, manually free deleted nodes
- Null last node: Always set next of new last node to null
Related HTML TutorialsΒΆ
Learning about data structures in Java? You might also be interested in:
- HTML Lists Tutorial - Learn about ordered, unordered, and definition lists in HTML
Quick NavigationΒΆ
Related SolutionsΒΆ
| Set | Link |
|---|---|
| Set A | Current Page |
| Set B | Solutions |
| Set C | Solutions |
| Set D | Solutions |
| Set E | Solutions |
| Set F | Solutions |
Last Updated: April 2026