Challenge - 5 Problems
Pointer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this pointer declaration code?
Consider the following C++ code snippet. What will be printed when it runs?
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 means the value pointed to by p.
✗ Incorrect
The pointer p holds the address of x. Using *p accesses the value stored at that address, which is 10.
❓ Predict Output
intermediate2:00remaining
What is the value of variable 'p' after this declaration?
Look at this code snippet. What does the pointer 'p' hold immediately after declaration?
C++
int* p;Attempts:
2 left
💡 Hint
Uninitialized pointers do not automatically point to nullptr.
✗ Incorrect
Declaring a pointer without initializing it means it contains whatever random data was in memory at that location, called a garbage value.
❓ Predict Output
advanced2:00remaining
What error does this pointer declaration cause?
What error will this code produce?
C++
int* p = 5;
Attempts:
2 left
💡 Hint
You cannot assign an integer directly to a pointer without casting.
✗ Incorrect
Assigning an integer literal to a pointer without a cast is invalid in C++ and causes a compilation error.
❓ Predict Output
advanced2:00remaining
What is the output of this pointer declaration and dereference?
What will this program print?
C++
#include <iostream> int main() { int x = 7; int* p = &x; *p = 20; std::cout << x << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Changing *p changes the value of x because p points to x.
✗ Incorrect
The pointer p points to x. Changing *p changes x's value to 20, so printing x outputs 20.
🧠 Conceptual
expert3:00remaining
Which pointer declaration is correct for a pointer to a constant integer?
You want to declare a pointer that points to an integer value that cannot be changed through the pointer. Which declaration is correct?
Attempts:
2 left
💡 Hint
The const keyword before int means the value pointed to is constant.
✗ Incorrect
const int* p means p points to a constant int value (value cannot be changed via p). int* const p means p itself is constant (cannot point elsewhere), but value can change. const int* const p means both pointer and value are constant. int const p* is invalid syntax.