Java: Final Exam

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/85

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

86 Terms

1
New cards

What is time complexity?

The amount of work an algorithm performs as input size grows, usually measured in number of steps rather than real time.

2
New cards

What is space complexity?

The amount of additional memory an algorithm needs as input size increases.

3
New cards

What does algorithm scaling mean?

How algorithm performance changes as input size grows; good scaling means performance remains practical for large inputs.

4
New cards

Linear search — best case

O(1): target is first element checked.

5
New cards

Linear search — worst case

O(n): must examine every element when the value is absent.

6
New cards

Binary search requirement

Array must be sorted before binary search can be used.

7
New cards

Binary search — best case

O(1): middle element matches target.

8
New cards

Binary search — worst case

O(log n): halves search space until exhausted.

9
New cards

Why worst-case analysis is used

Provides guaranteed performance bounds and avoids assumptions about typical inputs.

10
New cards

Average-case analysis difficulty

Requires specifying a probability distribution of inputs, which is usually not straightforward.

11
New cards

Definition of best-case performance

Situation where algorithm completes with minimal possible work.

12
New cards

Definition of worst-case performance

Situation where algorithm completes with maximal possible work.

13
New cards

System.currentTimeMillis purpose

Measures elapsed time in milliseconds; used for timing experiments but has limited precision.

14
New cards

How to experimentally measure performance

Run code with increasing input sizes and measure runtime to identify growth trends.

15
New cards

Binary search recurrence

T(n) = T(n/2) + O(1)

16
New cards

Difference between time and space complexity

Time is number of executed steps; space is memory required beyond input.

17
New cards

What is an interface?

A contract specifying methods a class must implement.

18
New cards

Why use interfaces for lists?

Allows switching between list implementations (ArrayList, LinkedList) without changing client code.

19
New cards

ArrayList underlying structure

Dynamic array that resizes as needed.

20
New cards

ArrayList access time

O(1) random access; O(n) insert/delete in middle.

21
New cards

LinkedList underlying structure

Sequence of nodes linked by references.

22
New cards

LinkedList access time

O(n) to access by index; O(1) insert/remove at known node.

23
New cards

When to use ArrayList

When frequent random access is needed.

24
New cards

When to use LinkedList

When frequent insertions or deletions occur at ends.

25
New cards

What is encapsulation?

Hiding internal data and exposing functionality through public methods.

26
New cards

What is a class?

A blueprint describing the fields and behaviors of an object.

27
New cards

What is an object?

An instance of a class containing actual data and methods.

28
New cards

Definition of recursion

A method solving a problem by calling itself with smaller inputs.

29
New cards

Two essential parts of recursion

Base case + recursive case.

30
New cards

What is a base case?

The condition under which recursion stops; returns a direct answer.

31
New cards

What is a recursive case?

The step where the method calls itself on a smaller subproblem.

32
New cards

Why base cases are required

To ensure the recursion eventually terminates.

33
New cards

Requirement for shrinking subproblems

Each recursive call must get closer to the base case.

34
New cards

Example: factorial recursion

n! = n * (n − 1)! with base case 0! = 1.

35
New cards

Call stack in recursion

Each recursive call pushes a new frame with its own parameters and locals.

36
New cards

Advantages of recursion

More natural expression of problems like trees, lists, divide-and-conquer.

37
New cards

Disadvantages of recursion

More memory use; risk of stack overflow.

38
New cards

Template for recursive methods

Define base case(s), define recursive case(s), combine results.

39
New cards

What does power(n) compute?

Computes 2ⁿ recursively.

40
New cards

Base case of power(n)

n == 0 → return 1.

41
New cards

Recursive case of power(n)

2 * power(n−1)

42
New cards

How power(n) shrinks input

Decreases n by 1 each call.

43
New cards

Small work in power(n)

Multiply recursive result by 2.

44
New cards

What does check(s, balance) compute?

Returns true if parentheses in s are balanced.

45
New cards

check base case: balance < 0

Returns false immediately.

46
New cards

check base case: empty string

Returns (balance == 0).

47
New cards

check recursive behavior

'(' → +1 balance; ')' → −1 balance; other chars → unchanged.

48
New cards

How check shrinks input

Uses s.substring(1) to remove first character.

49
New cards

Small work in check()

Examines first character and adjusts balance.

50
New cards

What does countOnes(n) do?

Counts the number of digit 1s in integer n.

51
New cards

countOnes base case

n == 0 → return 0.

52
New cards

countOnes recursive case

If last digit is 1 → 1 + countOnes(n/10); else → countOnes(n/10).

53
New cards

How countOnes shrinks input

Dividing n by 10 removes last digit each call.

54
New cards

Small work in countOnes

Check last digit and add 1 if equal to 1.

55
New cards

What does printReverse(n) output?

Prints n, n−1, …, 1.

56
New cards

printReverse base case

If n == 0, stop.

57
New cards

printReverse recursive case

Print n, then printReverse(n−1)

58
New cards

Small work in printReverse

Print the current value.

59
New cards

How isPalindrome works

Checks first and last chars; if equal, recurses on substring between them.

60
New cards

isPalindrome base case

String of length 0 or 1 is a palindrome.

61
New cards

isPalindrome recursive case

isPalindrome(s.substring(1, s.length−1))

62
New cards

Small work in isPalindrome

Compare first and last characters.

63
New cards

What does rightTriangle do?

Prints a right-aligned triangle of '*' characters.

64
New cards

rightTriangle base case

If level > height, stop.

65
New cards

rightTriangle recursive case

Print 'level' stars, then call rightTriangle(height, level+1)

66
New cards

Small work in rightTriangle

Print one row of '*'.

67
New cards

General rule for recursion design

Identify base case, identify how problem shrinks, identify how recursive result is combined with small local work.

68
New cards

Why recursion is useful for lists

Recursive structure of lists (head + rest) maps cleanly to recursive definitions.

69
New cards

Why recursion is useful for trees

Trees are inherently recursive: a tree contains subtrees.

70
New cards

Difference between direct and indirect recursion

Direct recursion calls itself; indirect recursion calls another method that eventually calls original.

71
New cards

How stack overflow occurs

Recursion depth becomes too large due to missing/incorrect base case or insufficient shrinking.

72
New cards

Iterative vs recursive efficiency

Iterative often uses less memory; recursive sometimes simpler and clearer.

73
New cards

Binary search small work

Compare mid element to target.

74
New cards

Binary search shrink method

Half the search range by adjusting left/right pointers.

75
New cards

Purpose of algorithm analysis

Predict performance growth independent of machine or language.

76
New cards

What does O(n) mean?

Runtime grows linearly with input size.

77
New cards

What does O(log n) mean?

Runtime grows slowly; each step reduces problem size multiplicatively.

78
New cards

Example of divide-and-conquer merge sort

Divide list in half, recursively sort halves, merge results.

79
New cards

Why dynamic arrays require resizing

To accommodate additional elements when capacity is reached.

80
New cards

Resizing cost in ArrayList

Occasionally O(n) but amortized constant time for insertions.

81
New cards

Why LinkedList has slow indexing

Requires walking through nodes sequentially to reach an index.

82
New cards

Relationship between classes and objects

Objects are instances created from class templates.

83
New cards

Meaning of "recursive definition"

A definition that describes something in terms of a simpler version of itself.

84
New cards

How recursive methods use stack frames

Each call stores its own local variables until it returns.

85
New cards

Why substring recursion is common in string algorithms

Strings naturally shrink by removing characters.

86
New cards