C Arrays Set 1

Ques 1: Which option should be replaced with ---- to get output as 0 0 0 0 0?
#include<stdio.h>
int main()
{
	int n=5;
	int a[n]=----;
	for(int i=0;i<5;i++)
		printf("%d",a[i]);
	return 0;
}
A. {0};
B. {0,0,0,0,0}; 
C. Either A or B
D. None of these
Description
Answer: D
Variable sized array cannot be initialized like this to instead loop must be used for this.
This will be valid int a[5] = {0}; but int a[n] = {0}; is neither valid in c not in c++.

Ques 2: output if base address of the array is 2686664 and integer takes 4 bytes?
#include<stdio.h>
int main()
{
	int a[]={0,1,2,3,4,5,6,7,8,9};
	printf("%u %u %u",a+1,&a+1,&a[0]+1);
}
A. 2686668 2686668 2686668 
B. Undefined behaviour.  
C. Compiler dependent.
D. None.
Description
Answer: D
 surprised..?? it will work fine and result will be 2686668 2686704 2686668. 
 In first one just one increment is there so it will print the address of 2nd element.
 But in second one &a means we are talking about address of whole array and increment by
 one mean increment one full array of same size that is why it is printing address of the next array.
 And 3rd one is also similar to first one.

Ques 3: What will be the output of the following program?
#include<stdio.h>
int main()
{
	int a[]={0,1,2,3,4};
	int n = sizeof(a)/sizeof(a[0]);
	printf("%d",n);
}
A. 1
B. 5
C. Error 
D. None of these
Description
Answer: B
 sizeof(a) will return sizeof(a) whole array that is 20 and sizeof(a[0]) will return size of a integer. 
 which will result as 5 total no. of element in the array.
 This approach may be used to find the length of array but only in the same function 
 where array has been defined.

Ques 4: Consider a matrix of 3X3 stored in a 2D array a[][] of same size, what will be the output of the following code segment?
int i,j;
for(i=0;i<3;i++){
       for(j=0;j<3;j++)
             printf("%d\t",a[j][i]);
       printf("\n");
}
A. It wil simply print the matrix as it is
B. It will print the matrix with columns reversed
C. It will print transpose of matrix
D. None of these
Description
Answer: C
Look the code carefully it is a[j][i] not a[i][j].

Ques 5: if mat[n][n] is an 2 dimensional array that represents a square matrix. what will be the minimum no. of loops required to print diagonal elements of the matrix?
A. 1
B. 2
C. 3
D. None of these.
Description
Answer: A
for(i=0;i<n;i++)
    printf("%d",a[i][i]);

Ques 6: what will be the declaration of fun() if it is being called as following?
int arr[5][10];
fun(arr);
A. void fun(int *ptr);
B. void fun(int ptr[][10]);
C. void fun(int (*ptr)[10]);
D. Any of the above
Description
Answer: D
Any of A,B or C will be valid but definition will be different for each of them.

Ques 7: Which is true about the following statement?
Statement-1:- (*ptr)[10];
Statement-2:- *ptr[10];
A. statement-1 is array of 10 pointers
B. statement-2 is array pointer to an array of 10 integers 
C. statement-1 is not valid in c.
D. None of these
Description
Answer: D
Stmnt-1 is pointer to an array of 10 integers.
     Stmnt-2 is array of 10 pointers.

Ques 8: Which is true about the following statement?
void (*fun[10])()
A. Error
B. an array of 10 functions that returns a void pointer
C. it is an array of 10 function pointers.
D. None of these
Description
Answer: C

Ques 9: Which is true about the following statement?
Stmnt-1:- (*ptr)[3][4];
Stmnt-2:- ptr[][3][4];
A. stmnt-1 is a pointer to a 2-D array
B. stmnt-2 is a pointer to a 2-D array
C. both are same i.e pointer to a 2-D array
D. stmnt-1 is pointer to a 2-D array but stmnt-2 is not valid in C.
Description
Answer: B

Ques 10: What will be the output of the following program?
#include<stdio.h>
int main()
{
	int i=5,a[]={0,1,2,3,4};
	while(i--){
		printf("%d",*a++);
	}
}
A. 01234
B. 1234
C. Error
D. None of these.
Description
Answer: C
 Base address of any array cannot be changed.


comments powered by Disqus