TEST 3 SHORT ANSWER PART 1 Flashcards

(26 cards)

1
Q

A “has‐a” relationship can exist between classes. What does this mean?

A

One class contains or aggregates another as a field

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

What is an “is‐a” relationship?

A

One class inherits from another

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

A program uses two classes: Animal and Dog. Which class is the superclass and which is the subclass?

A

Animal is the superclass; Dog is the subclass.

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

In the line public class Pet extends Dog, which is the superclass, and which is the subclass?

A

Dog is the superclass; Pet is the subclass.

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

What is the difference between a protected class member and a private class member?

A

protected: accessible within the same package and by subclasses (even in other packages).

private: accessible only within the defining class.

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

Can a subclass ever directly access the private members of its superclass?

A

No. Private members are hidden from subclasses

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

Which constructor is called first— that of the subclass or the superclass?

A

The superclass’s constructor runs before the subclass’s.

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

What is the difference between overriding a superclass method and overloading a superclass method?

A

Overriding - same signature

Overloading - same name, different signature

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

Reference variables can be polymorphic. What does this mean?

A

it can point to or refer to objects of different types, potentially at different times during the program’s execution.

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

When does dynamic binding take place?

A

At runtime, when an overridden method is invoked on a polymorphic reference, the JVM picks the actual object’s implementation.

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

What is an abstract method?

A

A declared method without an implementation.

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

What is an abstract class?

A

A class declared abstract that cannot be instantiated; it may contain abstract methods and/or concrete methods.

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

What are the differences between an abstract class and an interface?

A

Abstract class - single inheritance

Interface - supports multiple methods

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

When you instantiate an anonymous inner class, the class must do one of two things. What are they?

A

It must either extend an existing class or implement an interface.

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

What is a functional interface?

A

An interface with exactly one abstract method (used as the target for lambda expressions).

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

What is a lambda expression?

A

a concise way to define a short, single-use function within a larger program

17
Q

What is meant when it is said that an exception is thrown?

A

an unexpected event or error has occurred during the execution of the code

18
Q

What does it mean to catch an exception?

A

using a try-catch block to intercept and handle an error that might occur within a code block

19
Q

What happens when an exception is thrown, but the try statement does not have a catch clause that is capable of catching it?

A

The exception goes up the call stack until it terminates

20
Q

What is the purpose of a finally clause?

A

To execute cleanup code regardless of whether an exception was thrown or caught

21
Q

Where does execution resume after an exception has been thrown and caught?

A

With the first statement after the entire try‑catch‑finally block.

22
Q

When multiple exceptions are caught in the same try statement and some of them are related through inheritance, does the order in which they are listed matter?

A

Yes—more specific exceptions must be listed before more general ones, or the compiler reports an unreachable catch.

23
Q

What types of objects can be thrown?

A

Any instance of Throwable (typically Exception or Error) or its subclasses.

24
Q

When are you required to have a throws clause in a method header?

A

when a method may throw a “checked” exception, but it doesn’t explicitly handle it within its code block

25
What is the difference between a checked exception and an unchecked exception?
Checked: subclasses of Exception Unchecked: RuntimeException and Error
26
What is the difference between the throw statement and the throws clause?
throw: raises an exception object at runtime. throws: declares which checked exceptions the method may throw.