Cpp Programming Set 2
On This Page
This set of CPP Programming Multiple Choice Questions & Answers (MCQs) focuses on Cpp Programming Set 2
Q1 | Which of the following is not a member of class?
- Static function
- Friend function
- Const function
- Virtual function
Q2 | How can we restrict dynamic allocation of objects of a class using new?
- By overloading new operator
- By making an empty private new operator.
- By making an empty private new and new[] operators
- By overloading new operator and new[] operators
Q3 | Which of the following operators cannot be overloaded?
- . (Member Access or Dot operator)
- ?: (Ternary or Conditional Operator)
- :: (Scope Resolution Operator)
- All of the above
Q4 | Which of the following operators are overloaded by default by the compiler in every user defined classes even if user has not written? 1) Comparison Operator (==) 2) Assignment Operator (=)
- Both 1 and 2
- Only 1
- Only 2
- None of the two
Q5 | Which of the following operators should be preferred to overload as a global function rather than a member method?
- Postfix ++
- Comparison Operator
- Insertion Operator <<
- Prefix++
Q6 | How C++ compiler does differ between overloaded postfix and prefix operators?
- C++ doesn’t allow both operators to be overloaded in a class
- A postfix ++ has a dummy parameter
- A prefix ++ has a dummy parameter
- By making prefix ++ as a global function and postfix as a member function.
Q7 | Which of the following operator functions cannot be global?
- new
- delete
- Conversion Operator
- All of the above
Q8 | Which of the following is true about this pointer?
- It is passed as a hidden argument to all function calls
- It is passed as a hidden argument to all non-static function calls
- It is passed as a hidden argument to all static functions
- None of the above
Q9 | What is the use of this pointer?
- When local variable’s name is same as member’s name, we can access member using this pointer.
- To return reference to the calling object
- Can be used for chained function calls on an object
- All of the above
Q10 | Which of the following in Object Oriented Programming is supported by Function overloading and default arguments features of C++?
- Inheritance
- Polymorphism
- Encapsulation
- None of the above
Q11 | Output of the program?#includeusing namespace std;int fun(int x = 0, int y = 0, int z){ return (x + y + z); }int main(){cout << fun(10);return 0;}
- 10
- 0
- 20
- Compiler Error
Q12 | Output of following program?#include using namespace std;int fun(int=0, int = 0);int main(){cout << fun(5);return 0;}int fun(int x, int y){return (x+y);}
- Compiler Error
- 5
- 10
Q13 | Which of the following is true?
- Static methods cannot be overloaded.
- Static data members can only be accessed by static methods.
- Non-static data members can be accessed by static methods.
- Static methods can only access static members (data and methods)
Q14 | If a function is friend of a class, which one of the following is wrong?
- A function can only be declared a friend by a class itself.
- Friend functions are not members of a class, they are associated with it.
- Friend functions are members of a class.
- It can have access to all members of the class, even private ones.
Q15 | Which one of the following is correct, when a class grants friend status to another class?
- The member functions of the class generating friendship can access the members of the friend class.
- All member functions of the class granted friendship have unrestricted access to the members of the class granting the friendship.
- Class friendship is reciprocal to each other.
- There is no such concept.
Q16 | In C++, const qualifier can be applied to1) Member functions of a class2) Function arguments3) To a class data member which is declared as static4) Reference variables
- Only 1, 2 and 3
- Only 1, 2 and 4
- All
- Only 1, 3 and 4
Q17 | How to create a dynamic array of pointers (to integers) of size 10 using new in C++?Hint: We can create a non-dynamic array using int *arr[10]
- int *arr = new int *[10];
- int **arr = new int *[10];
- int *arr = new int [10];
- Not Possible
Q18 | Which of the following is true about new when compared with malloc:1) new is an operator, malloc is a function2) new calls constructor, malloc doesn’t3) new returns appropriate pointer, malloc returns void * and pointer needs to typecast to appropriate type.
- 1 and 3
- 2 and 3
- 1 and 2
- All 1, 2 and 3
Q19 | Predict the output?#include using namespace std;class Test{int x;Test(){x = 5;} };int main(){Test *t = new Test;cout << t->x;}
- Compiler Error
- 5
- Garbage Value
Q20 | Is it fine to call delete twice for a pointer?#includeusing namespace std;int main(){int *ptr = new int;delete ptr;delete ptr;return 0;}
- Yes
- No
- none
- all
Q21 | When the inheritance is private, the private methods in base class are __________ in thederived class (in C++).
- inaccessible
- accessible
- protected
- public
Q22 | What happens when delete is used for a NULL pointer?int *ptr = NULL;delete ptr;
- Compiler Error
- Run-time Crash
- No Error
- None
Q23 | Which of the following is true about virtual functions in C++?
- Virtual functions are functions that can be overridden in derived class with the same signature.
- Virtual functions enable run-time polymorphism in a inheritance hierarchy.
- If a function is ‘virtual’ in the base class, the most-derived class implementation of the function is called according to the actual type of the object referred to, regardless of the declared type of the pointer or reference. In non-virtual functions, the functions are called according to the type of reference or pointer.
- All of the above
Q24 | Which of the following is true about pure virtual functions?1) Their implementation is not provided in a class where they are declared.2) If a class has a pure virtual function, then the class becomes abstract class and aninstance of this class cannot be created.
- Both 1 and 2
- Only 1
- Only 2
- Neither 1 nor 2
Q25 | What is the size of wchar_t in C++?
- 2
- 4
- 2 or 4
- Based on the number of bits in the system