Sponsor

C MCQ Questions and Answers on Arrays and Pointers 2 | c programming Questions and Answers on Arrays and Pointers | c programming mcq

home.softwaretechit.com

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.





1) What is the output of C program with arrays and pointers.?

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]);

}

A) 8 6

B) 7 6

C) 6 6

D) Compiler error

Answer [=]

B

Explanation:

variable size is already defined. So a[size] is allowed. *(a+2) == a[2].

 

2) What is the output of C program with arrays.?

int main()

{

    int ary(3)=[20,30,40];

    printf("%d", a(1));

}

A) 20

B) 30

C) 0

D) Compiler error

Answer [=]

D

Explanation:

Array should be declared and defined with Square Brackets. Use ary[2] instead of ary(2).

int ary[3]={20,30,40};

 

3) What is the output of C Program with arrays.?

int main()

{

    int rollno[3]=[1001,1002,1003];

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

}

A) 1002

B) 1003

C) address of 1002

D) Compiler error

Answer [=]

D

Explanation:

You should use Flower Brackets or Braces to define elements like {1,2,3}. It is wrong to use [1,2,3].

 

4) What is the output of C program with arrays.?

int main()

{

   char grade={'A','B','C'};

   printf("%c", grade[0]);

}

A) A

B) B

C) C

D) Compiler error

Answer [=]

D

Explanation:

Notice that char grade is an character variable, not Character array variable. So declare as char grade[] = {'A','B','C'};

 

5) What is the value of an array element which is not initialized.?

A) By default Zero 0

B) 1

C) Depends on Storage Class

D) None of the above.

Answer [=]

C

Explanation:

For Automatic variables, default value is garbage. For static and global variables, default value is 0.

 

6) What happens when you try to access an Array variable outside its Size.?

A) Compiler error is thrown

B) 0 value will be returned

C) 1 value will be returned

D) Some garbage value will be returned.

Answer [=]

D

 

7) What is the size of an array in the below C program statement.?

int main()

{

    int ary[9];

    return 0;

}

A) 8

B) 9

C) 10

D) None of the above

Answer [=]

B

Explanation:

Array size is 9. So memory occupied by 9 integers are kept aside by the CPU.



 

8) What is the minimum and maximum Indexes of this below array.?

int main()

{

    int ary[9];

    return 0;

}

A) -1, 8

B) 0, 8

C) 1,9

D) None of the above

Answer [=]

B

Explanation:

Array index starts with 0 and ends with 8 for a 9 Size array. ary[0] to ary[8] are meaningful.

 

9) Can we change the starting index of an array from 0 to 1 in any way.?

A) Yes. Through pointers.

B) Yes. Through Call by Value.

C) Yes. Through Call by Reference.

D) None of the above.

Answer [=]

D

Explanation:

No. You can not change the C Basic rules of Zero Starting Index of an Array.

 

10) What is the need for C arrays.?

A) You need not create so many separate variables and get confused while using.

B) Using a single Array variable, you can access all elements of the array easily.

C) Code maintainability is easy for programmers and maintainers.

D) All the above.

Answer [=]

D

 

11) What is the output of C program with arrays.?

int main()

{

    int ary[4], size=4;

    printf("%d ", ary[size]);

    return 0;

}

A) 0

B) 1

C) Random number

D) Compiler error

Answer [=]

C

Explanation:

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.

 

12) What is the output of C Program with arrays.?

int main()

{

    int ary[4];

    ary[4] = {1,2,3,4};

    printf("%d ", ary[2]);

    return 0;

}

A) 2

B) 3

C) 0

D) Compiler error

Answer [=]

D

Explanation:

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

 

13) What is the output of C Program with arrays.?

int main()

{

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

    printf("%d %d",ary[2]);

    return 0;

}

A) 0

B) 2

C) Garbage value

D) Compiler error

Answer [=]

C

Explanation:

Though you initialized only two elements in a 3 Size array, it is valid. Third element is a garbage value.

 

14) What is a multidimensional array in C Language.?

A) It is like a matrix or table with rows and columns

B) It is an array of arrays

C) To access 3rd tow 2nd element use ary[2][1] as the index starts from 0 row or column

D) All the above.

Answer [=]

D



15) If an integer array pointer is incremented, how many bytes will be skipped to reach next element location.?

A) 1

B) 2

C) 8

D) None of the above

Answer [=]

B

Explanation:

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.

 

16) What is the output of C Program with arrays and pointers.?

int main()

{

    int ary[] = {10,20,30}, *p;

    p = &ary[0];

    int i=0;

    while(i<3)

    {

        printf("%d ", *p);

        p++;

        i++;

    }

    return 0;

}

A) 10 10 10

B) 10 20 20

C) 10 20 30

D) randomvalue randomvalue randomvalue

Answer [=]

C

Explanation:

First get the address of 1st element &ary[0]. Increment the pointer P to reach next element of the array.

 

17) What is the function used to allocate memory to an array at run time with Zero initial value to each.?

A) calloc()

B) malloc()

C) palloc()

D) kalloc()

Answer [=]

A

Explanation:

Yes. calloc() initialized the elements to 0. malloc() does not initialize. So garbage values will be there.

 

18) What is the function used to allocate memory to an array at run time without initializing array elements.?

A) calloc()

B) malloc()

C) palloc()

D) kalloc()

Answer [=]

B

 

19) Choose a correct Syntax for malloc() function to allocate memory to an array at run time.

A)

int *p;

p = (int*)malloc(10*sizeof(int));

B)

int *p;

p = (int*)malloc(10,sizeof(int));

C)

int *p;

p = (int*)malloc(sizeof(int), 10);

D)

int *p;

p = (int*)malloc(10*sizeof(int *));

Answer [=]

A

Explanation:

It allocates memory to hold 10 integers in an array.

 

 

20) What is the syntax of CALLOC to allocate memory to an array at runtime.?

A)

int *p;

p = (int*)calloc(10, sizeof(int));

B)

int *p;

p = (int*)calloc(10*sizeof(int));

C)

int *p;

p = (int*)calloc(sizeof(int), 10);

D)

int *p;

p = (int*)calloc(10, sizeof(int *));

Answer [=]

A

 


Post a Comment

0 Comments