Skip to content

Commit be11b4f

Browse files
Added quiz questions about modern C++ features (#7216)
1 parent fdc57bf commit be11b4f

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

c++/c++-quiz.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3469,3 +3469,63 @@ int main() {
34693469
After sorting, the vector becomes {1, 2, 5, 8, 9}. v[2] is the third element, which is 5.
34703470

34713471
[Reference](https://en.cppreference.com/w/cpp/algorithm/sort)
3472+
3473+
#### Q221. In modern C++ (C++17 and above), what does the `std::optional<T>` class template help achieve in function design?
3474+
3475+
- [x] It provides a type-safe way to represent a value that may or may not be present, avoiding the need for sentinel values or pointers
3476+
- [ ] It allows multiple values of the same type to be returned simultaneously
3477+
- [ ] It replaces the need for exception handling in all cases
3478+
- [ ] It is used to store polymorphic objects dynamically
3479+
3480+
**Explanation:**
3481+
`std::optional<T>` represents an optional value — it either contains a value of type `T` or nothing (`std::nullopt`). It’s safer than returning `nullptr` or using flags for missing values.
3482+
3483+
3484+
3485+
#### Q222. What happens if a class has a virtual destructor but the base class pointer deletes an object of a derived class that has already been partially destructed?
3486+
3487+
- [x] Undefined behavior — deleting an already destructed object leads to double destruction and memory corruption
3488+
- [ ] The base class destructor will be called twice safely
3489+
- [ ] The compiler automatically prevents deletion of derived objects
3490+
- [ ] The derived destructor will execute twice but without side effects
3491+
3492+
**Explanation:**
3493+
Once an object is destructed, any subsequent deletion of the same memory through a base pointer leads to undefined behavior — even if destructors are virtual.
3494+
3495+
3496+
3497+
#### Q223. In C++20, which of the following best describes the role of **concepts**?
3498+
3499+
- [x] They constrain template parameters to ensure that only types meeting specific requirements can be used
3500+
- [ ] They define runtime type checks for polymorphic objects
3501+
- [ ] They replace templates with runtime dispatching
3502+
- [ ] They allow implicit conversions between template types
3503+
3504+
**Explanation:**
3505+
Concepts provide **compile-time constraints** for templates, improving error messages and enforcing type correctness. For example, `template <Sortable T>` ensures the type meets the `Sortable` concept’s requirements.
3506+
3507+
3508+
3509+
#### Q224. What is the purpose of the **placement new** operator in C++?
3510+
3511+
- [x] It constructs an object at a specific pre-allocated memory address
3512+
- [ ] It allocates memory on the heap and initializes the object
3513+
- [ ] It creates multiple objects in a single memory block automatically
3514+
- [ ] It performs garbage collection for dynamic memory
3515+
3516+
**Explanation:**
3517+
Placement new allows you to explicitly specify the address where an object should be constructed, e.g. `new (buffer) MyClass();`. It’s used in performance-critical systems and custom memory allocators.
3518+
3519+
3520+
3521+
#### Q225. Suppose two threads access a shared variable without synchronization — one writes while the other reads. In C++, what is this condition called, and what does the standard say about it?
3522+
3523+
- [ ] It’s a data access warning but still well-defined behavior
3524+
- [x] It’s a data race, leading to undefined behavior under the C++ memory model
3525+
- [ ] It’s automatically handled by the compiler’s memory barrier insertion
3526+
- [ ] It’s only a problem if both threads write to the same variable
3527+
3528+
**Explanation:**
3529+
C++ defines **data races** as undefined behavior. Concurrent unsynchronized reads and writes on the same memory violate the memory model’s atomicity rules.
3530+
3531+

0 commit comments

Comments
 (0)