Beruflich Dokumente
Kultur Dokumente
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.
________________________________________________________________________
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.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 }
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)
a) printf(“%d”,printf("%d%d%c",10,1,'a'));
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).
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))
5 struct student {
What will be the output of the following programs? Write the outputs according to the given
inputs in the space provided below.