0
0
C++programming~20 mins

Pointers and arrays in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pointer and Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pointer arithmetic with arrays
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int arr[] = {10, 20, 30, 40};
    int *p = arr;
    std::cout << *(p + 2) << std::endl;
    return 0;
}
A20
B40
C10
D30
Attempts:
2 left
💡 Hint
Remember that pointer arithmetic moves by the size of the data type.
Predict Output
intermediate
2:00remaining
Value of pointer after increment
What will be the value printed by this program?
C++
#include <iostream>
int main() {
    int arr[3] = {5, 10, 15};
    int *ptr = arr + 1;
    std::cout << *ptr << std::endl;
    ptr++;
    std::cout << *ptr << std::endl;
    return 0;
}
A10\n15
B5\n10
C15\n10
D10\n10
Attempts:
2 left
💡 Hint
Pointer starts at second element, then moves to third.
🔧 Debug
advanced
2:00remaining
Identify the error in pointer usage
What error will this code produce when compiled or run?
C++
#include <iostream>
int main() {
    int *p;
    *p = 10;
    std::cout << *p << std::endl;
    return 0;
}
ACompilation error: uninitialized pointer
BRuntime error: segmentation fault
CCompilation error: missing semicolon
DOutputs 10
Attempts:
2 left
💡 Hint
Think about what happens when you dereference a pointer that has not been assigned a valid address.
🧠 Conceptual
advanced
2:00remaining
Size of array vs pointer
Given the code below, what will be the output?
C++
#include <iostream>
int main() {
    int arr[5] = {1,2,3,4,5};
    int *p = arr;
    std::cout << sizeof(arr) << " " << sizeof(p) << std::endl;
    return 0;
}
A20 8
B20 20
C5 8
D5 5
Attempts:
2 left
💡 Hint
sizeof(arr) gives total bytes of all elements, sizeof(p) gives size of pointer variable.
📝 Syntax
expert
2:00remaining
Correct pointer declaration and initialization
Which option correctly declares and initializes a pointer to the first element of an integer array named 'data'?
Aint ptr = &data[0];
Bint ptr* = data;
Cint *ptr = &data[0];
Dint *ptr = data[0];
Attempts:
2 left
💡 Hint
Pointer must be declared with * and initialized with address of an element.