Home » Aptitude Questions and Answers » C++ Aptitude Questions and Answers

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

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) A constructor is called when an object is being created?

  1. True
  2. False

2) If you created a parameterized and a copy constructor in the class and you create an object with no arguments (0 arguments), what will be the output?

  1. Program will execute successfully
  2. A compile-time will occur
  3. A run-time error will occur
  4. A syntax error will occur

3) When a destructor is called?

  1. When an object is being created
  2. After destroying the object
  3. We need to call destructor explicitly
  4. Just before destroying an object

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

#include<iostream>
using namespace std;

//class definition
class Example {
    public:
        void ~Example()
        {
            cout<<"Destroying the object";
        }
};

//main() code 
int main()
{
    Example Ex;
    return 0;
}
  1. Run-time error
  2. Compile-time error
  3. Destroying the object
  4. Executes successfully but no output

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

#include<iostream>
using namespace std;

//class definition
class Example {
    private:
        int a;
        int b;
    public:
        Example(int a, int b)
        {
            this->a = a;
            this->b = b;
        }
        int get_a()
        {
            return a;
        }
        int get_b()
        {
            return b;
        }
};

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.