Question 51
MediumWhen is memory for a local C++ object such as 'Point p(1, 2);' released?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
When is memory for a local C++ object such as 'Point p(1, 2);' released?
When the program ends
Never
Only when delete is called
Automatically when p goes out of scope
Sign up free to see the answer and a step-by-step explanation.
Which object relationship is 'a Library has Books, but Books can exist without the Library'?
Inheritance
Dependency
Composition
Aggregation
Sign up free to see the answer and a step-by-step explanation.
A House has Rooms, and the Rooms are created and destroyed with the House. Which relationship is this?
Inheritance
Aggregation
Association only
Composition
Sign up free to see the answer and a step-by-step explanation.
What is an anonymous object, such as new Printer().print(doc) in Java?
An object of an abstract class
An object used once without being stored in a variable
An object with no fields
An object with no class
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
import copy
class P:
def __init__(self):
self.x = 1
p = P()
q = p
r = copy.copy(p)
q.x = 2
print(p.x, r.x)
1 1
2 1
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
class A {
public:
int v;
A(int x) : v(x) {}
};
int main() {
A a(1);
A b = a;
b.v = 2;
cout << a.v << " " << b.v;
return 0;
}
1 2
Error
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Counter:
count = 0
def bump(self):
self.count += 1
t = Counter()
for _ in range(5):
t.bump()
print(Counter.count, t.count)
0 5
Error
Sign up free to see the answer and a step-by-step explanation.
What is the size of an object of an empty class in C++ (with typical compilers)?
8 bytes
1 byte
0 bytes
4 bytes
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Rect:
def __init__(self, w, h):
self.w, self.h = w, h
@property
def area(self):
return self.w * self.h
r = Rect(2, 4)
print(r.area)
8
Error
Sign up free to see the answer and a step-by-step explanation.
Which statement about the singleton pattern is correct?
It lets a class have one method only
It ensures a class has only one instance and provides a global point of access to it
It forbids static members
It creates a new object on every call
Sign up free to see the answer and a step-by-step explanation.
1 2
2 2
1 1
2 2
0 0
5 5
5
13