C++ constructor, destructor aptitude questions and answers (set 2)

C++ constructor and destructor questions: This section contains aptitude questions and answers (MCQs) on Constructors and Destructors in C++.
Submitted by IncludeHelp, on May 03, 2019

List of C++ Constructor and Destructor Aptitude Questions & Answers

1) Constructor(s) which is/are added automatically with a class, if we do not create our own constructor?

  1. Default Constructor
  2. Copy Constructor
  3. Both default and copy constructors
  4. None

2) Does assignment operator implement automatically with the class?

  1. Yes
  2. No

3) What will be the output of the following code?

#include<iostream>
using namespace std;

//class definition
class Example {
    Example() { 
        cout << "Constructor called"; 
    }
};
 
//main() code 
int main()
{
   Example Ex;
   return 0;
}
  1. Constructor called
  2. Program successfully executed – no output
  3. Compile time error
  4. Run time error

4) What will be the output of the following code?

#include <iostream>
using namespace std;

//class definition
class Example {
public:
    Example()
    {
        cout << "Constructor called ";
    }
};

//main() code
int main()
{
    Example Ex1, Ex2;
    return 0;
}
  1. Constructor called
  2. Constructor called Constructor called
  3. Compile time error
  4. Run time error

5) What will be the output of the following code?

#include <iostream>
using namespace std;

//class definition
class Example {
public:
    int a;
    int b;
};

//main() code
int main()
{
    Example Ex1 = { 10, 20 };
    cout << "a = " << Ex1.a << ", b = " << Ex1.b;
    return 0;
}
  1. Compile time error
  2. Run time error
  3. a = 10, b = 20
  4. a = 10, b = 10





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.