1Which keyword is used to overload an operator in C++?
A.override
B.overload
C.this
D.operator
Correct Answer: operator
Explanation:
The 'operator' keyword is used to declare a function that specifies what a particular operator means when applied to instances of a class.
Incorrect! Try again.
2Which of the following operators CANNOT be overloaded?
A.[]
B.==
C.::
D.+
Correct Answer: ::
Explanation:
The scope resolution operator (::), along with the dot operator (.), ternary operator (?:), and sizeof, cannot be overloaded.
Incorrect! Try again.
3When overloading a binary operator as a member function, how many arguments does the function take?
A.Zero
B.Three
C.Two
D.One
Correct Answer: One
Explanation:
As a member function, a binary operator takes one explicit argument (the right-hand operand); the left-hand operand is passed implicitly via the 'this' pointer.
Incorrect! Try again.
4When overloading a unary operator as a friend function, how many arguments must it take?
A.Two
B.One
C.Zero
D.Three
Correct Answer: One
Explanation:
Since a friend function does not have a 'this' pointer, the operand must be passed explicitly as an argument.
Incorrect! Try again.
5To convert a Basic type to a Class type, which method is primarily used?
A.Destructor
B.Casting Operator
C.Operator Function
D.Constructor
Correct Answer: Constructor
Explanation:
A constructor taking a single argument of a basic type is used to convert that basic type into the class type.
Incorrect! Try again.
6Which syntax is used to define a conversion function to convert a Class type to a Basic type?
A.type operator()
B.to type()
C.operator type()
D.conversion type()
Correct Answer: operator type()
Explanation:
The syntax 'operator type()' (e.g., operator int()) is used to define a casting operator function for class-to-basic conversion.
Incorrect! Try again.
7What is the return type specified in the declaration of a casting operator function (e.g., operator int())?
A.auto
B.int
C.void
D.No return type
Correct Answer: No return type
Explanation:
Casting operator functions do not specify a return type explicitly; the return type is inferred from the operator name itself.
Incorrect! Try again.
8Which relationship does Inheritance primarily model?
A.Has-A
B.Uses-A
C.Part-Of
D.Is-A
Correct Answer: Is-A
Explanation:
Inheritance models an 'Is-A' relationship (e.g., a Dog is an Animal).
Incorrect! Try again.
9In the syntax class Derived : access Base, what is the default access mode if none is specified?
A.public
B.private
C.protected
D.internal
Correct Answer: private
Explanation:
In C++, the default inheritance mode for classes is private.
Incorrect! Try again.
10Which type of inheritance involves a derived class inheriting from multiple base classes?
A.Multi-level Inheritance
B.Multiple Inheritance
C.Hierarchical Inheritance
D.Single Inheritance
Correct Answer: Multiple Inheritance
Explanation:
Multiple inheritance occurs when a class is derived from more than one base class.
Incorrect! Try again.
11In public inheritance, public members of the base class become __ in the derived class.
A.Public
B.Protected
C.Private
D.Inaccessible
Correct Answer: Public
Explanation:
Under public inheritance, public members of the base class retain their public status in the derived class.
Incorrect! Try again.
12In private inheritance, public members of the base class become __ in the derived class.
A.Protected
B.Inaccessible
C.Public
D.Private
Correct Answer: Private
Explanation:
Under private inheritance, all accessible members (public and protected) of the base class become private in the derived class.
Incorrect! Try again.
13Which members of a base class are never accessible to the derived class directly?
A.Static members
B.Public members
C.Protected members
D.Private members
Correct Answer: Private members
Explanation:
Private members of a base class are not directly accessible by the derived class, regardless of the inheritance mode.
Incorrect! Try again.
14What is the correct order of constructor execution in Single Inheritance?
A.Derived first, then Base
B.Simultaneous
C.Base first, then Derived
D.Random
Correct Answer: Base first, then Derived
Explanation:
Constructors are executed in the order of inheritance: the base class constructor is called first, followed by the derived class constructor.
Incorrect! Try again.
15What is the correct order of destructor execution in Single Inheritance?
A.Simultaneous
B.Derived first, then Base
C.Random
D.Base first, then Derived
Correct Answer: Derived first, then Base
Explanation:
Destructors are executed in reverse order of construction: the derived class destructor is called first, followed by the base class destructor.
Incorrect! Try again.
16If class C inherits from class B, and class B inherits from class A, this is an example of:
A.Multiple Inheritance
B.Multi-level Inheritance
C.Hybrid Inheritance
D.Hierarchical Inheritance
Correct Answer: Multi-level Inheritance
Explanation:
This chain (A -> B -> C) represents Multi-level inheritance.
Incorrect! Try again.
17What is the 'Diamond Problem' in C++ inheritance?
A.Ambiguity arising from multiple inheritance where two base classes inherit from a common ancestor
B.Memory leaks in derived classes
C.Private members becoming public
D.Circular dependency errors
Correct Answer: Ambiguity arising from multiple inheritance where two base classes inherit from a common ancestor
Explanation:
The Diamond Problem occurs in multiple inheritance when a class inherits from two classes that both inherit from the same super-base class, causing duplication of the super-base members.
Incorrect! Try again.
18How is the Diamond Problem resolved in C++?
A.Using static classes
B.Using friend functions
C.Using Virtual Base Classes
D.Using abstract classes
Correct Answer: Using Virtual Base Classes
Explanation:
Virtual inheritance ensures that only one copy of the base class members is inherited by the grandchild class.
Incorrect! Try again.
19What distinguishes Function Overriding from Function Overloading?
A.Overriding happens in derived classes with same signature; Overloading happens in same/derived scope with different signature
B.Overriding happens within the same scope; Overloading happens in derived classes
C.Overriding requires different return types
D.Overloading uses the virtual keyword
Correct Answer: Overriding happens in derived classes with same signature; Overloading happens in same/derived scope with different signature
Explanation:
Overriding redefines a base class function in a derived class with the exact same signature. Overloading involves functions with the same name but different parameters.
Incorrect! Try again.
20How can a derived class call an overridden member function of the base class?
A.parent->func()
B.Base::func()
C.this->func()
D.super.func()
Correct Answer: Base::func()
Explanation:
The scope resolution operator (::) is used to access the overridden function of the base class explicitly.
Incorrect! Try again.
21What does the protected access specifier ensure?
A.Members are accessible in the class and its derived classes
B.Members are accessible everywhere
C.Members are accessible only within the class
D.Members are accessible only to friend functions
Correct Answer: Members are accessible in the class and its derived classes
Explanation:
Protected members are inaccessible outside the class but can be accessed by derived classes.
Incorrect! Try again.
22Which concept represents a 'Has-A' relationship where the child cannot exist without the parent (Strong Association)?
A.Inheritance
B.Composition
C.Association
D.Aggregation
Correct Answer: Composition
Explanation:
Composition is a strong 'Has-A' relationship where the lifecycle of the contained object is managed by the container; if the container is destroyed, the contained object is also destroyed.
Incorrect! Try again.
23Which concept represents a 'Has-A' relationship where the child can exist independently of the parent (Weak Association)?
A.Aggregation
B.Encapsulation
C.Composition
D.Inheritance
Correct Answer: Aggregation
Explanation:
Aggregation implies a relationship where the child object can exist independently of the parent object.
Incorrect! Try again.
24When overloading the increment operator operator++(int), what does the dummy int parameter indicate?
A.It indicates Postfix increment
B.It indicates Prefix increment
C.It adds an integer to the object
D.It is a syntax error
Correct Answer: It indicates Postfix increment
Explanation:
The int argument is a dummy flag used by the compiler to distinguish post-increment (++x) from pre-increment (x++).
Incorrect! Try again.
25If class D : public B1, public B2 is defined, which constructor is called first?
A.D
B.B2
C.B1
D.It depends on the initializer list
Correct Answer: B1
Explanation:
In multiple inheritance, base class constructors are called in the order in which they are inherited (declared in the class header).
Incorrect! Try again.
26Which operator is typically overloaded to implement input/output for a user-defined class?
A.<< and >>
B.+ and -
C.new and delete
D.= and ==
Correct Answer: << and >>
Explanation:
The stream insertion (<<) and extraction (>>) operators are overloaded to facilitate I/O for custom classes.
Incorrect! Try again.
27Why must operator<< and operator>> be overloaded as friend functions usually?
A.Because the left operand is a stream object, not the class object
B.Because they return void
C.Because they are static
D.Because they modify private data
Correct Answer: Because the left operand is a stream object, not the class object
Explanation:
If they were member functions, the syntax would be object << cout. To support cout << object, the function must be a non-member (usually friend) where the first argument is the stream.
Incorrect! Try again.
28Can a constructor be inherited?
A.Only default constructors
B.No, constructors are not inherited
C.Only copy constructors
D.Yes, always
Correct Answer: No, constructors are not inherited
Explanation:
Constructors and destructors are not inherited; however, the derived class constructor invokes the base class constructor.
Incorrect! Try again.
29What happens if a derived class defines a function with the same name as a base class function but different arguments?
A.It merges with the base function
B.It causes a compile error
C.It hides the base function
D.It overrides the base function
Correct Answer: It hides the base function
Explanation:
This is called name hiding. The base class function becomes hidden in the derived scope unless explicitly brought in with a using declaration.
Incorrect! Try again.
30Which inheritance mode makes public members of the base class protected in the derived class?
A.public
B.virtual
C.private
D.protected
Correct Answer: protected
Explanation:
Protected inheritance changes public and protected members of the base class to protected in the derived class.
Incorrect! Try again.
31In hierarchical inheritance, how many derived classes exist?
A.A chain of derived classes
B.Exactly one
C.Two or more derived from a single base
D.One derived from multiple bases
Correct Answer: Two or more derived from a single base
Explanation:
Hierarchical inheritance involves a single base class serving as a parent to multiple derived classes.
Incorrect! Try again.
32How do you pass arguments to a base class constructor from a derived class constructor?
A.Using the super keyword
B.Using the Member Initializer List
C.It is handled automatically
D.Inside the constructor body
Correct Answer: Using the Member Initializer List
Explanation:
Arguments are passed to the base constructor using the syntax Derived(args) : Base(args) { ... }.
Incorrect! Try again.
33To prevent a class from being inherited, C++11 introduced which keyword?
A.final
B.static
C.const
D.stop
Correct Answer: final
Explanation:
The final specifier prevents a class from being used as a base class.
Incorrect! Try again.
34What is the return type of the overloaded assignment operator (operator=) generally?
A.Reference to the class type
B.void
C.int
D.The class type by value
Correct Answer: Reference to the class type
Explanation:
It returns a reference to the class type (*this) to allow for chained assignments (e.g., a = b = c).
Incorrect! Try again.
35Which operator cannot be overloaded using a friend function?
A.-
B.==
C.+
D.=
Correct Answer: =
Explanation:
The assignment operator (=), function call operator (()), subscript operator ([]), and arrow operator (->) must be overloaded as member functions.
Incorrect! Try again.
36If a base class has a private member int x, and class D : public Base, how can D access x?
A.It cannot access it directly
B.Using this->x
C.Using Base::x
D.Directly as x
Correct Answer: It cannot access it directly
Explanation:
Private members are never accessible in derived classes. Access must be provided via public/protected getter methods in the base class.
Incorrect! Try again.
37What is the ambiguity resolution operator used in multiple inheritance?
A.::
B.->
C.*
D..
Correct Answer: ::
Explanation:
The scope resolution operator (::) is used to specify which base class's member is being accessed (e.g., Base1::print()).
Incorrect! Try again.
38In composition, if the container object is destroyed:
A.It depends on the destructor
B.The contained objects remain in memory
C.The contained objects become null
D.The contained objects are also destroyed
Correct Answer: The contained objects are also destroyed
Explanation:
Composition implies ownership; the lifetime of the part is bound to the lifetime of the whole.
Incorrect! Try again.
39Which of the following is true about friend functions and inheritance?
A.Friendships are not inherited
B.Friends become public members
C.Derived classes automatically befriend the base's friends
D.Friendships are inherited
Correct Answer: Friendships are not inherited
Explanation:
Friendship is not inherited. A friend of the base class is not automatically a friend of the derived class.
Incorrect! Try again.
40When overloading the subscript operator [], what is it commonly used for?
A.To cast types
B.To modify the inheritance mode
C.To access elements of an object as if it were an array
D.To create arrays of objects
Correct Answer: To access elements of an object as if it were an array
Explanation:
Overloading [] allows objects (like a custom Vector or String class) to be indexed using array syntax.
Incorrect! Try again.
41What is a major advantage of Operator Overloading?
A.Increases execution speed
B.Reduces memory usage
C.Allows user-defined types to behave like built-in types
D.Enables multiple inheritance
Correct Answer: Allows user-defined types to behave like built-in types
Explanation:
It improves code readability and allows custom classes to integrate seamlessly with standard operators.
Incorrect! Try again.
42If a constructor is declared explicit, what does it prevent?
A.Object destruction
B.Overloading
C.Inheritance
D.Implicit type conversion
Correct Answer: Implicit type conversion
Explanation:
The explicit keyword prevents the compiler from using that constructor for implicit type conversions (e.g., passing an int to a function expecting a Class object).
Incorrect! Try again.
43In a class-to-basic type conversion function, can arguments be passed?
A.Yes, multiple arguments
B.No, it takes no arguments
C.Only default arguments
D.Yes, one argument
Correct Answer: No, it takes no arguments
Explanation:
Conversion functions (e.g., operator double()) take no arguments.
Incorrect! Try again.
44Virtual Base Classes are primarily used to solve ambiguity in which type of inheritance?
A.Hierarchical
B.Single
C.Hybrid/Multiple
D.Multilevel
Correct Answer: Hybrid/Multiple
Explanation:
They are used in multipath (hybrid) inheritance involving multiple inheritance (the Diamond Problem).
Incorrect! Try again.
45Which header file is generally required for standard I/O operator overloading?
A.<conio.h>
B.<iostream>
C.<stdlib.h>
D.<fstream>
Correct Answer: <iostream>
Explanation:
<iostream> defines istream and ostream, which are necessary for overloading << and >>.
Incorrect! Try again.
46What is the syntax to declare a virtual base class?
A.class B : virtual public A
B.virtual class B : public A
C.class B : public virtual A
D.Both A and B
Correct Answer: Both A and B
Explanation:
The virtual keyword can be placed before or after the access specifier (e.g., virtual public or public virtual).
Incorrect! Try again.
47In aggregation, the relationship between the Whole and the Part is:
A.The Part can exist independently of the Whole
B.The Whole is a friend of the Part
C.The Whole inherits from the Part
D.The Part cannot exist without the Whole
Correct Answer: The Part can exist independently of the Whole
Explanation:
Aggregation is a weak relationship (e.g., A Department has Teachers; if the Department closes, Teachers still exist).
Incorrect! Try again.
48What is the main benefit of Inheritance?
A.Code Reusability
B.Encapsulation
C.Data Hiding
D.Operator Overloading
Correct Answer: Code Reusability
Explanation:
Inheritance allows new classes to reuse code from existing classes, reducing redundancy.
Incorrect! Try again.
49When overloading the function call operator (), how many arguments can it accept?
A.Two
B.Any number
C.One
D.Zero
Correct Answer: Any number
Explanation:
The function call operator operator() is unique in that it can be overloaded to accept any number of arguments.
Incorrect! Try again.
50If a class Derived inherits Protectedly from Base, and Base has a public function foo(), can main() call DerivedObj.foo()?
A.Only if foo is virtual
B.Only if Derived is static
C.Yes
D.No
Correct Answer: No
Explanation:
Since the inheritance is protected, foo() (which was public in Base) becomes protected in Derived, making it inaccessible to the outside world (like main).