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 dereferencing
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { int x = 10; int* p = &x; std::cout << *p << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that *p gives the value stored at the address p points to.
✗ Incorrect
The pointer p stores the address of x. Using *p accesses the value at that address, which is 10.
❓ Predict Output
intermediate2:00remaining
Value after pointer modification
What is the output of this C++ code?
C++
#include <iostream> int main() { int a = 5; int* ptr = &a; *ptr = 20; std::cout << a << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Changing *ptr changes the value of a.
✗ Incorrect
The pointer ptr points to a. Assigning *ptr = 20 changes a's value to 20.
❓ Predict Output
advanced2:00remaining
Pointer arithmetic and dereferencing
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { int arr[] = {1, 2, 3, 4}; int* p = arr; std::cout << *(p + 2) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Pointer arithmetic moves the pointer by elements, not bytes.
✗ Incorrect
p points to arr[0]. Adding 2 moves to arr[2], which is 3.
❓ Predict Output
advanced2:00remaining
Output of double pointer dereferencing
What does this C++ code print?
C++
#include <iostream> int main() { int val = 7; int* p1 = &val; int** p2 = &p1; std::cout << **p2 << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Double pointer points to a pointer, dereferencing twice gets the value.
✗ Incorrect
p2 points to p1, which points to val. **p2 accesses val's value, 7.
❓ Predict Output
expert3:00remaining
Pointer and reference combined output
What is the output of this C++ code?
C++
#include <iostream> int main() { int x = 3; int& ref = x; int* ptr = &ref; *ptr = 10; std::cout << x << std::endl; return 0; }
Attempts:
2 left
💡 Hint
References are aliases; pointer to reference is pointer to original variable.
✗ Incorrect
ref is an alias for x. &ref is the address of x. Changing *ptr changes x to 10.