0% found this document useful (0 votes)
45 views

Quiz On c5

The document contains solutions to 15 questions about arrays in C programming. Some key points: 1) Arrays are accessed by index, elements are stored contiguously in memory. 2) To compare two arrays, a for loop and equality check can be used on each element. 3) The size of an array cannot be changed after initialization, elements cannot be added or deleted. 4) When an array is passed as a function argument, the base address is passed, not individual elements.

Uploaded by

Cherry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Quiz On c5

The document contains solutions to 15 questions about arrays in C programming. Some key points: 1) Arrays are accessed by index, elements are stored contiguously in memory. 2) To compare two arrays, a for loop and equality check can be used on each element. 3) The size of an array cannot be changed after initialization, elements cannot be added or deleted. 4) When an array is passed as a function argument, the base address is passed, not individual elements.

Uploaded by

Cherry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

WEEK 6 ASSIGNMENT SOLUTION

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
WEEK 6 ASSIGNMENT SOLUTION

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
WEEK 6 ASSIGNMENT SOLUTION

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
WEEK 6 ASSIGNMENT SOLUTION

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.

You might also like