How Well Do You Know C++ UB?


adaptablebastio

Created 6/20/2024

1

90%

Q & A


Share This Quiz


Put your knowledge of C++ Undefined Behavior to the test with this quiz!

Put your knowledge of C++ Undefined Behavior to the test with this quiz!

1. What does UB stand for in C++?

Undefined Behavior
Uniform Binary
Unlimited Bounds
Universal Base

2. Which of the following is a common cause of undefined behavior in C++?

Using a smart pointer
Casting an int to a float
Dereferencing a null pointer
Using a for loop

3. What can be a result of invoking undefined behavior in a C++ program?

The program runs slower
The program could produce unpredictable results
The code will always give a compile-time error
The program uses more memory

4. Which of the following operations on an integer can lead to undefined behavior?

Addition
Subtraction
Division by zero
Multiplication

5. Is it safe to modify a string literal in C++?

Yes, it is safe
Yes, but only if the literal is not used again
No, it results in undefined behavior
No, it is safe but generally not recommended

6. What happens when you access an array out of its bounds in C++?

Nothing happens
It results in undefined behavior
The program catches an exception
You get a warning at compile time

7. Which of the following is NOT a cause of undefined behavior?

Reading uninitialized variables
Shifting bits by a negative amount
Correctly using unique_ptr
Accessing a dangling pointer

8. Why is it important to avoid undefined behavior in C++ programs?

It can make the code less readable
It can lead to non-portable code
It can introduce security vulnerabilities
It uses more CPU resources

9. Which of these can lead to undefined behavior when using pointers?

Initializing a pointer with nullptr
Dereferencing a null pointer
Assigning one pointer to another
Using a const pointer

10. What is the recommendation to prevent undefined behavior related to type conversions in C++?

Avoid using typecasting
Carefully use static_cast, dynamic_cast, etc.
Use only implicit type conversions
Ignore compiler warnings