4.4 - Arrays in Programming.


Back Button Home

4.4.1 - Passing Array to Function.


1. What is an array in programming?
  • (A) A single value data structure
  • (B) A collection of variables of the same type
  • (C) A data structure that can hold multiple data types
  • (D) A function to manipulate data
Correct Answer: (B) A collection of variables of the same type.
2. How do you declare a one-dimensional array in C?
  • (A) int arr(5);
  • (B) int arr[5];
  • (C) array arr[5];
  • (D) arr[5] int;
Correct Answer: (B) int arr[5];
3. Which of the following accesses the third element of an array named arr?
  • (A) arr[2]
  • (B) arr[3]
  • (C) arr[1]
  • (D) arr(2)
Correct Answer: (A) arr[2]
4. What will be the output of the following code?
                int arr[3] = {1, 2, 3};
                printf("%d", arr[1]);
                
  • (A) 1
  • (B) 2
  • (C) 3
  • (D) Error
Correct Answer: (B) 2
5. What is the result of trying to access an out-of-bounds index in an array?
  • (A) It returns 0
  • (B) It produces a runtime error
  • (C) It leads to undefined behavior
  • (D) It returns the last element
Correct Answer: (C) It leads to undefined behavior.
6. Which of the following statements correctly passes an array to a function in C?
  • (A) func(arr);
  • (B) func(&arr);
  • (C) func(arr[]);
  • (D) func(arr[5]);
Correct Answer: (A) func(arr);
7. How do you define a two-dimensional array with 3 rows and 4 columns?
  • (A) int arr[3][4];
  • (B) int arr[4][3];
  • (C) int arr(3, 4);
  • (D) int arr{3, 4};
Correct Answer: (A) int arr[3][4];
8. What is the output of the following code?
                int arr[3] = {5, 10, 15};
                printf("%d", arr[0] + arr[2]);
                
  • (A) 10
  • (B) 15
  • (C) 20
  • (D) 30
Correct Answer: (D) 20
9. Which function is used to determine the size of an array in bytes?
  • (A) sizeof(array)
  • (B) length(array)
  • (C) size(array)
  • (D) count(array)
Correct Answer: (A) sizeof(array)
10. What is the primary advantage of using arrays?
  • (A) Dynamic memory allocation
  • (B) Ease of manipulation and retrieval of data
  • (C) Supports multiple data types
  • (D) Slower access time
Correct Answer: (B) Ease of manipulation and retrieval of data.
11. Which of the following correctly initializes an array with values?
  • (A) int arr[] = {1, 2, 3};
  • (B) int arr = {1, 2, 3};
  • (C) int arr[3] = (1, 2, 3);
  • (D) int arr(1, 2, 3);
Correct Answer: (A) int arr[] = {1, 2, 3};
12. Which of the following can be used to access the elements of a two-dimensional array?
  • (A) Single index
  • (B) Two indices
  • (C) Using pointers only
  • (D) Any index
Correct Answer: (B) Two indices.
13. What will happen if you declare an array with a size of 0 in C?
  • (A) It is allowed and can be used
  • (B) It will cause a runtime error
  • (C) It will compile but cannot be used
  • (D) It will create an infinite loop
Correct Answer: (C) It will compile but cannot be used.
14. How do you pass an individual element of an array to a function?
  • (A) func(arr);
  • (B) func(arr[i]);
  • (C) func(&arr);
  • (D) func(arr[5]);
Correct Answer: (B) func(arr[i]);
15. In which scenario is using an array more efficient than using separate variables?
  • (A) When there are fewer than 10 variables
  • (B) When variables are of different data types
  • (C) When the number of variables is large and of the same type
  • (D) When the data is not related
Correct Answer: (C) When the number of variables is large and of the same type.
16. What will be the output of the following code?
                int arr[3] = {2, 4, 6};
                printf("%d", arr[0] * arr[1]);
                
  • (A) 8
  • (B) 12
  • (C) 16
  • (D) 2
Correct Answer: (B) 8
17. What does the following declaration represent?
                int (*ptr)[5];
                
  • (A) Pointer to an integer
  • (B) Pointer to an array of 5 integers
  • (C) Array of 5 pointers
  • (D) Pointer to an array of integers
Correct Answer: (B) Pointer to an array of 5 integers.
18. Which of the following correctly accesses the last element of an array named arr with 5 elements?
  • (A) arr[4]
  • (B) arr[5]
  • (C) arr[last]
  • (D) arr[-1]
Correct Answer: (A) arr[4]
19. How do you dynamically allocate memory for an array of 10 integers?
  • (A) int arr[10];
  • (B) int *arr = malloc(10 * sizeof(int));
  • (C) int arr = (int*)malloc(10);
  • (D) int *arr = new int[10];
Correct Answer: (B) int *arr = malloc(10 * sizeof(int));
20. What will happen if you try to assign a new value to an element of an array declared as const?
  • (A) It will compile without errors
  • (B) It will cause a compilation error
  • (C) It will run but ignore the assignment
  • (D) It will create a new variable
Correct Answer: (B) It will cause a compilation error.