Bird
Raised Fist0
Cprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of array element modification
What is the output of this C program?
C
#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    arr[2] = arr[0] + arr[4];
    printf("%d\n", arr[2]);
    return 0;
}
A3
B5
C6
D9
Attempts:
2 left
💡 Hint
Remember array indexing starts at 0. arr[2] is changed to arr[0] + arr[4].
❓ Predict Output
intermediate
2:00remaining
Sum of elements in an array
What is the output of this C program?
C
#include <stdio.h>

int main() {
    int nums[] = {10, 20, 30, 40};
    int sum = 0;
    for(int i = 0; i < 4; i++) {
        sum += nums[i];
    }
    printf("%d\n", sum);
    return 0;
}
A40
B100
C90
D10
Attempts:
2 left
💡 Hint
Add all elements: 10 + 20 + 30 + 40.
🔧 Debug
advanced
2:00remaining
Identify the error in array access
What error will this C program produce when run?
C
#include <stdio.h>

int main() {
    int arr[3] = {1, 2, 3};
    printf("%d\n", arr[3]);
    return 0;
}
AUndefined behavior (out-of-bounds access)
BCompilation error: array index out of range
CRuntime error: segmentation fault
DPrints 0
Attempts:
2 left
💡 Hint
Array indices go from 0 to size-1. Accessing arr[3] is outside the array.
📝 Syntax
advanced
2:00remaining
Which option causes a compilation error?
Which of the following array declarations will cause a compilation error in C?
Aint arr[3] = {1, 2, 3, 4};
Bint arr[] = {1, 2, 3, 4, 5};
Cint arr[5] = {1, 2, 3, 4, 5};
Dint arr[5];
Attempts:
2 left
💡 Hint
Check if the initializer list fits the declared array size.
🚀 Application
expert
2:00remaining
Find the number of elements in the array
What is the value of 'count' after running this C program?
C
#include <stdio.h>

int main() {
    int data[] = {5, 10, 15, 20, 25, 30};
    int count = sizeof(data) / sizeof(data[0]);
    printf("%d\n", count);
    return 0;
}
A4
B5
C24
D6
Attempts:
2 left
💡 Hint
sizeof(data) gives total bytes, sizeof(data[0]) gives bytes per element.

Practice

(1/5)
1. What is the starting index of a one-dimensional array in C?
easy
A. 0
B. 1
C. -1
D. Depends on the array size

Solution

  1. Step 1: Understand array indexing in C

    In C, arrays always start indexing from 0, meaning the first element is at index 0.
  2. Step 2: Confirm the starting index

    This is a fixed rule in C and does not depend on array size or other factors.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    Array indexing starts at 0 [OK]
Hint: Remember: C arrays always start at zero index [OK]
Common Mistakes:
  • Thinking arrays start at 1 like in some other languages
  • Assuming negative indexes are valid
  • Believing index depends on array size
2. Which of the following is the correct way to declare an integer array of size 5 in C?
easy
A. int arr = 5;
B. int arr();
C. array int arr[5];
D. int arr[5];

Solution

  1. Step 1: Recall C array declaration syntax

    In C, arrays are declared by specifying the type, name, and size in square brackets, like int arr[5];
  2. Step 2: Check each option

    int arr[5]; matches the correct syntax. int arr(); uses parentheses which is for functions. int arr = 5; assigns a single int, not an array. array int arr[5]; uses invalid keyword 'array'.
  3. Final Answer:

    int arr[5]; -> Option D
  4. Quick Check:

    Array declaration uses square brackets [OK]
Hint: Use square brackets for array size in declaration [OK]
Common Mistakes:
  • Using parentheses instead of square brackets
  • Assigning a single value instead of declaring array
  • Using invalid keywords like 'array'
3. What will be the output of the following code?
int arr[3] = {10, 20, 30};
printf("%d", arr[1]);
medium
A. 10
B. 30
C. 20
D. Compilation error

Solution

  1. Step 1: Identify the array elements and indexes

    The array arr has elements: arr[0]=10, arr[1]=20, arr[2]=30.
  2. Step 2: Understand the printf statement

    printf prints the value at arr[1], which is 20.
  3. Final Answer:

    20 -> Option C
  4. Quick Check:

    arr[1] = 20 [OK]
Hint: Remember array index 1 is second element [OK]
Common Mistakes:
  • Confusing index 1 with index 0
  • Expecting output 10 or 30 incorrectly
  • Thinking code causes compilation error
4. Find the error in this code snippet:
int arr[4];
arr[4] = 10;
medium
A. Array index out of bounds
B. Missing semicolon
C. Wrong data type
D. No error

Solution

  1. Step 1: Check array size and indexing

    The array arr has size 4, so valid indexes are 0,1,2,3 only.
  2. Step 2: Identify the invalid index usage

    arr[4] accesses the fifth element, which is outside the array bounds causing undefined behavior.
  3. Final Answer:

    Array index out of bounds -> Option A
  4. Quick Check:

    Index must be less than array size [OK]
Hint: Index must be less than array size [OK]
Common Mistakes:
  • Using index equal to array size
  • Ignoring zero-based indexing
  • Assuming no error for out-of-bounds access
5. Given the array declaration int nums[5] = {1, 2, 3, 4, 5};, which code snippet correctly calculates the sum of all elements?
hard
A. int sum = 0; for(int i = 0; i <= 5; i++) sum += nums[i];
B. int sum = 0; for(int i = 0; i < 5; i++) sum += nums[i];
C. int sum = 0; for(int i = 1; i < 5; i++) sum += nums[i];
D. int sum = 0; for(int i = 0; i < 4; i++) sum += nums[i];

Solution

  1. Step 1: Understand array size and valid indexes

    The array nums has 5 elements with indexes 0 to 4.
  2. Step 2: Analyze each loop condition

    int sum = 0; for(int i = 0; i <= 5; i++) sum += nums[i]; uses i <= 5 which accesses nums[5] (out of bounds). int sum = 0; for(int i = 0; i < 5; i++) sum += nums[i]; uses i < 5 which correctly iterates from 0 to 4. int sum = 0; for(int i = 1; i < 5; i++) sum += nums[i]; starts at 1, missing nums[0]. int sum = 0; for(int i = 0; i < 4; i++) sum += nums[i]; stops at 4, missing nums[4].
  3. Final Answer:

    int sum = 0; for(int i = 0; i < 5; i++) sum += nums[i]; -> Option B
  4. Quick Check:

    Loop from 0 to less than size sums all elements [OK]
Hint: Loop from 0 to less than array size for all elements [OK]
Common Mistakes:
  • Using <= size causing out-of-bounds access
  • Starting loop at 1 missing first element
  • Stopping loop early missing last element
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