C++ Constructors

    Last Updated :
    Discuss
    Comments

    Question 1

    When a copy constructor may be called?

    • When an object of the class is returned by value.

    • When an object of the class is passed (to a function) by value as an argument.

    • When an object is constructed based on another object of the same class

    • When compiler generates a temporary object.

    • All of the above

    Question 2

    Predict output of the following program 

    C
    #include <stdio.h>
    int main()
    {
       printf("\new_c_question\b\r");
       printf("geeksforgeeks"); 
       getchar();
       return 0;
    }
    
    • ew_c_question
      geeksforgeeks
       

    • new_c_ques
      geeksforgeeks
       


    • geeksforgeeks
       

    • Depends on terminal configuration

    Question 3

    Output of following program? 
     

    C++
    #include <iostream>
    using namespace std;
    
    class Point {
        Point() { cout << "Constructor called"; }
    };
    
    int main()
    {
        Point t1;
        return 0;
    }
    
    • Runtime Error

    • None of these

    • Constructor called

    • Compiler Error

    Question 4

    Output of following C++ code will be?

    C++
    #include <iostream>
    using namespace std;
    
    class X 
    {
    public:
        int x;
    };
    
    int main()
    {
        X a = {10};
        X b = a;
        cout << a.x << " " << b.x;
        return 0;
    }
    
    • Compiler Error

    • 10 followed by Garbage Value

    • 10 10

    • 10 0

    Question 5

    Predict the output of the following program:

    C++
    #include <iostream>
    using namespace std;
    
    class GfG {
        public:
        int val;
        
        GfG(): val(11) {}
        GfG(int x): val(x) {};
        GfG(const GfG& other): GfG(other.val) {}
    };
    
    int main() {
        GfG gfg1(22);
        GfG gfg2(gfg1);
        cout << gfg2.val;
        return 0;
    }
    


    • 0

    • 22

    • Compiler Error

    • 11

    Question 6

    What is the output of following program? CPP
    #include <iostream>
    using namespace std;
    
    class Point
    {
        int x, y;
    public:
       Point(const Point &p) { x = p.x; y = p.y; }
       int getX() { return x; }
       int getY() { return y; }
    };
    
    int main()
    {
        Point p1;
        Point p2 = p1;
        cout << "x = " << p2.getX() << " y = " << p2.getY();
        return 0;
    }
    
    • x = garbage value y = garbage value

    • x = 0 y = 0

    • Compiler Error

    Question 7

    Predict the output of the following program:

    C++
    #include <iostream>
    using namespace std;
    
    class GfG {
        public:
        int val;
        
        GfG(int x = 22) {
            this->val = x;
        }
    };
    
    int main() {
        GfG gfg;
        cout << gfg.val;
        return 0;
    }
    
    • 0

    • 22

    • Runtime Error

    • Compiler Error

    Question 8

    Predict the output of the following program?

    C++
    #include <iostream>
    using namespace std;
    
    class GfG {
        GfG() {
            cout << "Constructor Called" << endl;
        }
    };
    
    int main() {
        cout << "Main Started" << endl;
        return 0;
    }
    
    • Compiler Error

    • Main Started
      Constructor Called

    • Constructor Called
      Main Started

    • Main Started

    Question 9

    Implicit return type of a class constructor is:

    • not of class type itself

    • class type itself

    • a destructor of class type

    • a destructor not of class type

    Question 10

    We must use initializer list in a constructor when

    • There is a reference variable in class

    • There is a constant variable in class

    • There is an object of another class. And the other class doesn't have default constructor.

    • All of the above

    There are 25 questions to complete.

    Take a part in the ongoing discussion