Sie sind auf Seite 1von 8

ESC 101: Fundamentals of Computing Major Quiz II (4 Nov 2019)

Name 50 marks
Roll No Dept. Section Page 1 of 8 B
Instructions:
1. This question paper contains a total of 3 pages (6 sides of paper). Please verify.
2. Write your name, roll number, department, and section on every side of every sheet of
this booklet
3. Write final answers neatly with a pencil/blue/black pen in the given boxes.
________________________________________________________________________

Q. 1: Write T or F in the box for True and False, respectively. (1 x 10 = 10 marks)

T
1. A struct cannot have a member of the same struct type.
F
2. A macro is solved first and the result is replaced wherever it is called.
T
3. Any recursive function can always be written in an iterative form.
F
4. A function cannot call itself.
T A static member of a function retains its value across multiple
5.
function calls.
F
6. Enums can be assigned floating point values.
F
7. A function must always return some value.
T We cannot directly access a node in a linked list without traversing
8.
the list from the beginning or from the end
T
9. malloc() will always allocate a contiguous chunk of memory.
F
10. calloc() is faster than malloc()

Q. 2.: Multiple choice, SINGLE correct. (Tick box beside correct option) (2x4=8 marks)

2.1 What will be the output of the program on the right?

a) 352 #include <stdio.h>


typedef union Student {
b) 256 char name[16]; short roll_no;
union Student* next;
c) 8
} MyStudent;
d) 320 struct marks {
MyStudent student;
union Student *head;
unsigned long course1;
};
int main() {
struct marks my_marks[8];
printf("%lu", sizeof(my_marks));
return 0;
}
Page 2 of 8

2.2 Consider a static 3-d matrix A[3][4][5]. Which of the following statements is True? (hint:
instead of thinking about individual dimensions of the array, think about the overall index)

#include <stdio.h>
a) ***(A+4) gives value 4 int main() {
Accessing A[0][1][8] will int A[3][4][5];
b) for (int i = 0; i < 3; i++)
give segmentation fault.
for (int j = 0; j < 4; j++)
c) A[2][2][5] gives value 230 for (int k = 0; k < 5; k++)
d) A[2][3][5] gives value 235 A[i][j][k] = 100*i + 10*j + k;
return 0;
}

2.3 Which of the following statements is False? The input is a positive integer.
1 #include <stdio.h>
For all inputs, global_count will be equal to 2 int global_count = 0;
a) max_count after the function execution (line 3 int max_count;
16) 4 int fun() {
5 static int count = 0;
6 global_count++;
b) For input 3, the program outputs 5, 3 7 count++;
8 if (count == max_count)
9 return count;
The program terminates for all values of the 10 fun();
c) 11 count++;
input.
12 return count;
count (Line 17) is strictly greater than 13 }
d) global_count (Line 18) for all values of 14 int main() {
input. 15 scanf(“%d”, &max_count);
16 int count = fun();
17 printf("%d, ", count);
18 printf("%d\n", global_count);
19 return 0;
20 }

2.4 Which of the following statements is True?

a) Accessing kth element is faster in linked lists as compared to arrays.

b) For a pointer ptr to a struct with a field x, *ptr.x and ptr -> x are equivalent.

c) Linked list takes more storage than arrays to store the same number of elements.

Given the head pointer of a linked list, we can modify any element in constant
d)
time.
ESC 101: Fundamentals of Computing Major Quiz II (4 Nov 2019)
Name 50 marks
Roll No Dept. Section Page 3 of 8 B
Q. 3: Multiple choice, MULTIPLE correct. B (Tick all correct options) (3 x 2 = 6 marks)

3.1. Which of the following might give Segmentation Fault on Execution?

a) printf(“%d”,printf("%d%d%c",10,1,'a'));

b) int *a = (int*)malloc(1); free(&a);

c) char *p = NULL; char *q = &p;

d) char *w = (char *)malloc(4); w = NULL;

3.2 Which of these is true about the free() function?

a) It cannot be used to free the memory allocated for a static array

b) It can be used to free the memory allocated for a malloced/calloced array

c) It only frees the memory that is a multiple of 8 bytes

d) It only frees the memory that is a multiple of 4 bytes

Q4: In the following question, you will be given incomplete code. Fill in the blanks neatly with
code so that the program ends up doing what is specified in the question. If you need to
indicate a space, leave a small gap (don’t write a dot like Prutor).(0.5+0.5+1+3= 5 marks)
(Coin Change Problem) The partially complete program in the box below is supposed to
count the number of ways we can make a change for n bucks. We are given an array S =
{S1, S2, .. , Sm} valued coins. In a certain change, the order of coins does not matter. An
example is provided below for demonstration. (Multiple solns may exist, but should be
consistent with comments)
Example:
n = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1}, {1,1,2}, {2,2}, {1,3}
So, the output is 4.
n = 10 and S = {2,5,3,6} there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5}, {5,5}
So, the output is 5.
Page 4 of 8
// A recursive function which takes 3 arguments:
// S = {S1, S2, S3, .., Sm}; m (the size); n (amount)
/* Should return the number of ways change
can be made */
int count( int S[], int m, int n )
{
// Base Case 1 (what if amount given is zero)
if (__n == 0__)
__return 1__;
// Base Case 2 (what if amount given is negative)
if (__n < 0__)
__return 0__;
// Base Case 3 (what if we don’t have any coins)
if (__m <= 0 && n >= 1__)
__return 0__;
// Think of the solution recursively
//(as a single line statement).

return count(S, m-1, n) + count(S, m, n-S[m-1]);


}

Q5: In the following question, you are given an incorrect code. Correct the code by pointing
out the line numbers that require correction (in the table below), as well as write down the
corrected line. Point out and correct all types of errors (compilation, runtime, logical).
Frivolous and unnecessary corrections may receive negative marks. (5 marks)
The program shown on the next page is supposed to calculate the average of marks for all
the students (given in the main function). A new data type has been declared to store each
student’s information.

Line Corrected
No Code
2 #define SUM(A,B) ((A) + (B))

3 #define DIV(A,B) ((A) / (B))

4 #define MUL(A,B) ((A) * (B))

5 struct student {

10 float calculate_average(float scaled_marks[])

19 struct student studs[5] = {{160177, 37, 34},


ESC 101: Fundamentals of Computing Major Quiz II (4 Nov 2019)
Name 50 marks
Roll No Dept. Section Page 5 of 8 B
1 #include <stdio.h>
2 #define SUM(A,B) (A)+(B)
3 #define DIV(A,B) (A)/(B)
4 #define MUL(A,B) (A)*(B)
5 union student {
6 int roll;
7 int m_marks;
8 int e_marks;
9 };
10 int calculate_average(int scaled_marks)
11 {
12 float sum = 0;
13 for(int i=0;i<5;i++)
14 sum += scaled_marks[i];
15 return DIV(sum, 5);
16 }
17 int main()
18 {
19 union student studs[5] = {{160177, 37, 34},
20 {160194, 31, 28},
21 {160538, 35, 31},
22 {160790, 32, 36},
23 {160822, 29, 38}};
24 float scaled_marks[5];
25 for(int i=0;i<5;i++)
26 scaled_marks[i] = MUL(SUM(studs[i].m_marks, studs[i].e_marks), 1.25);
27 printf("Average : %f\n", calculate_average(scaled_marks));
28 return 0;
29 }
Page 6 of 8

Q6: Write the output. (4+4+6+2 = 16 marks)

What will be the output of the following programs? Write the outputs according to the given
inputs in the space provided below.

INPUT OUTPUT #include<stdio.h>


int func(int x) {
4 1 if(!x)
return 0;
7 3 else
return (x&1) + func(x>>1);
11 3
}
1023 10 int main() {
int x;
scanf("%d", &x);
printf("%d", func(x));
return 0;
}

INPUT OUTPUT #include<stdio.h>


#define N 3
123 147
456 258 int main() {
789 369 int a[N][N], b[N][N];
for(int i = 0; i < N; i++)
1 -2 3 1 2 -3 for(int j = 0; j < N; j++) {
2 5 -9 -2 5 9 scanf("%d", &a[i][j]);
-3 9 8 3 -9 8 b[i][j] = a[i][j];
}
Hint/note: The code on the right will compile b[0][3] = a[0][1]; b[0][6] =
and run fine even with negative or out-of-range a[0][2];
indices in the code. b[1][-2] = a[1][0]; b[1][4] =
a[1][2];
b[2][-4] = a[2][0]; b[2][-1] =
a[2][1];

for(int i = 0; i < N; i++) {


for(int j = 0; j < N; j++)
printf("%d ", b[i][j]);
printf("\n");
}
return 0;
}
ESC 101: Fundamentals of Computing Major Quiz II (4 Nov 2019)
Name 50 marks
Roll No Dept. Section Page 7 of 8 B
INPUT OUTPUT #include<stdio.h>
#include<stdlib.h>
75 6712345 struct Node {
7654321 int data;
struct Node* next;
53 21543
12345 };
typedef struct Node list;
list *head,*tail;
void push(int new_data) {
list* new_node =
(list*)malloc(sizeof(list));
new_node -> data = new_data;
new_node -> next = head;
head = new_node;
}
int main() {
int n, k, x;
scanf("%d %d", &n, &k);
for(int i = 0; i < n; i++){
scanf("%d", &x);
push(x);
if(i == 0)
tail = head;
}
tail->next = head;
while(k--){
head = head->next;
}
while(n--){
printf("%d ", head -> data);

head = head -> next;


}
return 0;
}
Page 8 of 8

INPUT OUTPUT #include<stdio.h>


int k;
6 6 int* change(){
18 static int a=0;
a+=k;
10 10 printf("%d\n",a);
30 return &a;
}
int main() {
scanf(“%d”, &k);
int *x=change();
*x=*x+k;
change();
}

SPACE FOR ROUGH WORK

Das könnte Ihnen auch gefallen