• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C++ Constructors

Question 1

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 2

CPP
#include <iostream>
using namespace std;

class Point
{
    int x, y;
public:
   Point(int i = 0, int j = 0) { x = i; y = j; }
   int getX() { return x; }
   int getY() { return y; }
};

int main()
{
    Point p1;
    Point p2 = p1;
    cout << \"x = \" << p2.getX() << \" y = \" << p2.getY();
    return 0;
}
  • Compiler Error
  • x = 0 y = 0
  • x = garbage value y = garbage value

Question 3

Predict the output of following program. C
#include<iostream>
#include<stdlib.h>
using namespace std;

class Test
{
public:
   Test()
   { cout << \"Constructor called\"; }
};

int main()
{
    Test *t = (Test *) malloc(sizeof(Test));
    return 0;
}
  • Constructor called
  • Empty
  • Compiler Error
  • Runtime error

Question 4

CPP
<br>
#include < iostream ><br>
using namespace std;<br><br>

class Test<br>
{<br>
public:<br>
Test() { cout << \"Hello from Test() \"; }<br>
} a;<br><br>

int main()<br>
{<br>
cout << \"Main Started \";<br>
return 0;<br>
}
  • Main Started
  • Main Started Hello from Test()
  • Hello from Test() Main Started
  • Compiler Error: Global objects are not allowed

Question 5

Output? CPP
#include<iostream>
#include<string.h>
using namespace std;

class String
{
    char *str;
public:
     String(const char *s);
     void change(int index, char c) { str[index] = c; }
     char *get() { return str; }
};

String::String(const char *s)
{
    int l = strlen(s);
    str = new char[l+1];
    strcpy(str, s);
}

int main()
{
   String s1(\"geeksQuiz\");
   String s2 = s1;
   s1.change(0, \'G\');
   cout << s1.get() << \" \";
   cout << s2.get();
}
  • GeeksQuiz geeksQuiz
  • GeeksQuiz GeeksQuiz
  • geeksQuiz geeksQuiz
  • geeksQuiz GeeksQuiz

Question 6

Predict the output of following program. C
#include<iostream>
using namespace std;
class Point {
    int x;
public:
    Point(int x) { this->x = x; }
    Point(const Point p) { x = p.x;}
    int getX() { return x; }
};

int main()
{
   Point p1(10);
   Point p2 = p1;
   cout << p2.getX();
   return 0;
}
  • 10
  • Compiler Error: p must be passed by reference
  • Garbage value
  • None of the above

Question 7

Which of the following is true about constructors. 1) They cannot be virtual. 2) They cannot be private. 3) They are automatically called by new operator.
  • All 1, 2, and 3
  • Only 1 and 3
  • Only 1 and 2
  • Only 2 and 3

Question 8

C
#include<iostream>
using namespace std;
 
class Test
{
public:
  Test();
};
 
Test::Test()  {
    cout << \" Constructor Called. \";
}
 
void fun() {
  static Test t1;
}
 
int main() {
    cout << \" Before fun() called. \";
    fun();
    fun();
    cout << \" After fun() called. \";  
    return 0;
}
  • Constructor Called. Before fun() called. After fun() called.
  • Before fun() called. Constructor Called. Constructor Called. After fun() called.
  • Before fun() called. Constructor Called. After fun() called.
  • Constructor Called. Constructor Called. After fun() called.Before fun() called.

Question 9

Predict the output of following program? CPP
#include <iostream>
using namespace std;
class Test
{
private:
    int x;
public:
    Test(int i)
    {
        x = i;
        cout << \"Called\" << endl;
    }
};

int main()
{
    Test t(20);
    t = 30; // conversion constructor is called here.
    return 0;
}
  • Compiler Error
  • Called
    Called
    
  • Called
    

Question 10

C
#include<iostream>
using namespace std;

class Test
{
public:
   Test(Test &t) { }
   Test()        { }
};

Test fun()
{
    cout << \"fun() Called\\n\";
    Test t;
    return t;
}

int main()
{
    Test t1;
    Test t2 = fun();
    return 0;
}
  • fun() Called
  • Empty Output
  • Compiler Error: Because copy constructor argument is non-const

There are 26 questions to complete.

Last Updated :
Take a part in the ongoing discussion