General Flashcards

(20 cards)

1
Q

What is the difference between a class and an object?

A

A class is a blueprint, while an object is an instance created from that blueprint

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

What is the difference between primitive and reference types in Java?

A

Primitives store actual values (e.g., int, double), while references store memory addresses pointing to objects

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

What is an abstract class

A

A class that cannot be instantiated and may contain both abstract and concrete methods

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

What is an interface in Java?

A

A contract that specifies methods a class must implement; cannot contain state (except constants)

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

Can a class extend multiple abstract classes? Can it implement multiple interfaces

A

No, only single inheritance for classes. Yes, multiple interface implementation is allowed

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

Define subtype polymorphism.

A

The ability to treat an object of a subclass as if it were of its superclass or interface type

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

Difference between subclass and subtype?

A

Subclass is a syntactic relationship (extends). Subtype is a semantic relationship: it must preserve behavior (Liskov Substitution Principle)

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

What is Liskov’s Substitution Principle (LSP)?

A

Subtypes must be usable anywhere their supertypes are expected, without altering correctness

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

Open-Closed Principle (OCP)?

A

Software entities should be open for extension but closed for modification

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

Single Responsibility Principle (SRP)?

A

A class should have only one reason to change (one responsibility)

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

Dependency Inversion Principle (DIP)?

A

A: Code against abstractions, not concrete implementations

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

Which pattern solves “choose an algorithm at runtime”?

A

Strategy

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

Which pattern ensures only one instance of a class exists?

A

Singleton

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

Which pattern allows attaching observers dynamically to be notified of state changes?

A

Observer

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

Which pattern allows adding responsibilities to objects dynamically?

A

Decorator

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

Which pattern adapts an existing class to a different interface?

17
Q

What happens if you write AnimalHouse<Dog> house = new AnimalHouse<Animal>();?</Animal></Dog>

A

Compile-time error: generics in Java are invariant

18
Q

How can parametric polymorphism prevent runtime ClassCastException?

A

By enforcing type constraints at compile time (e.g., Printer<String> instead of Printer<object>)</object></String>

19
Q

What are the responsibilities in MVC?

A

A: Model = data + logic, View = display, Controller = user interaction. View should not contain business logic.