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

Assignment 6 July 2022 Solution

The document provides solutions to 10 multiple choice questions about C programming concepts like array initialization, passing arrays to functions, and using switch statements. Key concepts covered include declaring and initializing arrays, finding elements in arrays given an index, and the behavior of switch statements without breaks.

Uploaded by

Subanandhini S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Assignment 6 July 2022 Solution

The document provides solutions to 10 multiple choice questions about C programming concepts like array initialization, passing arrays to functions, and using switch statements. Key concepts covered include declaring and initializing arrays, finding elements in arrays given an index, and the behavior of switch statements without breaks.

Uploaded by

Subanandhini S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Week 6 Assignment Solution

1. What is the right way to initialise an array in C?


a) int arr{}= {1,2, 5,6,9}
b) int arr[5] = {1,2, 5,6,9}
c) int arr{5} = {1,2, 5,6,9}
d) int arr() = {1,2, 5,6,9}

Solution: (b)
2. An integer array of dimension 10 is declared in a C program. The memory location
of the first byte of the array is 1000. What will be the location of the 9th element of
the array? (Assume integer takes 4 bytes of memory and the element stored at 1000
is identified as 1st element)
a) 1028
b) 1032
c) 1024
d) 1036

Solution: (b) Integer takes four 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
1000+(8×4)

3. What will be the output after execution of the program?


#include <stdio.h>
int main()
{
int i, a[4]={3,1,2,4}, result;
result=a[0];
for(i=1; i<4; i++)
{
if(result<a[i])
continue;
result=a[i];
}
printf("%d", result);
return 0;
}

a) 1
b) 2
c) 3
d) 4
Solution: (a) The program finds the minimum element of an array. Hence, the output
is 1.

4. Which of the statements is/are correct?


a) An array may contain more than one element
b) All elements of array have to be of same data type
c) The size of array has to be declared upfront
d) All of the above
Week 6 Assignment Solution

Solution: (d) All of the above

5. What actually gets passed when you pass an array as an argument to a function

a) Value of elements in array


b) First element of the array
c) Base address of the array
d) Address of the last element of array

Solution: (c) The base address of the array gets passed.

6. Find the output of the following C program


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

a) 5, 4
b) 5, 5
c) 4, 4
d) 3, 4

Solution: (c)

The execution steps are as follows:


1. arr[1] = ++arr[1];  arr[1]=++2=3 so, arr={1, 3, 3, 4, 5}
2. a = arr[1]++;  a=arr[1]=3 (due to post increment). arr remains the same as
step 1.
3. arr[1] = arr[a++];  arr[1]=arr[a]=arr[3]=4. arr={1, 4, 3, 4, 5}. a is
incremented to 3+1=4 after the assignment is done.
4. Finally, a=4 and arr[1]=4 are printed

7. What will be the output?


#include <stdio.h>
int main()
{
int p;
int arr[10]={1,2,3,4,5,6,9,10};
p=(arr+1)[5];
printf("%d", p);
return 0;
}
Week 6 Assignment Solution

a) 5
b) 6
c) 9
d) 10

Solution: (c)
x[i] is equivalent to *(x + i),
so (buf + 1)[5] is *(buf + 1 + 5), i.e. buf[6]=9.

8. An array of the void data type


a) can store any data-type
b) only stores element of similar data type to first element
c) acquires the data type with the highest precision in it
d) It is not possible have an array of void data type

Solution: (d) It is not possible to have an array of void datatype.


9. What will be the output?
#include<stdio.h>
int main()
{
int n = 3;
int sum = 4;
switch(n)
{
case 2: sum = sum-2;
case 3: sum*=5;
break;
default:
sum =0;
}
printf("%d", sum);
return 0;
}

Solution: 20 ( Short answer type)


N=3 therefore switch(3) i.e. case 3 will be executed. Inside case 3 sum becomes sum=
sum*5 = 20. So, finally 20 is printed.

10. How many ‘a’ will be printed when the following code is executed?
#include <stdio.h>
int main()
{
int i = 0;
char c = 'a';
while (i < 5)
{
i++;
Week 6 Assignment Solution

switch (c)
{
case 'a':
printf("%c ", c);
break;
}
}
printf("a\n");
return 0;
}

Solution: 6 (short answer type)


Initially, i=0, which satisfies the while condition. Case ‘a’ is always executed inside
the while loop for i=1 to i=5 i.e., 5 times. Finally, another ‘a’ will be printed that is
outside of the while loop. Therefore, a total of 6 times ‘a’ is printed.

You might also like