Final Exam Study Guide Flashcards
(50 cards)
What is a generic?
A feature that lets you define classes, interfaces, and methods with type parameters like <T></T>
How do generics help with errors?
Catch errors at compile time instead of runtime.
Prevent ClassCastException by enforcing type-checking
How do generics help with code duplication?
Methods and classes can work with any type.
Do you need to cast objects when retrieving them using generics?
Nope!
What is an upper bounded wildcard?
<? 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.
When to use upper bounded wildcards?
<? 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.
What is a JUnit 5 test?
A piece of code written to test the validity of other code in Java.
What is a parameterized test in Java?
A JUnit test that is to be run multiple times with different inputs.
What is @CsvSource when it comes to JUnit testing?
@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));
What is the Controller in the MVC pattern?
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.
What is the View in the MVC pattern?
Displays data from the model to the user and updating the interface when the model changes.
What is the Model in the MVC pattern?
Manages the application’s data, logic, and rules. It represents the state of the program and notifies the view when changes occur.
What is the MVC design pattern?
Architectural pattern that separates an application into three components.
Model
View
Controller
Improves modularity and makes code easier to manage and scale.
How can misuse of the Controller lead to poor design?
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
What should the Controller not do?
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 to keep a controller reusable?
Do not overload it with complex logic that cannot be easily reused in different contexts.
Downsides of a bloated Controller?
Hard to debut and complicates unit testing.
What does <? extends T> do?
Upper bound:
Accepts any type that is a subclass of T.
What does <? super T> do?
Accepts any type that is a superclass of T.
Read / Write access to <? extends T>
Read is safe
Write is restricted
Read / Write access to <? super T>
Write is safe, but read is restricted. (Can only read as object)
Common use for <? extends T>
For reading values without modifying them.
Common use for <? super T>
For modifying values or adding elements to a collection
How is <? extends T> flexible?
More flexible for reading, less for writing.