Object-Oriented Programming Flashcards

(25 cards)

1
Q

What is a class in JavaScript OOP?

A

A class is a blueprint for creating objects with shared structure and behavior. It defines properties and methods that instances of the class will have.

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

How do you define a class in JavaScript?

A

Using the class keyword followed by the class name and a body that includes a constructor and methods.

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

What is the purpose of the constructor method in a class?

A

The constructor initializes new object instances with given values when they are created using new.

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

What is an instance in OOP?

A

An instance is a concrete object created from a class definition. Each instance has its own copies of instance properties.

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

How do you create an instance of a class in JavaScript?

A

By using the new keyword, e.g., const dog = new Animal("Rex").

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

What is inheritance in OOP?

A

Inheritance allows one class to extend another, inheriting its properties and methods, which promotes code reuse.

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

How is inheritance implemented in JavaScript?

A

Using the extends keyword to create a subclass that inherits from a parent class.

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

What is the purpose of the super() function in a subclass constructor?

A

It calls the parent class’s constructor, ensuring the base class is properly initialized before using this.

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

Can JavaScript classes override parent methods?

A

Yes. Subclasses can define a method with the same name as one in the parent class to override it.

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

What does method overriding mean in OOP?

A

Method overriding occurs when a subclass provides a new implementation for a method already defined in its superclass.

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

What is encapsulation in OOP?

A

Encapsulation is the practice of keeping an object’s internal state private and exposing controlled access through methods or getters/setters.

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

How can you create private fields in modern JavaScript classes?

A

By prefixing the property name with a # (e.g., #balance), making it accessible only within the class body.

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

What are getters and setters in JavaScript?

A

Special methods that control access to object properties, allowing custom logic when getting or setting a value.

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

Why is encapsulation important?

A

It helps prevent unintended interference with internal data, improving maintainability and security of code.

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

How does JavaScript implement OOP concepts differently from traditional class-based languages?

A

JavaScript is prototype-based, meaning objects can directly inherit from other objects without classical inheritance.

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

What are prototypes in JavaScript?

A

Prototypes are objects that other objects inherit properties and methods from. Every JS object has an internal link to a prototype.

17
Q

How does the prototype property work in constructor functions?

A

Methods added to a constructor’s prototype are shared by all instances, saving memory and ensuring consistent behavior.

18
Q

How does class syntax relate to prototypes in JavaScript?

A

The class syntax is syntactic sugar over prototype-based inheritance; under the hood, it still uses prototypes.

19
Q

What is polymorphism in OOP?

A

Polymorphism allows different objects to respond to the same method name in their own ways, often through method overriding.

20
Q

What is the difference between instance and static methods?

A

Instance methods belong to individual objects, while static methods belong to the class itself and are called directly on the class.

21
Q

Why is OOP used in JavaScript?

A

It helps organize code into reusable, modular pieces that model real-world entities and relationships.

22
Q

What are the four main principles of OOP?

A

Encapsulation, Abstraction, Inheritance, and Polymorphism.

23
Q

What is the difference between a class and an object?

A

A class is a blueprint; an object is an instance of that blueprint containing real data.

24
Q

What does this keyword refer to inside a class method?

A

this refers to the current instance of the class on which the method was called.

25
Can functions outside of a class still behave like methods?
Yes. JavaScript is flexible; standalone functions can act as object methods when bound with `.call()`, `.apply()`, or `.bind()`.