Final Exam Study Guide Flashcards

(50 cards)

1
Q

What is a generic?

A

A feature that lets you define classes, interfaces, and methods with type parameters like <T></T>

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

How do generics help with errors?

A

Catch errors at compile time instead of runtime.

Prevent ClassCastException by enforcing type-checking

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

How do generics help with code duplication?

A

Methods and classes can work with any type.

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

Do you need to cast objects when retrieving them using generics?

A

Nope!

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

What is an upper bounded wildcard?

A

<? extends T>

Used in generics to specify that a method can accept any type that is a subtype of a specified class (T).

This includes T and all of its subclasses.

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

When to use upper bounded wildcards?

A

<? extends T>

When you want the method to work with a type that is a subclass of T - making code more flexible.

When you want to read values from a generic object, but not write to it

Helps working with hierarchies of classes.

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

What is a JUnit 5 test?

A

A piece of code written to test the validity of other code in Java.

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

What is a parameterized test in Java?

A

A JUnit test that is to be run multiple times with different inputs.

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

What is @CsvSource when it comes to JUnit testing?

A

@CsvSource is a list of input values that are comma separated, each row representing one test case.

Example:
@ParameterizedText
@CsvSource({
“2, 3, 5”,
“10, 5, 15”
})
void testAdd(int a, int b, int expected){
assertEquals(expected, add(a,b));

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

What is the Controller in the MVC pattern?

A

Handles user input and translates it into actions for the model or updates to the view.

Acts as the intermediary between the user interface and the underlying data or logic.

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

What is the View in the MVC pattern?

A

Displays data from the model to the user and updating the interface when the model changes.

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

What is the Model in the MVC pattern?

A

Manages the application’s data, logic, and rules. It represents the state of the program and notifies the view when changes occur.

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

What is the MVC design pattern?

A

Architectural pattern that separates an application into three components.

Model
View
Controller

Improves modularity and makes code easier to manage and scale.

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

How can misuse of the Controller lead to poor design?

A

If too much business logic is placed in the Controller, it becomes a “God Object”, making it difficult to maintain.

Violates SRP and makes code harder to test

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

What should the Controller not do?

A

Manipulate the View’s internal UI components directly or change the Model’s state directly without proper methods.

  • This creates tight coupling and breaks the MVC architecture
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to keep a controller reusable?

A

Do not overload it with complex logic that cannot be easily reused in different contexts.

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

Downsides of a bloated Controller?

A

Hard to debut and complicates unit testing.

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

What does <? extends T> do?

A

Upper bound:
Accepts any type that is a subclass of T.

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

What does <? super T> do?

A

Accepts any type that is a superclass of T.

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

Read / Write access to <? extends T>

A

Read is safe
Write is restricted

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

Read / Write access to <? super T>

A

Write is safe, but read is restricted. (Can only read as object)

22
Q

Common use for <? extends T>

A

For reading values without modifying them.

23
Q

Common use for <? super T>

A

For modifying values or adding elements to a collection

24
Q

How is <? extends T> flexible?

A

More flexible for reading, less for writing.

25
How is flexible?
More flexible for writing, less flexible reading
26
Where is more appropriate than ?
When you want to write or add elements of type T or its subtypes to a collection Example: sorting a list
27
What is the adapter design pattern?
Converts the interface of a class into another interface the client expects. It lets classes work together that couldn't otherwise because of incompatible interfaces without modifying existing code.
28
What is the observer design pattern?
A behavioral design pattern where an object (the Subject) maintains a list of dependent objects (Observers) Subject will automatically notify Observers and automatically notify them of any state changes.
29
What does the Subject do in the Observer design pattern?
When the Subject's state changes, it notifies all registered observers. Has a way to add/remove observers from it's list, notify observers.
30
What does the Observer do in the Observer design pattern?
The observer is an interface that defines the method for receiving updates. Observers react based on updated information from the subject.
31
What built-in utility class does Java provide to help with the observer design pattern?
java.util.Observable
32
Example of Observer Design pattern
GUI event handling A stock market app where multiple views update when the price changes.
33
Pros of Observer Pattern
Subject does not need to know details about its Observers New observers can be added without modifying the subject Multiple observers can be updated of changes automatically
34
Cons of the Observer Pattern
Observers may not be removed properly -leading to memory leaks Observers might not be notified in an unpredictable order Handling observer updates in a multi-threaded environment can be challenging
35
What is the Open/Closed Principle (OCP)
One of the SOLID principles. Software entities should be open for extension but closed for modification Once a class is designed and tested, its behavior should not be changed. It should however be possible to extend the class to add new functionality without modifying existing code.
36
How to apply Open/Closed principles?
Inheritance, interfaces, and polymorphism. Add new features or behaviors by adding new classes that extend or implement existing ones, rather than modifying the existing code.
37
What is a multi threaded application?
Runs multiple threads (independent paths of execution) at the same time.
38
In a multi threaded application, what problems can arise with the Observer Pattern?
Multiple threads modifying the Subject or list of Observers concurrently, can lead to inconsistent states. If an observer is added or removed while the Subject is notifying existing observers, this can cause a ConcurrentModificationException If an Observer synchronizes on a resource while being notified by the Subject, and the Subject tries to acquire the same resource, a deadlock can occur. A slow observer can delay the entire notification process if the Subject notifies observers sequentially.
39
How to mitigate ConcurrentModificationException?
Use CopyOnWriteArrayList for the list of Observers in the Subject. This prevents ConcurrentModificationException without explicit synchronization.
40
How to prevent race conditions in Observer pattern?
Ensure the method for adding, removing, or notifying observers is synchronized.
41
How to prevent slow observers in the Observer pattern?
Use a ExecutorService to notify observers asynchronously.
42
How to prevent memory leaks in the Observer Pattern?
Use WeakReference to ensure observers can be garbage collected when no longer in use.
43
How to make sure Observers do not delay the notification process in the Observer Pattern?
Ensure that observer methods do not perform long-running operations. Instead they should delegate heavy tasks to a separate thread.
44
What is the purpose of the hashCode() method?
Returns an integer (hash code) representing the object. It is used to quickly locate an object in hash-based collections like HashSet, HashMap, or HashTable.
45
If two objects are equal according to equals() method, what does that mean?
They must have the same hash code!
46
What could be the case if two objects have the same hash code?
They may or may not be equal.
47
What does the equals() Method do?
Determines whether two objects are considered "equal"
48
Why must the equals() and hashCode() method be overridden together?
If two objects are considered equal by equals() they must have the same hash code to ensure they are stored and retrieved correctly in hash-based collections. If you override quals() but not hashCode(), the hash-based collection may place objects in t he wrong bucket, causing - Duplicate entries in HashSet - Incorrect key-value mappings in HashMap
49
What are the SOLID Principles?
S: Each class should have one responsibility O: Open for extension but closed for modification L: Subtypes must be replaceable without breaking functionality I: Prefer many specific interfaces over one general interface D: Depend on abstractions, not concrete classes
50
What is a Maven project and what is it used for?
A build management tool for Java, handling dependencies, build, and project structure