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.
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.
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.
Imagine you are debugging a C++ application where a certain condition seems to always evaluate to true, even when you expect it not to. What could be a common mistake related to relational operators?
- Confusing = with ==
- Using !== as an inequality operator
- Always using > instead of >=
- Relying only on the < operator
In C++, the single equals (=) is an assignment operator, meaning it assigns a value to a variable. The double equals (==) is a comparison operator, which checks for equality. A common mistake is to use a single equals sign instead of a double equals sign when trying to compare values, leading to unexpected behavior.
Which keyword is used to define a function in C++?
- class
- struct
- define
- void
In C++, the keyword to define a function isn't necessarily a specific reserved word like "class" or "struct". Instead, functions begin with their return type, such as "void" for functions that don't return any value. But it's worth noting that "void" is just one possible return type, and not strictly a keyword exclusively for function definition.
What is the basic idea behind recursion in programming?
- Breaking down a problem into smaller steps.
- Running a function multiple times.
- A function calling itself.
- Creating a loop for iteration.
Recursion in programming refers to a technique where a function calls itself to solve a problem. The main idea is to break the problem down into smaller instances of the same problem. This approach allows for solutions to be built incrementally, mirroring mathematical recursion.
Imagine a scenario where you have to maintain a collection of elements while keeping them sorted after insertions and deletions. Which STL container would be the most efficient choice for this task?
- vector
- list
- set
- queue
The STL set container maintains its elements in sorted order by keys. It automatically reorders itself after insertions and deletions, making it the most efficient choice for maintaining a sorted collection without requiring manual reordering.
What is the role of the return statement in a lambda function?
- To indicate the end of the lambda function.
- To return a value from the lambda to the calling function.
- It determines the type of the lambda.
- It can't be used in lambda functions.
Lambda functions, like regular functions, can return values. If the lambda's body contains more than one statement, a return statement can be used to specify which value is returned. If the lambda's body consists of a single return statement, the return type is automatically inferred by the compiler.
What is the primary reason for using smart pointers over raw pointers in modern C++?
- To increase execution speed
- To use less memory
- To look modern
- To manage memory automatically
Smart pointers, such as unique_ptr, shared_ptr, and weak_ptr, were introduced in C++ to provide automated memory management. They automatically handle the destruction of objects they point to, thereby preventing common issues like memory leaks and dangling pointers.
You are working on a large-scale simulation software where numerous animal species are modeled. Which type of inheritance might be most suitable to model individual animal species without encountering the diamond problem?
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
Single inheritance ensures that a class inherits from only one other class, thus avoiding the diamond problem. The diamond problem arises in languages that support multiple inheritance and can result in ambiguity in the inheritance hierarchy. Single inheritance ensures a clear, linear progression without any ambiguities.
The keyword _______ is used to grant a function or class access to the private and protected members of another class in C++.
- using
- friend
- grant
- access
The keyword "friend" in C++ is used to specify that a particular external function or another class can access the private and protected members of the class in which it is declared. This is fundamental to the concept of friend functions and friend classes.
In C++ STL, the function _______ is used to remove consecutive duplicate elements in a range.
- remove
- unique
- delete
- erase
The unique algorithm in C++ STL is used to eliminate consecutive duplicate elements from a range. The range after the unique elements is left with unspecified values.