Array Questions and Answers
Question 1:
Explain what an array is and why it is needed in programming.
Answer:
An array is a collection of elements of the same data type stored in contiguous memory locations
and referenced using a common name. It is needed in programming to efficiently manage large sets
of related data. Without arrays, programmers would have to declare multiple individual variables,
making the code lengthy and difficult to manage. For example, storing marks for 1000 students
using individual variables is impractical, whereas using an array like int studMark[1000]; simplifies
declaration and manipulation.
Question 2:
Describe how to declare a one-dimensional array in C and provide an example.
Answer:
A one-dimensional array in C is declared using the syntax:
data_type array_name[array_size];
Example:
int numbers[10]; // Declares an integer array that can store 10 elements.
Question 3:
How can we initialize an array at the time of declaration? Provide an example.
Answer:
Arrays in C can be initialized during declaration using curly braces {}. The syntax is:
data_type array_name[size] = {values};
Example:
int numbers[] = {10, 20, 30, 40, 50};
This creates an integer array with 5 elements. The compiler assigns size 5 automatically.
Question 4:
Explain the difference between a one-dimensional and a two-dimensional array with examples.
Answer:
A one-dimensional array is a linear collection of elements accessed using a single index.
Example:
int marks[5] = {10, 20, 30, 40, 50};
printf("%d", marks[2]); // Outputs 30
A two-dimensional array is a table-like structure with rows and columns, requiring two indices for
access.
Example:
int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
printf("%d", matrix[1][2]); // Outputs 6
Question 5:
Write a C program to find the sum of all elements in an array of size 10.
Answer:
#include <stdio.h>
int main() {
int arr[10], sum = 0, i;
printf("Enter 10 integers:\n");
for(i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum of all elements: %d\n", sum);
return 0;