C++ Programming MCQ
C++ Programming MCQ
Which of the followings is/are automatically added to every class, if we do not write our own?
- Copy constructor
- Assignment operator
- A Constructor without any parameter
- All the above
View Answer
All the above
When a copy constructor may be called?
- When an object of a class returned by a value
- When an object of a class is passed by a value as an argument
- When an object is constructed based on another object of same class
- All of the above
View Answer
All the above
#include
using namespace std;
class Point {
Point() { cout << “”Constructor called””; }
};
int main()
{
Point t1;
return 0;
}
- Compile error
- Runtime error
- Constructor called
- linker error
View Answer
Compile time error
#include
using namespace std;
class Point {
public:
Point() { cout << “”Constructor called””; }
};
int main()
{
Point t1, *t2;
return 0;
}
- Compile error
- Constructor called
Constructor called - Constructor called
- Runtime error
View Answer
Constructor called
#include
using namespace std;
class X
{
public:
int x;
};
int main()
{
X a = {10};
X b = a;
cout << a.x << “” “” << b.x;
return 0;
}
- Compile error
- 10 followed by Garbage value
- 10 10
- 10 0
View Answer
10 10
Which of the following is FALSE about references in C++
- References cannot be NULL
- A reference must be initialized when declared
- Once reference is created, it cannot be later made to reference another object; it cannot be reset
- References cannot refer to constant value.
View Answer
References cannot refer to constant value.
Which of the following functions must use reference?
- Assignment operator function
- Copy constructor
- Destructor
- Parameterized Constructor
View Answer
Copy constructor
Predict the output
#include
using namespace std;
int &fun()
{
static int x = 10;
return x;
}
int main()
{
fun() = 30;
cout << fun();
return 0;
}
- Compiler error:Function cannot be used as value
- 10
- 30
- runtime error
View Answer
30
#include
using namespace std;
int &fun()
{
int x = 10;
return x;
}
int main()
{
fun() = 30;
cout << fun();
return 0;
}
- May cause runtime error
- May cause compiler error
- Always works fine
- 0
View Answer
May cause runtime error
#include
using namespace std;
int main()
{
int x = 10;
int& ref = x;
ref = 20;
cout << “”x = “” << x << endl ;
x = 30;
cout << “”ref = “” << ref << endl;
return 0;
}
- x = 20
- Ref = 30
- x = 20
Ref = 20 - x = 10
Ref = 30
View Answer
May cause runtime error
#include
using namespace std;
int main()
{
int x = 10;
int& ref = x;
ref = 20;
cout << “”x = “” << x << endl ;
x = 30;
cout << “”ref = “” << ref << endl;
return 0;
}
- x = 20
- Ref = 30
- x = 20
Ref = 20 - x = 10
Ref = 30
View Answer
x = 20
#include
using namespace std;
class Empty {};
int main()
{
cout << sizeof(Empty);
return 0;
}
- 0
- Garbage Value
- Compiler error
- Runtime error
View Answer
Compiler error
Which of the following is true?
- All objects of a class share all data members of class
- Objects of a class do not share non-static members. Every object has its own copy.
- Objects of a class do not share codes of non-static methods, they have their own copy
- None of the above
View Answer
Objects of a class do not share non-static members. Every object has its own copy.
Which of the following is true?
- All objects of a class share all data members of class
- Objects of a class do not share non-static members. Every object has its own copy.
- Objects of a class do not share codes of non-static methods, they have their own copy
- None of the above
View Answer
Objects of a class do not share non-static members. Every object has its own copy.
A member function can always access the data in __________(in C++).
- The class of which it is member
- The object of which it is a member
- The public part of its class
- The private part of its class
View Answer
The class of which it is member
Which of the following is not correct for virtual function in C++ ?
- Must be declared in public section of the class
- Virtual function can be static
- Virtual function should be accessed using pointers
- Virtual function is defined in base class
View Answer
Virtual function can be static
Which of the following cannot be passed to a function in C++ ?
- Constant
- Structure
- Array
- Header file
View Answer
Header file
Which of the following is not a correct statement?
- Every class containing abstract method must be declared abstract.
- Abstract class can be initiated.
- Abstract class does not contain any definition of implementation
- Abstract class can directly be initiated with ‘new’ operator.
View Answer
Abstract class can directly be initiated with ‘new’ operator.