Arrays and Pointers in C Tutorial and MCQ
Table of Contents
Arrays and Pointers in C MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests
An Array is a group of elements of same data type. C Programming Language allows programmers to deal with maintainability of a C program with tens of hundreds of variables used for a specific purpose.
Arrays and Pointers in C
An Array can be thought of as Single Row of Chairs in a Cinema Theater. Each row has a Number. Also, each Chair in that row has a number.
Syntax of Array:
int pincodes[20]; //Array Declaration
Facts about Arrays:
- Every array has a Name which is nothing but the Variable Name.
- Every Array has a Size. It can NOT be be specified at run time or later.
- Every Array has a Data Type.
In the above array declaration, pincodes is an array variable with a capacity of 20 elements. All elements belong to int data type. Even before storing data in the array, memory is reserved in contiguous or continuous memory locations. These contiguous locations can be thought of as Reserved or Booked Chairs in a Cinema Theater.
Array Index Out of Bounds or accessing index more than array size does not produce any error. It simply shows garbage values. Java language shows errors or exceptions in this case.
Array size must be specified in between two Brackets [ ]. It is also called Subscript. A new word INDEX is used to identify the location or order of a particular element in an Array. Eg. pincodes [5] = 10;. Array index always starts with ZERO ‘0’. So pincodes[5] represents 6th element in the array.
Array elements can be used in all Arithmetic operations.
Array Initialization
By default, array elements are initialized with some default values even if you do not assign any value. Depending on the Storage Class, array elements get some default values. ‘auto’ and ‘register’ type array elements hold garbage values by default. ‘static’ and ‘extern’ type array elements hold ZERO as default values. By default, ‘auto’ storage class is applied.
//Declaration and Initialization int pincodes[4] = {1234, 4321, 2345, 5432}; int marks[] = {98, 75, 89, 34, 28, 65}; int buses[3]; //Declaration //Initialization buses[0] = 23; buses[1] = 56; buses[2] = 34;
Array elements can be initialized at the time of Declaration it self. Usually, we assign values to array elements at run time or during execution of program like in buses example above.
Note that we have not specified array size for the variable marks. Because we are initializing the array at the same time. In the case of buses, specifying array size is mandatory as initialization is not done.
Arrays and Pointers in C examples
Example 1
int main() { int marks[] = {23, 45, 78}; int num = 3, i=0; for(i=num-1; i >= 0; i--) printf("a[%d]=%d, ", i, marks[i]); return 9; } //output //a[2]=78, a[1]=45, a[0]=23,
In the above example, Array “marks” contains 3 elements. Array index starts from 0 and ends with 2.
Arrays and Pointers
Example 2: Passing array elements with / without Pointers
We can pass array elements to a function using Pass By Value and Pass By Reference. Pass By Reference is achieved using Pointers.
void display(int, int*); int main() { int x=5, y=8; display(x,y); printf("%d, %d", x, y); return 9; } //a = call by value //*b = call by reference void display(int a, int *b) { *b = *b + 1; printf("%d, %d", a, b); } //OUTPUT //5, 8 //5, 9 //8 becomes after incrementing using pointers
Multidimensional Arrays
An array with only one Subscript is called a Single Dimensional Array or 1D array. An array with more than one dimension is called a Multidimensional Array or nD array. A 2D array is composed of 1 row and 1 column. Size of an array is obtained by multiplying the sizes of individual 1D arrays. For example, students[2][3] represents the element present at 3rd row 4th column.
In a multidimensional array, specifying last subscript or Last Dimension is mandatory during combined declaration and initialization. If you are initializing elements later, mentioning all subscripts or dimension sizes are mandatory.
Example
int main() { //3 Rows each consisting of 2 columns. //WE HAVE NOT MENTIONED ROW SIZE ********** int chairs[][2] = {{22,33},{44,55},{66,88}}; int i=0; j=0; while(i<=(3-1)) { while(j <= (2-1)) { printf("%d,", chairs[i][j]); j++; } printf("\n"); i++; } return 9; } //OUTPUT //22,33, //44,55, //66,88 //Last element of this 2D array = chairs[2][1]
We have used WHILE loop instead of FOR loop in this example. Most of the developers use FOR loop to handle multidimensional arrays.
Using Pointers with Arrays
Arrays are handled internally using pointers. & Operator is called Address Of Operator. &a represents the address of variable ‘a’. * (STAR) operator is called VALUE AT ADDRESS operator. So *p represents the value at address if p is a pointer to an address.
To pass an array, we usually pass the Base Address or Address of First Element of an Array.
Arrays and Pointers in C examples
Example 1: Passing an array to a function
void show(int[]); void show2(int *); int main() { int a[3] = {3,4,5}; show(&a[0]); //We are passing base address show2(&a[0]); return 9; } void show(int k[]) { printf("%d,", k[0]); //print 1st element } void show2(int *p) { p++; //points to 2nd element printf("%d, *p); } //OUTPUT //3,4,
Notes on Array Pointers
We use below code for notes.
int ary[3]; int *p = &ary[0]; int *q = ary; int cats[i]; int kites[i][j]; int bats[a][b][c];
1. ary[5] = *(p+4);
2. q[i] = i[q] = *(q+i) = *(i+q)
3. cats[ i ] = i [ cats] = *(cats+i) = *(i+cats)
4. kites[i][j] = *(*(kites+i) + j)
5. bats[a][b][c] = *(*(*(bats + a) + b) + c)
6. Incrementing an array pointer points to next memory location after skipping memory bytes of the data type
[WpProQuiz 35]
Arrays and Pointers in C interview MCQ
1) What is the dimension of the C array int ary[10][5].?
A) 1
B) 2
C) 5
D) 10
Answer [=] B
Explanation:
See the number of [ ] square bracket pairs. Here there are 2 ary[10][5]. So the dimension is TWO 2.
2) What is the dimension of the below C Array.?
int ary[]={1,3,5,7};
A) 1
B) 2
C) 3
D) 5
Answer [=] A
Explanation:
It is a Single Dimension Array. Only [] one pair of square brackets is present.
3) Choose a correct statement with array pointers.
A) It is valid to add an integer number to an array pointer. Result can be anything.
B) It is valid to subtract an integer number from array pointer. Result can be anything.
C) Difference of pointers to two elements of an array gives the difference between their indexes.
D) All the above
Answer [=] D
4) Choose correct statement about C array pointers.
A) You can compare two array elements with *p == *(p+i)
B) You can compare two pointers with p1==p2.
C) Accessing out of bounds index element is valid and it returns a garbage value.
D) All the above.
Answer [=] D
5) What is the output of C Program with arrays.?
int main() { int ary[] = {1, 3, 5}; printf("%d %d", ary[-1], ary[4]); return 0; }
A) 1 5
B) 0 0
C) Compiler error
D) None of the above
Answer [=] D
Explanation:
You are accessing -1 index and 4 index which is outside the limit 0 to 2. You get only garbage values.
6) What is the output of C Program with arrays.?
int main() { static int ary[] = {1, 3, 5}; printf("%d %d", ary[-1], ary[5]); return 0; }
A) 0 0
B) -1 -1
C) Compiler error
D) None of the above
Answer [=] A
Explanation:
0 0 is the answer. Storage Class static makes all uninitialized variables to hold default ZERO values.
7) What is the output of C Program with arrays and pointers.?
int main() { int ary[] = {11, 33, 55}; int *p, *q; p = ary; q = ary+2; printf("%d %d",*p, *q); return 0; }
A) 11 55
B) 11 13
C) 11 33
D) Compiler error
Answer [=] A
Explanation:
Incrementing a pointer points to the address of next element. ary points to first element. ary +1 points to second element. ary+2 points to the third element.
8) Difference between C Arrays, ary[10] and cry[10][10] is.?
A) ary[10] is a single dimensional array. cry[10][10] is a Multidimensional array.
B) ary[10] is a multidimensional array. cry[10][10] is a single dimensional array.
C) Size of ary[10] is sizeof(10* int). Size of cry[10][10] is sizeof(10*int).
D) None of the above.
Answer [=] A
Explanation:
One square bracket = Single Dimension or One dimension.
9) Array of Arrays is also called.?
A) Multi Data Array
B) Multi Size Array
C) Multi Dimensional Array
D) Multi Byte Array
Answer [=] C
10) What is the output of C program with multidimensional array.?
int main() { int ary[3][2] = {1,2,3,4,5,6}; printf("%d %d", ary[0][0], ary[2][1]); return 0; }
A) 2 5
B) 1 6
C) 1 5
D) 2 6
Answer [=] B
Explanation:
[3] represents 3 rows. [2] represents 2 columns. So each row contains two elements. Index of row and column start with ZERO 0. So ary[2][1] represents 3rd row, 2nd element.
11) What is the output of C program with multidimensional arrays.?
int main() { int ary[3][] = {6,5,4,3,2,1}; printf("%d %d", ary[0][0], ary[2][1]); return 0; }
A) 6 1
B) 6 2
C) 5 1
D) Compiler error
Answer [=] D
Explanation:
ary[3][] has missing column count. Column count is a must for any multidimensional array.
12) What is the output of C program with multidimensional arrays.?
int main() { int ary[][3] = {6,5,4,3,2,1}; printf("%d %d", ary[0][0], ary[1][0]); return 0; }
A) 6 2
B) 6 3
C) 6 1
D) Compiler error
Answer [=] B
Explanation:
ary[][3] divides all 6 elements into two rows each containing 3 elements. 6/col = 6/3 = 2 rows. {{6,5,4}, {3,2,1}}
13) Choose an alternative definition of C Multidimensional array.?
int ary[][3] = {6,5,4,3,2,1};
A) int ary[2][3] = {6,5,4,3,2,1};
B) int ary[2][3] = {{6,5,4},{3,2,1}};
C) int ary[][3] = {{6,5,4},{3,2,1}};
D) All the above.
Answer [=] D
14) What is the output of C Program with arrays.?
int main() { int ary[][2][3] = { {{1,2,3},{4,5,6}}, {{7,8,9},{10,11,12}} }; printf("%d %d", ary[0][0][0], ary[1][1][1]); return 0; }
A) 1 12
B) 1 11
C) 7 12
D) 1 6
Answer [=] B
Explanation:
a[][2][3] has missing first dimension. It is valid to ignore first dimensional value if you want. 1st dimension = 12/(2*3) = 12/6 = 2.
15) Choose a correct statement about a C Multidimensional array.
A) First Dimension size is optional when initializing the array at the same time.
B) Last Dimension size is optional when initializing the array at the same time.
C) It is a must to specify all dimensions of a multidimensional array.
D) Memory locations of elements of a multidimensional array is not sequential.
Answer [=] A
Explanation:
int ary[] = {1,2,3}; int bry[][2] = {{1,2},{3,4}}
16) What is the output of C Program.?
int main() { int ary[2][2][3] = { {{1,2,3},{4,5,6}}, {{7,8,9},{10,11,12}} }; int *p; p = &ary; printf("%d %d",*p, *p+11); return 0; }
A) 1 11
B) 1 12
C) 2 12
D) Compiler error
Answer [=] B
Explanation:
*p points to the first element. *p + 11 is the 12th element which is 12 in the array. It is same as ary[1][1][2].
17) What is the output of C Program.?
int main() { int ary[2][2][3] = { {{1,2,3},{4,5,6}}, {{7,8,9},{10,11,12}} }; printf("%d",*(*(*(ary+1)+ 1)+2)); return 0; }
A) 10
B) 11
C) 12
D) Compiler error
Answer [=] C
Explanation:
*(*(*(ary+i)+ )+k) = ary[i][j][k]
18) Choose a correct C Statement to choose number 66 in the array, int ary[3][2] = {{11,22},{33,44},{55,66}};
A) ary[2][1]
B) *(*(ary+2)+1)
C) *ary[2]+1
D) All the above
Answer [=] D
19) A multidimensional array of dimension N is a collection of.?
A) Single Dimensional Arrays
B) N dimensional arrays
C) N-1 dimension arrays
D) N-2 dimension arrays
Answer [=] C
Explanation:
A 4 dimensional array is a collection of 3 dimensional arrays.
20) Choose a correct statement about a Multidimensional array and pointer.?
A) int *ptr[N] is an array of N integer pointers. Size is N * sizeof(1*int).
B) int (*ptr)[N] is a pointer to an array of N elements. Size of ptr is size of 1 integer.
C) An multidimensional array or a single dimensional array can contain pointer elements.
D) All the above
Answer [=] D