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

Assignment - 0 Solution

The document contains a programming assignment for Modern C++ with multiple-choice questions covering various topics such as data types, enums, pointers, arrays, and structures. Each question includes code snippets, possible answers, and explanations for the correct answers. The total marks for the assignment are 30.

Uploaded by

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

Assignment - 0 Solution

The document contains a programming assignment for Modern C++ with multiple-choice questions covering various topics such as data types, enums, pointers, arrays, and structures. Each question includes code snippets, possible answers, and explanations for the correct answers. The total marks for the assignment are 30.

Uploaded by

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

Programming in Modern C++: Assignment Week 0

Total Marks : 30

Partha Pratim Das


Department of Computer Science and Engineering
Indian Institute of Technology Kharagpur 721302
[email protected], [email protected]

June 14, 2024

Question 1
Consider the below code snippet. [MCQ, Marks 2]
#include <stdio.h>

int main() {

typedef double num[3];

num array[5] = {1,2,3,4,5,6}; // LINE-1

printf("%u", sizeof(array));

printf(" %.2f", array[1][1]);

return 0;
}
What will be the output/error (sizeof(double) = 8 bytes)?
a) 40 2.00

b) 120 5.00

c) 120 2.00

d) Compilation error at LINE-1


Answer: b)
Explanation:
typedef is a keyword used in C language to assign alternative names to existing data types.
Here, num array[5] is equivalent to double array[5][3]. So, sizeof(array) = 5 * 3 * 8 =
120 bytes.
The structure of array looks like array[5][3] = {{1.00, 2.00, 3.00}, {4.00, 5.00,
6.00}, {0.00, 0.00, 0.00}, {0.00, 0.00, 0.00}, {0.00, 0.00, 0.00}}.
Hence, array[1][1] gives output 5.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;
}

What will be the output?

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;
}

What will be the output/error?

a) case 1

b) case 2

c) default block

d) Compilation error: ’x’ expected to be an integer or a character constant

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]

I -> I -> T -> K -> G -> P

What is the output of the following function when it is called with the head of the list?

void fun(struct node* start) {


if (start == NULL)
return;
printf("%c ", start->data); // Considering data is of ’char’ type
if (start->next != NULL)
fun(start->next->next);
printf("%c ", start->data);
}

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]

a) (t1 = MAXSIZE/2) and (t2 = MAXSIZE/2+1)

b) t1 + t2 = MAXSIZE

c) (t1 = MAXSIZE/2) or (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]

void fun(Queue *que) {

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.

What does the above function do?

a) Remove the last element from que

b) Reverse the elements in the que

c) Keeps the que unchanged

d) Makes que empty

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;
}

What will be the output?

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;

printf("%d,%d", *p, n);

return 0;
}

What will be the output?

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;
}

What will be the output?

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>

void teller1(char* msg) {


printf("teller1: %s\n", msg);
}
void teller2(char* msg) {
printf("teller2: %s\n", msg);
}
void teller3(char* msg) {
printf("teller3: %s\n", msg);
}

__________________________ // 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

a) void (*F PTR)(char*);

b) typedef void (*F PTR)(char*);

c) void *F PTR(char*);

d) typedef void (*f ptr)(char*) F PTR;

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;

for(ip = array + 4, i = 0; i < 5; i++)


printf("%d ", ________); // LINE-1

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};

printf("%ld ", sizeof(da));


printf("%d %d %d", da.c.a, da.c.b, da.d);

return 0;
}

What will be the output?

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--;

printf("%d %d %d", x, y, z);

return 0;
}

What will be the output?

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;

printf("%d ", ++(p+q+5));

return 0;
}

What will be the output/error?

a) 35

b) 36

c) 41

d) Compilation error: lvalue required as increment operand

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

You might also like