Functions and OOP Features - user-defined functions, parameter passing, virtual functions, constructors, destructors, overloading, templates - One Line Questions
1.
What is the syntax for a destructor in C++? —
~ClassName()
2.
In the context of virtual functions, what is an abstract class? —
A class that cannot be instantiated and typically contains one or more pure virtual functions.
3.
In C++, `std::vector<int>` is an example of using: —
A class template
4.
A class template allows you to define: —
A blueprint for classes that can operate on different data types.
5.
A function template in C++ is used to create: —
A blueprint for functions that can operate on different data types.
6.
A copy constructor is called when: —
Both B and C
7.
When a variable is passed by value to a function, any changes made to the parameter inside the function: —
Are local to the function and do not affect the original variable.
8.
Pass by pointer is similar to pass by reference in that: —
Both can modify the original variable.
9.
Which of the following is NOT a valid way to achieve function overloading? —
Changing the return type of the function.
10.
A template function is defined using which keyword prefix? —
template
11.
Which of the following is NOT a common reason to use user-defined functions? —
Automatic memory management
12.
Which member function is automatically called when an object of a class is destroyed? —
Destructor
13.
What is the term for a constructor that can be called with a single argument? —
Implicitly converting constructor
14.
Which OOP feature allows an object to take on many forms? —
Polymorphism
15.
A virtual function in a base class allows derived classes to provide their own specific implementation, which is known as: —
Polymorphism
16.
Which of the following is a key benefit of using virtual functions? —
Allowing dynamic dispatch of function calls based on the actual type of the object at runtime.
17.
Which concept allows a single operator symbol (like `+` or `-`) to perform different operations based on the operands it is applied to? —
Operator overloading
18.
Consider a function `void display(int a, int b)`. If you create another function `void display(double x, double y)`, this is an example of: —
Function overloading
19.
When using templates, the compiler generates actual code for each specific data type used. This process is known as: —
Instantiation
20.
What does a constructor achieve that a regular function cannot regarding object initialization? —
It is automatically invoked when an object is created, ensuring proper initial state.
21.
What does `~` signify when used before a class name in C++? —
It indicates a destructor.
22.
What is the primary advantage of pass by reference over pass by value for large objects? —
It avoids the overhead of copying the entire object.
23.
A pure virtual function is a virtual function that: —
Has no implementation in the base class and is assigned 0.
24.
What happens if a class has multiple constructors? —
The compiler selects the appropriate constructor based on the arguments provided during object creation.
25.
If a class has a destructor, when is it guaranteed to be called? —
When the object goes out of scope or is explicitly deleted.
26.
What is the main difference between function overloading and function overriding? —
Overloading involves different function signatures, while overriding involves the same signature in a derived class.
27.
A constructor that takes no arguments or has default values for all its arguments is known as a: —
Default constructor
28.
Which parameter passing mechanism allows a function to modify the original variable passed to it? —
Pass by reference
29.
Function overloading allows multiple functions with the same name to exist if they differ in: —
Number and/or types of parameters
30.
Operator overloading is a form of: —
Compile-time polymorphism
31.
What is the keyword used in C++ to declare a virtual function? —
virtual
32.
Consider a base class `Animal` and a derived class `Dog`. If `speak()` is a virtual function in `Animal` and is overridden in `Dog`, calling `speak()` on a `Dog` object through a pointer to `Animal` will invoke: —
The `speak()` function of the `Dog` class.
33.
A function template can be specialized for a specific data type. This means: —
The compiler generates a unique version of the template function for that specific type.
34.
If a base class has a virtual destructor, what is the implication for derived class objects? —
The derived class destructor is called, followed by the base class destructor, ensuring proper cleanup.
35.
A derived class can access public and protected members of its base class. What is the purpose of `protected` access specifier? —
To allow access within the class and by derived classes.
36.
What is the primary goal of encapsulation in OOP? —
To bundle data (attributes) and methods (functions) that operate on the data into a single unit and restrict direct access to some components.
37.
What is the scope resolution operator (`::`) used for? —
To access members of a class or namespace from outside the class/namespace.
38.
What is the purpose of a template in C++? —
To enable generic programming, allowing code to work with different data types without rewriting.
39.
In C++, what is a virtual function primarily used for? —
To enable runtime polymorphism through inheritance.
40.
What is the main role of a constructor in object-oriented programming? —
To initialize the data members of an object when it is created.
41.
What is the primary purpose of a user-defined function in programming? —
To encapsulate a specific task or set of operations into a reusable block of code.
42.
What is the purpose of the `typename` keyword when declaring a template parameter for a dependent type? —
To inform the compiler that the identifier is a type name, not a variable or function name.
43.
What is the purpose of the `this` pointer in C++? —
To point to the current object within a member function.
44.
What is the primary benefit of using templates? —
To reduce the number of lines of code by writing generic code.
45.
A destructor can be overloaded. —
False, a class can have only one destructor
46.
If a function parameter is declared as `int& var`, it means the parameter is passed by: —
Reference
47.
If a function is declared as `void func(int *ptr)`, the parameter `ptr` is passed by: —
Pointer
48.
Which of the following is an example of compile-time polymorphism? —
Function overloading
49.
When does a constructor typically get called? —
When an object is declared or created.
50.
When is it generally recommended to use pass by reference rather than pass by pointer? —
All of the above