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

Week 12 Assignment Solution

This document contains solutions to 10 multiple choice questions about C programming concepts related to structures, pointers, arrays, and file input/output. The questions cover topics such as: what is passed when a structure variable is passed to a function, valid functions for writing strings to files, pointer arithmetic in 2D arrays, structures containing pointers to their own type, and evaluating expressions involving structure sizes.

Uploaded by

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

Week 12 Assignment Solution

This document contains solutions to 10 multiple choice questions about C programming concepts related to structures, pointers, arrays, and file input/output. The questions cover topics such as: what is passed when a structure variable is passed to a function, valid functions for writing strings to files, pointer arithmetic in 2D arrays, structures containing pointers to their own type, and evaluating expressions involving structure sizes.

Uploaded by

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

Week 12 Assignment Solution

1. Which of the following are themselves a collection of different data types?

a) String
b) Structure
c) Char
d) All of the mentioned

Solution (b) Structure is a collection of different datatypes.

2. Which of the following comments about the usage structures is true?


a) Storage class can be assigned to individual member
b) Individual members can be initialized within a structure type declaration
c) The scope of the member name is confined to the particular structure, within
which it is defined
d) None of the above

Solution: (c) The scope of the member name is confined to the particular structure, within
which it is defined

3. What is actually passed if you pass a structure variable to a function?


a) Copy of structure variable
b) Reference of structure variable
c) Starting address of structure variable
d) Ending address of structure variable
Answer: (a)
If you pass a structure variable by value without & operator, only a copy of the variable is
passed. So, changes made within that function do not reflect in the original variable.

4. Which function is used to write a string to a file?


a) fputs()
b) fprintf()
c) fwrite()
d) All of the above
Answer: d) All of the above
Explanation: All the functions fputs(), fprintf(), and fwrite() can be used to write a string to a
file, but they are used in different contexts and formats.
5. Find the output of the following program
#include<stdio.h>
int main()
{
char A[] = {'a','b','c','d','e','f','g','h'};
char *p = A;
Week 12 Assignment Solution

++p;
while(*p != 'e')
printf("%c", *p++);
return 0;
}

a) abcd
b) bcd
c) cd
d) abcdfgh
Solution: (b)
First, the pointer points to the base of A. Then it's incremented by one to point to the element
b. While p is not pointing e, the loop keeps prints the elements one after another. Therefore,
the final answer is bcd.

6. Match the following


A. Newton Method 1. Integration
B. Lagrange Polynomial 2. Root finding
C. Trapezoidal Method 3. Differential Equation
D. Runge Kutta Method 4. Interpolation

a) A-2, B-4, C-1, D-3


b) A-3, B-1, C-2, D-4
c) A-1, B-4, C-3, D-2
d) A-2, B-3, C-4, D-1

Solution: (a) Appropriate methods for the problems need to be matched.

7. What is the output of the following C code? Assume that the address of x is 2000 (in
decimal) and an integer requires four bytes of memory.
int main()
{
unsigned int x[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
printf("%u,%u, %u", x+3, *(x+3),*(x+2)+3);
return 0;
}

(a) 2036, 2036, 2036


(b) 2012, 4, 2204
(c) 2036, 10, 10
(d) 2012, 4, 6

Solution: (a)
All these points to the same element. Therefore, the same value will be printed. This is an
example of pointer arithmetic in an 2D array.

8. Can a structure contain a pointer to its own type?


Week 12 Assignment Solution

a) Yes
b) No
c) Only as an array
d) Only if the structure is anonymous

Answer: a) Yes

Explanation: A structure can contain a pointer to its own type, which is often used to create
linked data structures like linked lists.

9. What is the output of the following code snippet?

struct Point {
int x;
int y;
};
struct Point *arr[2];
struct Point p1 = {1, 2}, p2 = {3, 4};
arr[0] = &p1;
arr[1] = &p2;
printf("%d", arr[1]->y);

a) 1
b) 2
c) 3
d) 4

Answer: d) 4

Explanation: arr[1] points to p2, and arr[1]->y accesses the y member of p2, which is 4.

10. What is the output of the following C program?


#include <stdio.h>
struct p
{
int x;
char y;
};

int main()
{
struct p p1[] = {1, 90, 62, 33, 3, 34};
struct p *ptr1 = p1;
int x = (sizeof(p1) / 3);
if (x == sizeof(int) + sizeof(char))
printf("True");
else
printf("False");
return 0;
Week 12 Assignment Solution

a) True
b) False
c) No output
d) Compilation error
Solution: (b) Size of the structure is the maximum size of the variable inside structure. Thus,
the size of each element of structure p is 4 bytes (in gcc compiler, it can vary based on
compiler). Thus, sizeof(p1) is 6*4=24. x will be 24/3=8. In the next step,
sizeof(int)+sizeof(char) is 5 which is not equal to x. Hence, false will be printed.

You might also like