Study C MCQ Questions and Answers on Arrays, Multidimensional Arrays and Pointers. Easily attend technical interviews after reading these Multiple Choice Questions.
Go through C Theory Notes on Arrays before studying questions.
int main() { int size=4; int a[size]; a[0]=5;a[1]=6; a[2]=7;a[3]=8; printf("%d %d", *(a+2), a[1]); }
variable size is already defined. So a[size] is allowed. *(a+2) == a[2].
int main() { int ary(3)=[20,30,40]; printf("%d", a(1)); }
Array should be declared and defined with Square Brackets. Use ary[2] instead of ary(2).
int ary[3]={20,30,40};
int main() { int rollno[3]=[1001,1002,1003]; printf("%d", rollno[1]); }
You should use Flower Brackets or Braces to define elements like {1,2,3}. It is wrong to use [1,2,3].
int main() { char grade={'A','B','C'}; printf("%c", grade[0]); }
Notice that char grade is an character variable, not Character array variable. So declare as char grade[] = {'A','B','C'};
For Automatic variables, default value is garbage. For static and global variables, default value is 0.
int main() { int ary[9]; return 0; }
Array size is 9. So memory occupied by 9 integers are kept aside by the CPU.
int main() { int ary[9]; return 0; }
Array index starts with 0 and ends with 8 for a 9 Size array. ary[0] to ary[8] are meaningful.
No. You can not change the C Basic rules of Zero Starting Index of an Array.
int main() { int ary[4], size=4; printf("%d ", ary[size]); return 0; }
Yes. Some random number will be printed as the array is not initialized and the index is out of range. But, you do not get any compiler error. It is your responsibility.
int main() { int ary[4]; ary[4] = {1,2,3,4}; printf("%d ", ary[2]); return 0; }
You can not initialize the array in next line or statement once its type and size is defined first.
int ary[4]={1,2,3,4}; //works
int main() { int ary[3]={1,2}; printf("%d %d",ary[2]); return 0; }
Though you initialized only two elements in a 3 Size array, it is valid. Third element is a garbage value.
In Turbo C, integer occupies 2 bytes. So in an integer array, if array pointer is incremented, it will reach the next element after two bytes. In this below 4 element integer array, elements are available at 1001, 1003, 1005 and 1007 byte addresses.
1001 1002 1003 1004 1005 1006 1007 1008.
int main() { int ary[] = {10,20,30}, *p; p = &ary[0]; int i=0; while(i<3) { printf("%d ", *p); p++; i++; } return 0; }
First get the address of 1st element &ary[0]. Increment the pointer P to reach next element of the array.
Yes. calloc() initialized the elements to 0. malloc() does not initialize. So garbage values will be there.
int *p; p = (int*)malloc(10*sizeof(int));
int *p; p = (int*)malloc(10,sizeof(int));
int *p; p = (int*)malloc(sizeof(int), 10);
int *p; p = (int*)malloc(10*sizeof(int *));
It allocates memory to hold 10 integers in an array.
int *p; p = (int*)calloc(10, sizeof(int));
int *p; p = (int*)calloc(10*sizeof(int));
int *p; p = (int*)calloc(sizeof(int), 10);
int *p; p = (int*)calloc(10, sizeof(int *));