Week 7-12 Flashcards

1
Q

What are the three different types of errors in java.

A

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.

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

Distinguish between making a checked and unchecked exception.

A

checked: extend Exception class
unchecked: extend RuntimeException class

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

Distinguish between function, statement, branch and condition coverage.

A

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.

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

Syntax for an anonymous inner class called area of type calculator.

A

Calculator area = new Calculator() {

@Override
public double calculate(double width, double height) {
return width * height;
}

};

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

What is SAM?

A

Single abstract method. Essentially an anonymous function which implements a functional interface.

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

What is a functional interface?

A

An interface with only one method.
Declared with @FunctionalInterface

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

Syntax for a SAM called area of type calculator. Give both method without parameter typing and with.

A

Calculator area = (width, height) -> width * height;

Calculator area = (double width, double height) -> {
return width * height;
};

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

What is advantage of switch with lambda?

A

Allows sets of values in case statement joined by coma.

E.g. case 1,2 -> sout(“Number less than three”);

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

What is Person::compareTo?

A

A method reference.

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

Syntax for stream API?

A

List<Type> listName = originalList.stream()
.instruction
.instruction ...
.collect(Collectors.toList());</Type>

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

What are the two main challenges of threading and concurrency?

A

Deadlock: when threads are waiting for each other, but neither is running.
Livelock: when threads are running but not responding / updating some state.

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

Explain thread methods. run, start, sleep, wait and join.

A

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

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

Why are lambda expressions used for threading?

A

Because runnable is a SAM interface.

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

How you can stop threads counting the same thing twice? Two methods.

A

Make the mutator method synchronized with keyword after visibility before return type.

synchronized (counter) {
counter.increment();
counter.notifyAll();
}

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

Which of the following best describes a principle of good object
design?

  1. Using only primitive data types in a class to reduce memory
    overhead.
  2. Ensuring all class methods are public to maximize reusability.
  3. Designing classes so they depend on specific concrete
    implementations rather than abstractions.
  4. Avoiding the use of constructors to allow greater flexibility in
    object creation.
  5. Encapsulating behavior and data within a class to minimize
    interaction with unrelated classes.
A

5

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

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.

A

3

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

Which of the following statements about encapsulation in Java is correct?

  1. Encapsulation is implemented by using final keyword with variables.
  2. Encapsulation ensures that all methods in a class are public.
  3. Encapsulation allows restricting access to internal data using access modifiers like private.
  4. Encapsulation is only applicable to primitive data types.
  5. Encapsulation requires using abstract classes and interfaces.
A

3

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

Which of the following is a valid use of a functional interface in Java?

  1. Defining a class that implements multiple methods from an interface.
  2. Creating an anonymous class that overrides a single abstract method.
  3. Using a functional interface to define constants for a class.
  4. A functional interface can have more than one abstract method but must include static methods.
  5. Lambda expressions can only be used with interfaces that extend Runnable
A

2

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

What will happen if a subclass overrides a method declared final in the superclass?

  1. The method in the subclass will be treated as a completely separate method.
  2. The code will compile successfully, but the overridden method will not be executed.
  3. The program will throw a runtime exception when the subclass method is called.
  4. The code will fail to compile with a “cannot override final method” error.
  5. The overridden method will still call the original superclass implementation.
20
Q

Principle of error handling in java.

A

Throw early, catch late.

21
Q

What happens when an exception is thrown in a try block but there is no matching catch block?

  1. The program will terminate immediately without executing the finally block.
  2. The exception is ignored, and the program continues execution.
  3. The exception will propagate to the method’s caller.
  4. The exception will be automatically handled by the JVM and execution will resume.
  5. The program will execute the finally block and terminate gracefully.
22
Q

T or F. The finally block will always execute even if there is no matching catch.

23
Q

What is serialization in java?

A

The conversion of the state of an object into a byte stream.

24
Q

When is a nullPointerException thrown?

A

At runtime, when we attempt to dereference an object with value NULL.

25
If you attempt to catch an Exception and then catch a more specific exception such as IllegalArgument what will happen?
Code will not compile
26
What will happen if a Serializable class does not implement the Serializable interface and you attempt to serialize an instance of it? 1. The program will compile, but serialization will fail at runtime with a NotSerializableException. 2. The program will fail to compile due to missing Serializable implementation. 3. The program will compile and serialize the object without any issues. 4. The program will throw a runtime exception when the object is serialized. 5. The program will throw a ClassCastException at compile time.
1 Because Serializable has no methods.
27
Which of the following is a valid way to define a lambda expression for the Runnable interface in Java? (Multiple correct) 1. Runnable r = () -> { System.out.println("Running!"); }; 2. Runnable r = { System.out.println("Running!"); }; 3. Runnable r = () -> System.out.println("Running!"); 4. Runnable r = (String msg) -> System.out.println(msg); 5. Runnable r = () -> { System.out.println("Running!"); return; };
1 and 3, runnable interface has no parameters
28
What is loose coupling in java?
Classes depend on each other but are unaware of each other’s implementation details.
29
What is the factory method pattern?
To create objects in a superclass but allow subclasses to alter the type of objects that will be created.
30
What happens if a class implements two interfaces with conflicting default methods? 1. The class will be forced to override the conflicting methods. 2. The class will compile, but the default methods will be ignored. 3. The class will inherit both default methods, and no compilation error will occur. 4. The class will not compile unless one of the interfaces is removed. 5. The class will throw an exception at runtime.
1
31
What is true about the variables used in a lambda expression? 1. Lambda expressions can only use local variables that are final or effectively final. 2. Lambda expressions can modify local variables. 3. Lambda expressions cannot access variables outside their scope. 4. Variables used in lambda expressions must be static. 5. Variables used in lambda expressions must be declared as final.
1
32
Which of the following is true about inheritance in Java? 1. A subclass can inherit private members of its superclass. 2. A subclass can access only public members of its superclass. 3. A subclass can inherit both public and private members of its superclass. 4. A subclass can override the constructor of its superclass. 5. A subclass cannot inherit members from its superclass.
2
33
Which of the following is an example of polymorphism using an interface in Java? 1. A class implements multiple interfaces and overrides the methods from all of them. 2. A subclass inherits all methods from its superclass. 3. A method in a subclass is called even though it is defined in an interface. 4. A method in a class overrides a method in the interface without using the implements keyword. 5. An interface can inherit another interface and provide default method implementations.
1
34
What happens when a serialized class is changed (e.g., field added or removed) between serialization and deserialization? 1. The deserialization will fail if the serial version UID is changed. 2. Deserialization will succeed and ignore the new fields. 3. The new field will be automatically assigned a default value during deserialization. 4. The system will throw a ClassNotFoundException. 5. It causes a ClassCastException at runtime.
1
35
What is the primary advantage of using buffered streams (like BufferedReader and BufferedWriter) over unbuffered streams? 1. They provide automatic handling of exception cases. 2. They offer faster reading and writing by reducing the number of read/write operations on the disk. 3. They can handle both binary and text data seamlessly. 4. They automatically close the file when reading or writing is complete. 5. They support serialization of objects.
2
36
Which class is used for reading bytes directly from a file in Java? BufferedReader FileReader FileInputStream ObjectInputStream Scanner
FileInputStream
37
Which of the following classes in Java is used to serialize objects? ObjectOutputStream ObjectInputStream FileOutputStream PrintStream DataOutputStream
Object output stream (serializes) Object input stream (deserializes)
38
Which class in Java is used to get metadata information about a class at runtime using reflection? Class Object Reflection Method Constructor
Class
39
Which of the following methods in the Class class can be used to create a new instance of a class dynamically using reflection? newInstance() getConstructor() newObject() getClass() invoke()
newInstance()
40
String s1 = "abc"; String s2 = "abc"; System.out.println(s1 == s2); What is the output?
true
41
Define abstraction.
Hiding implementation details using interfaces.
42
Define overloading.
Creating multiple methods with the same name but different parameters.
43
Define polymorphism.
Allowing objects to take many forms based on their behaviour.
44
Define encapsulation.
Involves hiding implemention details by combining data and methods that operate on that data into a single unit called a class. While still allowing external access to the methods.
45
Which keyword is used in Java to achieve runtime polymorphism?
override