CPP Programming Set 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CPP Programming MCQs [set-1]

1. Which of the followings is/are automatically added to every class, if we do not


write our own?
A. Copy Constructor
B. Assignment Operator
C. A constructor without any parameter
D. All of the above
Answer: D

2. When a copy constructor may be called?


A. When an object of the class is returned by value.

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

. c
C. When an object is constructed based on another object of the same class
D. All of the above te
Answer: D a
q M
c
3. Constructors have _____ return type.
A. void
B. char
M
C. int
D. no
Answer: D

4. 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
Answer: B

5. 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.
A. All 1, 2, and 3
B. Only 1 and 3
C. Only 1 and 2
D. Only 2 and 3
Answer: B

6. Output of following program?


#include<iostream>
using namespace std;
class Point {
Point() { cout << "Constructor called"; }
}; int main()
{
Point t1;
return 0;
}
A. Compiler Error
B. Runtime Error
C. Constructor called
D. None of the above
Answer: A

7. #include<iostream>
using namespace std;
class Point {
public:
Point() { cout << "Constructor called"; }
};
int main()
{
Point t1, *t2;
return 0;
}
A. Compiler Error
B. Constructor called Constructor called

View all MCQ's at McqMate.com


C. Constructor called
D. None of the above
Answer: C

8. Which operator is having the highest precedence?


A. postfix
B. unary
C. shift
D. equality
Answer: D

9. Which of the following is FALSE about references in C++?


A. References cannot be NULL
B. A reference must be initialized when declared
C. Once a reference is created, it cannot be later made to reference another object; it cannot be
reset.
D. References cannot refer to constant value
Answer: D

10. Which of the following functions must use reference?


A. Assignment operator function
B. Copy Constructor
C. Destructor
D. Parameterized constructor
Answer: B

11. Output of following C++ program?


#include<iostream>
using namespace std;
int main()
{
int x = 10;
int& ref = x;
ref = 20;
cout << "x = " << x << endl ;
x = 30;
cout << "ref = " << ref << endl;

View all MCQ's at McqMate.com


return 0;
}
A. x = 20; ref = 30
B. x = 20; ref = 20
C. x = 10; ref = 30
D. x = 30; ref = 30
Answer: A

12. What is the difference between struct and class in C++?


A. All members of a structure are public and structures don’t have constructors and destructors
B. Members of a class are private by default and members of struct are public by default. When
deriving a struct from a class/struct, default access-specifier for a base class/struct is public and
when deriving a class, default access specifier is private.
C. All members of a structure are public and structures don’t have virtual functions
D. All of the above
Answer: B

13. Predict the output of following C++ program.


#include<iostream>
using namespace std;
class Empty {};
int main() {
cout << sizeof(Empty);
return 0;
}
A. A non-zero value
B. 0
C. Compiler Error
D. Runtime Error
Answer: A

14. class Test {


int x;
};
int main() {
Test t;
cout << t.x;

View all MCQ's at McqMate.com


return 0;
}
A. 0
B. Garbage Value
C. Compiler Error
D. None
Answer: C

15. Which of the following is true?


A. All objects of a class share all data members of class
B. Objects of a class do not share non-static members. Every object has its own copy.
C. Objects of a class do not share codes of non-static methods, they have their own copy
D. None of the above
Answer: B

16. A member function can always access the data in __________, (in C++).
A. the class of which it is member
B. the object of which it is a member
C. the public part of its class
D. the private part of its class
Answer: A

17. Which of the following is not correct for virtual function in C++?
A. Must be declared in public section of class.
B. Virtual function can be static.
C. Virtual function should be accessed using pointers.
D. Virtual function is defined in base class.
Answer: B

18. Which of the following is not correct (in C++)?


1. Class templates and function templates are instantiated in the same way
2. Class templates differ from function templates in the way they are initiated
3. Class template is initiated by defining an object using the template argument
4. Class templates are generally used for storage classes
A. (1)
B. (2), (4)
C. (2), (3), (4)

View all MCQ's at McqMate.com


D. (4)
Answer: C

19. Which of the following cannot be passed to a function in C++?


A. Constant
B. Structure
C. Array
D. Header file
Answer: D

20. Which of the following, in C++, is inherited in a derived class from base class?
A. Constructor
B. Destructor
C. Data members
D. Virtual methods
Answer: C

21. Which of the following is a correct statement?


A. Composition is a strong type of association between two classes with full ownership.
B. Composition is a strong type of association between two classes with partial ownership.
C. Composition is a weak type of association between two classes with partial ownership.
D. Composition is a weak type of association between two classes with strong ownership.
Answer: A

22. Which of the following is not a correct statement?


A. Every class containing abstract method must be declared abstract.
B. Abstract class can directly be initiated with ‘new’ operator.
C. Abstract class can be initiated.
D. Abstract class does not contain any definition of implementation.
Answer: B

23. When a method in a subclass has the same name and type signatures as a
method in the superclass, then the method in the subclass _____ the method in the
superclass.
A. Overloads
B. Friendships
C. Inherits

View all MCQ's at McqMate.com


D. Overrides
Answer: D

24. It is possible to define a class within a class termed as nested class. There are
_____ types of nested classes.
A. 2
B. 3
C. 4
D. 5
Answer: A

25. When one object reference variable is assigned to another object reference
variable then
A. a copy of the object is created.
B. a copy of the reference is created.
C. a copy of the reference is not created.
D. it is illegal to assign one object reference variable to another object reference variable.
Answer: B

View all MCQ's at McqMate.com

You might also like