Quiz: Data structures stack and queue implementation

This set of flashcards covers the implementation of stack and queue data structures, including their definitions, operations, and applications in computer science.

Panda14·48 flashcards·48 questions
collegecomputer_sciencealgorithms
0
Known
1 / 48
0
Learning
Front

What is a stack?

Tap to flip
Back

A stack is a linear data structure that follows the Last In First Out (LIFO) principle, meaning the last element added is the first one to be removed.

Tap to flip
Got it
Still learning

Quiz(48 questions)

Question 1 of 48

1. What is the primary principle that a stack operates on?

Terms in this Study Set(48)

Stack Basics(16)

What is a stack?

A stack is a linear data structure that follows the Last In First Out (LIFO) principle, meaning the last element added is the first one to be removed.

Key operations of a stack?

- Push: Add an item - Pop: Remove the top item - Peek: View the top item without removing it

True or False: A stack allows random access to its elements.

False. Stacks only allow access to the top element, following the LIFO principle.

Comparing Stack and Queue: Access order?

Stack: Last In First Out (LIFO) Queue: First In First Out (FIFO)

What does 'push' do?

Push adds an element to the top of the stack, increasing its size by one.

What does 'pop' return?

Pop returns the top element of the stack and removes it from the stack.

Fill in the blank: The _____ operation does not change the stack but shows the top element.

Peek

Example of using a stack?

Using a stack to track function calls in programming. Each call is pushed onto the stack, and completed calls are popped.

What is an empty stack?

An empty stack is a stack with no elements. It cannot perform pop or peek operations.

True or False: You can remove a bottom element without removing others in a stack.

False. You can only remove the top element in a stack.

What happens during a stack overflow?

Stack overflow occurs when too many elements are pushed onto the stack, exceeding its capacity.

Describe the stack data structure.

A stack is often implemented using arrays or linked lists, with operations limited to the top element.

Cause → Effect: Push operation followed by pop.

Push adds an element, then pop retrieves and removes the last added element.

What is 'stack underflow'?

Stack underflow occurs when a pop operation is attempted on an empty stack.

Difference between stack and array?

Stack: LIFO access Array: Random access to any element

True or False: Stacks are ideal for backtracking algorithms.

True. Stacks help remember previous states during backtracking.

Queue Fundamentals(16)

What is a queue?

A queue is a linear data structure that follows the First In, First Out (FIFO) principle. Elements are added at the back and removed from the front.

Key operations of a queue?

- Enqueue: Add an element - Dequeue: Remove an element - Peek: View the front element - IsEmpty: Check if the queue is empty

True or False: A queue allows random access.

False. A queue does not allow random access; elements can only be accessed in the order they were added.

Fill in the blank: The queue data structure is based on the _____ principle.

FIFO (First In, First Out)

What is the time complexity of enqueue?

The time complexity of enqueue operation in a queue is O(1) since it involves adding an element to the back.

Cause → Effect: What happens when you dequeue from an empty queue?

An error or exception is raised indicating that no elements are available to remove.

What is the difference between a queue and a stack?

A queue uses FIFO, while a stack uses LIFO (Last In, First Out). This affects how elements are added and removed.

Describe the peek operation.

The peek operation retrieves the front element of the queue without removing it, allowing you to check which item is next in line.

Example of enqueue operation.

If a queue contains [1, 2, 3] and you enqueue 4, the queue becomes [1, 2, 3, 4].

What is the primary use of queues?

Queues are commonly used in scenarios like scheduling, managing tasks, and handling requests in servers.

What data structure can implement a queue?

A queue can be implemented using arrays or linked lists, each having its own pros and cons.

True or False: Queues can be circular.

True. Circular queues allow for efficient use of space by connecting the end of the queue back to the front.

What do we call a queue that can grow in size?

A dynamic queue. Unlike static queues, dynamic queues can expand as needed.

Time complexity of dequeue?

The time complexity of the dequeue operation is O(1), as it simply removes the front element.

What is meant by 'queue underflow'?

Queue underflow occurs when a dequeue operation is attempted on an empty queue, leading to errors.

How do you check if a queue is empty?

You can check if the queue's size is 0 or use an isEmpty() function, returning true if it is empty.

Applications of Stacks and Queues(16)

What is a common application of stacks?

Stacks are used in function calls to maintain the order of execution and keep track of local variables.

True or False: Stacks can be used for undo mechanisms.

True. Stacks store previous states, allowing users to revert to earlier actions.

Fill in the blank: A stack is often used in ____ parsing.

syntax. It helps manage nested structures.

What data structure is used in breadth-first search?

Queues are used to explore nodes level by level.

Compare stacks and queues in terms of data access.

Stacks follow LIFO (Last In, First Out), while queues follow FIFO (First In, First Out).

What application uses queues for managing requests?

Web servers use queues to manage incoming requests efficiently.

Describe how stacks assist in backtracking.

Stacks track previous states, allowing the algorithm to return to prior positions easily.

True or False: Queues are ideal for scheduling tasks.

True. Queues help manage tasks in the order they arrive.

What is an example of a stack in a programming language?

Programming languages use call stacks to manage function calls and returns.

List two features of queues in real-time applications.

- Task scheduling - Print job management

How do stacks help in expression evaluation?

Stacks are used to evaluate postfix (RPN) and infix expressions by managing operators and operands.

What role do queues play in handling customer service?

Queues manage customers as they arrive, ensuring they are served in the order they enter.

Fill in the blank: Stacks are essential for ____ algorithms.

recursive. They maintain state during recursive calls.

What is a real-world example of a stack?

Browser history allows users to navigate back through recently visited pages.

Name a common data structure used in message passing.

Queues are often used to handle messages between processes or threads.

Cause of using stacks in programming?

They provide a simple way to manage function calls and memory.

Questions in this Study Set(48)

1. What is the primary principle that a stack operates on?

A.Last In First Out (LIFO)
B.First In First Out (FIFO)
C.Random Access
D.Circular Queue

2. What does FIFO stand for in the context of queues?

A.First In, First Out
B.First In, Final Out
C.Final In, First Out
D.First Out, First In

3. What is a primary application of stacks in programming?

A.Function call management
B.Data sorting
C.Memory allocation
D.File storage

4. Which operation would you use to add an element to a stack?

A.Pop
B.Push
C.Peek
D.Shift

5. Which operation adds an element to a queue?

A.Dequeue
B.Enqueue
C.Peek
D.IsEmpty

6. True or False: Stacks can be utilized to implement backtracking algorithms.

A.True
B.False
C.Only in recursive scenarios
D.Only in iterative scenarios

7. What does the 'Peek' operation do in a stack?

A.Adds an element
B.Removes the top element
C.Shows the top element without removing it
D.Clears the stack

8. What will happen if you try to dequeue from an empty queue?

A.The queue will be emptied
B.An element will be returned
C.An error or exception will occur
D.The queue will become full

9. Fill in the blank: A queue is commonly used in ____ management systems.

A.memory
B.task
C.data
D.graph

10. In what scenario would you experience a stack overflow?

A.When the stack is empty
B.When too many elements are pushed onto the stack
C.When you remove an element
D.When you access an element directly

11. Which of the following is NOT a common operation of a queue?

A.Enqueue
B.Dequeue
C.Peek
D.Push

12. What data structure is primarily used for depth-first search?

A.Stack
B.Queue
C.Array
D.Tree

13. Which of the following statements is true regarding stacks?

A.You can access any element at any time
B.You can only remove the top element
C.Stacks are always implemented using linked lists
D.Stacks can only hold integers

14. What is the primary purpose of a queue in computing?

A.Random access of items
B.Storing data permanently
C.Handling tasks in order
D.Storing large datasets

15. How do stacks compare to queues in terms of data retrieval?

A.LIFO vs. FIFO
B.FIFO vs. LIFO
C.Random access vs. Sequential access
D.Ordered vs. Unordered

16. What happens during a stack underflow?

A.An element is added to the stack
B.The stack becomes empty
C.A pop operation is called on an empty stack
D.The stack grows in size

17. How can you determine if a queue is empty?

A.Check if size is zero
B.Check if the first item is null
C.Count the number of items
D.Always assume it's empty

18. Which application relies on queues for managing tasks?

A.Game state management
B.Web server request handling
C.Data storage
D.User preference settings

19. What is one way stacks are commonly used in programming?

A.Tracking the current state of a program
B.Sorting data
C.Searching elements
D.Storing large datasets

20. In what scenario is a circular queue particularly useful?

A.When data needs to be accessed randomly
B.To use space efficiently
C.When order of processing is irrelevant
D.For storing large amounts of data

21. What is the role of a stack in expression evaluation?

A.To store all expressions
B.To sort expressions
C.To manage operators and operands
D.To compile expressions

22. Which of the following is NOT a characteristic of a stack?

A.LIFO structure
B.Random access to elements
C.Push and pop operations
D.Limited access to the top element

23. What is the time complexity of the dequeue operation?

A.O(n)
B.O(log n)
C.O(1)
D.O(n^2)

24. True or False: Queues are suitable for implementing a priority scheduling algorithm.

A.True
B.False
C.Only for simple tasks
D.Only for high-priority tasks

25. What is the result of performing a pop operation on an empty stack?

A.The stack returns null
B.The stack throws an exception
C.The stack deletes itself
D.The stack returns the top element

26. Which of the following describes the peek operation?

A.Adds an element to the back
B.Removes the front element
C.Returns the front element without removing it
D.Checks if the queue is empty

27. Which scenario is NOT a typical use case for stacks?

A.Undo operations
B.Function call management
C.Task scheduling
D.Expression evaluation

28. How does a stack compare to a queue in terms of access order?

A.Both use FIFO
B.Both use LIFO
C.Stack uses LIFO, queue uses FIFO
D.Stack uses FIFO, queue uses LIFO

29. What is queue underflow?

A.Adding to a full queue
B.Removing an element from an empty queue
C.Queue size exceeding its capacity
D.Queue operations taking too long

30. How do queues function in customer service systems?

A.To prioritize customers
B.To manage customer flow
C.To track customer data
D.To analyze customer behavior

31. If you push three elements onto a stack and then pop two, how many elements remain in the stack?

A.One
B.Two
C.Three
D.Zero

32. What distinguishes a queue from a stack?

A.Queues are faster
B.Stacks are LIFO and queues are FIFO
C.Queues are circular
D.Stacks allow random access

33. Fill in the blank: Stacks are essential for ____ algorithms, as they keep track of execution states.

A.recursive
B.iterative
C.sorting
D.searching

34. Which operation would you use to remove the top element of a stack?

A.Push
B.Peek
C.Pop
D.Add

35. Which data structures can be used to implement a queue?

A.Only arrays
B.Only linked lists
C.Both arrays and linked lists
D.Only hash tables

36. What is a common example of a queue in everyday applications?

A.Call stack
B.Waiting line at a bank
C.Browser history
D.File system

37. Why are stacks useful in backtracking algorithms?

A.They allow random access
B.They store all elements
C.They follow LIFO, helping retrace steps
D.They are faster than queues

38. What is a dynamic queue?

A.A queue with fixed size
B.A queue that can grow in size
C.A queue that only allows unique elements
D.A queue that does not allow duplicates

39. Which of the following is a reason for using stacks in programming?

A.To optimize data retrieval
B.To manage function call states
C.To sort data efficiently
D.To store large datasets

40. What type of data structure is a stack commonly implemented with?

A.Linked list or array
B.Tree
C.Graph
D.Set

41. What is the effect of enqueueing an element when the queue is full?

A.The queue expands automatically
B.An error or exception occurs
C.The oldest element is removed
D.The new element is added at the front

42. Which data structure is commonly used for inter-process communication?

A.Stack
B.Queue
C.Linked list
D.Array

43. Which of the following operations does not change the contents of the stack?

A.Push
B.Pop
C.Peek
D.Clear

44. What is the expected time complexity of the isEmpty() operation?

A.O(n)
B.O(log n)
C.O(1)
D.O(n^2)

45. How do stacks assist in maintaining history in applications?

A.By deleting old entries
B.By storing previous states
C.By compressing data
D.By encrypting information

46. Which of the following best describes the stack data structure?

A.A stack allows access to the last element added only.
B.A stack allows access to any element randomly.
C.A stack follows the First In First Out principle.
D.A stack can be implemented using a binary tree.

47. What happens to the order of elements in a queue when you dequeue an element?

A.The front element is removed and the order of remaining elements is preserved.
B.The last element added is removed, altering the order significantly.
C.Elements are shuffled randomly after a dequeue operation.
D.The queue becomes empty immediately after one dequeue.

48. Which of the following is NOT an application of queues?

A.Task scheduling
B.Print job management
C.Browser history navigation
D.Web request handling

Related Study Sets

Create Your Own Study Set

Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.