0
0
C++programming~20 mins

Common pointer mistakes 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 arithmetic
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int arr[3] = {10, 20, 30};
    int* p = arr;
    std::cout << *(p + 1) << std::endl;
    return 0;
}
ACompilation error
B10
C30
D20
Attempts:
2 left
💡 Hint
Pointer arithmetic moves the pointer by the size of the pointed type.
Predict Output
intermediate
2:00remaining
Dereferencing a null pointer
What happens when this C++ code runs?
C++
#include <iostream>
int main() {
    int* p = nullptr;
    std::cout << *p << std::endl;
    return 0;
}
ARuntime error (segmentation fault)
BPrints 0
CCompilation error
DPrints garbage value
Attempts:
2 left
💡 Hint
Dereferencing a null pointer is unsafe.
Predict Output
advanced
2:00remaining
Dangling pointer after delete
What is the output of this code?
C++
#include <iostream>
int main() {
    int* p = new int(42);
    delete p;
    std::cout << *p << std::endl;
    return 0;
}
AUndefined behavior (likely garbage or crash)
BCompilation error
C42
D0
Attempts:
2 left
💡 Hint
Accessing memory after delete is unsafe.
Predict Output
advanced
2:00remaining
Pointer type mismatch
What error or output does this code produce?
C++
#include <iostream>
int main() {
    int x = 5;
    double* p = (double*)&x;
    std::cout << *p << std::endl;
    return 0;
}
APrints 5.0
BUndefined behavior (prints garbage or crashes)
CCompilation error
DPrints 0
Attempts:
2 left
💡 Hint
Casting pointer types incorrectly can cause problems.
🧠 Conceptual
expert
2:00remaining
Common cause of memory leak with pointers
Which option best describes a common cause of memory leaks when using pointers in C++?
ACasting pointers to void*
BUsing nullptr instead of a valid pointer
CForgetting to delete memory allocated with new
DDereferencing a null pointer
Attempts:
2 left
💡 Hint
Memory leaks happen when allocated memory is not freed.