Challenge - 5 Problems
Pointer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pointer and variable modification
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { int x = 10; int* p = &x; *p = 20; std::cout << x << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that *p accesses the value pointed to by p.
✗ Incorrect
The pointer p points to x. Changing *p changes x's value to 20. So printing x outputs 20.
❓ Predict Output
intermediate2:00remaining
Pointer reassignment effect
What is the output of this C++ code?
C++
#include <iostream> int main() { int a = 5, b = 10; int* p = &a; p = &b; *p = 20; std::cout << a << " " << b << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Pointer p is changed to point to b before modifying *p.
✗ Incorrect
Initially p points to a, but then p points to b. Changing *p changes b to 20. a remains 5.
❓ Predict Output
advanced2:00remaining
Pointer arithmetic and variable values
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { int arr[3] = {1, 2, 3}; int* p = arr; *(p + 1) = 10; std::cout << arr[1] << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Pointer arithmetic moves the pointer to the next element.
✗ Incorrect
p points to arr[0]. *(p + 1) accesses arr[1], which is set to 10. So arr[1] prints 10.
❓ Predict Output
advanced2:00remaining
Pointer to pointer value change
What is the output of this C++ program?
C++
#include <iostream> int main() { int x = 5; int* p = &x; int** pp = &p; **pp = 15; std::cout << x << std::endl; return 0; }
Attempts:
2 left
💡 Hint
**pp accesses the value pointed to by the pointer p.
✗ Incorrect
pp points to p, which points to x. Changing **pp changes x to 15.
❓ Predict Output
expert2:00remaining
Pointer and variable relationship with const correctness
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { int x = 10; const int* p = &x; // *p = 20; // Uncommenting this line causes error x = 20; std::cout << *p << std::endl; return 0; }
Attempts:
2 left
💡 Hint
const int* means you cannot change the value through the pointer, but the variable itself can change.
✗ Incorrect
p points to x as a const pointer. Changing x directly to 20 updates the value. *p prints 20.