Assignment - 0 Solution
Assignment - 0 Solution
Total Marks : 30
Question 1
Consider the below code snippet. [MCQ, Marks 2]
#include <stdio.h>
int main() {
printf("%u", sizeof(array));
return 0;
}
What will be the output/error (sizeof(double) = 8 bytes)?
a) 40 2.00
b) 120 5.00
c) 120 2.00
Note: Default initialization of the array element is 0.00 though it varies from compiler to
compiler.
1
Question 2
Consider the following code segment. [MCQ, Marks 2]
#include <stdio.h>
enum Covid_prevention {
Sanitizer = 1,
Wear_mask = 2,
Soc_distance = 4
};
int main() {
int myCovidPrevention = Wear_mask | Soc_distance;
printf("%d", myCovidPrevention);
return 0;
}
a) 2
b) 4
c) 6
d) 8
Answer: c)
Explanation:
The enum value Wear mask and Soc distance are assigned with a bitwise OR (The pipe
symbol, |) operator to the integer variable myCovidPrevention. So value of Wear mask |
Soc distance = 2 | 4 = 000010 | 000100 = 000110 = 6.
Note that, int is of 4 bytes though we are considering the right most six bits for calculation
and ignore all zeros in the left.
2
Question 3
Consider the below program. [MCQ, Marks 2]
#include <stdio.h>
int main() {
int x = 1;
switch(x)
{
case x:
printf("case 1 ");
break;
case x + 1;
printf("case 2 ");
break;
default:
printf("default block");
break;
}
return 0;
}
a) case 1
b) case 2
c) default block
Answer: d)
Explanation:
The case statement accepts only an int or char constant expression. As the case statement
is having variable i.e. not constant expression, the program will give compilation error.
3
Question 4
Consider the following linked list: [MCQ, Marks 2]
What is the output of the following function when it is called with the head of the list?
a) I T G I G
b) I T G G
c) I T G G T I
d) I T G I T G
Answer: c)
Explanation:
fun() prints alternate nodes of the given Linked List, first from head to end, and then from
end to head. If the Linked List has an even number of nodes, then skips the last node.
4
Question 5
A single array A[1..MAXSIZE] is used to implement two stacks. The two stacks grow from
opposite ends of the array. Variables t1 and t2 (tl < t2) point to the location of the topmost
element in each of the stacks. If the space is to be used efficiently, the condition for stack full
is: [MCQ, Marks 2]
b) t1 + t2 = MAXSIZE
d) t1 = t2 - 1
Answer: d)
Explanation:
If we are to use space efficiently, then the size of the any stack can be more than MAXSIZE/2.
Both stacks will grow from both ends and if any of the stack top reaches near to the other top
then the stacks are full. So the condition will be t1 = t2 -1 (given that t1 ≤ t2)
5
Question 6
Consider the following code segment. [MCQ, Marks 2]
Stack Stk;
while (!isEmpty(que)) {
push(&Stk, deQueue(que));
}
while (!isEmpty(&Stk)) {
enQueue(que, pop(&Stk));
}
}
where push and pop are two standard functionalities of stack data structure. Similarly, enQueue
and deQueue are two standard functionalities of queue data structure to insert and delete the
items respectively. And isEmpty checks if the stack or the queue is empty or not.
Answer: b)
Explanation:
The function takes a queue que as an argument. It dequeues all items of que and pushes them
to a stack Stk. Then pops all items of Stk and enqueues the items back to que. Since stack is
LIFO order, all items of queue are reversed.
6
Question 7
Consider the below code segment. [MCQ, Marks 2]
#include <stdio.h>
int main() {
int x = 1;
int y;
y = (x=x+5, x*5);
printf("%d",y);
return 0;
}
a) 25
b) 30
c) 6
d) 5
Answer: b)
Explanation:
Comma operator evaluates multiple expressions from left to right. The value of rightmost
(x*5) expression is assigned to y. Here first x=x+5 will be evaluated and make the value of x
as 6. Then second expression x*5 will be evaluated and 6*5=30 will be assigned to y.
7
Question 8
Consider a three-dimensional array arr[5][10][20]. An element from this array can be
represented as arr[i][j][k] where 0 ≤ i ≤ 4, 0 ≤ j ≤ 9 and 0 ≤ k ≤ 19. How can you write
arr[2][6][10] in an equivalent pointer expression? [MSQ, Marks 2]
a) ((**(*a+2)+6)+10)
b) (**(*(a+2)+6)+10)
c) (*(**(a+2)+6)+10)
d) *(*(*(a+2)+6)+10)
Answer: d)
Explanation:
C represents an array as row-major. As a multidimensional array is stored in one dimensional
fashion in memory, the innermost index is the slowest to change. Hence, the equivalent pointer
which points to the element arr[i][j][k] is *(*(*(a+i)+j)+k). Hence, the correct option
is d).
8
Question 9
Consider the code segment below. [MCQ, Marks 2]
#include <stdio.h>
int main() {
int *p, n = 5;
p = &n;
*p += 1;
return 0;
}
a) 5,5
b) 5,6
c) 6,5
d) 6,6
Answer: d)
Explanation:
The address of variable n is assigned to the pointer variable p. So, whatever changes are done
in pointer variable will be reflected to n. The value of *p is incremented by 1. So, the value of
n will also be incremented by 1. Hence, output will be 6,6.
9
Question 10
Consider the code segment below. [MCQ, Marks 2]
#include <stdio.h>
struct result {
char subject[20];
int mark;
};
int main() {
struct result r[] = {
{"Maths",95},
{"Science",93},
{"English",80}
};
printf("%s ", r[1].subject);
printf("%d", (*(r+2)).mark);
return 0;
}
a) Science 80
b) Science 93
c) English 80
d) English 93
Answer: a)
Explanation:
r is an array variable of structure result type. The first print statement will print subject
value of the second array item. Similarly, the second print statement will print mark value of
the third array element. Hence, the correct option is a).
10
Question 11
Consider the code segment below. [MCQ, Marks 2]
#include <stdio.h>
__________________________ // LINE-1
void caller(char *msg, F_PTR fp) {
fp(msg);
}
int main() {
caller("Hello", &teller1);
caller("Hi", &teller2);
caller("Good Morning", &teller3);
return 0;
}
Identify the correct option to fill in the blank at LINE-1, such that the output is:
teller1: Hello
teller2: Hi
teller3: Good Morning
c) void *F PTR(char*);
Answer: b)
Explanation:
Since F PTR is the name of the function pointer, we need to define it with typedef. Thus, the
correct option is b).
11
Question 12
Consider the code segment below. [MSQ, Marks 2]
#include <stdio.h>
int main() {
int array[] = {10, 20, 30, 40, 50};
int *ip, i;
return 0;
}
Identify the correct option/s to fill in the blank at LINE-1, such that the output is:
50 40 30 20 10
a) -i[ip]
b) ip[-i]
c) -ip[i]
d) (-i)[ip]
Answer: b), d)
Explanation:
-i[ip] is equivalent to -*(i + ip), which prints -50 followed by 4 garbage values. So it is
wrong option.
ip[-i] is equivalent to *(ip - i), which prints 50 40 30 20 10.
-ip[i] is equivalent to -*(i + ip), which prints -50 followed by 4 garbage values. So it is
wrong option.
(-i)[ip] is equivalent to *(-i + ip), which prints 50 40 30 20 10.
12
Question 13
Consider the code segment below. [MCQ, Marks 2]
Assume that the sizeof(int) = 4
#include <stdio.h>
union uData {
int a;
int b;
};
struct sData {
union uData c;
int d;
};
int main() {
struct sData da = {10, 20};
return 0;
}
a) 8 10 10 20
b) 16 10 20 <garbage-value>
c) 16 10 <garbage-value> 20
d) 8 10 <garbage-value> 20
Answer: a)
Explanation:
sizeof(union uData) = 4, since the size of a union is same as the size of the largest element
of the union
Thus, the sizeof(da) = sizeof(struct sData) = sizeof(union uData) + sizeof(d) =
sizeof(union uData) + sizeof(int) = 8.
10 is intialized to the union data member. Hence, both union data member will hold 10 when
accessed. Hence, da.c.a = 10, da.c.b = 10 and da.d = 20.
13
Question 14
Consider the code segment below. [MCQ, Marks 2]
#include <stdio.h>
int main() {
int x = 8, y, z;
y = --x;
z = x--;
return 0;
}
a) 8 7 7
b) 8 7 6
c) 6 7 7
d) 6 7 6
Answer: c)
Explanation:
The first expression is y = --x; so, x becomes 7 because of pre-decrement operator and value
of y = 7. in the next step, the post decrement operator is applied on x. So, current value of
x is assigned to z (z = 7) then x decremented by 1 (x = 6). Hence, final values will be x=6,
y=7 and z=7.
14
Question 15
Consider the code segment below. [MCQ, Marks 2]
#include <stdio.h>
int main() {
int p = 5, q = 6;
return 0;
}
a) 35
b) 36
c) 41
Answer: d)
Explanation:
The operand of unary operator (increment / decrement) must be a variable, not a constant or
expression. In our case, unary increment operator is applied on expression p+q+5 which throws
a compilation error.
15