Lab Demo#1 Flashcards

(17 cards)

1
Q

In terms of data processing time, what are your findings from lab exercise 1?

A

iterative methods are faster than recursive methods
-Recursive methods require additional space in the call stack
and time to create and destroy multiple stack frames

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

What are your findings on a recursive solution to any problem in terms of processing time, and memory requirement?

A

-require less lines of code
However:
-Requires more space to store variables and return addresses
-Eventually has to remove the stack frame when the function completes which creates overhead

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

When would you prefer iterative solutions over recursive ones?

A

-Whenever memory efficiency is important
for simple tasks such as:
-iterating through a data structure

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

True or False: the same code, executed in the same machine, always results in the same execution time between two runs

A

False (depends on how the system works in the background)
-other programs may run at the same time
-the JIT compiler optimizes the code faster with each run

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

True or False: Although factorial operation is only true for integer-type data, depending on the datatype, we choose to hold on to the result of the factorial operation, we may encounter data overflow.

A

True (the data type chosen can lead to overflow)
-Overflow can happen if the result of the factorial is greater than the capacity of int or long variable types

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

If you need to find the max and min values from the data set where the available data items are between 18 and 65, what will be your choice in selecting the reference for min and max.

A

Use integer type variables to initialize 18 as the “max” variable, and “min” as 65
-Iterate through the data set using a for loop, updating “max” since every number inside the data structure would be greater than 18 and meet the condition for the loop

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

If we can handle any expected error related issue in our code without checking Exceptions, we should that path. What is the rationale for this comment?

A

Yes
-expected errors should be mitigated with use of loops or if else statements
-Catching and throwing Exceptions can slow your program and requires memory
-Exceptions are meant for unexpected conditions not anticipated errors

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

Exception handling separates error handling code from normal programming tasks, thus making programs easier to read and to modify. However, what is the price we need to pay to enjoy this advantage?

A

Exception handling requires more time and resources because it requires:
-instantiating a new exception object
-going back to the program’s steps to figure out where the error happened
-sending the error back to the part of the program that can handle it

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

What do we mean by “exception is thrown”?

A

means that something unexpected happened in the code which requires a special block of code to deal with it safely and quickly

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

What is the rationale behind declaring the Pair<> class as a generic one?

A

Don’t need to create different class for different data types such as Pair<String, Integer> or Pair<Double, String>
-Makes the code usable time and time again for different programs

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

What is the disadvantage of using arrays instead of linked list to realize a queue or a stack. Is there any advantage in using an array to realize stack and queue?

A

-length of arrays need to manually shrink or grown
-arrays are faster than linked lists
-arrays are simpler than linked lists
-easier to access the elements of a stack or queue using an array

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

Is the implementation of stack and queue, with the help of arrays, truly dynamic?

A

No (arrays have a fixed size)
-would need to create a new array, copy the elements from the old array into the new and continue with the program
-this takes up more lines of code, time, and memory

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

What do we mean by dynamic and static data structures?

A

Static data structures have a fixed size at compilation
-End up wasting space if you don’t have enough indices for your data thus creating a new array
Dynamic data structures can shrink and grow during runtime
-makes use of memory more wisely

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

How can you realize a stack using queue data structure?

A

1 Method (create 2 queues)
-1 queue will store the inputted values
-Other queue will store the removed values from the first queue until only one index is left
-The one index is supposed to be the Last In, First Out aka the popped element of the stack

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

Instead of declaring as a local reference variable, what is the rationale behind using the Scanner reference variable as a field in the driver class?

A

-The Scanner variable is accessible across all the methods
-Simplifies the code without making it redundant
-Easier to close the Scanner at the end of the program

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

Explain with diagram how you implemented removeFromLastIndex() method. Also, removeFromFirstIndex(), addAtFirstIndex(), and toStrings() methods.

A

-Do this on paper

17
Q

Explain the advantage of implementing the toString() method

A

-We override the toString() to convert arrays into string when displaying it
-If not, you would see the hash code of the object