Question 1
EasyWhat is a class in object-oriented programming?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
What is a class in object-oriented programming?
A built-in data type only
A single object in memory
A function that returns a value
A blueprint that defines the data and behaviour of objects
Answer: D
A class describes attributes and methods; objects are instances created from it.
What is an object?
A file stored on disk
A type of variable that can't change
An instance of a class
A keyword used for loops
Answer: C
Each object has its own copy of the instance data defined by its class.
Which keyword creates an object in Java and C++ (on the heap)?
new
create
make
object
Answer: A
new allocates memory for the object and calls its constructor.
What is a method?
A variable inside a class
A class with no data
A loop in a class
A function defined inside a class that describes an object's behaviour
Answer: D
Methods act on an object's data, for example account.deposit(500).
What does the 'this' keyword refer to in Java and C++?
The current object
The parent class
The main function
A new object
Answer: A
Inside a method, this refers to the object on which the method was called.
How many objects can be created from one class?
Exactly one
None
Any number
At most two
Answer: C
A class is a template; you can create as many instances as you need (unless it is designed as a singleton).
What is the output of this code?
#include <iostream>
using namespace std;
class Point {
public:
int x;
};
int main() {
Point p;
p.x = 5;
cout << p.x;
return 0;
}
5
0
Answer: A
p.x is set to 5 and then printed.
What is the state of an object?
The values of its attributes at a given moment
The memory address of its class
The number of its methods
The class it belongs to
Answer: A
Two objects of the same class can be in different states, such as two bank accounts with different balances.
What is the behaviour of an object?
The file it is stored in
What it can do: the operations defined by its methods
Its name in the program
Its attribute values
Answer: B
Behaviour is defined by methods, such as deposit() and withdraw() for an account.
What is meant by an object's identity?
Its first attribute
The property that makes it distinct from every other object, even one with the same state
Its class name
Its return value
Answer: B
Two objects can hold identical values and still be two different objects.
Garbage value
Error