0
0
C++programming~20 mins

One-dimensional arrays in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of array sum calculation
What is the output of this C++ code that sums elements of an array?
C++
#include <iostream>
int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int sum = 0;
    for(int i = 0; i < 5; i++) {
        sum += arr[i];
    }
    std::cout << sum << std::endl;
    return 0;
}
A14
BError: array index out of bounds
C15
D10
Attempts:
2 left
💡 Hint
Add all numbers from 1 to 5.
Predict Output
intermediate
2:00remaining
Value of array element after modification
What is the value of arr[2] after running this code?
C++
#include <iostream>
int main() {
    int arr[4] = {10, 20, 30, 40};
    arr[2] = arr[1] + arr[3];
    std::cout << arr[2] << std::endl;
    return 0;
}
A60
B50
C30
DCompilation error
Attempts:
2 left
💡 Hint
arr[2] is set to arr[1] + arr[3].
🔧 Debug
advanced
2:00remaining
Identify the error in array initialization
What error does this code produce?
C++
#include <iostream>
int main() {
    int arr[3] = {1, 2, 3, 4};
    std::cout << arr[0] << std::endl;
    return 0;
}
ARuntime error: array index out of bounds
BNo error, outputs 4
COutput: 1
DCompilation error: too many initializers for 'int [3]'
Attempts:
2 left
💡 Hint
Array size is 3 but 4 values are given.
📝 Syntax
advanced
2:00remaining
Correct syntax to declare and initialize an array
Which option correctly declares and initializes an array of 4 integers with values 5, 10, 15, 20?
Aint arr[4] = {5, 10, 15, 20};
Bint arr[4] = (5, 10, 15, 20);
Cint arr[] = {5, 10, 15, 20};
Dint arr = {5, 10, 15, 20};
Attempts:
2 left
💡 Hint
Use curly braces and specify size correctly.
🚀 Application
expert
2:00remaining
Find the number of elements greater than 10
What is the output of this program that counts elements greater than 10 in an array?
C++
#include <iostream>
int main() {
    int arr[6] = {5, 12, 7, 18, 10, 20};
    int count = 0;
    for(int i = 0; i < 6; i++) {
        if(arr[i] > 10) {
            count++;
        }
    }
    std::cout << count << std::endl;
    return 0;
}
A2
B3
C5
D4
Attempts:
2 left
💡 Hint
Count how many numbers are strictly greater than 10.