What is the primary purpose of a function in C++ programming?
- To print outputs
- To define variables
- To store data
- To perform a specific task
Functions in C++ allow code to be modularized, providing a way to encapsulate and reuse code. Their primary role is to perform a specific task or computation, ensuring the code is organized, clear, and maintainable.
When a float is converted to an int, the value is _______.
- truncated
- rounded
- incremented
- exponentiated
When a floating-point number is converted to an integer in C++, any fractional part is discarded (i.e., truncated). This does not involve rounding, just a simple removal of the decimal and beyond.
In professional C++ coding, it is often recommended to avoid using break and continue in favor of _______.
- structured logic
- recursive functions
- exception handling
- inline functions
While break and continue have their uses, over-reliance on them can make code harder to read and debug. Instead, it's often recommended to use clear and structured logic that doesn't rely on abrupt jumps within loops.
The friendship granted by a class A to a function or class B is _______ reciprocal, meaning [choose the correct statement].
- always
- occasionally
- one-way
- not
The friendship in C++ is one-way. If class A declares class B or a function as its friend, it doesn't mean that class A automatically becomes a friend of class B or that function. Friendship must be explicitly granted. This means the granting of friendship is not reciprocal by default. Class B or the function would need to separately declare class A as a friend for the reverse to be true.
What is the impact of a function having a default argument?
- It allows function overloading.
- It allows multiple return types for the function.
- It can be called with fewer arguments than defined.
- It enhances runtime performance.
When a function has a default argument, it can be called with fewer arguments than it defines. The compiler will use the default value for any arguments not provided by the caller. This feature aids in providing flexibility while calling functions but doesn't directly enhance runtime performance.
Imagine you are developing an e-commerce system where different types of users (Admin, Buyer, and Seller) exist. Which inheritance model might be beneficial to maintain, expand, and utilize polymorphic behavior?
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Hierarchical inheritance is where a single class serves as a base class (superclass) for more than one derived class (subclass). In this scenario, the common attributes and methods of users can be part of the base class, while unique functionalities for Admin, Buyer, and Seller can be defined in their respective derived classes.
In what year was the C++17 standard released?
- 2011
- 2014
- 2017
- 2019
The C++17 standard, commonly referred to as C++17, was released in 2017. It brought several new features and improvements over the previous standards, streamlining the language and enhancing its capabilities for modern software development.
If you want to force floating-point division in C++ when dividing two integers, one of the numbers should be _______.
- casted as float
- multiplied by zero
- negated
- subtracted by one
In C++, to force floating-point division when dividing two integers, you can cast one or both of the integers to a floating-point type (like float or double). By doing this, the division will result in a floating-point value, preserving any fractional part. For instance, (float)5 / 2 would result in 2.5 instead of 2.
The memory allocated for a struct is equal to the sum of the memory of its individual members, considering _______.
- padding
- initialization
- inheritance
- encapsulation
Memory alignment requirements can cause "padding" between members of a struct, which can increase the total memory size of the struct. This is essential for data to be accessed in an optimized manner on the hardware.
What is the purpose of the throw keyword in exception handling in C++?
- to propagate
- to hide
- to prevent
- to execute
In C++ exception handling, the throw keyword is used to signal the occurrence of an exception. When an error condition arises, using the throw keyword can help propagate the exception up the call stack until it's caught by an appropriate catch block.