1/40
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What do we mean by “high cohesion, low coupling”?
High cohesion → A class/module does one well-defined job.
Low coupling → A class depends on as few other classes as possible.
Result: Code is easier to maintain, change, and test.
What does it mean for class A to depend on class B?
Class A depends on B if A needs B to work — e.g., A calls B’s methods, uses B objects, or extends B.
If B changes, A may break → this is coupling.
What is SRP?
A class should have one and only one reason to change.
How to identify SRP violations?
Class is doing multiple jobs (e.g., logic + saving files + UI handling).
Method names vary wildly in purpose.
Why is SRP useful?
Makes code easier to maintain, test, and update.
What is OCP?
Classes should be open for extension, closed for modification.
How to identify OCP violations?
You must edit existing code every time a new feature is added instead of extending.
Why is OCP useful?
Reduces bugs by not constantly touching stable code.
What is LSP?
Subclasses must be usable anywhere the parent class is expected.
How to identify LSP violations?
Subclass throws errors for normal parent behavior.
Subclass removes expected functionality.
Why is LSP useful?
Ensures inheritance behaves predictably.
What is ISP?
Don’t force classes to implement large, bloated interfaces.
Instead: many small, specific interfaces.
How to identify ISP violations?
Class implements methods it doesn’t use.
Why is ISP useful?
Prevents unnecessary code and confusing designs.
What is DIP?
High-level modules should depend on abstractions, not concrete classes.
How to identify DIP violations?
High-level class uses new ConcreteClass() directly.
No interfaces are used between layers.
Why is DIP useful?
Allows swapping implementations easily (e.g., switching databases).
What is a user story and who is the target audience?
A short description of a feature from the user’s perspective.
Format: “As a <user>, I want <goal> so that <benefit>.”
Audience: Developers + product team to understand user needs.
How can we identify stakeholders in our program?
Ask: Who uses the system? Who benefits? Who is impacted?
Stakeholders include: end users, admins, developers, business owners, clients.
What is an API?
An Application Programming Interface — a structured way for programs to communicate.
In Java, you typically use an API by:
Making an HTTP request.
Parsing the JSON response.
Using the data in your program.
What is JSON and how is it relevant to an API?
JSON = JavaScript Object Notation, a lightweight data format.
APIs commonly send and receive data in JSON.
In Java, libraries like Jackson or Gson convert JSON ↔ objects.
What is the primary purpose of Exceptions in Java?
To report exceptional conditions (unusual, strange, unexpected) that require special treatment, moving away from the normal program execution flow.
Why is using a throw/catch block locally (in the same method) often considered bad practice?
Because the situation the exception reports is often not truly exceptional, and real uses of exceptions are generally not local; throw and catch are typically not in the same block of code.
What are the main benefits of using exceptions?
1. Less programmer time spent on handling errors. 2. Cleaner program structure (isolates exceptional situations). 3. Separation of concerns (local attention to the algorithm, global attention to errors).
What is the root class of the Java exception hierarchy?
Throwable.
What are the two main direct subclasses of Throwable?
Error and Exception
Give examples of Error and RuntimeException subclasses.
Error Subclasses: AssertionError, OutOfMemoryError. RuntimeException Subclasses: ArithmeticException, ClassCastException, IndexOutOfBoundsException, NullPointerException
What is a Checked Exception?
An exception that the compiler checks to ensure it is either caught by a calling method or declared with throws in the method signature
What is an Unchecked Exception?
An exception (subclass of RuntimeException) that the program throws without compile-time checking to see if it's caught. If not caught, it crashes the program and prints the stack trace
When should you typically use a Checked Exception (according to Joshua Bloch)?
For conditions from which the caller can reasonably be expected to recover
When should you typically use an Unchecked Exception (RuntimeException)?
To indicate programming errors (precondition violations), especially if the programmer could have predicted the exception. Also for things the program will throw without checking if eventually caught (e.g., NullPointerException).
Are methods required to handle Error or RuntimeException?
No. These are unchecked. Errors are serious problems that reasonable applications should not try to catch
How do you "throw an exception" in Java?
Use the throw keyword followed by a new Throwable instance (e.g., throw new Throwable(message);).
What is the difference between throw and a return statement?
throw exits the current method like a return statement, but it signals an exceptional (bad) state.
What are the two mandatory choices if you call code that may throw a non-RuntimeException (checked exception)?
. Wrap the code in a try-catch block. 2. Use throws in your method signature to declare that your code may throw the exception.
When is the finally block executed?
The finally clause is always executed, regardless of whether an exception was thrown or caught. (It is typically used for clean-up, like closing open files).
How does Cascading Catching work?
Catch blocks are executed in order. A catch block for a superclass exception (ExSup) should be placed after catch blocks for its subclasses (ExSubA) to ensure the subclass exception is handled by the most specific catch block first
What is the common design guideline regarding where to throw and catch exceptions?
Throw low, catch high." Throwing and catching should ideally not be in the same method
What is a Chained Exception?
A Throwable that contains a cause (another Throwable) that led to the current exception being thrown, allowing for a chain of exceptions
Why is Wrapping Exceptions (Chained Exceptions) important for DIP/Abstraction?
It prevents the upper layer's API from being tied to the implementation details of a lower layer (e.g., preventing a database-specific SQLException from propagating). The upper layer throws a custom, higher-level exception, containing the low-level exception as the cause