0% found this document useful (0 votes)
5 views2 pages

Array Questions and Answers

An array is a collection of elements of the same data type stored in contiguous memory locations, with types including one-dimensional, two-dimensional, and multidimensional arrays. Arrays have a fixed size, can be initialized, and allow for random access, but have limitations such as being unable to resize dynamically and only supporting a single data type. Key concepts include passing arrays to functions, differences between arrays and pointers, and the existence of jagged arrays, which are arrays of arrays with varying sizes.

Uploaded by

mdashikur24125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Array Questions and Answers

An array is a collection of elements of the same data type stored in contiguous memory locations, with types including one-dimensional, two-dimensional, and multidimensional arrays. Arrays have a fixed size, can be initialized, and allow for random access, but have limitations such as being unable to resize dynamically and only supporting a single data type. Key concepts include passing arrays to functions, differences between arrays and pointers, and the existence of jagged arrays, which are arrays of arrays with varying sizes.

Uploaded by

mdashikur24125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1. What is an array?

An array is a collection of elements of the same data type stored in contiguous


memory locations. Arrays allow you to store multiple values in a single variable.

2. What are the types of arrays in C?


- One-dimensional array: Stores data in a linear sequence (e.g., int arr[5]).
- Two-dimensional array: Stores data in rows and columns, like a matrix (e.g., int
arr[3][4]).
- Multidimensional array: Extends to more than two dimensions (e.g., int arr[3][4]
[2]).

3. What is the syntax to declare an array?


data_type array_name[size];
Example: int arr[10]; declares an array of 10 integers.

4. How are elements accessed in an array?


Elements are accessed using their index, which starts from 0.
Example: arr[0] accesses the first element of the array arr.

5. Can the size of an array be changed after declaration?


No, the size of an array in C is fixed once declared.

6. What is the default value of elements in an uninitialized array?


- For static arrays, the default values are 0.
- For local (automatic) arrays, the values are garbage (uninitialized).

7. How do you initialize an array?


int arr[5] = {1, 2, 3, 4, 5};
Unspecified elements are initialized to 0.

8. What is a two-dimensional array?


A two-dimensional array is an array of arrays, used to represent a matrix-like
structure.
Example: int arr[2][3]; represents a 2x3 matrix.

9. How do you pass an array to a function?


You can pass an array to a function by passing its name, which acts as a pointer to
the first element.
Example:
void display(int arr[], int size);
display(arr, 5);

10. What is the difference between an array and a pointer?


- An array is a collection of elements, while a pointer is a variable that holds
the address of another variable.
- Arrays have a fixed size, while pointers can point to any memory location
dynamically.

11. What is a multidimensional array?


A multidimensional array is an array with more than two dimensions.
Example: int arr[2][3][4]; represents a 3D array with 2 blocks, 3 rows, and 4
columns.

12. What is the maximum size of an array in C?


The maximum size of an array depends on the system's memory and the compiler. It is
generally limited by the stack or heap size.

13. What is the difference between row-major and column-major order?


- Row-major order: Elements of a row are stored in contiguous memory locations.
(Used in C)
- Column-major order: Elements of a column are stored in contiguous memory
locations. (Used in Fortran)

14. How do you dynamically allocate an array?


Using malloc or calloc in the <stdlib.h> library.
Example:
int *arr = (int *)malloc(n * sizeof(int));

15. What happens if you access an out-of-bounds array element?


Accessing out-of-bounds elements results in undefined behavior. It may cause a
segmentation fault or read garbage values.

16. What are the advantages of arrays?


- Easy to store and access multiple data items.
- Random access is possible using indices.
- Saves memory when compared to individual variable declarations.

17. What are the limitations of arrays?


- Fixed size: Cannot be resized dynamically.
- Only supports elements of the same data type.
- Insertion and deletion are slow as elements need to be shifted.

18. What is an array of pointers?


An array of pointers stores the addresses of variables or other arrays.
Example:
int *arr[5];

19. How do you copy one array into another?


Use a loop to copy each element:
for (i = 0; i < size; i++)
array2[i] = array1[i];

20. What is a jagged array?


A jagged array is an array of arrays where the inner arrays can have different
sizes. C does not support jagged arrays directly but can be implemented using
pointers.

You might also like