A one-dimensional array in C is a collection of elements of the same type stored in contiguous memory locations. It is like a list where each element can be accessed by its index.
Click to reveal answer
beginner
How do you declare a one-dimensional array of 5 integers in C?
You declare it like this: int arr[5]; This creates an array named arr that can hold 5 integers.
Click to reveal answer
beginner
How do you access the third element of an array named arr?
You access it using the index 2: arr[2]. Remember, array indexes start at 0, so the first element is arr[0].
Click to reveal answer
intermediate
What happens if you try to access an array element outside its declared size?
Accessing outside the array size causes undefined behavior. It may read or write memory it shouldn't, leading to bugs or crashes.
Click to reveal answer
beginner
How can you initialize a one-dimensional array with values at the time of declaration?
You can write: int arr[3] = {10, 20, 30}; This sets the first element to 10, second to 20, and third to 30.
Click to reveal answer
What is the index of the first element in a C array?
A0
B1
C-1
DDepends on the array size
✗ Incorrect
In C, array indexing always starts at 0.
Which of the following declares an array of 10 floats?
Aint arr[10];
Bfloat arr[10];
Cfloat arr;
Dfloat arr(10);
✗ Incorrect
The correct syntax for declaring an array of 10 floats is float arr[10];.
What will happen if you write to arr[5] when arr is declared as int arr[5];?
AIt causes undefined behavior
BIt writes to the last element safely
CIt throws a compile-time error
DIt automatically resizes the array
✗ Incorrect
Accessing arr[5] is outside the bounds since valid indexes are 0 to 4, causing undefined behavior.
How do you initialize an array with all zeros in C?
Aint arr[5] = NULL;
Bint arr[5] = 0;
Cint arr[5]; // automatically zeroed
Dint arr[5] = {0};
✗ Incorrect
Initializing with {0} sets the first element to zero and all others to zero by default.
Which statement correctly accesses the last element of int arr[7];?
Aarr[-1]
Barr[7]
Carr[6]
Darr[0]
✗ Incorrect
The last element index is size minus one, so for 7 elements, it is arr[6].
Explain what a one-dimensional array is and how you use it in C.
Think of it like a row of boxes where each box holds a value.
You got /4 concepts.
Describe how to declare, initialize, and access elements in a one-dimensional array in C.
Remember the array size and zero-based indexing.
You got /4 concepts.
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
Step 1: Understand array indexing in C
In C, arrays always start indexing from 0, meaning the first element is at index 0.
Step 2: Confirm the starting index
This is a fixed rule in C and does not depend on array size or other factors.
Final Answer:
0 -> Option A
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
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];
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'.
Final Answer:
int arr[5]; -> Option D
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
Step 1: Identify the array elements and indexes
The array arr has elements: arr[0]=10, arr[1]=20, arr[2]=30.
Step 2: Understand the printf statement
printf prints the value at arr[1], which is 20.
Final Answer:
20 -> Option C
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
Step 1: Check array size and indexing
The array arr has size 4, so valid indexes are 0,1,2,3 only.
Step 2: Identify the invalid index usage
arr[4] accesses the fifth element, which is outside the array bounds causing undefined behavior.
Final Answer:
Array index out of bounds -> Option A
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
Step 1: Understand array size and valid indexes
The array nums has 5 elements with indexes 0 to 4.
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].
Final Answer:
int sum = 0;
for(int i = 0; i < 5; i++) sum += nums[i]; -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.