Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Array Set2

Uploaded by

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

Array Set2

Uploaded by

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

Quiz

array-set2
Scan to view
digital version

1. How do you declare a one-dimensional array of integers with 10 elements in C?

A. int arr(10);
B. int arr[10];
C. int arr{10};
D. int arr = 10;

2. What is the index of the first element in a C array?

A. 0
B. 1
C. -1
D. Depends on the array size

3. Which of the following correctly initializes a 2D array of 2 rows and 3 columns in C?

A. int arr[3][2] = {{1,2},{3,4},{5,6}};


B. int arr[2,3] = {1,2,3,4,5,6};
C. int arr[2][3] = {{1,2,3},{4,5,6}};
D. int arr[2][3] = {1,2,3,4,5,6};

4. How do you access the element in the second row and third column of a 2D array named matrix?

A. matrix[2][3]
B. matrix(1,2)
C. matrix[2,3]
D. matrix[1][2]

5. What will be the output of the following code?

int arr[3] = {10, 20, 30};


printf("%d", arr[1]);

A. 10
B. 20
C. 30
D. Compilation error
6. Which loop is commonly used to traverse all elements of a one-dimensional array in C?
A. for loop
B. while loop
C. do-while loop
D. switch loop

7. What is the total number of elements in a two-dimensional array declared as int mat[4][5]?
A. 9
B. 10
C. 20
D. 25

8. In C, what happens if you try to access an array element out of its declared bounds?
A. The program throws a compile-time error
B. Undefined behavior (may crash or give garbage values)
C. The element is automatically initialized to zero
D. The program automatically resizes the array

9. How do you pass a one-dimensional array to a function in C?


A. By passing the whole array copy
B. By passing the array size only
C. By passing a pointer to the last element
D. By passing the array name (pointer to first element)

10. Which header file must be included to use printf() for displaying array elements?
A. stdio.h
B. stdlib.h
C. string.h
D. math.h

11. What is the output of this code snippet?

int arr[3] = {5};


printf("%d %d %d", arr[0], arr[1], arr[2]);
A. 5 5 5
B. 5 1 1
C. 5 0 0
D. Undefined values

12. Which of these is NOT a valid way to declare a two-dimensional array in C?


A. int arr[3][4];
B. int arr[3,4];
C. int arr[2][5];
D. int arr[1][1];
13. How do you find the number of elements in a one-dimensional array named arr with size 10?

A. sizeof(arr) / sizeof(arr[0])
B. sizeof(arr)
C. sizeof(arr[0])
D. 10

14. What is the result of the following code?

int arr[2][2] = {{1,2},{3,4}};


printf("%d", *(*(arr + 1) + 0));

A. 3
B. 1
C. 2
D. 4

15. Which statement correctly copies contents of one array to another in C?

A. arr2 = arr1;
B. arr2 == arr1;
C. Use a loop to copy each element
D. arr2 := arr1;

16. What does the following code print?

int arr[5] = {0};


for(int i=0; i<5; i++) printf("%d ", arr[i]);

A. Random values
B. 0 1 2 3 4
C. Compilation error
D. 0 0 0 0 0

17. Which of the following best describes a two-dimensional array in C?

A. An array with two data types


B. An array of arrays
C. An array with two elements
D. An array with pointers

18. What will be the output of this code?

int arr[3] = {1, 2, 3};


printf("%d", *(arr + 2));

A. 3
B. 1
C. 2
D. Compilation error
19. Which is the correct way to declare a pointer to a 2D array in C?

A. int *ptr[3];
B. int ptr*;
C. int *ptr;
D. int (*ptr)[3];

20. If you have int arr[5], which expression gives the address of the last element?

A. arr + 4
B. &arr[5]
C. arr + 5

21. What is the main advantage of using a two-dimensional array over multiple one-dimensional arrays?

A. It uses less memory than one-dimensional arrays


B. It organizes data in rows and columns for easier access
C. It allows storing different data types in the same array
D. It automatically resizes when more elements are added

22. What is the correct way to declare and initialize a one-dimensional array of characters with the word "hello"
in C?

A. char arr[5] = {'h', 'e', 'l', 'l', 'o'};


B. char arr[] = "hello";
C. char arr[6] = {'h', 'e', 'l', 'l', 'o'};
D. char arr = "hello";

23. Which of the following statements about memory layout of two-dimensional arrays in C is true?

A. Elements are stored column by column in memory


B. Elements are stored row by row in memory
C. Elements are stored randomly in memory
D. Elements are stored as separate arrays for each row

24. What is the effect of partially initializing a one-dimensional array in C, like int arr[5] = {1, 2};?

A. The remaining elements contain garbage values


B. The remaining elements are initialized to zero
C. The program will not compile
D. Only the first element is initialized

25. How is memory allocated for a two-dimensional array in C?

A. Elements are stored column-wise, one column at a time


B. Elements are stored row-wise, one row at a time
C. Elements are stored randomly in memory
D. Each element is stored at a separate memory location with gaps
26. When passing a two-dimensional array to a function in C, what must be specified in the function parameter?

A. Only the total number of elements


B. The size of all dimensions except the first
C. No size information is needed
D. Only the number of rows

27. What is the difference between an array and a pointer in C when used to access elements?

A. Arrays can be resized at runtime, pointers cannot


B. Arrays represent a fixed block of memory, pointers can point anywhere
C. Pointers store multiple values, arrays store only one
D. Pointers are automatically initialized, arrays are not

28. How can you dynamically allocate memory for a two-dimensional array in C?

A. Declare as int arr[][] with variable sizes


B. Use malloc to allocate array of pointers and then each row
C. Declare as int arr[rows][cols] with fixed sizes
D. Use a single malloc call for the entire 2D array without pointers

29. Which of the following statements about two-dimensional arrays in C is true?

A. Elements of each column are stored contiguously in memory


B. Elements of each row are stored contiguously in memory
C. The array name is a pointer to a pointer
D. Two-dimensional arrays are stored as arrays of pointers

30. What is the correct way to declare a function parameter that accepts a two-dimensional array in C?

A. void func(int arr[][])


B. void func(int arr[][5])
C. void func(int arr[])
D. void func(int *arr)

31. What is a common use case for two-dimensional arrays in C programming?

A. Storing a list of unrelated variables


B. Representing tables or matrices with rows and columns
C. Holding a sequence of characters in a string
D. Managing a single continuous block of memory without structure
1. How do you declare a one-dimensional array of integers with 10 elements in C?

B. int arr[10];
The correct syntax for declaring an integer array of size 10 is int arr[10];.
Other options either use wrong syntax or wrong data types.

2. What is the index of the first element in a C array?

A. 0
Array indexing in C starts at 0, so the first element is at index 0.
Other options are incorrect because C does not use 1-based indexing.

3. Which of the following correctly initializes a 2D array of 2 rows and 3 columns in C?

C. int arr[2][3] = {{1,2,3},{4,5,6}};


The correct syntax is int arr[2][3] = {{1,2,3},{4,5,6}}; which initializes all elements.
Other options have syntax errors or wrong dimensions.

4. How do you access the element in the second row and third column of a 2D array named matrix?

D. matrix[1][2]
Since indexing starts at 0, second row is index 1 and third column is index 2, so matrix[1][2] accesses that
element.
Other options use incorrect indices or syntax.

5. What will be the output of the following code?

int arr[3] = {10, 20, 30};


printf("%d", arr[1]);

B. 20
arr[1] refers to the second element which is 20.
Other options are values from other indices or incorrect.

6. Which loop is commonly used to traverse all elements of a one-dimensional array in C?

A. for loop
A for loop is typically used for controlled iteration over array indices.
While and do-while loops can be used but are less common for arrays.

7. What is the total number of elements in a two-dimensional array declared as int mat[4][5]?

C. 20
The total elements = rows × columns = 4 × 5 = 20.
Other answers are incorrect calculations.

8. In C, what happens if you try to access an array element out of its declared bounds?

B. Undefined behavior (may crash or give garbage values)


Accessing out-of-bounds elements leads to undefined behavior which may cause runtime errors or
garbage values.
C does not perform automatic bounds checking.
9. How do you pass a one-dimensional array to a function in C?

D. By passing the array name (pointer to first element)


You pass the array name which acts as a pointer to its first element.
Other options are incorrect syntax or concepts.

10. Which header file must be included to use printf() for displaying array elements?

A. stdio.h
stdio.h contains the declaration for printf().
Other headers do not provide printf().

11. What is the output of this code snippet?

int arr[3] = {5};


printf("%d %d %d", arr[0], arr[1], arr[2]);

C. 5 0 0
Only arr[0] is initialized to 5; the rest are zero by default in this context.
Hence output is 5 0 0.

12. Which of these is NOT a valid way to declare a two-dimensional array in C?

B. int arr[3,4];
int arr[3,4]; is invalid syntax; comma is not used in array size declaration.
Others use correct syntax.

13. How do you find the number of elements in a one-dimensional array named arr with size 10?

A. sizeof(arr) / sizeof(arr[0])
You can use sizeof(arr)/sizeof(arr[0]) to find number of elements.
Other options do not correctly compute the count.

14. What is the result of the following code?

int arr[2][2] = {{1,2},{3,4}};


printf("%d", *(*(arr + 1) + 0));

A. 3
*(*(arr + 1) + 0) accesses element at row 1, column 0, which is 3.
Other options correspond to different elements or errors.

15. Which statement correctly copies contents of one array to another in C?

C. Use a loop to copy each element


Arrays cannot be assigned directly; elements must be copied individually or using functions like memcpy.
Other options show invalid assignments.
16. What does the following code print?

int arr[5] = {0};


for(int i=0; i<5; i++) printf("%d ", arr[i]);

D. 0 0 0 0 0
Initializing with {0} sets all elements to zero.
Hence the output is 0 0 0 0 0.

17. Which of the following best describes a two-dimensional array in C?

B. An array of arrays
A 2D array is an array of arrays, like a matrix with rows and columns.
Other options are incorrect or incomplete definitions.

18. What will be the output of this code?

int arr[3] = {1, 2, 3};


printf("%d", *(arr + 2));

A. 3
*(arr + 2) accesses the third element, which is 3.
Other options are incorrect values.

19. Which is the correct way to declare a pointer to a 2D array in C?

D. int (*ptr)[3];
int (*ptr)[3] declares a pointer to an array of 3 integers (a row).
Other options have incorrect pointer syntax.

20. If you have int arr[5], which expression gives the address of the last element?

A. arr + 4
arr + 4 points to the last element (index 4).
Other options are incorrect addresses or syntax.

21. What is the main advantage of using a two-dimensional array over multiple one-dimensional arrays?

B. It organizes data in rows and columns for easier access


A two-dimensional array groups related data in a structured matrix form, making it easier to manage
and access elements using row and column indices. Multiple one-dimensional arrays do not inherently
represent this grid structure, which can complicate data handling.

22. What is the correct way to declare and initialize a one-dimensional array of characters with the word "hello"
in C?

B. char arr[] = "hello";


The correct declaration uses char array with the string literal "hello" which includes the null terminator.
Using double quotes initializes the array with characters plus '\0'. Other options either use incorrect syntax
or do not include the null terminator properly.
23. Which of the following statements about memory layout of two-dimensional arrays in C is true?

B. Elements are stored row by row in memory


In C, two-dimensional arrays are stored in row-major order, meaning elements of each row are stored
contiguously in memory. This allows pointer arithmetic to access elements efficiently. Column-major order
is used in other languages like Fortran, but not in C.

24. What is the effect of partially initializing a one-dimensional array in C, like int arr[5] = {1, 2};?

B. The remaining elements are initialized to zero


When an array is partially initialized, the remaining elements are automatically set to zero by default in C.
This means arr[0] and arr[1] will be 1 and 2 respectively, while arr[2] to arr[4] will be 0. This behavior helps
avoid garbage values in uninitialized elements.

25. How is memory allocated for a two-dimensional array in C?

B. Elements are stored row-wise, one row at a time


In C, two-dimensional arrays are stored in row-major order, meaning all elements of a row are stored
contiguously in memory before moving to the next row. This allows efficient access and predictable
memory layout.

26. When passing a two-dimensional array to a function in C, what must be specified in the function parameter?

B. The size of all dimensions except the first


In C, when passing a 2D array to a function, the size of all dimensions except the first must be specified to
correctly calculate offsets in memory. This allows the compiler to know how to navigate rows and columns.

27. What is the difference between an array and a pointer in C when used to access elements?

B. Arrays represent a fixed block of memory, pointers can point anywhere


An array name acts as a constant pointer to its first element but is not a modifiable pointer itself. A pointer
variable can be reassigned to point elsewhere. Arrays have fixed size determined at compile-time, while
pointers can be used to dynamically access memory.

28. How can you dynamically allocate memory for a two-dimensional array in C?

B. Use malloc to allocate array of pointers and then each row


Dynamic allocation requires using pointers and functions like malloc to allocate memory at runtime. Static
arrays are fixed size, but dynamic arrays can be sized during execution.

1. Use a pointer to pointer (e.g., int **arr).


2. Allocate memory for row pointers.
3. Allocate memory for each row separately.

Static declaration does not allow dynamic sizing.

29. Which of the following statements about two-dimensional arrays in C is true?

B. Elements of each row are stored contiguously in memory


Two-dimensional arrays in C are stored in row-major order, meaning that elements of each row are stored
in contiguous memory locations. This allows efficient access when iterating row-wise. Other options are
incorrect because columns are not stored contiguously, and the array name represents the address of the
first element, not a pointer to a pointer.
30. What is the correct way to declare a function parameter that accepts a two-dimensional array in C?

B. void func(int arr[][5])


In C, when passing a two-dimensional array to a function, the size of all dimensions except the first must
be specified so the compiler can correctly calculate element offsets.

1. The first dimension size is optional because the pointer to the first element is passed.
2. Omitting the size of the second dimension leads to compilation errors.
3. Using a pointer to an array is another valid way but still requires dimension sizes.

Incorrect options either omit the second dimension size or use invalid syntax.

31. What is a common use case for two-dimensional arrays in C programming?

B. Representing tables or matrices with rows and columns


Two-dimensional arrays are often used to represent grid-like structures such as matrices, tables, or
images, where data is organized in rows and columns. This allows for easier access and manipulation of
elements based on two indices.

You might also like