Bird
Raised Fist0
C++programming~20 mins

Common pointer mistakes in C++ - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What happens if you try to dereference a pointer that has not been initialized in C++?
int *p;
int x = *p;
easy
A. It causes undefined behavior and may crash the program.
B. It automatically initializes the pointer to zero.
C. It assigns zero to the variable x.
D. It prints the address stored in p.

Solution

  1. Step 1: Understand pointer initialization

    The pointer p is declared but not initialized, so it points to an unknown memory location.
  2. Step 2: Dereferencing uninitialized pointer

    Dereferencing *p tries to access memory at an undefined address, causing undefined behavior or crash.
  3. Final Answer:

    It causes undefined behavior and may crash the program. -> Option A
  4. Quick Check:

    Uninitialized pointer dereference = crash [OK]
Hint: Never use pointers before initializing them [OK]
Common Mistakes:
  • Assuming uninitialized pointers are null
  • Thinking dereferencing gives zero
  • Ignoring pointer initialization
2. Which of the following is the correct way to initialize a pointer to null in C++?
easy
A. int *p = NULL;
B. int *p = nullptr;
C. int *p = '0';
D. int *p = 0x0;

Solution

  1. Step 1: Understand null pointer initialization

    In modern C++, nullptr is the preferred way to initialize a pointer to null.
  2. Step 2: Check other options

    NULL is old style (macro), 0x0 is a literal but less clear, and '0' is a char, not a pointer.
  3. Final Answer:

    int *p = nullptr; -> Option B
  4. Quick Check:

    Use nullptr for null pointers [OK]
Hint: Use nullptr for null pointers in modern C++ [OK]
Common Mistakes:
  • Using character '0' instead of nullptr
  • Using integer 0 instead of nullptr
  • Confusing NULL macro with nullptr
3. What is the output of the following code?
int x = 10;
int *p = &x;
*p = 20;
std::cout << x << std::endl;
medium
A. 10
B. Address of x
C. Compilation error
D. 20

Solution

  1. Step 1: Understand pointer assignment

    Pointer p stores the address of x. So *p refers to x.
  2. Step 2: Modify value through pointer

    Assigning *p = 20; changes the value of x to 20.
  3. Final Answer:

    20 -> Option D
  4. Quick Check:

    Pointer dereference changes original variable [OK]
Hint: Changing *p changes the original variable [OK]
Common Mistakes:
  • Confusing pointer value with pointed value
  • Expecting original variable unchanged
  • Thinking *p prints address
4. Identify the error in the following code snippet:
int *p = nullptr;
std::cout << *p << std::endl;
medium
A. Missing semicolon after nullptr.
B. Pointer p is not declared.
C. Dereferencing a null pointer causes runtime error.
D. std::cout cannot print pointer values.

Solution

  1. Step 1: Analyze pointer initialization

    Pointer p is initialized to nullptr, meaning it points to nothing.
  2. Step 2: Dereferencing null pointer

    Trying to access *p causes a runtime error (crash) because it points to no valid memory.
  3. Final Answer:

    Dereferencing a null pointer causes runtime error. -> Option C
  4. Quick Check:

    Dereference nullptr = crash [OK]
Hint: Never dereference nullptr or uninitialized pointers [OK]
Common Mistakes:
  • Assuming nullptr can be dereferenced safely
  • Ignoring runtime crash possibility
  • Confusing pointer declaration with usage
5. Given the code below, what is the safest way to avoid a dangling pointer?
int *p = new int(5);
delete p;
// What should be done next?
hard
A. Set p to nullptr after delete.
B. Assign p to a new integer immediately.
C. Do nothing; pointer is automatically safe.
D. Use delete[] p; instead of delete p;.

Solution

  1. Step 1: Understand delete effect

    After delete p;, the pointer p still holds the old address but memory is freed (dangling pointer).
  2. Step 2: Prevent dangling pointer

    Setting p = nullptr; makes it clear pointer is invalid and safe to check before use.
  3. Final Answer:

    Set p to nullptr after delete. -> Option A
  4. Quick Check:

    Nullify pointer after delete to avoid dangling [OK]
Hint: Nullify pointers after delete to avoid dangling [OK]
Common Mistakes:
  • Using delete[] for single object
  • Not setting pointer to nullptr after delete
  • Assuming pointer auto-clears after delete
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard