Test
Constructors
Este test tiene tiempo límite, así que se abre en el reproductor de siempre.
Preguntas del test (10)
1Which of the followings is/are automatically added to every class, if we do not write our own.
- ACopy Constructor
- BAssignment Operator
- CA constructor without any parameter
- DAll of the above
2When a copy constructor may be called?
- AWhen an object of the class is returned by value.
- BWhen an object of the class is passed (to a function) by value as an argument.
- CWhen an object is constructed based on another object of the same class
- DWhen compiler generates a temporary object.
- EAll of the above
3Output of following program?
#include<iostream>
using namespace std;
class Point {
Point() { cout << "Constructor called"; }
};
int main()
{
Point t1;
return 0;
}- ACompiler Error
- BRuntime Error
- CConstructor called
4#include<iostream>
using namespace std;
class Point {
public:
Point() { cout << "Constructor called"; }
};
int main()
{
Point t1, *t2;
return 0;
}- ACompiler Error
- BConstructor called
Constructor called - CConstructor called
5Output of following program?
#include<iostream>
using namespace std;
class Point {
public:
Point() { cout << "Normal Constructor calledn"; }
Point(const Point &t) { cout << "Copy constructor calledn"; }
};
int main()
{
Point *t1, *t2;
t1 = new Point();
t2 = new Point(*t1);
Point t3 = *t1;
Point t4;
t4 = t3;
return 0;
}- ANormal Constructor called
Normal Constructor called
Normal Constructor called
Copy Constructor called
Copy Constructor called
Normal Constructor called
Copy Constructor called - BCopy Constructor called
Normal Constructor called
Copy Constructor called
Normal Constructor called
Copy Constructor called - CNormal Constructor called
Copy Constructor called
Copy Constructor called
Normal Constructor called
- ANormal Constructor called
6#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;
}- A10 10
- B10 0
- C10 followed by Garbage Value
- DCompiler Error
7What is the output of following program?
#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;
}- ACompiler Error
- Bx = 0 y = 0
- Cx = garbage value y = garbage value
8#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;
}- Ax = 0 y = 0
- BCompiler Error
- Cx = garbage value y = garbage value
9Predict the output of following program.
#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;
}- AConstructor called
- BEmpty
- CCompiler Error
- DRuntime error
10#include <iostream>
using namespace std;
class Test
{
public:
Test() { cout << "Hello from Test() "; }
} a;
int main()
{
cout << "Main Started ";
return 0;
}- AHello from Test() Main Started
- BCompiler Error: Global objects are not allowed
- CMain Started Hello from Test()
- DMain Started
Información
- Descripción
- C++ Test sobre Constructors, creado por PathaPadha Support el 27/10/2020.
- Autor
- PathaPadha Support
- Idioma
- English
- Visitas
- 60
- Etiquetas
- Valoración
- Aún sin valoraciones Valora este test