Week 7-12 Flashcards
What are the three different types of errors in java.
Internal errors - Unrecoverable. E.g. OutOfMemoryError
Unchecked exceptions - RuntimeException error in code. E.g. IllegalArgumentException
Checked exceptions - Something wrong outside of your control. E.g. FileNotFoundException.
Distinguish between making a checked and unchecked exception.
checked: extend Exception class
unchecked: extend RuntimeException class
Distinguish between function, statement, branch and condition coverage.
F: Every function has been tested at least once.
S: Every statement within code has been tested, all lines of code reached.
B: Every possibility for each statement is executed (true and false)
C: Every possibility for each part of the condition.
Syntax for an anonymous inner class called area of type calculator.
Calculator area = new Calculator() {
@Override
public double calculate(double width, double height) {
return width * height;
}
};
What is SAM?
Single abstract method. Essentially an anonymous function which implements a functional interface.
What is a functional interface?
An interface with only one method.
Declared with @FunctionalInterface
Syntax for a SAM called area of type calculator. Give both method without parameter typing and with.
Calculator area = (width, height) -> width * height;
Calculator area = (double width, double height) -> {
return width * height;
};
What is advantage of switch with lambda?
Allows sets of values in case statement joined by coma.
E.g. case 1,2 -> sout(“Number less than three”);
What is Person::compareTo?
A method reference.
Syntax for stream API?
List<Type> listName = originalList.stream()
.instruction
.instruction ...
.collect(Collectors.toList());</Type>
What are the two main challenges of threading and concurrency?
Deadlock: when threads are waiting for each other, but neither is running.
Livelock: when threads are running but not responding / updating some state.
Explain thread methods. run, start, sleep, wait and join.
run: he process body of the Thread; defines what it will do
start: schedules the Thread and at some point calls run
sleep: suspends the Thread for n milliseconds
wait: suspends the Thread for a period of time, until some
other state is reached
join: suspend the Thread until some other Thread has terminated
Why are lambda expressions used for threading?
Because runnable is a SAM interface.
How you can stop threads counting the same thing twice? Two methods.
Make the mutator method synchronized with keyword after visibility before return type.
synchronized (counter) {
counter.increment();
counter.notifyAll();
}
Which of the following best describes a principle of good object
design?
- Using only primitive data types in a class to reduce memory
overhead. - Ensuring all class methods are public to maximize reusability.
- Designing classes so they depend on specific concrete
implementations rather than abstractions. - Avoiding the use of constructors to allow greater flexibility in
object creation. - Encapsulating behavior and data within a class to minimize
interaction with unrelated classes.
5
What is a key benefit of polymorphism in object-oriented
programming?
1. A. It allows a single method to accept many different types of
argument.
2. It eliminates the need for defining interfaces in code.
3. It enables a method or object to behave differently depending
on the runtime type of the object
4. It enforces strict compile-time type checking for all method
calls.
5. It enables the ability to override many methods in the super
class to provide flexibility.
3
Which of the following statements about encapsulation in Java is correct?
- Encapsulation is implemented by using final keyword with variables.
- Encapsulation ensures that all methods in a class are public.
- Encapsulation allows restricting access to internal data using access modifiers like private.
- Encapsulation is only applicable to primitive data types.
- Encapsulation requires using abstract classes and interfaces.
3
Which of the following is a valid use of a functional interface in Java?
- Defining a class that implements multiple methods from an interface.
- Creating an anonymous class that overrides a single abstract method.
- Using a functional interface to define constants for a class.
- A functional interface can have more than one abstract method but must include static methods.
- Lambda expressions can only be used with interfaces that extend Runnable
2
What will happen if a subclass overrides a method declared final in the superclass?
- The method in the subclass will be treated as a completely separate method.
- The code will compile successfully, but the overridden method will not be executed.
- The program will throw a runtime exception when the subclass method is called.
- The code will fail to compile with a “cannot override final method” error.
- The overridden method will still call the original superclass implementation.
4
Principle of error handling in java.
Throw early, catch late.
What happens when an exception is thrown in a try block but there is no matching catch block?
- The program will terminate immediately without executing the finally block.
- The exception is ignored, and the program continues execution.
- The exception will propagate to the method’s caller.
- The exception will be automatically handled by the JVM and execution will resume.
- The program will execute the finally block and terminate gracefully.
3
T or F. The finally block will always execute even if there is no matching catch.
True.
What is serialization in java?
The conversion of the state of an object into a byte stream.
When is a nullPointerException thrown?
At runtime, when we attempt to dereference an object with value NULL.