Java: Exam 2 Chapter 4-6

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/63

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.

64 Terms

1
New cards

What is an array in Java?

A fixed-size, ordered collection of elements of the same type stored in contiguous memory.

2
New cards

How do arrays differ from ArrayLists?

Arrays are fixed-size and store primitives; ArrayLists are resizable and store objects.

3
New cards

What is the index of the first element in an array?

0

4
New cards

What happens if you access an invalid array index?

Java throws an ArrayIndexOutOfBoundsException.

5
New cards

How do you get the number of elements in an array?

array.length

6
New cards

How is a 2D array structured in Java?

As an array of arrays, often representing rows and columns.

7
New cards

What is the difference between a pixel grid and a Cartesian grid?

Pixel grid origin (0,0) is top-left with y increasing downward; Cartesian grid origin is bottom-left with y increasing upward.

8
New cards

What is a ragged array?

A 2D array where each row can have a different number of columns.

9
New cards

What is the role of loops with 2D arrays?

Nested loops are used to access or process each element (row and column).

10
New cards

What is the purpose of System.nanoTime() or System.currentTimeMillis()?

To measure actual runtime for empirical time complexity testing.

11
New cards

Why can using system time for algorithm analysis be unreliable?

Because it depends on system load, hardware, and environment — results may vary between runs.

12
New cards

What is algorithm analysis?

Studying how an algorithm’s runtime and memory usage scale as input size increases.

13
New cards

What is Big-O notation?

A way to express an algorithm’s asymptotic upper bound — how runtime grows with input size.

14
New cards

Why do we ignore constants in Big-O?

Because constants have little impact on growth rate for large n.

15
New cards

What is O(1)?

Constant time — operation time does not depend on input size.

16
New cards

What is O(n)?

Linear time — operation time grows proportionally to input size.

17
New cards

What is O(n²)?

Quadratic time — runtime grows with the square of input size (common with nested loops).

18
New cards

What is O(log n)?

Logarithmic time — typical for divide-and-conquer algorithms like binary search.

19
New cards

What is the runtime of binary search?

O(log n)

20
New cards

What is the runtime of linear search?

O(n)

21
New cards

Why is binary search faster than linear search?

It halves the search space with each comparison.

22
New cards

What condition must be true to use binary search?

The array must be sorted.

23
New cards

What does Big-Θ (Theta) represent?

A tight bound — both upper and lower growth limits.

24
New cards

What does Big-Ω (Omega) represent?

An asymptotic lower bound — best-case performance.

25
New cards

What are the basic steps of binary search?

Check the middle element, compare, and recurse or iterate into the left or right half.

26
New cards

What does “time complexity” measure?

The number of basic operations an algorithm performs relative to input size n.

27
New cards

What does “space complexity” measure?

The amount of memory used by an algorithm relative to input size n.

28
New cards

How can we empirically test algorithm performance in Java?

Use timers (nanoTime) to compare execution durations for different inputs.

29
New cards

Why is empirical testing insufficient for algorithm analysis?

It depends on system performance and cannot generalize for all cases.

30
New cards

What is the purpose of algorithm analysis?

To evaluate and compare algorithms independent of machine or implementation.

31
New cards

What does the Arrays.sort() method use internally?

A dual-pivot quicksort for primitives and a stable mergesort for objects.

32
New cards

What is the difference between selection sort and insertion sort?

Selection sort repeatedly finds the minimum; insertion sort builds the sorted list one element at a time.

33
New cards

What are the time complexities of selection and insertion sort?

Both are O(n²).

34
New cards

What is merge sort’s time complexity?

O(n log n)

35
New cards

What are the advantages of merge sort?

Faster for large datasets and stable (preserves order of equal elements).

36
New cards

What is a list in computer science?

An ordered collection of elements that can grow or shrink dynamically.

37
New cards

What class implements a resizable list in Java?

ArrayList

38
New cards

What are the key methods of ArrayList?

add(), get(), set(), remove(), size(), clear()

39
New cards

What is the time complexity of accessing an element in ArrayList?

O(1)

40
New cards

Why are insertions and deletions slower in ArrayList?

Elements must be shifted to maintain order.

41
New cards

What is a linked list?

A data structure where elements (nodes) contain data and references to the next node.

42
New cards

What does a node in a linked list store?

An element (data) and a reference (pointer) to the next node.

43
New cards

What is the advantage of a linked list?

Efficient insertions and deletions (no shifting).

44
New cards

What is the disadvantage of a linked list?

Slower access because you must traverse nodes sequentially (O(n)).

45
New cards

What is the runtime of inserting or deleting from the beginning of a linked list?

O(1)

46
New cards

What is the runtime of accessing the nth element in a linked list?

O(n)

47
New cards

What interface do both ArrayList and LinkedList implement?

java.util.List

48
New cards

Why program to the List interface instead of a specific class?

To allow flexible substitution between different list implementations.

49
New cards

What are generics in Java?

A feature that allows classes, interfaces, and methods to operate on different data types while maintaining type safety.

50
New cards

What role do generics play in lists?

They ensure all elements in the list are of the same type and prevent runtime type errors.

51
New cards

What happens if you omit generics when declaring a list?

The list defaults to type Object, losing compile-time type checking.

52
New cards

What keyword declares a generic type in Java?

53
New cards

How does Java handle autoboxing in lists?

Automatically converts primitives (e.g., int) to wrapper objects (Integer).

54
New cards

What is the main idea behind using nodes and linked structures?

To dynamically allocate memory and efficiently manage collections that frequently change in size.

55
New cards

What is the difference between an array-based and node-based list?

Arrays use contiguous memory; linked lists use scattered nodes connected by references.

56
New cards

What are wrapper classes in Java?

Classes like Integer, Double, Boolean that let primitive values act as objects for use in collections.

57
New cards

What is the purpose of using generics with wrapper classes?

They allow ArrayLists to store objects corresponding to primitive values safely.

58
New cards

How do generics improve code reusability?

They enable writing flexible data structures that can work with any object type.

59
New cards

How can you iterate through an ArrayList or LinkedList?

Using a for-each loop, traditional for loop, or Iterator.

60
New cards

What is an Iterator?

An object that sequentially accesses elements in a collection without exposing internal structure.

61
New cards

What happens if you modify a list while using an Iterator?

It throws a ConcurrentModificationException.

62
New cards

What data structure would you use for fast random access?

ArrayList

63
New cards

What data structure would you use for frequent insertions or deletions?

LinkedList

64
New cards