0
0
C++programming~20 mins

Type modifiers in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type Modifiers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of const pointer vs pointer to const

What is the output of the following C++ code?

C++
#include <iostream>

int main() {
    int x = 5;
    int y = 10;
    const int* ptr1 = &x;
    int* const ptr2 = &y;

    // *ptr1 = 6; // Line A (commented out)
    ptr1 = &y;    // Line B

    *ptr2 = 15;   // Line C
    // ptr2 = &x;  // Line D (commented out)

    std::cout << "*ptr1 = " << *ptr1 << ", *ptr2 = " << *ptr2 << std::endl;
    return 0;
}
A*ptr1 = 5, *ptr2 = 15
B*ptr1 = 5, *ptr2 = 10
C*ptr1 = 10, *ptr2 = 10
D*ptr1 = 10, *ptr2 = 15
Attempts:
2 left
💡 Hint

Remember that const int* ptr means the value pointed to is constant, but the pointer itself can change. int* const ptr means the pointer is constant, but the value pointed to can change.

Predict Output
intermediate
1:30remaining
Effect of volatile modifier on variable

What will be the output of this C++ program?

C++
#include <iostream>

int main() {
    volatile int a = 10;
    int b = 10;

    a = a + 5;
    b = b + 5;

    std::cout << "a = " << a << ", b = " << b << std::endl;
    return 0;
}
Aa = 10, b = 15
Ba = 15, b = 15
Ca = 15, b = 10
DCompilation error due to volatile
Attempts:
2 left
💡 Hint

The volatile keyword tells the compiler not to optimize access to the variable, but it does not prevent normal operations.

Predict Output
advanced
1:30remaining
Output of signed and unsigned arithmetic

What is the output of this C++ code snippet?

C++
#include <iostream>

int main() {
    unsigned int u = 5;
    int s = -10;
    auto result = u + s;
    std::cout << result << std::endl;
    return 0;
}
ACompilation error due to mixed types
B-5
C4294967291
D5
Attempts:
2 left
💡 Hint

When mixing signed and unsigned integers, the signed integer is converted to unsigned before the operation.

Predict Output
advanced
1:30remaining
Effect of mutable keyword in const object

What will be the output of this C++ program?

C++
#include <iostream>

class Counter {
public:
    mutable int count = 0;
    void increment() const {
        count++;
    }
};

int main() {
    const Counter c;
    c.increment();
    std::cout << c.count << std::endl;
    return 0;
}
A1
B0
CCompilation error due to modifying const object
DUndefined behavior
Attempts:
2 left
💡 Hint

The mutable keyword allows modification of a member even in a const object.

🧠 Conceptual
expert
2:00remaining
Which option causes a compilation error due to type modifiers?

Which of the following code snippets will cause a compilation error because of incorrect use of type modifiers?

Aconst int* p = nullptr; *p = 10;
Bvolatile int v = 10; v = 20;
Cint* const p = new int(5);
Dconst int x = 5; int* p = (int*)&x; *p = 10;
Attempts:
2 left
💡 Hint

Think about what const int* means and whether you can modify the value it points to.