To prevent memory leaks, every call to new should be matched with a call to _______.
- delete
- free
- clear
- remove
When dynamic memory allocation is done using new, it's necessary to release that memory using delete to prevent memory leaks in C++. This ensures that every byte of memory allocated is properly released, maintaining the health and efficiency of the system.
Which of the following data types is not a primitive data type in C++?
- int
- float
- string
- double
While "int", "float", and "double" are primitive data types in C++, the "string" data type is a part of the C++ Standard Library and is, in fact, a class, not a primitive data type.
How does encapsulation aid in reducing software development complexity?
- By allowing multiple inheritance.
- By segregating the program into separate modules.
- By promoting reusability of code.
- By bundling data and methods that operate on that data.
Encapsulation in C++ is the bundling together of data and the methods that operate on that data, restricting direct access to some of the object's components. This is a fundamental concept in object-oriented programming. By encapsulating data and functions into a single unit, we can hide the internal details and reduce complexity.
To achieve runtime polymorphism in C++, _______ are used.
- classes
- macros
- virtual functions
- inline functions
Runtime polymorphism in C++ is achieved through the use of virtual functions. These are functions in the base class that you can override in derived classes. The decision about which method to call (the one in the base class or the one in the derived class) is made at runtime, based on the actual type of the object being pointed to, rather than the type of the pointer. This allows for more dynamic behavior and is a core feature of object-oriented programming in C++.
What is the default access specifier for a base class in C++?
- private
- public
- protected
- internal
In C++, if you don't specify an access specifier for the members of a class, they are implicitly set to private. This means that they can't be accessed or viewed from objects of the class or any derived class unless friends.
How does C++ handle char literals that are assigned to unsigned char variables?
- Converts to signed char
- Preserves exact value
- Converts to int
- Triggers an error
When a char literal is assigned to an unsigned char variable, C++ preserves its exact value. If the char literal's value is negative, when assigned to an unsigned char, it wraps around using standard two's complement arithmetic.
Which of the following smart pointers does not take ownership of the pointed object?
- unique_ptr
- shared_ptr
- weak_ptr
- auto_ptr
weak_ptr is a smart pointer that holds a non-owning reference to an object managed by shared_ptr. It doesn't affect the reference count and won't prevent the object from being deleted.
Imagine you're working on a large-scale software project involving numerous classes. Some classes are instantiating objects of another, and suddenly an object is accidentally deleted. What techniques or principles might be used to safeguard against accidental deletion in such a situation?
- Use of smart pointers.
- Overloading the delete operator.
- Enabling strict type-checking.
- Always use dynamic allocation.
Smart pointers in C++ are template classes that manage the lifetime of dynamically allocated objects. They ensure that the objects they manage are properly deleted, thus preventing accidental deletions or memory leaks. Overloading the delete operator or enabling strict type-checking wouldn't inherently prevent accidental deletions.
What is the output of a function with a void return type?
- An integer
- A floating point number
- Nothing
- A character
In C++, a function declared with a void return type does not return any value. It is used when the function needs to perform an action but doesn't need to send any data back to the calling function.
Which of the following is a correct way to declare a function pointer in C++?
- int func()
- int *func()
- int (*func)()
- int &func()
In C++, a function pointer points to the address of a function. The correct syntax for declaring a function pointer is type (*pointer_name) (parameter list). In the given options, int (*func)() is the correct way to declare a function pointer returning an int.