OOPS Flashcards

(110 cards)

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

What are the four main principles of Object-Oriented Programming (OOP)?

A

Abstraction, Encapsulation, Inheritance, and Polymorphism.

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

What is abstraction in OOP?

A

Abstraction hides implementation details and exposes only essential features through interfaces or abstract classes.

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

Why is abstraction important?

A

It reduces complexity and allows developers to focus on what an object does instead of how it does it.

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

What is encapsulation in OOP?

A

Encapsulation bundles data and methods together and restricts direct access to internal state using access modifiers.

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

Why is encapsulation useful?

A

It protects data integrity and prevents unintended modification from outside code.

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

What is inheritance in OOP?

A

Inheritance allows one class to acquire properties and methods of another class using extends.

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

Why use inheritance?

A

It promotes code reuse and establishes hierarchical relationships between classes.

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

What is polymorphism in OOP?

A

Polymorphism allows objects to behave differently depending on context, typically via method overriding or overloading.

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

What are the two main types of polymorphism?

A

Compile-time polymorphism (method overloading) and runtime polymorphism (method overriding).

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

What is runtime polymorphism?

A

It occurs when a superclass reference points to a subclass object and the method executed is determined at runtime.

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

What is the difference between abstraction and encapsulation?

A

Abstraction hides complexity of implementation, while encapsulation hides internal state and protects data.

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

Which OOP principle supports code reuse most directly?

A

Inheritance.

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

Which OOP principle improves security and data protection?

A

Encapsulation.

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

Which OOP principle enables flexibility and extensibility?

A

Polymorphism.

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

Real-world example of OOP principles working together?

A

A Payment class hierarchy where abstraction defines payment methods, encapsulation protects transaction data, inheritance creates subclasses like CreditCardPayment, and polymorphism allows different payment processing behaviors.

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

What is the difference between Object-Oriented and Object-Based programming languages?

A

Object-Oriented languages support all OOP principles including inheritance and polymorphism, while Object-Based languages support objects and encapsulation but typically lack inheritance and runtime polymorphism.

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

What defines an Object-Oriented Programming language?

A

A language that supports abstraction, encapsulation, inheritance, and polymorphism as core features.

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

What defines an Object-Based Programming language?

A

A language that supports objects and encapsulation but usually does not support inheritance or true polymorphism.

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

Give examples of Object-Oriented languages.

A

Java, C++, C#, Python.

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

Give examples of Object-Based languages.

A

JavaScript (traditionally classified), VBScript, and some scripting languages.

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

Do Object-Based languages support inheritance?

A

Traditionally no class-based inheritance, though some support prototype-based reuse.

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

What is prototype-based programming?

A

A style where objects inherit directly from other objects instead of classes.

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

Is JavaScript purely Object-Based?

A

Historically yes, but modern JavaScript supports class syntax and inheritance, making it multi-paradigm.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Which paradigm is more powerful for large systems?
Object-Oriented programming, because inheritance and polymorphism allow scalable and extensible architectures.
26
Which paradigm is simpler for small scripts?
Object-Based programming, because it avoids complex class hierarchies.
27
What feature mainly distinguishes OOP from Object-Based languages?
Support for inheritance and runtime polymorphism.
28
Do both paradigms support encapsulation?
Yes. Both Object-Oriented and Object-Based languages support encapsulation.
29
Why is inheritance important in OOP languages?
It enables code reuse, hierarchical design, and extensibility.
30
Why might a language intentionally avoid inheritance?
To reduce complexity and avoid tight coupling between components.
31
What is the default value of an object reference instance variable in Java?
null.
32
Are instance variables automatically initialized in Java?
Yes. Instance variables receive default values if not explicitly initialized.
33
What is the default value of object references?
null.
34
What does null mean for an object reference?
It means the reference does not point to any object in memory.
35
What happens if you use an object reference that is null?
A NullPointerException occurs at runtime.
36
Do local object references default to null?
No. Local variables must be explicitly initialized before use.
37
Which variables get default values automatically?
Instance variables and static variables.
38
What is the default value of primitive instance variables?
Numeric → 0, boolean → false, char → '\u0000'.
39
Why does Java initialize instance variables automatically?
To ensure objects start in a predictable state and avoid undefined memory values.
40
Is this valid: class A { Object obj; }?
Yes. obj defaults to null until assigned.
41
Can null be assigned to any object reference type?
Yes, because null is compatible with all reference types.
42
Why is null considered dangerous in Java?
Because dereferencing it causes runtime exceptions if not checked.
43
Why do we need constructors in Java?
Constructors initialize objects and set their initial state when an object is created.
44
What is a constructor in Java?
A constructor is a special method used to initialize an object; it has the same name as the class and no return type.
45
When is a constructor executed?
It runs automatically when an object is created using the new keyword.
46
Is a constructor required to create an object?
Yes. Every object creation invokes a constructor, either user-defined or default.
47
Does Java provide a default constructor?
Yes. If no constructor is defined, the compiler automatically provides a no-argument default constructor.
48
When is the default constructor NOT provided?
If you define any constructor, Java does not generate a default one automatically.
49
What does a default constructor do?
It initializes instance variables with default values and performs no custom logic.
50
Can constructors be overloaded?
Yes. A class can have multiple constructors with different parameter lists.
51
Can constructors have return types?
No. Constructors never declare a return type, not even void.
52
What is the difference between a constructor and a method?
A constructor initializes objects and runs once at creation, while methods define behavior and can be called many times.
53
What happens if you define a parameterized constructor but no default constructor?
The compiler will not create a default constructor, so you must explicitly define one if needed.
54
Can a constructor be private?
Yes. Private constructors are used to restrict object creation, such as in singleton patterns.
55
What keyword is used to call another constructor in the same class?
this()
56
What keyword is used to call superclass constructor?
super()
57
Is constructor inheritance allowed?
No. Constructors are not inherited, but subclass constructors can call superclass constructors.
58
Why do we need default constructor in Java classes?
Default constructor is the no-argument constructor that is automatically generated by Java if no other constructor is defined. Java specification says that it will provide a default constructor if there is no overloaded constructor in a class. But it does not say anything about the scenario in which we write an overloaded constructor in a class. We need at least one constructor to create an object, that’s why Java provides a default constructor. When we have overloaded constructor, then Java assumes that we want some custom treatment in our code. Due to which it does not provide default constructor. But it needs default constructor as per the specification. So it gives error.
59
Why do we need constructors in Java?
Constructors initialize objects and set their initial state when an object is created.
60
What is a constructor in Java?
A constructor is a special method used to initialize an object; it has the same name as the class and no return type.
61
When is a constructor executed?
It runs automatically when an object is created using the new keyword.
62
Is a constructor required to create an object?
Yes. Every object creation invokes a constructor, either user-defined or default.
63
Does Java provide a default constructor?
Yes. If no constructor is defined, the compiler automatically provides a no-argument default constructor.
64
When is the default constructor NOT provided?
If you define any constructor, Java does not generate a default one automatically.
65
What does a default constructor do?
It initializes instance variables with default values and performs no custom logic.
66
Can constructors be overloaded?
Yes. A class can have multiple constructors with different parameter lists.
67
Can constructors have return types?
No. Constructors never declare a return type, not even void.
68
What is the difference between a constructor and a method?
A constructor initializes objects and runs once at creation, while methods define behavior and can be called many times.
69
What happens if you define a parameterized constructor but no default constructor?
The compiler will not create a default constructor, so you must explicitly define one if needed.
70
Can a constructor be private?
Yes. Private constructors are used to restrict object creation, such as in singleton patterns.
71
What keyword is used to call another constructor in the same class?
this()
72
What keyword is used to call superclass constructor?
super()
73
Is constructor inheritance allowed?
No. Constructors are not inherited, but subclass constructors can call superclass constructors.
74
What value does a constructor return in Java?
Constructors do not have a return type and do not return values; they initialize a newly created object.
75
Does a constructor explicitly return an object?
No. The new operator creates the object and the constructor initializes it; the constructor itself does not return anything.
76
Why can’t constructors declare a return type?
Because Java syntax defines constructors as special members without return types; declaring one makes it a regular method instead.
77
What actually creates an object in Java?
The new keyword allocates memory and creates the object; the constructor initializes it.
78
Is this valid constructor: public MyClass() { return this; }?
No. Constructors cannot use return statements to return values.
79
What happens if you add a return type to a constructor?
It becomes a normal method, not a constructor.
80
Why does it seem like a constructor returns an object?
Because object creation syntax assigns the new object reference to a variable after constructor execution.
81
Example: MyClass obj = new MyClass(); what does each part do?
new allocates memory, constructor initializes object, assignment stores reference in obj.
82
Can a constructor return null?
No. Constructors cannot return any value at all.
83
What is returned by the new operator?
A reference to the newly created object.
84
Is constructor execution optional during object creation?
No. Every object creation always invokes a constructor.
85
What is the main purpose of a constructor?
To initialize object state immediately after memory allocation.
86
Can constructors be inherited in Java?
No. Constructors are not inherited by subclasses.
87
Why aren’t constructors inherited?
Because constructors belong to a specific class and are responsible for initializing that class’s own state, not subclasses.
88
If constructors aren’t inherited, how are parent constructors used?
Subclass constructors call them using super().
89
What happens if a subclass constructor does not call super()?
The compiler automatically inserts a call to the parent’s no-argument constructor.
90
What error occurs if parent has no default constructor and subclass doesn’t call one?
A compile-time error occurs because the superclass constructor must be explicitly invoked.
91
Can a subclass call a parameterized superclass constructor?
Yes, using super(arguments).
92
When is a superclass constructor executed?
It runs before the subclass constructor during object creation.
93
Execution order of constructors in inheritance?
Superclass constructor → subclass constructor.
94
Can constructors be overridden?
No. Constructors cannot be overridden because they are not inherited and are not regular methods.
95
Can constructors be overloaded?
Yes. A class can define multiple constructors with different parameter lists.
96
What keyword is used to invoke another constructor in the same class?
this()
97
What keyword is used to invoke parent constructor?
super()
98
Why must superclass constructor run first?
Because the parent portion of the object must be initialized before the child portion.
99
Why can’t constructors be declared final in Java?
Because constructors cannot be overridden, and final only prevents overriding, so marking a constructor final has no purpose.
100
Why can’t constructors be static in Java?
Because constructors initialize objects, and static members belong to the class rather than an instance.
101
Why can’t constructors be abstract in Java?
Because abstract methods have no body, but constructors must contain implementation to initialize objects.
102
Can constructors be overridden?
No. Constructors are not inherited and therefore cannot be overridden.
103
What modifiers are allowed for constructors?
Access modifiers (public, protected, private) and annotations are allowed, but not static, final, abstract, or synchronized.
104
Why does a constructor always need a body?
Because it must initialize object state during creation.
105
What would happen if constructors were static?
They would not be tied to object creation, defeating their purpose of initializing instances.
106
Why is abstract incompatible with constructors conceptually?
Because constructors must execute code immediately when an object is created.
107
Can constructors be private?
Yes. Private constructors restrict object creation and are used in patterns like Singleton.
108
Can constructors be synchronized?
No. Synchronization applies to methods, but constructors cannot be synchronized.
109
What keyword triggers constructor execution?
The new keyword.
110
Why must constructors be instance-specific?
Because they initialize instance variables for each object.