Free Bachelor of Computer Engineering Data Structure Questions and Answers — Questions and Answers
Question 1: Which of the following best sums up an array?
- Container of objects of similar types (Correct answer)
- Array is not a data structure
- A data structure that shows a hierarchical behavior
- Arrays are immutable once initialised
Correct answer: Container of objects of similar types
An array is a fundamental data structure that stores a fixed-size sequential collection of elements of the same data type. All elements in an array are of the same type (e.g., all integers, all characters), and they are stored contiguously in memory, allowing for efficient access by index.
Question 2: How can an array be initialized in C?
- int arr[3] = {1,2,3}; (Correct answer)
- int arr(3) = (1,2,3);
- int arr[3] = (1,2,3);
- int arr(3) = {1,2,3};
Correct answer: int arr[3] = {1,2,3};
In C programming, an array is initialized by declaring its type and size, followed by an equals sign and a list of initial values enclosed in curly braces `{}`. The syntax `int arr[3] = {1,2,3};` correctly declares an integer array named `arr` of size 3 and initializes its elements to 1, 2, and 3, respectively.
Question 3: In Java, how do you create an array?
- int arr[];
- int arr() = new int(3);
- int arr[] = new int(3);
- int arr[] = new int[3]; (Correct answer)
Correct answer: int arr[] = new int[3];
In Java, arrays are objects, so they must be created using the `new` keyword. The syntax `new int[3]` allocates memory for an array of 3 integers. The `int arr[]` part declares a variable `arr` that can hold an array of integers, and the assignment links it to the newly created array object.
Question 4: Which of the following notions uses arrays the most?
- Scheduling of processes
- Spatial locality (Correct answer)
- Binary trees
- Caching
Correct answer: Spatial locality
Spatial locality refers to the tendency of a program to access data items that are physically close to each other in memory. Arrays store elements in contiguous memory locations, which naturally promotes spatial locality. When an element in an array is accessed, it's highly probable that nearby elements will be accessed soon, making arrays highly beneficial for caching and memory optimization based on spatial locality.
Question 5: The action of adding a stack member is known as .
- Evaluation
- Pop
- Create
- Push (Correct answer)
Correct answer: Push
In a stack data structure, the operation of adding an element is known as 'push'. This operation places the new element at the top of the stack. Stacks follow a Last-In, First-Out (LIFO) principle, meaning the last element added is the first one to be removed.
Question 6: The action of eliminating a stack element is known as .
- Evaluation
- Push
- Pop (Correct answer)
- Create
Correct answer: Pop
In a stack data structure, the operation of eliminating an element is known as 'pop'. This operation removes the element that is currently at the top of the stack. Stacks adhere to the Last-In, First-Out (LIFO) principle, so the most recently added element is always the one removed by a pop operation.
Question 7: It is known as ________ in a stack when a user attempts to remove an element from an empty stack.
- Garbage Collection
- Empty collection
- Underflow (Correct answer)
- Overflow
Correct answer: Underflow
Attempting to remove an element from an empty stack is known as an 'underflow' condition. This is an error state because there are no elements available to be popped. Proper stack implementations typically check if the stack is empty before performing a pop operation to prevent underflow.
Question 8: If you add one element to a stack that already has five elements and has a size of 5, the stack will become .
- Underflow
- Overflow (Correct answer)
- User flow
- Crash
Correct answer: Overflow
When a user attempts to add an element to a stack that is already full (i.e., it has reached its maximum capacity), this condition is known as 'overflow'. In this scenario, the stack cannot accommodate any more elements. Proper stack implementations should check for overflow before performing a push operation to prevent data loss or program errors.
Question 9: The term "linear list" refers to a collection of elements where deletions may only be made from one end (front) and insertions can only be made from the other end (rear).
- Tree
- Queue (Correct answer)
- Linked list
- Stack
Correct answer: Queue
A queue is a linear data structure that follows the First-In, First-Out (FIFO) principle. Elements are always added at one end, called the 'rear' (enqueue operation), and removed from the other end, called the 'front' (dequeue operation). This behavior perfectly matches the description of deletions only from the front and insertions only from the rear.
Question 10: Another name for circular queue is
- Square Buffer
- Curve Buffer
- Ring Buffer (Correct answer)
- Rectangle Buffer
Correct answer: Ring Buffer
A circular queue is a linear data structure where the last element points back to the first element, forming a circle. This arrangement allows for efficient reuse of empty slots, preventing the need to shift elements after deletions. It is also commonly known as a 'Ring Buffer' due to its circular nature and buffering capabilities.
Question 11: What is the name of a linear collection of data pieces where the linear node is indicated by a pointer?
- Primitive list
- Unordered list
- Linked list (Correct answer)
- Node list
Correct answer: Linked list
A linked list is a linear collection of data elements, called nodes, where each node contains both data and a pointer (or reference) to the next node in the sequence. This pointer-based structure allows elements to be stored non-contiguously in memory. Unlike arrays, linked lists do not require contiguous memory allocation and can grow or shrink dynamically.
Question 12: Which of the following statements about the use of an array is false?
- Insertion based on position
- Accessing elements at specified positions (Correct answer)
- Fixed size
- There are chances of wastage of memory space if elements inserted in an array are lesser than the allocated size
Correct answer: Accessing elements at specified positions
Arrays are highly efficient for accessing elements at specified positions using their index, which takes constant time (O(1)). This is a primary advantage and a fundamental 'use' of arrays, making the statement true. The question asks for a *false* statement about array use; therefore, 'accessing elements at specified positions' is the correct answer because it is a *true* and beneficial aspect, unlike the other options which describe limitations or potential drawbacks.
Question 13: How long does it take to insert at the end of a dynamic array?
- O(logn)
- O(n)
- O(1)
- Either O(1) or O(n) (Correct answer)
Correct answer: Either O(1) or O(n)
Inserting an element at the end of a dynamic array typically takes O(1) time on average (amortized constant time). However, if the array's underlying storage becomes full, a new, larger array must be allocated, and all existing elements copied over. This resizing operation takes O(n) time, where 'n' is the number of elements. Thus, the time complexity can be either O(1) in most cases or O(n) in the worst-case scenario during a resize.
Question 14: How long does it take to count all the elements in the linked list?
- O(logn)
- O(1)
- O(n2)
- O(n) (Correct answer)
Correct answer: O(n)
To count all the elements in a linked list, you must traverse the entire list from the head node to the tail node. This involves visiting each node exactly once to increment a counter. Therefore, the time complexity for this operation is directly proportional to the number of elements 'n' in the list, resulting in O(n) time.
Question 15: What is a double linked list that uses little memory?
- The list has breakpoints for faster traversal
- A doubly linked list that uses bitwise AND operator for storing addresses
- Each node has only one pointer to traverse the list back and forth (Correct answer)
- An auxiliary singly linked list acts as a helper list to traverse through the doubly linked list
Correct answer: Each node has only one pointer to traverse the list back and forth
A memory-efficient doubly linked list, often called an XOR linked list, achieves its efficiency by storing only one pointer in each node. Instead of separate 'next' and 'previous' pointers, it stores the bitwise XOR of the addresses of the previous and next nodes. This allows traversal in both directions by using the address of the current node and the XOR sum to deduce the address of the other adjacent node, effectively reducing memory overhead.
Question 16: What real-world situations from the list below would you identify with a stack data structure?
- offer services based on the priority of the customer
- tatkal Ticket Booking in IRCTC
- piling up of chairs one above the other (Correct answer)
- people standing in a line to be serviced at a counter
Correct answer: piling up of chairs one above the other
A stack data structure operates on a Last-In, First-Out (LIFO) principle, meaning the last item added is the first one to be removed. Piling up chairs one above the other perfectly illustrates this concept: you add new chairs to the top, and when you want to remove a chair, you typically take the one from the very top first. This mirrors the push and pop operations of a stack.
Which of the following best sums up an array?