OOP with C++ MCQs (CMCE & CCEE Practice)
69 OOP with C++ MCQs with answers and explanations for C-DAC CMCE and CCEE practice: classes, inheritance, polymorphism, references and operators.
How many keywords are available in C++ (standard C++98)?
Standard C++98 defines 63 keywords. C has only 32. Later standards (C++11 onwards) add more.
Which is the only language that is purely object-oriented?
In Smalltalk everything is an object, including numbers and classes. C++ and Java also have primitive types.
Which was the first OOP language, introduced in the 1960s?
Simula (1960s) introduced classes and objects and inspired later languages, including C++.
Writing functions with the same name but a different number, type or order of arguments is called function ________.
Same name with a different parameter list is function overloading. Overriding redefines a base class function in a derived class.
The operator used with cin is called the ________ operator (>>).
cin >> x extracts data from the input stream, so >> is the extraction operator.
The operator used with cout is called the ________ operator (<<).
cout << x inserts data into the output stream, so << is the insertion operator.
The values stored in the data members of an object are called the ________ of the object.
An object has state (data member values), behavior (member functions) and identity (its unique address).
The ________ of an object is decided by its member functions.
Behavior is what the object can do, defined by its member functions.
Default arguments must be given in ________.
Once a parameter has a default value, every parameter to its right must also have one, so defaults go right to left.
The size of an object of an empty class is always ________.
An empty class object still needs a unique address, so the compiler gives it 1 byte, which is 8 bits.
Not every function marked inline is replaced by the compiler. Which of these may not be replaced?
The compiler may ignore inline for functions with loops or switch statements and for recursive functions. Inline is only a request.
Which is the first procedure-oriented (POP) language?
FORTRAN (1950s) is generally considered the first high-level procedural language.
Which is the first object-based language?
Ada is commonly cited as the first object-based language. Object-based means it has objects but not full inheritance and polymorphism.
C++ supports all data types provided by C and adds the data types ________.
C++ adds the built-in types bool and wchar_t.
When the call to a function is resolved at compile time it is compile time polymorphism. It is achieved using ________.
Function overloading and operator overloading are resolved at compile time. Overriding with virtual functions is resolved at run time.
Removal of a small object does not affect the big object. This relationship is called ________.
Answer as given in the source material. Be aware that many textbooks call this aggregation, and call the relationship in the next question composition (containment). Check which terminology your course uses.
Removal of a small object affects the big object. This relationship is called ________.
Answer as given in the source material. Many textbooks call a part whose lifetime depends on the whole composition (containment), and aggregation a weaker relationship. Check which terminology your course uses.
In C++, the default mode of inheritance for a class is ________.
For a class, the default inheritance mode is private. For a struct it is public.
A function that is called depending on the type of the object rather than the type of the pointer is called a ________.
A virtual function is resolved at run time through the object's actual type (dynamic binding).
A class that contains at least one pure virtual function is called a ________ class.
A class with at least one pure virtual function is abstract and cannot be instantiated.
Storing the address of a derived class object in a base class pointer is called ________.
Moving up the hierarchy (derived to base) is up casting. It is implicit and safe.
Storing the address of a base class object in a derived class pointer is called ________.
Moving down the hierarchy (base to derived) is down casting. It needs an explicit cast and can be unsafe.
Assigning a derived class object to a base class object copies only the base class portion and cuts off the rest. This is called ________.
The derived part is sliced off when a derived object is assigned by value to a base class object.
A pointer that points to memory which is no longer available is called a ________.
A dangling pointer refers to memory that was freed or has gone out of scope.
Which of the following is called automatically each time an object is created?
The constructor runs automatically when an object is created.
Which statement is true about an abstract class?
An abstract class cannot be instantiated. It exists as a base for other classes. It can have data members, constructors and a destructor.
Which function is invoked automatically when an object goes out of scope?
The destructor runs automatically when the object's lifetime ends.
The mechanism by which one class acquires the properties of another class is called ________.
Inheritance lets a derived class reuse the members of a base class.
When a derived class inherits from more than one base class it is called ________.
One derived class with two or more base classes is multiple inheritance.
One base class with more than one derived class is called ________.
One base with many derived classes is hierarchical inheritance.
Single inheritance carried over multiple levels is called ________.
A chain such as A to B to C is multilevel inheritance.
One base class with only one derived class is called ________.
One base and one derived class is single inheritance.
Which statement is NOT correct about a static member function of a class?
Static member functions can be called through an object or the class name, and non-static functions can call them. Option 4 is the incorrect statement: a static function can access static data members, but it cannot directly access non-static members because it has no this pointer.
Which of the following is NOT a key component of OOP?
The core OOP concepts are encapsulation, abstraction, inheritance and polymorphism. Virtualization is not one of them.
A class defined inside another class is called ________.
A class declared within another class is a nested class.
Which keyword is used to refer to the current object of a class?
this is a pointer to the object on which a member function was called.
"I have many roles in life: teacher, employee, student, cricket player and many more." Which object-oriented concept does this statement illustrate?
One entity taking many forms or roles is polymorphism (many forms).
What is the output of the following code? #include<iostream> using namespace std; int main() { int a = 5; int &b = a; int c = 10; b = c; cout << a << " " << b << endl; c = 20; cout << a << " " << b << endl; return 0; }
b is a reference bound to a, so b = c assigns 10 to a (it does not rebind b). Both print 10 10. Changing c later does not affect a or b, so the second line is also 10 10.
A member function declared in a base class and redefined by a derived class is a ________.
Redefining a base class virtual function in the derived class is overriding, which gives run time polymorphism.
Every non-const member function of a class is a ________.
A member function that can modify the object's state is a mutator (modifier). A const member function is an inspector.
A class is a ________.
A class is a user defined data type.
Which of the following statements is NOT true about a destructor?
A destructor takes no parameters and returns nothing, so it cannot accept a class object as a parameter.
Which of the following statements is true about a destructor?
A destructor is a member function that finalizes the object, with no return value and no parameters.
Which is the correct declaration of calculate() as a pure virtual function?
A pure virtual function needs both the virtual keyword and the = 0 specifier.
To eliminate the side effects of macros we can use ________.
Inline functions give macro-like speed but evaluate arguments once and are type checked, avoiding macro side effects.
C++ was developed by ________.
Bjarne Stroustrup developed C++ at Bell Labs.
C++ was invented in the year ________.
Stroustrup began the work in 1979 as "C with Classes". It was renamed C++ in 1983.
Which of the following is a properly defined structure?
A structure definition needs braces and a terminating semicolon.
private and public are known as ________ in C++.
private, protected and public are access specifiers.
Which of the following functions can be called without arguments?
Only a function whose parameters all have default values can be called with no arguments.
Which of the following is a valid class declaration?
A class definition must end with a semicolon. public class and object are not C++ syntax.
The data members and functions of a class in C++ are by default ________.
Members of a class are private by default. Members of a struct are public by default.
How are constructors different from other member functions of the class?
All three statements describe how a constructor differs from ordinary member functions.
The this pointer is ________ and passed ________.
this is a constant pointer and the compiler passes it implicitly to non-static member functions.
Which of the following describes a destructor?
A destructor cleans up resources the object acquired, when the object is destroyed.
What is the general syntax for accessing a namespace variable?
Use the scope resolution operator ::, for example std::cout.
What is the syntax for defining a destructor of class A?
A destructor has the class name prefixed with a tilde and no parameters or return type.
Which keyword is used to access a variable in a namespace without qualifying it each time?
The using keyword brings a namespace or its names into scope.
________ is the standard namespace in C++.
The standard library names live in the std namespace.
By default, how are variable values passed in C++?
Arguments are passed by value unless you use a reference or pointer parameter.
How are constants declared in C++?
The preferred C++ way is the const keyword, which is type checked. #define also creates a symbolic constant, but it is a preprocessor macro, not a typed constant.
Inside a constant member function, which keyword lets us modify a non-constant data member?
A data member declared mutable can be modified even inside a const member function.
What is the syntax for defining a static data member?
A static data member is defined outside the class using the scope resolution operator.
Which one of the following operators can NOT be overloaded?
The ternary conditional operator ?: cannot be overloaded. Neither can ::, . and .*, and sizeof.
Which feature of OOP illustrates code reusability?
Inheritance lets derived classes reuse base class code.
What happens with the following code? #include<iostream> using namespace std; int main() { int &a = 5; cout << a << endl; return 0; }
A non-const reference cannot bind to a literal. This is rejected at compile time (const int &a = 5 would be valid).
Which one of the following does NOT represent compile time polymorphism?
Function overriding with virtual functions is resolved at run time.
Which of the following operators can be overloaded using a member function?
The operators =, [], -> and () can only be overloaded as member functions.
What is the output of the following C++ code? #include<iostream> using namespace std; int main(void) { int # int a = 5; &num = a; cout << num; return 0; }
A reference must be initialized when it is declared, so int # fails to compile.
Quiz Complete!
📖 Study first: View Cheat Sheet