Skip to main content

array mcq questions in c

1. The elements of an array can be accessed by its
           a) name
           b) index
           c) dimension
           d) none of the above
Solution: (b) An array is accessed by its index.

2. The elements of array are stored in contiguous memory due to
            a) This way computer can keep track only the address of the first element and the addresses                     of  other elements can be calculated.
            b) The architecture of computer does not allow arrays to store other than serially
            c) Both
            d) None
Solution: (a) This way computer can keep track only the address of the first element and the addresses of other elements can be calculated easily.

3. Which of the following statement is correct for following code snippet?
int num[7];
num[7]=8;
           a) In the first statement 7 specifies a particular element, whereas in the second statement it                       specifies a type;
           b) In the first statement 7 specifies a particular element, whereas in the second statement it                       specifies the array size.
           c) In the first statement 7 specifies the array size, whereas in the second statement it specifies                   a particular element of array.
           d) In both the statement 7 specifies array size.
Solution: (c) The statement 'c' is correct, because int num[7]; specifies the size of array and num[7]=8; designates the particular element(8th element) of the array.

4. Which of the following is correct statement to access 10th element in a array arr[] of size 30?
           a) arr[10]
           b) arr[9]
           c) arr{10}
           d) arr{9}
Solution: (b) arr[9] is the correct syntax to access the 10th element, as in C index starts from 0.

5. To compare two arrays, we can use
         a) Comparison operator ‘==’ directly on arrays
         b) Use ‘switch case’
         c) Using ‘for’ loop
         d) Using ternary operator on arrays
Solution: (c) We can use for loop and equality check operator on each element of the arrays to compare.

6. Can we add or delete an element after assigning an array?
Can we add or delete an element after assigning an array as arr[5] = {10, 20, 30, 40, 50}?
           a) Yes
           b) No
           c) May be
           d) Depends on compiler
Solution: (b) After assignment of an array, the elements can’t be deleted or new element can’t be added.

7. An 1 dimensional integer array of size 10 is declared in a C program. The memory location of the first byte of the array is 1050. What will be the location of the 9th element of the array? (Assuming an integer takes 2 bytes of memory)
           a) 1066
           b) 1084
           c) 1068
           d) 1064
Solution: (a) Integer takes two bytes of memory. As the memory assignment to the elements are consecutive and the index starts from 0, the 9th element will be located at 1050+(8×2) =1066.

8. The index of an array can take
           a) only integer
           b) only float
           c) both integer and float
           d) character
Solution: (a) Only integer variables are allowed as array index.

9. What will be the output after execution of the program?
#include <stdio.h>
int main()
{
int i, a[7]={5,6,8,2,9,3,6}, result;
result = a[0];
for(i=1;i<7;i++)
{
if(result>a[i])
continue;
result=a[i];
}
printf("%d",result);
return 0;
}
           a) 8
           b) 2
           c) 9
           d) Compiler error because of two six in the array.
Solution: (c) The program finds the maximum element of an array. Hence, the output is 9.

10. What is the output of the following C program?
#include<stdio.h>
int main()
{
int arr[4] = {1, 2, 3, 4, 5};
printf("%d", arr[0]);
return 0;
}

            a) 1
            b) 2
            c) No output
            d) Compilation error
Solution: (d) The size of the array does not match with the dimension of the array. Thus the compiler will show error.

11. Find the output of the following C program

#include<stdio.h>
int main()
{
int a;
int arr[5] = {1, 2, 3, 4, 5};
arr[0] = ++arr[1] - arr[1]--;
a = arr[0]++;
arr[0] = ++arr[a++];
printf("%d,%d,%d", a, arr[0],arr[1]);
return 0;
}
           a) 1,2,3
           b) 1,2,2
           c) 2,2,2
           d) 2,3,3
Solution: (b)
Please follow the operator precedence table and compute the values of each step. The answer will come as 1,2,2

12. What will be output?
#include<stdio.h>
int main()
{
int i;
int arr[3] = {1};
for (i = 0; i < 3; i++)
printf("%d ", arr[i]);
return 0;
}
            a) 1 followed by garbage values
            b) 1 1 1
            c) 1 0 0
            d) Syntax error
Solution: (c)
If array is initialized with few elements, remaining elements will be initialized to 0. Therefore, 1 followed by 0, 0, will be printed.

13. While passing an array as an argument to a function, which of the following is actually passed to the function?
            a) Array size
            b) First element of the array
            c) Last element of the array
            d) Base address of the array
Solution: (d) The base address of the array is passed to the function as an argument.

14. Predict the output of the following code.

#include <stdio.h>
int main()
{
int arr[1]={5};
printf("%d", 0[arr]);
return 0;
}
                 a) 0
                 b) 1
                 c) 5
                 d) Syntax error
Solution: (c) printf("%d", 0[arr]); It prints the first element value of the variable arr. Hence, the output is 5.

15. What will the output?

                            #include <stdio.h>
                            int main()
                           {
                            int arr1[]={1,2,3};
                            int arr2[]={4,5,6};
                            int arr3[];
                            arr3[]=arr1[]+arr2[];
                            printf("%d",arr3[]);
                            return 0;
                            }

                 a) 1,2,3
                 b) 5,6,7
                 c) 5,7,9
                 d) Error
Solution: (d) Error.
Such operations are not allowed. Wrong syntax used. Element wise addition has to be done in array using loop.

Comments

Popular posts from this blog

c programming MCQ questions and answers

c programming MCQ  questions and answers 1. Which among the following is the most complete definition of Programming?            a) Programming is a process to solve any logical problems.            b) Programming is a process to solve any numerical problem.            c) Programming is a process to solve any real life problems.            d) Programming is the process of taking an algorithm and encoding it into a notation (using a                    programming language), so that it can be executed by a computer. Solution: (d) Programming is the process of taking an algorithm and encoding it into a notation, a programming language, so that it can be executed by a computer 2. Which of the following controls the way in which the computer system works and give means by which users can interact wit...

Introduction to c language

Introduction to c C is a programming language that lets us give a computer very specific commands. C was invented in 1972. It's one of the oldest languages to be used even today! It is one of the most widely used programming languages in the world. It has influenced many popular languages, especially c++. But why do we still use such an old programming language? why not something newer and easier to understand,like python? Let's find out. History of C Language History of C language  is interesting to know. Here we are going to discuss a brief history of the c language. C programming language  was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A. Dennis Ritchie   is known as the   founder of the c language . It was developed to overcome the problems of previous languages such as B, BCPL, etc. Initially, C language was developed to be used in  UNIX operating sys...