Unit 2 subjective questions Introduction to Array Concepts
INT322 — Computing System And Technologies 20 questions with model answers. Write yours first, then compare.
-
Question 1 of 20
Define an array. Explain how a one-dimensional array can be declared and initialized with suitable examples.
Model answer
Array: An array is a linear data structure that stores a fixed number of elements of the same data type in contiguous memory locations. Each element is accessed using an index.
Declaration:
- General form:
dataType arrayName[size]; - Example:
int marks[5];
Initialization:
- At declaration:
int marks[5] = {70, 75, 80, 85, 90}; - With inferred size:
int marks[] = {70, 75, 80, 85, 90}; - Individual assignment:
marks[0] = 70;
For an array of size , valid indexes range from to . Thus,
marks[0]is the first element andmarks[4]is the last element of an array of size . - General form:
-
Question 2 of 20
Explain the memory representation of a one-dimensional array and derive the formula used to calculate the address of an element.
Model answer
A one-dimensional array is stored in contiguous memory locations. If every element requires bytes, the addresses of successive elements differ by bytes.
Let:
- be the base address of the array.
- be the lower-bound index.
- be the size of each element in bytes.
- be the required index.
The address is:
For a zero-based array, , so:
For example, if the base address is , each integer occupies bytes, and , then:
This direct calculation enables constant-time indexed access, with complexity .
-
Question 3 of 20
What is array traversal? Describe an algorithm for traversing a one-dimensional array and state its complexity.
Model answer
Array traversal is the process of visiting every element of an array, usually from the first element to the last, to read, display, or process it.
Algorithm:
- Start with index .
- Access or process
A[i]. - Increment by .
- Repeat until .
Pseudocode:
for i = 0 to n - 1process A[i]Complexity:
- Time complexity: , because all elements are visited.
- Auxiliary space complexity: , if only a loop variable is used.
Traversal is commonly used for printing, summing, counting, searching, and updating array elements.
-
Question 4 of 20
Describe how an element is inserted at a specified position in an array. Include an algorithm and complexity analysis.
Model answer
To insert an element into an array, sufficient unused capacity must be available. Elements at and after the insertion position must be shifted one place to the right.
Algorithm for inserting
itemat indexpos:- Check whether the array has free capacity.
- Validate that .
- Starting from index , shift each element right until
posis reached. - Store
itematA[pos]. - Increase the logical size by .
Pseudocode:
for i = n - 1 down to posA[i + 1] = A[i]A[pos] = itemn = n + 1Complexity:
- Insertion at the end: , when capacity is available.
- Insertion at the beginning: .
- Insertion at an arbitrary position: in the worst case.
-
Question 5 of 20
Explain the deletion of an element from an array. Why is shifting required, and what is the operation's complexity?
Model answer
Array deletion removes an element from a specified index while preserving the order and contiguous logical arrangement of the remaining elements.
Algorithm for deleting the element at index
pos:- Check that the array is not empty.
- Validate that .
- Save
A[pos]if the removed value is required. - Shift every element after
posone position to the left. - Decrease the logical size by .
Pseudocode:
item = A[pos]for i = pos to n - 2A[i] = A[i + 1]n = n - 1Shifting closes the gap created by deletion.
Complexity:
- Deletion from the end: .
- Deletion from the beginning: .
- Worst-case deletion from an arbitrary position: .
-
-
Question 6 of 20
Define linear search and explain its algorithm, applications, and complexity.
Model answer
Linear search examines array elements sequentially until the required key is found or the end of the array is reached. It works with both sorted and unsorted arrays.
Algorithm:
- Begin at index .
- Compare the current element with the search key.
- Return the index if they are equal.
- Otherwise, continue to the next element.
- Return failure after all elements have been checked.
Pseudocode:
for i = 0 to n - 1if A[i] == keyreturn ireturn -1Complexity:
- Best case: , when the first element matches.
- Average case: .
- Worst case: , when the key is last or absent.
- Auxiliary space: .
It is suitable for small or unsorted collections.
-
Question 7 of 20
Explain binary search with an algorithm. State its prerequisite and derive its time complexity.
Model answer
Binary search repeatedly divides the search interval into two halves. Its essential prerequisite is that the array must be sorted.
Algorithm:
- Set
low = 0andhigh = n - 1. - Calculate
mid = low + (high - low) / 2using integer division. - If
A[mid]equals the key, returnmid. - If the key is smaller, set
high = mid - 1. - If the key is larger, set
low = mid + 1. - Repeat while
low <= high; otherwise, report failure.
After iterations, the remaining size is approximately . The process ends when:
Therefore:
Complexity:
- Best case: .
- Average and worst cases: .
- Iterative auxiliary space: .
- Set
-
Question 8 of 20
Compare linear search and binary search.
Model answer
Linear search and binary search differ as follows:
- Data requirement: Linear search works on sorted or unsorted data, whereas binary search requires sorted data.
- Method: Linear search checks elements sequentially; binary search repeatedly halves the search interval.
- Best-case time: Both can take .
- Worst-case time: Linear search takes , whereas binary search takes .
- Implementation: Linear search is simpler; binary search requires careful management of low, high, and middle indexes.
- Suitable use: Linear search is useful for small or frequently changing unsorted data. Binary search is preferable for large, sorted arrays.
Although binary search is faster for repeated searches, the cost of sorting must be considered if the original data is unsorted.
-
Question 9 of 20
Describe bubble sort with an example and analyze its best- and worst-case complexity.
Model answer
Bubble sort repeatedly compares adjacent elements and swaps them when they are in the wrong order. After each pass, the largest unsorted element moves to its final position.
For
[5, 2, 4, 1], the first pass performs:- Compare and :
[2, 5, 4, 1] - Compare and :
[2, 4, 5, 1] - Compare and :
[2, 4, 1, 5]
The value is now in its correct final position. Further passes produce
[1, 2, 4, 5].Optimized algorithm: Use a Boolean
swappedflag and stop if a complete pass performs no swaps.Complexity:
- Best case with the optimization: .
- Average case: .
- Worst case: .
- Auxiliary space: .
Bubble sort is stable when equal elements are not swapped.
- Compare and :
-
Question 10 of 20
Derive the worst-case number of comparisons performed by bubble sort on an array of elements.
Model answer
In standard bubble sort, the first pass makes comparisons. The second makes comparisons because the largest element is already in position. This continues until the final pass makes one comparison.
Thus, the total number of comparisons is:
Using the sum of the first positive integers:
Expanding gives:
The term dominates as grows, so the worst-case time complexity is . In a reverse-sorted array, a swap may be required after every comparison, resulting in up to swaps as well.
-
Question 11 of 20
Analyze and compare the time complexities of common array operations.
Model answer
Complexities of common array operations:
- Access by index: because the element address is calculated directly.
- Traversal: because every element is visited.
- Linear search: Best case ; worst case .
- Binary search: Best case ; worst case , provided the array is sorted.
- Insertion at the end: when unused capacity is available.
- Insertion at the beginning or middle: because elements may need to be shifted.
- Deletion from the end: .
- Deletion from the beginning or middle: because remaining elements must be shifted.
- Bubble sort: Average and worst cases are .
These results show that arrays provide fast random access but can make position-based insertion and deletion expensive.
-
Question 12 of 20
Define a stack and explain its basic operations and applications.
Model answer
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. The most recently inserted element is removed first. All insertions and deletions occur at one end called the top.
Basic operations:
- Push: Adds an element to the top.
- Pop: Removes and returns the top element.
- Peek or Top: Reads the top element without removing it.
- isEmpty: Checks whether the stack contains no elements.
- isFull: Checks whether an array-based stack has reached its capacity.
- Traversal: Visits stack elements, generally from top to bottom.
Applications:
- Function-call and recursion management
- Expression evaluation and conversion
- Parenthesis matching
- Undo and redo operations
- Backtracking and depth-first search
Push, pop, and peek normally take time.
-
Question 13 of 20
Explain how an array-based stack is traversed. Why should traversal not modify the value of
top?Model answer
Stack traversal visits the stored elements without inserting or deleting them. In an array-based stack,
topcontains the index of the most recently pushed element.To traverse in LIFO order, begin at
topand move toward index .Pseudocode:
if top == -1report "Stack is empty"elsefor i = top down to 0process stack[i]The loop uses a separate variable
i. It must not decrementtop, becausetoprepresents the actual state of the stack. Changing it during traversal would logically remove elements or make them inaccessible.Complexity:
- Time complexity: for stored elements.
- Auxiliary space: for iterative traversal.
-
Question 14 of 20
Describe the push operation in an array-based stack, including the overflow test and complexity.
Model answer
The push operation inserts a new element at the top of a stack. For an array stack of capacity
MAX,topis initially .Overflow condition:
top == MAX - 1If this condition is true, no free position remains and insertion cannot proceed.
Push algorithm:
- Test whether
top == MAX - 1. - If true, report stack overflow.
- Otherwise, increment
top. - Assign the new value to
stack[top].
Pseudocode:
if top == MAX - 1report "Stack overflow"elsetop = top + 1stack[top] = itemThe operation takes time and auxiliary space because no traversal or shifting is required.
- Test whether
-
Question 15 of 20
Describe the pop operation and distinguish between stack underflow and stack overflow.
Model answer
The pop operation removes and returns the element at the top of a stack.
Pop algorithm:
- Check whether
top == -1. - If true, report stack underflow.
- Otherwise, save
stack[top]. - Decrement
top. - Return the saved element.
Pseudocode:
if top == -1report "Stack underflow"elseitem = stack[top]top = top - 1return itemUnderflow versus overflow:
- Underflow occurs when pop or peek is attempted on an empty stack.
- Overflow occurs when push is attempted on a full fixed-size stack.
Pop normally takes time and auxiliary space.
- Check whether
-
Question 16 of 20
Define a queue and explain its basic operations and common applications.
Model answer
A queue is a linear data structure that follows the First In, First Out (FIFO) principle. The element inserted first is removed first. Insertion occurs at the rear, while deletion occurs at the front.
Basic operations:
- ENQ or enqueue: Inserts an element at the rear.
- DEQ or dequeue: Removes and returns the front element.
- Front or peek: Reads the front element without removing it.
- isEmpty: Checks whether the queue has no elements.
- isFull: Checks whether a fixed-size queue has reached capacity.
- Traversal: Visits elements from front to rear.
Applications:
- CPU and process scheduling
- Printer spooling
- Request handling in servers
- Breadth-first search
- Input and output buffering
Properly implemented ENQ and DEQ operations take time.
-
Question 17 of 20
Explain queue traversal for both a linear queue and a circular queue.
Model answer
Queue traversal visits active queue elements in FIFO order, beginning at
frontand ending atrear.Linear queue:
- If
front == -1, the queue is empty. - Otherwise, process elements from index
frontthroughrear.
for i = front to rearprocess queue[i]Circular queue: Indexes wrap around the array. Begin at
front, process each element, and advance with:Stop after processing the element at
rear.Important point: Traversal should use a temporary index and must not alter
frontorrear, because they define the queue's current state.For active elements, traversal takes time and auxiliary space.
- If
-
Question 18 of 20
Describe the ENQ operation in an array-based linear queue and discuss its overflow condition.
Model answer
The ENQ operation inserts a new element at the rear of a queue. Assume both
frontandrearare initially and the array capacity isMAX.Overflow condition for a linear queue:
rear == MAX - 1Algorithm:
- If
rear == MAX - 1, report queue overflow. - If the queue is initially empty, set
front = 0. - Increment
rear. - Store the item at
queue[rear].
Pseudocode:
if rear == MAX - 1report "Queue overflow"elseif front == -1front = 0rear = rear + 1queue[rear] = itemENQ takes time. A limitation of a linear array queue is that freed positions before
frontcannot be reused without shifting or converting the structure into a circular queue. - If
-
Question 19 of 20
Describe the DEQ operation in an array-based queue, including how the queue is reset after its last element is removed.
Model answer
The DEQ operation removes and returns the element at the front of a queue.
Algorithm:
- If
front == -1orfront > rear, report queue underflow. - Save the element at
queue[front]. - If
front == rear, the removed element was the last one, so reset both indexes to . - Otherwise, increment
front. - Return the saved element.
Pseudocode:
if front == -1 or front > rearreport "Queue underflow"elseitem = queue[front]if front == rearfront = rear = -1elsefront = front + 1return itemDEQ takes time because it changes only the front index and does not shift elements.
- If
-
Question 20 of 20
Explain queue underflow and overflow conditions. How does a circular queue improve the use of array space?
Model answer
Queue underflow occurs when DEQ or front access is attempted on an empty queue. In a typical array implementation, emptiness may be represented by
front == -1.Queue overflow occurs when ENQ is attempted but no usable position is available.
For a linear queue, the overflow test is usually:
rear == MAX - 1This can cause false overflow: positions before
frontmay be empty but cannot be reused becauserearhas reached the final array index.A circular queue treats the array's end as connected to its beginning. Indexes advance using:
Its full condition is:
Its empty condition is commonly represented by:
By wrapping
rearto the beginning, a circular queue reuses positions freed by DEQ operations. This improves space utilization while retaining ENQ and DEQ operations.
That's unit 2
Long answers stick best when they're tested again soon. Run the MCQs on this unit, then come back to the questions you found hardest.
Nearly through. Did unit 2 of INT322 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 2 of INT322 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill; the rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks.