CodeSignal Technical Assessment Object-Oriented Programming 1 — Questions and Answers
Question 1: What is encapsulation in object-oriented programming?
- Bundling data and methods within a class while restricting direct access to internal state (Correct answer)
- The ability of a class to inherit properties from another class
- Defining multiple methods with the same name in a class
- Creating objects from abstract blueprints
Correct answer: Bundling data and methods within a class while restricting direct access to internal state
Encapsulation bundles data and the methods that operate on it into a single unit (class) while hiding internal state from outside access.
Question 2: Which keyword is used in Java, C++, and JavaScript to create a new instance of a class?
- create
- new (Correct answer)
- instance
- init
Correct answer: new
The `new` keyword allocates memory and invokes the constructor to create a new object instance in most OOP languages.
Question 3: What is inheritance in object-oriented programming?
- A mechanism where one class acquires the properties and methods of another class (Correct answer)
- The process of hiding internal implementation details from users
- The ability to define multiple methods with the same name
- The conversion of one data type to another at runtime
Correct answer: A mechanism where one class acquires the properties and methods of another class
Inheritance allows a child (subclass) to acquire attributes and behaviors from a parent (superclass), enabling code reuse and hierarchical classification.
Question 4: What is a constructor in OOP?
- A method that destroys an object and frees its memory
- A special method automatically called when a new object is created (Correct answer)
- A method that converts an object to its string representation
- A static method that belongs to the class rather than its instances
Correct answer: A special method automatically called when a new object is created
A constructor is a special method automatically invoked upon object creation, used to initialize the object's fields and set its initial state.
Question 5: What does the `this` keyword (or `self` in Python) refer to inside an instance method?
- The parent class of the current class
- The current instance of the class on which the method is called (Correct answer)
- The class itself in a static context
- The return value produced by the method
Correct answer: The current instance of the class on which the method is called
`this` / `self` is a reference to the current object instance, allowing methods to access and modify the object's own properties.
Question 6: Which OOP principle means different objects can respond to the same method call in their own specific way?
- Encapsulation
- Polymorphism (Correct answer)
- Abstraction
- Composition
Correct answer: Polymorphism
Polymorphism allows objects of different types to be treated through a common interface, with each type providing its own implementation of the method.
Question 7: Which of the following best describes an abstract class?
- A class that contains only static methods and no instance methods
- A class that cannot be instantiated directly and may contain abstract methods subclasses must implement (Correct answer)
- A class with no fields, only method signatures
- A class that inherits from two or more parent classes simultaneously
Correct answer: A class that cannot be instantiated directly and may contain abstract methods subclasses must implement
An abstract class cannot be instantiated on its own; it serves as a blueprint that concrete subclasses extend and whose abstract methods they must implement.
What is encapsulation in object-oriented programming?