1) Can we define a class without creating constructors?
- Yes
- No
Correct Answer - 1
Yes
Yes, we can define a class without creating constructors.
2) Which is the correct form of default constructor for following class?
#include <iostream>
using namespace std;
class sample
{
private:
int x,y;
};
- public: sample(){}
- public: sample(){ x=0; y=0;}
- public: sample(int a,int b){ x=a; y=b;}
- Both 1 and 2
Correct Answer - 4
Both 1 and 2
3) What will be the output of following program?
#include <iostream>
using namespace std;
class sample {
private:
int x, y;
public:
sample(int a, int b)
{
x = a;
y = b;
}
};
int main()
{
sample s;
return 0;
}
- Compile Time Error
- Run Time Error
- No Error
- Warning
Correct Answer - 1
Compile Time Error
There is no default constructor in this class definition, and we cannot create an object like this, the correct form of creating object is : sample s(10,20); - pass two values because parameterized constructor is exists in the class definition.
4) What will be the output of following program?
#include <iostream>
using namespace std;
class sample {
private:
int x;
public:
sample()
{
x = 0;
printf("Object created.");
}
sample(int a) { x = a; }
};
int main()
{
sample s;
return 0;
}
- Compile Time Error
- Object Created.
- Run Time Error
- Can’t be predicted
Correct Answer - 2
Object Created.