What is the output of the following C++ code?
#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; }
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.
const int* ptr1 means you cannot change the value through ptr1, but you can change where ptr1 points. So ptr1 = &y; is valid.
int* const ptr2 means the pointer itself cannot change, but the value it points to can be changed. So *ptr2 = 15; is valid, but ptr2 = &x; is not.
Therefore, *ptr1 points to y which is 10, and *ptr2 is changed to 15.
What will be the output of this C++ program?
#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; }
The volatile keyword tells the compiler not to optimize access to the variable, but it does not prevent normal operations.
The volatile modifier means the variable can be changed unexpectedly (e.g., by hardware), so the compiler must always read it fresh. However, normal arithmetic and assignment work as usual. So both a and b become 15.
What is the output of this C++ code snippet?
#include <iostream> int main() { unsigned int u = 5; int s = -10; auto result = u + s; std::cout << result << std::endl; return 0; }
When mixing signed and unsigned integers, the signed integer is converted to unsigned before the operation.
The signed integer -10 is converted to unsigned (assuming 32-bit unsigned int) which becomes 4294967286. Adding 5 results in 4294967291.
Note: The exact output depends on the system's unsigned int size, but typically 32-bit.
What will be the output of this C++ program?
#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; }
The mutable keyword allows modification of a member even in a const object.
The member count is declared mutable, so it can be changed inside a const method and even when the object is const. The increment() method is const but modifies count. So after calling increment(), count becomes 1.
Which of the following code snippets will cause a compilation error because of incorrect use of type modifiers?
Think about what const int* means and whether you can modify the value it points to.
Option A tries to modify the value pointed to by a const int*, which is not allowed and causes a compilation error.
Option A is valid; volatile does not prevent assignment.
Option A is valid; a constant pointer (int* const) can be initialized at declaration.
Option A uses a cast to remove constness and modifies a const variable, which is undefined behavior but compiles.