C++ Constructors

Last Updated :
Discuss
Comments

Question 1

When a copy constructor may be called?

  • A

    When an object of the class is returned by value.

  • B

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

  • C

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

  • D

    When compiler generates a temporary object.

  • E

    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;
}
  • A

    ew_c_question
    geeksforgeeks
     

  • B

    new_c_ques
    geeksforgeeks
     

  • C


    geeksforgeeks
     

  • D

    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;
}
  • A

    Runtime Error

  • B

    None of these

  • C

    Constructor called

  • D

    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;
}
  • A

    Compiler Error

  • B

    10 followed by Garbage Value

  • C

    10 10

  • D

    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;
}


  • A

    0

  • B

    22

  • C

    Compiler Error

  • D

    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;
}
  • A

    x = garbage value y = garbage value

  • B

    x = 0 y = 0

  • C

    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;
}
  • A

    0

  • B

    22

  • C

    Runtime Error

  • D

    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;
}
  • A

    Compiler Error

  • B

    Main Started
    Constructor Called

  • C

    Constructor Called
    Main Started

  • D

    Main Started

Question 9

Implicit return type of a class constructor is:

  • A

    not of class type itself

  • B

    class type itself

  • C

    a destructor of class type

  • D

    a destructor not of class type

Question 10

We must use initializer list in a constructor when

  • A

    There is a reference variable in class

  • B

    There is a constant variable in class

  • C

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

  • D

    All of the above

There are 25 questions to complete.

Take a part in the ongoing discussion