Question 1
Can destructors be private in C++?
Yes
No
Question 2
#include <iostream>
using namespace std;
int i;
class A
{
public:
~A()
{
i=10;
}
};
int foo()
{
i=3;
A ob;
return i;
}
int main()
{
cout << foo() << endl;
return 0;
}
Question 3
Question 4
#include <iostream>
using namespace std;
class A
{
int id;
static int count;
public:
A() {
count++;
id = count;
cout << "constructor for id " << id << endl;
}
~A() {
cout << "destructor for id " << id << endl;
}
};
int A::count = 0;
int main() {
A a[3];
return 0;
}
constructor for id 1 constructor for id 2 constructor for id 3 destructor for id 3 destructor for id 2 destructor for id 1
constructor for id 1 constructor for id 2 constructor for id 3 destructor for id 1 destructor for id 2 destructor for id 3
Compiler Dependent.
constructor for id 1 destructor for id 1
Question 5
Can destructors be virtual in C++?
Yes
No
Question 6
What will be the output?
#include <iostream>
class Test {
public:
~Test() {
std::cout << "Destructor called\n";
}
};
int main() {
Test t1;
}
No output
Destructor called
Compilation error
Runtime Error
Question 7
What will be the output?
#include <iostream>
class Sample {
public:
~Sample() {
std::cout << "Destructor executed\n";
}
};
void createObject() {
Sample s;
}
int main() {
createObject();
std::cout << "Main done\n";
}
Main done
Destructor executed
Destructor executed
Main done
Main done
Destructor executed
There are 7 questions to complete.