Ch 3: Stacks, Queues, Exceptions, Templates Flashcards

1
Q

what is a stack?

A

an ADT in which items are only inserted on or removed from the top of the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what are the two operations of stacks and what do they do?

A
  1. push: inserts an item on the top of the stack

2. pop: removes and returns the item at the top of the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is the acronym rule for stacks?

A

FILO- first in last out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what does peek do for stacks?

A

returns but does not remove the item at the top of the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

does peek change the stack?

A

no, just returns the item at the top of the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

should peek and pop be called on an empty stack? why?

A

they should not be called on empty stacks because the behavior may be undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is a queue?

A

an ADT in which items are inserted at the end of the queue and removed from the front of the queue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what are the two operations of queues and what do they do?

A
  1. enqueue: insert an item at the end of the queue

2. dequeue: removes and returns the item at the front of the queue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is the acronym rule for queues?

A

FIFO - first in first out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is a deque (deck) ?

A
  • an ADT in which items can be inserted and removed at both the front and back
  • aka “double-ended” queue”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is an array-based list?

A

a list ADT implemented using an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what happens if there is no more room for objects in the array?

A
  • array must be resized

- array size is doubled

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is the Big O for resizing and why?

A

O(N) because it has to go through each item in the array and copy it to the new array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is the Big O for push back when there is no more room in an array and why?

A
  • push back is O(1) because it always knows where the end of the list is
  • having to resize is O(N)
  • because you double the size when resizing, the impact of O(N) is minimal as it continues to do it, so, therefore, push back and having to resize is O(1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is error-checking code?

A

code a programmer writes to detect and handle errors that occur during program execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what is an exception?

A

a circumstance that a program was not designed to handle

17
Q

what are exception-handling constructs?

A
  • special constructs to keep error-checking code separate and to reduce redundant checks
  • try, catch, throw
18
Q

what is the try block?

A

surrounds normal code, which is exited immediately if a throw statement executes

19
Q

what is a throw statement?

A
  • appears within a try block; if reached, execution jumps immediately to the end of the try block
  • written so only error situations lead to reaching a throw
20
Q

what is a catch clause?

A
  • immediately follows a try block; if the catch was reached due to an exception of the catch clauses’ parameter type, the clause executes
  • catches the thrown exception
21
Q

what is a handler?

A

catch block; handles the exception

22
Q

what is the point of try-catch?

A
  • to simplify code

- so that code is not bombarded with endless if-statements checking for error

23
Q

do catch blocks have to follow try blocks?

A

yes

24
Q

explain try-catches in main & function

A

try block is in main, when it calls a function, the throw is in the function if it is in a situation that it doesn’t know how to handle. then the function ends and the throw is taken back to main and the catch is executed

25
Q

explain how throws and catches work for different types

A

throws can be for different types, therefore there are catches of different types

26
Q

what is “catch(…)”?

A
  • a catch-all handler that catches any type

- useful when listed as the last handler

27
Q

should the catch-all ( catch(…)) be before the other catches?

A

no because the catches are run in order when the exception is thrown. since the catch-all is for last resorts, it should be called after the other handlers

28
Q

what is a function template?

A

a function definition having a special type parameter that may be used in place of types in the function

29
Q

what is a type parameter?

A

used through the function for any parameter types, return types or local variable types

30
Q

what is a class template?

A

a class definition having a special type parameter that may be used in place of type in the class

31
Q

what is the point of templates?

A

avoid writing and maintaining redundant classes that only differ by data type