6 Lecture
CS301
Midterm & Final Term Short Notes
Stack From the Previous Lecture
A stack is an abstract data type that represents a collection of elements with two main operations: push, which adds an element to the top of the stack, and pop, which removes the top element from the stack. It follows the Last-In-First-Out (LIF
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
- Which of the following is NOT a characteristic of a stack? a. Follows LIFO principle b. Has two main operations: push and pop c. Can only be implemented using arrays d. Topmost element is the last one added
Answer: c. Can only be implemented using arrays
- What is the time complexity of push and pop operations in a stack implemented using an array? a. O(1) b. O(log n) c. O(n) d. O(n^2)
Answer: a. O(1)
- Which data structure is often used to implement a stack? a. Array b. Linked List c. Queue d. Binary Tree
Answer: b. Linked List
- Which of the following is NOT a common application of stacks? a. Reversing a string b. Evaluating postfix expressions c. Implementing depth-first search in a graph d. Sorting an array
Answer: d. Sorting an array
- Which operation in a stack does not modify the stack? a. Push b. Pop c. Peek d. Size
Answer: c. Peek
- What happens when we try to pop an element from an empty stack? a. The program crashes b. An error message is displayed c. The topmost element becomes NULL d. Nothing happens
Answer: b. An error message is displayed
- Which of the following is NOT a disadvantage of using an array to implement a stack? a. Fixed size b. Elements must be contiguous in memory c. Dynamic resizing is difficult d. Push operation is slower than pop operation
Answer: d. Push operation is slower than pop operation
- What is the maximum number of elements a stack implemented using an array can hold if its size is n? a. n b. n-1 c. 2n d. 2n-1
Answer: b. n-1
- Which of the following is NOT a potential application of stacks in computer science? a. Function calls and return values b. Undo and redo operations c. Implementing breadth-first search in a graph d. Checking for balanced parentheses in an expression
Answer: c. Implementing breadth-first search in a graph
- What is the time complexity of searching for an element in a stack implemented using a linked list? a. O(1) b. O(log n) c. O(n) d. O(n^2)
Answer: c. O(n)
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is a stack, and how does it follow the LIFO principle? Answer: A stack is an abstract data type that represents a collection of elements with two main operations: push, which adds an element to the top of the stack, and pop, which removes the top element from the stack. It follows the Last-In-First-Out (LIFO) principle, where the last element added is the first to be removed.
How can you implement a stack using an array? What are the advantages and disadvantages of this approach? Answer: A stack can be implemented using an array by maintaining a variable to keep track of the topmost element's index. Advantages of this approach include constant time complexity for push and pop operations and efficient use of memory. Disadvantages include fixed size and difficulty in dynamic resizing.
How can you implement a stack using a linked list? What are the advantages and disadvantages of this approach? Answer: A stack can be implemented using a linked list by using the head of the list to represent the topmost element. Advantages of this approach include dynamic resizing, efficient use of memory, and easy implementation. Disadvantages include slower access time than an array-based implementation.
What is the purpose of the peek operation in a stack? Answer: The peek operation allows you to view the topmost element in the stack without removing it.
What happens when you try to pop an element from an empty stack? Answer: When you try to pop an element from an empty stack, an error message is displayed.
What is the time complexity of push and pop operations in a stack implemented using a linked list? Answer: The time complexity of push and pop operations in a stack implemented using a linked list is O(1).
How can stacks be used to evaluate postfix expressions? Answer: Stacks can be used to evaluate postfix expressions by iterating over the expression and performing operations based on the current element. When an operand is encountered, it is pushed onto the stack. When an operator is encountered, the two most recent operands are popped from the stack, and the operation is performed, with the result being pushed back onto the stack.
What is a potential application of stacks in checking for balanced parentheses in an expression? Answer: A stack can be used to check for balanced parentheses in an expression by pushing opening parentheses onto the stack and popping them off when a closing parenthesis is encountered. If the stack is empty at the end of the expression, then the parentheses are balanced.
What is a potential disadvantage of using an array to implement a stack? Answer: A potential disadvantage of using an array to implement a stack is that the size is fixed and cannot be dynamically resized.
What is the time complexity of searching for an element in a stack implemented using an array? Answer: The time complexity of searching for an element in a stack implemented using an array is O(n), as you need to iterate over each element to find the desired one.