0
0
C++programming~20 mins

Address and dereference operators 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
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;
}
AAddress of x
B10
CCompilation error
D0
Attempts:
2 left
💡 Hint
Remember that *p gives the value stored at the address p points to.
Predict Output
intermediate
2: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;
}
A20
BAddress of a
C5
DCompilation error
Attempts:
2 left
💡 Hint
Changing *ptr changes the value of a.
Predict Output
advanced
2: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;
}
ACompilation error
B2
C4
D3
Attempts:
2 left
💡 Hint
Pointer arithmetic moves the pointer by elements, not bytes.
Predict Output
advanced
2: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;
}
ACompilation error
BAddress of p1
C7
DAddress of val
Attempts:
2 left
💡 Hint
Double pointer points to a pointer, dereferencing twice gets the value.
Predict Output
expert
3: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;
}
A10
B3
CCompilation error
DUndefined behavior
Attempts:
2 left
💡 Hint
References are aliases; pointer to reference is pointer to original variable.