What value does a function with return type void return?
- 0
- Null
- It doesn't return a value
- An undefined value
A function with a void return type doesn't return any value. It is often used for functions that perform an action but do not need to send a result back to the caller.
What is the primary purpose of encapsulation in object-oriented programming in C++?
- Code compression
- Memory saving
- Hide complexity
- Code sharing
Encapsulation is a core concept in object-oriented programming. Its primary purpose in C++ and other OOP languages is to hide the internal representation, or state, of an object from the outside and to bundle the data (attributes) and the methods (functions) that operate on the data into a single unit, thus hiding complexity.
In C++, the method used to allocate memory dynamically during runtime within the object is called _______.
- malloc
- new
- alloc
- reserve
In C++, dynamic memory allocation for objects is achieved using the new operator. Unlike malloc in C, new in C++ initializes the allocated memory.
The problem in which a derived class may inherit the same member more than once from a base class hierarchy is known as the _______ problem.
- Replication
- Duplication
- Diamond
- Looping
The "Diamond Problem" arises in object-oriented languages that support multiple inheritance. If a class is derived from two classes that have a common base class, the derived class may inherit the same member more than once.
Which of the following is true about function overloading in C++?
- Functions must be in the same scope
- Return type can be used to differentiate overloaded functions
- Overloaded functions must have a different number of parameters
- Only member functions can be overloaded
Function overloading in C++ allows multiple functions with the same name but different parameters (types and/or number). They must be in the same scope. Return type alone cannot be used to distinguish overloaded functions, and both member and non-member functions can be overloaded.
Which type of function cannot be virtual in C++?
- Inline functions
- Constructor functions
- Friend functions
- Static member functions
Constructors in C++ cannot be virtual. When creating an instance of a derived class, it's essential to know the exact type of the object being created, so the correct constructor gets called. Making constructors virtual doesn't make sense in the context of object-oriented design, as it would introduce ambiguity in the object creation process.
To check for possible errors or failures in file operations, you should check the _______.
- failbit
- seekg
- open
- tellg
The failbit is a status flag in C++ streams that indicates a file operation failed. When working with file I/O in C++, it's crucial to check this flag (or use functions like fail()) to determine if a particular operation has encountered an error.
How does C++ handle template instantiation when the same instantiation is required in multiple translation units?
- Via separate instantiation
- Via shared instantiation
- Via static instantiation
- Via dynamic instantiation
C++ typically handles template instantiation on a per-translation unit basis. This means each translation unit will have its own instance of the template. Linkers are smart enough to discard duplicate instances, ensuring that only one instance remains in the final executable.
Which of the following C++ versions introduced the auto keyword for automatic type deduction?
- C++03
- C++11
- C++14
- C++98
The auto keyword for automatic type deduction was introduced in C++11. It allows the compiler to automatically deduce the type of a variable based on its initializer, enhancing code readability and maintainability.
In a system that processes user commands, you notice that the if-else chain for command processing has become excessively long and difficult to manage. Which refactorization strategy might be most effective?
- Introduce a command pattern
- Split the chain into functions
- Use a nested if-else
- Increase the use of comments
The Command Pattern encapsulates a command request as an object, allowing the parameters to be passed, queued, and executed at a later time. This makes the code modular, maintainable, and ensures decoupling between classes that invoke operations from those that perform operations.