0
0
C++programming~20 mins

Pointer declaration in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pointer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
AAddress of x
B0
CCompilation error
D10
Attempts:
2 left
💡 Hint
Remember that *p means the value pointed to by p.
Predict Output
intermediate
2: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;
AIt holds the address of some variable
BIt holds nullptr
CIt holds a garbage (undefined) value
DIt holds 0
Attempts:
2 left
💡 Hint
Uninitialized pointers do not automatically point to nullptr.
Predict Output
advanced
2:00remaining
What error does this pointer declaration cause?
What error will this code produce?
C++
int* p = 5;
ACompilation error: cannot convert int to int*
BRuntime error: segmentation fault
CWarning but compiles
DNo error, p points to address 5
Attempts:
2 left
💡 Hint
You cannot assign an integer directly to a pointer without casting.
Predict Output
advanced
2: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;
}
A20
BGarbage value
CCompilation error
D7
Attempts:
2 left
💡 Hint
Changing *p changes the value of x because p points to x.
🧠 Conceptual
expert
3: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?
Aint* const p;
Bconst int* p;
Cconst int* const p;
Dint const p*;
Attempts:
2 left
💡 Hint
The const keyword before int means the value pointed to is constant.