You are on page 1of 45

Problem Solving through Programming in C

Week 1 Assignment Solution

1. What type of device is computer webcam?


a) Memory
b) Output
c) Storage
d) Input

Solution: (d) Input

2. Set of instructions to be provided to an electronic machine to perform a task is called


a) Programming
b) Processing
c) Computing
d) Compiling

Solution: (a) Programming is the process of creating a set of instructions that tell a computer how to perform a
task.

3. Which of the following language does the computer understand?


a) Computer understands only C Language
b) Computer understands only Assembly Language
c) Computer understands only Binary Language
d) Computer understands only BASIC

Answer: (c) Computer understands only Binary Language


The Computer understands only binary language which is written in the form of 0s & 1s. A
computer can understand assembly language but an assembler is required which convert the
assembly language to binary language. Similarly, for understanding high level languages,
compilers/interpreters are required.

4. Which of the following is known as the language made up of binary-coded instructions?


a) High level
b) BASIC
c) C
d) Machine
Answer: (d) Machine
The language made up of binary-coded instructions built into the hardware of a particular computer
and used directly by the computer is machine language.

5. Algorithm is-
a) A process or set of rules to be followed in calculations or other problem-solving operations,
especially by a human.
b) A process or set of rules to be followed to solve numerical problems only.
c) A process or set of rules to be followed in calculations or other problem-solving operations,
especially by a computer.
d) A process or set of rules to be followed in to solve logical problems only.
Problem Solving through Programming in C

Week 1 Assignment Solution

Solution: (c) A process or set of rules to be followed in calculations or other problem-solving operations,
especially by a computer

6. A 2D diagram to represent the steps to be followed to solve a problem is known as


a) Flow-chart
b) Pseudo-code
c) Both (a) and (b)
d) None of these

Solution: (a) A flow-chart is a representation of an algorithm using diagrams.

7. Which one of the following statement is the most appropriate?


a) Pseudo code is basically a diagrammatic representation of the algorithm. Whereas in
flowchart normal English language is translated into the programming languages to be
worked on.
b) Flowchart is diagrammatic representation of the algorithm. Pseudo code is just another
name of algorithm.
c) Pseudo code is another name of programming. Whereas in flowchart is diagrammatic
representation of algorithm.
d) Flowchart is basically a diagrammatic representation of the algorithm. Whereas in pseudo
code normal English language is translated into the programming languages to be worked
on.

Solution: (d) Flowchart is basically a diagrammatic representation of the algorithm. Whereas in pseudo
code normal English language is translated into the programming languages to be worked on.

8. The input N from the user is 6. The output of the following algorithm is
Problem Solving through Programming in C

Week 1 Assignment Solution

a) 21
b) 720
c) 1
d) 1024

Solution: (c) The condition “i>=N” fails in the first iteration because i=1 and N=6. Thus, the execution
jumps directly to the print command. The initial assigned value of X will be printed which is 1.

9. What will be the output of the algorithm given below?


Problem Solving through Programming in C

Week 1 Assignment Solution

a) 51
b) 52
c) 50
d) Compilation error

Solution: (d) The assignment X=Y is incorrect. “Equals to” is a left to right assignment. The variable Y is
not declared before assignment. This the compiler will throw error at this step.

10. The section of the CPU that selects, interprets and sees to the execution of program instructions

a) Memory
b) Register Unit
c) Control Unit
d) ALU

Solution: (c) Control unit of the computer helps in maintaining sequence of steps and execute the program
Problem Solving through Programming in C

Week 2 Assignment Solution

1. Which of the following is not a C variable?


a) Count123
b) Count_123
c) Count@123
d) X_123_Count

Solution: (c) Only alphanumeric characters and few special characters like ‘_’ are allowed in
variable name is C. The special character @ is not allowed.

2. A function
a) is a block of statements to perform some specific task
b) is a fundamental modular unit to perform some task
c) has a name and can be used multiple times
d) All the above options are true

Solution: (d) All the above options are true

3. The execution of any C program is

a) Sequential
b) Parallel
c) Multi-threading
d) None of these

Solution: (a) The execution of the C program is sequential.


4. Syntax error occurs when
a) The rules of grammar of the programming language is violated
b) The statements in the program have no meaning
c) The program gives wrong or undesired output
d) Some illegal operation (e.g. divide by zero) is performed
Solution: (a) The rules of grammar of the programming language is violated
5. If integer needs two bytes of storage, then the minimum value of a signed integer in C
would be

a) -65535
b) 0
𝑐) -32,767
d) -32,768

Solution: (d) The first bit is used to indicate whether it is signed or unsigned integer. So it
will be −215 i.e. -32,768
Problem Solving through Programming in C

Week 2 Assignment Solution

6. What will be the output of the program given below?


#include <stdio.h>
int main()
{
a=9;
printf("%d", a);
return 0;
}

a) 9
b) 0
c) 1001
d) Compilation Error

Solution: (d) Compilation Error

variable ‘a’ is not declared therefore a compilation error.

7. What is the output?


#include<stdio.h>
#define fun(x) (x*x)
int main()
{
float i;
i = 64.0/fun(2);
printf("%.2f", i);
return 0;
}

a) 8.00
b) 4.00
c) 0.00
d) 16.00
Solution: (d) The pre-processing replaces fun(2) with (2*2). Thus fun(2)=4, so,
i=64.0/4=16.00

8. The following C program swaps the value of two numbers without using any third
variable. What will be the correct option to fill up the blank?

#include <stdio.h>
Problem Solving through Programming in C

Week 2 Assignment Solution

int main()
{
int a=2, b=3;
printf("The values before swapping a = %d, b=%d", a, b);

____________________________________

printf("The values after swapping a = %d, b=%d", a, b);


return 0;
}

a) a=a-b; b=a-b; a=a+b;


b) a=a%b; b=a+b; a=a/b;
c) a=a+b; b=a-b; a=a-b;
d) None of the above
Solution: (c) a=a+b; b=a-b; a=a-b;

9. What will be the output?


#include <stdio.h>
int main() {
int x = 1, y = 3;
int t = x;
x = y;
y = t;
printf("%d %d", x, y);
return 0;
}

a) 1 3
b) 3 1
c) 1 1
d) 3 3

Solution: (b) 3 1
Here the program is swapping the values of the variables x and y. A temporary variable t is
used for the swapping purpose.
10. When executed the following code will print _______.
#include <stdio.h>
int main() {
int sum = 3 + 6 / 2 + 6 * 2;
printf("%d", sum);
return 0;
}

Solution: 18 (short answer type)


Apply the BODMAS rule to evaluate the expression.
Week 3: Assignment Solution

1. Which of the following statement is correct?


a) Operator precedence determines which operator is performed first in an expression with
more than one operator with different precedence. Associativity is used when two
operators of same precedence appear in an expression
b) Operator associativity determines which operator is performed first in an expression
with more than one operator with different associativity. Precedence is used when two
operators of same precedence appear in an expression
c) Operator precedence and associativity are same.
d) None of the above

Solution: (a) Operator precedence determines which operator is performed first in an expression with
more than one operator with different precedence, whereas associativity is used when two operators of
same precedence appear in an expression

2. Find the output of the following C code


#include<stdio.h>
int main()
{
int a=50, b=20, c=6, d=3, result;
result=a+a*-b/c%d+c*d;
printf(“%d”, result);
return 0;
}

a) 67
b) -36
c) 66
d) -37

Solution: (a) Following the precedence rule, we can conclude that the operation steps are
 Result=50+50*- 20/6%3+6*3
 Result=50-1000/6%3+6*3
 Result=50-166%3+6*3
 Result=50-1+6*3
 Result=50-1+18
 Result=49+18
 Result=67

3. What is the output of the following C code?


#include <stdio.h>
int main()
{
int h = 8;
int b = 4 * 6 + 3 * 4 < h*5 ?4 : 3;
printf("%d\n", b);
return 0;
}
Week 3: Assignment Solution

a) 0
b) 3
c) 4
d) Compilation error

Solution: (c) ‘?:’ is Conditional Expression. If Condition is true ? then value X : otherwise value Y.
After simplifying the expression, we get 36<40?4:3. The condition in LHS of ? is true. Thus 4 will be
stored in b.

4. Find the output of the following C code


#include <stdio.h>
int main()
{
int x=1;
if ((3>5) || (2!=3))
printf("IITKGP\n");
else if (x&=0)
printf("IITD\n");
else
printf("IITM\n");
return 0;
}

a) IITKGP
b) IITD and IITM
c) IITKGP and IITM
d) IITM
Solution: (a) Only the first if condition will be executed as the condition inside the if statement is true.
Thus IITKGP will be printed.

5. What will be the output?


#include <stdio.h>
int main()
{
if ((-10 && 10) || (20 && -20))
printf("Condition is true.");
else
printf("Condition is false.");
return 0;
}

a) Condition is true
b) Condition is false
Week 3: Assignment Solution

c) Error
d) No output possible
Solution: (a) Condition is true

Any non-zero value is treated as true for condition. Consider the expressions: if( (-10 &&
10)||(20 && -20) )
=if( (1) || (1) )
=if(1)

6. What is the output of the following program?


#include<stdio.h>
int main()
{
int i;
if(i=0,2,3)
printf("NPTEL ");
else
printf("Programming on C ");
printf("%d\n", i);
return 0;
}

a) Programming on C 0
b) NPTEL 0
c) NPTEL 3
d) Compilation error

Solution: (b) At first zero will assign in ‘i’ then comma operator returns the last value which is 3 and
condition becomes true.

7. What is the output of the C program given below

#include <stdio.h>
int main()
{
int x = 0;
if (x++)
printf("true\n");
else if (x == 1)
printf("false\n");
return 0;
}

a) true
b) false
c) Compiler dependent
d) Compiler error
Week 3: Assignment Solution

Solution: (b) ++ is a post increment operator. In x++, first 0 will be assigned and then x will be
incremented by 1. Thus the next else if condition will be evaluated and false will be printed.

8. What will be the output?


#include<stdio.h>
int main()
{
int x;
x= 10==20!=30;
printf("%d", x);
return 0;
}

a) 0
b) 1
c) 10
d) 30
Solution: (b) 1

Here, operators == and != have same precedence. The associativity of both == and != is left to right,
i.e, the expression on the left is executed first and moves towards the right.

Thus, the expression above is equivalent to :

((10 == 20) != 30)

i.e, (10 == 20) executes first resulting into 0 (false)

then, (0 != 30) executes resulting into 1 (true)

9. What will be the output?

#include <stdio.h>
int main()
{
int a = 100, b = 200, c = 300;
if (c > b > a)
printf("TRUE");
else
printf("FALSE");
return 0;
}
Week 3: Assignment Solution

a) TRUE
b) FALSE
c) Syntax Error
d) Compilation Error
Solution: (b) FALSE :: (c > b > a) is treated as ((c > b) > a), associativity of '>'
is left to right. Therefore the value becomes ((300 > 200) > 100) which becomes (1 > 100) thus FALSE

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


#include <stdio.h>
int main()
{
int y = 10;
int z = y +(y == 10);
printf("%d\n", z);
return 0;
}

a)10
b) 11
c)20
d) Compiler error

Solution: (b) ‘==’ is a relational operator. It returns 1 if the condition is true or 0 if the condition is
false. Thus y==10 will return true (which is 1) and 10+1 will be 11. Hence, the value of z will be 11.
Week 4 Assignment Solution

1. What is the purpose of the "if-else" statement in C?


a) To execute a block of code repeatedly.
b) To declare variables and constants.
c) To test a condition and execute different code based on the result.
d) To perform mathematical calculations.

Solution: (c) To test a condition and execute different code based on the result.

2. What is the correct syntax for an "if-else" statement in C?


a) if condition { statement1; statement2; } else { statement3; }
b) if condition then { statement1; } else { statement2; }
c) if (condition) { statement1; } else { statement2; }
d) if condition then statement1; else statement2;

Solution: (c) if (condition) { statement1; } else { statement2; }

3. Which of the following is true about nested "if-else" statements?


a) They are not allowed in C.
b) The "else" part is mandatory for every "if" statement.
c) They allow you to test multiple conditions and execute different blocks of code
based on the results.
d) Nested "if-else" statements are only allowed up to two levels deep.

Solution: (c) They allow you to test multiple conditions and execute different
blocks of code based on the results.

4. What happens if there is no "else" part in an "if-else" statement?


a) The program will not compile.
b) The program will crash at runtime.
c) If the condition is true, nothing happens; if the condition is false, the program
crashes.
d) If the condition is true, the program executes the code inside the "if" block; if
the condition is false, nothing happens.

Solution: (d) If the condition is true, the program executes the code inside the "if"
block; if the condition is false, nothing happens.

5. Which of the following operators can be used to combine multiple conditions in an


"if" statement?
a) && (logical AND)
b) || (logical OR)
c) ! (logical NOT)
d) All of the above

Solution: (d) All of the above


Week 4 Assignment Solution

6. Compute the printed value of i of the C program given below


#include<stdio.h>
int main()
{
int i=2;
i=i++;
printf("%d", i);
return 0;
}

a) 2
b) 3
c) 4
d) Compiler error
Solution: (a) i++ is a post-increment operator. It assigns first and then increments the
operator by one. Therefore, i value after the assignment remains 2

7. If multiple conditions are used in a single “if” statement then the testing of those
conditions are done
a) From Right to Left
b) From Left to right
c) Randomly
d) None of the above

Solution: (b) Multiple conditions are tested from Left to the right.
8. What is the purpose of the given program? n is the input number given by the
user.
#include <stdio.h>
int main()
{
int n, x = 0, y;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
y = n % 10;
x = x - y;
n = n/10;
}
printf("Output is = %d", x);
return 0;
}
a) Sum of the digits of a number
b) The negative sum of the digits of a number
c) The reverse of a number
d) The same number is printed
Solution: (b) Negative sum of the digits of a number
Week 4 Assignment Solution

Please take a number and follow the operation step-by-step. You will be able to find the
negative sum number as output.

9. What will be the value of a, b, c after the execution of the followings

int a=5, b=7, c=111;


c /= ++a * b--;

a) a=5, b=6,c=2;
b) a=6, b=7,c=1;
c) a=6, b=6,c=2;
d) a=5, b=7,c=1;

Solution: (c) ++a * b-- is computed as (a=a+1)*(b) 🡪 (6)*(7)=42


c/=42 🡪 c=c/42 🡪 c=111/42=2 (as c is integer)
Hence the right answer is a=6, b=6 and c=2

10. What will be the output of the following program?


#include <stdio.h>
int main()
{
int x = 1;
switch (x)
{
case 1: printf("Choice is 1 \n");
default: printf("Choice other than 1 \n");
}
return 0;
}

a) Choice is 1
b) Choice other than 1
c) Both (a) and (b)
d) Syntax error
Solution: (c)
Since the “break;” statement is not used after the print statement, it will execute the default
instruction as well.
Week 5 Assignment Solution

1. The statement that transfers control to the beginning of the loop is called
a) break
b) continue
c) goto
d) None of the above
Solution: (b) continue

2. In C three way transfer of control is possible using


a) Unary operator
b) Logical operator
c) Ternary operator
d) None
Solution: (c) Ternary operator
3. What is the output of the following code?
#include <stdio.h>
int main()
{
int i=0;
do
{
printf("while vs do-while\n");
}while(i==0);
printf("Out of loop");
return 0;
}

a) ‘while vs do-while’ once


b) ‘Out of loop’ infinite times
c) Both ‘while vs do-while’ and ‘Out of loop’ once
d) ‘while vs do-while’ infinite times
Solution: (d) As the condition inside the while statement is always true, the loop will be executed infinite times and
the statement inside the loop will be printed infinite number of times.

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


#include <stdio.h>
int main()
{
int a = 0, i;
for (i = 0; i < 5; i+=0.5)
{
a++;
continue;
}
printf("%d", a);
return 0;
}

a) 5
b) 10
c) No output
d) Compilation error
Week 5 Assignment Solution

Solution: (c) As i is initialized as an integer variable, integer value of i after the operation (i=i+0.5) will be zero.
Thus, the loop will never be ended and the control will not come to the printf statement at all. So, nothing will be
printed.

5. What is the output of the following C code?


#include <stdio.h>
int main()
{
int a = 1;
if (a--)
printf("True\n");
if (++a)
printf("False\n");
return 0;
}

a) True
b) False
c) Both ‘True’ and ‘False’
d) Compilation error

Solution: (c) ‘a--’ post-increment the value of a. Thus, the if statement is executed as the value of a is
considered as 1 which is true. ‘++a’ pre-increment the value of a. Thus, the decremented value of a (which
is 0) is incremented first and then assigned. So, both the if statements are executed ad correspondingly both
True and False will be printed.

6. What will be the output?


#include <stdio.h>
int main()
{
int x=1;
do
{
continue;
printf("%d", x);
x++;
break;
}while(x<=10);
printf("\nAfter loop x=%d", x);
printf("\n");
return 0;
}

a) After loop x=1


b) 1
After loop x=2
c) 1 2 3 4 5 6 7 8 9 10
d) No output

Solution: (d) No output


do while is an exit controlled loop, here loop body executed first, then condition will be checked. However
due to continue statement, the lines after the continue statement are skipped. Hence nothing will be printed.
Week 5 Assignment Solution

7. What will be the output?


#include <stdio.h>
int main()
{
float k = 0;
for (k = 0.5; k < 3; k++)
printf("I love C\n");
return 0;
}

a) Error
b) I love C - will be printed 3 times
c) I love C - will be printed 6 times
d) I love C - will be printed 5 times
Solution: (b) I love C will be printed 3 times

8. What will be the output?


#include <stdio.h>
int main()
{
int x;
x = 4 < 8 ? 5 != 1 < 5 == 0 ? 1: 2: 3;
printf("%d", x);
return 0;
}

a) 1
b) 2
c) 3
d) Error
Solution: (c) 3
exp1? exp2: exp3
(4 < 8) ? (5!= 1 < 5 == 0)? 1: 2 : 3;

exp1 is true, so exp2 will be evaluated, which is true so 2 will be printed.

9. The following program is used to find the reverse of a number using C language. Find the missing
condition inside while statement (indicated as ‘xxxx’).
#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;

printf("Enter an integer: ");


scanf("%d", &n);

while(xxxx)
Week 5 Assignment Solution

{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}

printf("Reversed Number = %d", reversedNumber);

return 0;
}

a) n!=0
b) n==0
c) n%10==0
d) n/10==0

Solution: (a) The loop should be continued till the value of n becomes zero. Thus, the right option is n!=0.

10. Compute the printed value of i & j of the C program given below
#include <stdio.h>
int main()
{
int i = 0, j = 15;
while (i<8, j >9)
{
i++;
j--;
}
printf("%d, %d\n", i, j);
return 0;
}

a) 8,10
b) 8,9
c) 6, 9
d) 7, 10
Solution: (c) The while condition checks the last condition (i.e. j>9) and till the condition is satisfied the block inside
the loop is executed. Thus the loop is run for 6 times. i will be incremented by 6 and j will be decremented by 6. The
final values of i and j will be i=6 and j=9.
Week 6 Assignment Solution

1. What is an array in C?
a) A collection of similar data elements with the same data type.
b) A built-in function that performs mathematical calculations.
c) A keyword used for declaring variables.
d) A data type used to store characters only.

Solution: a) A collection of similar data elements with the same data type.

Explanation: An array in C is a collection of elements of the same data type stored in


contiguous memory locations. Each element in the array can be accessed using an
index, which starts from 0 and goes up to (array_size - 1).

2. What is the index of the first element in an array?


a) 0
b) 1
c) -1
d) The index can vary depending on the array size.

Solution: a) 0

Explanation: In C, array indices start from 0. Therefore, the index of the first element in an
array is 0, the second element's index is 1, and so on.

3. Which loop is commonly used to iterate through all elements of an array in C?


a) for loop
b) while loop
c) do-while loop
d) switch loop

Solution: a) for loop

Explanation: The "for" loop is commonly used to iterate through all elements of an array in C.
It allows precise control over the loop variable and is well-suited for iterating over a
range of elements, as required in array traversal.

4. An integer array of dimension 15 is declared in a C program. The memory location of


the first byte of the array is 2000. What will be the location of the 13th element of the
array? Assume int takes 2 bytes of memory.
a) 2013
b) 2024
c) 2026
d) 2030
Solution: (b) Integer takes two bytes of memory. As the memory assignment to the elements is
consecutive and the index starts from 0, the 13th element will be located at 2000+(12×2)
=2024.

5. How can you find the sum of all elements in a 1D array "arr" with 5 elements using
loop in C?
a) sum = arr[0] + arr[1] + arr[2] + arr[3] + arr[4];
Week 6 Assignment Solution

b) sum = arr[5];
c) for (int i = 0; i <= 5; i++) { sum += arr[i]; }
d) for (int i = 0; i < 5; i++) { sum += arr[i]; }

Solution: d) for (int i = 0; i < 5; i++) { sum += arr[i]; }

Explanation: To find the sum of all elements in a 1D array "arr" with 5 elements, you can use
a "for" loop to iterate through the array and add each element to the "sum" variable. The
code provided correctly calculates the sum of all elements.

6. What is the output of the following code?

#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int i = 0;
while (i < 5) {
printf("%d ", arr[i]);
i += 2;}
return 0;
}

a) 135
b) 12345
c) 123
d) 14

Solution: a) 1 3 5

Explanation: The provided code uses a "while" loop to print elements of the array "arr" with
an increment of 2 for the loop counter "i." The loop starts at index 0 and prints elements
at indices 0, 2, and 4. The output will be "1 3 5."

7. What will be the output?


#include <stdio.h>
int main()
{
int arr[]={1,2,3,4,5,6};
int i,j,k;
j=++arr[2];
k=arr[1]++;
i=arr[j++];
printf("i=%d, j=%d, k=%d", i, j, k);
return 0;
}

a) i=5, j=5, k=2


b) i=6, j=5, k=3
Week 6 Assignment Solution

c) i=6, j=4, k=2


d) i=5, j=4, k=2

Solution: (a) k=arr[1]++ due to post increment operation, assignment is done first. so it actually
becomes k=arr[1]=2. j=++arr[2]=++3=4. i=arr[j++]=arr[4++]=arr[4]=5 (as its post
increment hence assignment is done first). Due to post increment in i=arr[j++], value of
j is also incremented and finally becomes 5. So, finally i=5, j=5, k=2.

8. What will be the output after execution of the program?


#include <stdio.h>
int main()
{
int i, a[4]={3,1,2,4}, result;
result=a[0];
for(i=1; i<4; i++)
{
if(result>a[i])
continue;
result=a[i];
}
printf("%d", result);
return 0;
}

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

Solution: (d) The program finds the maximum element of an array. Hence, the output is 4.

9. What will be the output?


#include<stdio.h>
int main()
{
int n = 2;
int sum = 5;
switch(n)
{
case 2: sum = sum-3;
case 3: sum*=4;
break;
default:
sum =0;

}
printf("%d", sum);
return 0;
}
Week 6 Assignment Solution

Solution: 8 ( Short answer type)


n=2 therefore switch(2) i.e. case 2 will be executed. Inside case 2, sum becomes sum-3 = 5-2
= 2. As there is no break statement after case 2, therefore case 3 is also executed. Inside
case 3, sum becomes sum*4= 2*4=8. After that the execution finds a break statement
and comes out of the switch. So, finally 8 is printed.

10. Find the output of the following C program


#include<stdio.h>
int main()
{
int a;
int arr[5] = {1, 2, 3, 4, 5};
arr[1] = ++arr[1];
a = arr[1]++;
arr[1] = arr[a++];
printf("%d, %d", a, arr[1]);
return 0;
}

a) 5, 4
b) 5, 5
c) 4, 4
d) 3, 4

Solution: (c)
The execution steps are as follows:
1. arr[1] = ++arr[1];  arr[1]=++2=3 so, arr={1, 3, 3, 4, 5}
2. a = arr[1]++;  a=arr[1]=3 (due to post increment). arr remains the same as step 1.
3. arr[1] = arr[a++];  arr[1]=arr[a]=arr[3]=4. arr={1, 4, 3, 4, 5}. a is incremented to 3+1=4
after the assignment is done.
4. Finally, a=4 and arr[1]=4 are printed
Week 7 Assignment Solution

1. Which of the following statements are correct?


1) A string is a collection of characters terminated by '\0'.
2) The format specifier %s is used to print a string.
3) The length of the string can be obtained by strlen().
4) strcon() function is used to join two strings.

a) 1,2
b) 1,2,3
c) 2,4
d) 1,3
Solution: (b) Clearly, we know first three statements are correct, but fourth statement is
wrong because strcat() is used to join two strings in C.

2. The right method of initializing a 2D array is


a) int abc[2][2] = {1, 2, 3 ,4 }
b) int abc[ ][ ] = {1, 2, 3 ,4 }
c) int abc[2][ ] = {1, 2, 3 ,4 }
d) all of the above
Solution: (a) The valid initialization is option (a). Next two are invalid declaration because
the second dimension must be specified.

3. Array passed as an argument to a function is interpreted as


a) Address of all the elements in an array
b) Value of the first element of the array
c) Address of the first element of the array
d) Number of element of the array

Solution: (c) Address of the first element of the array or the base address of the array.

4. What will be the output?


#include <stdio.h>
int main()
{
int disp[3][4] = {{5, 6, 8, 2}, {4, 5, 3, 7}, {1,10,13,15}};
printf("%d\n", disp[2][1]);
return 0;
}

Solution: 10 (short answer type)

5. Find the output of the following C program.


#include <stdio.h>
int main()
{
char a[10][8] = {"hi", "hello", "fellows"};
printf("%s", a[2]);
return 0;
}
Week 7 Assignment Solution

a) fellows
b) h
c) fello
d) Compiler error
Solution: (a) a[2] indicates the 3rd string of the 2D array. Thus fellows will be printed.

6. What will be the output?


# include <stdio.h>
int main()
{
char str1[] = "Week-7-Assignment";
char str2[] = {'W', 'e', 'e', 'k', '-', '7', '-', 'A', 's','s','i','g','n','m','e','n','t'};
int n1 = sizeof(str1)/sizeof(str1[0]);
int n2 = sizeof(str2)/sizeof(str2[0]);
printf("n1 = %d, n2 = %d", n1, n2);
return 0;
}

a) n1=18, n2=17
b) n1=18, n2=18
c) n1=17, n1=17
d) n1=17, n2=18

Solution: (a) The size of str1 is 18 and size of str2 17.


When an array is initialized with string in double quotes, compiler adds a ‘\0’ at the end.
7. Consider the following C program segment:
#include<stdio.h>
#include<string.h>
int main()
{
char p[20];
char s[] = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
p[i] = s[length - i];
printf("%s", p);
return 0;
}
The output would be-

a) gnirts
b) gnirt
c) string
Week 7 Assignment Solution

d) no output is printed
Solution: (d)
Let us consider below line inside the for loop p[i] = s[length — i];
For i = 0, p[i] will be s[6 - 0] and s[6] is ‘\0′
So p[0] becomes ‘\0’. It doesn’t matter what comes in p[1], p[2]….. as P[0] will not change
for i >0. Nothing is printed if we print a string with first character ‘\0′

8. If the starting address of an float array Arr[10][10] is 2000, what would be the
memory address of the element Arr[5][6]? (float takes 4 bytes of memory)
a) 2268
b) 2120
c) 2224
d) 2144
Solution: (c) If ‘a’, ‘b’ and ‘c’ denotes the starting address, number of columns and size in
bytes for each element respectively of array Arr[][], then the location of Arr[i][j] can be
calculated as
𝐴𝑑𝑑𝑟𝑒𝑠𝑠 = 𝑎 + (𝑖 ∗ 𝑏 + 𝑗) ∗ 𝑐
Thus the address of Arr[5][6] is 2000+(5*10+6)*4=2224

9. In C, the placement of elements of a two dimensional array is


a) Row wise
b) Column wise
c) Diagonal wise
d) Bottom to top wise
Solution: (a) In C the placement of 2D array in memory is row wise.

10. What will be the value of ‘i’ after the execution of the C code fragment given below?

static char str1[] = "dills";


static char str2[20];
static char str3[] = "daffo";
int i;
i = strcmp(strcat(str3, strcpy(str2, str1)), "daffodills");

Solution: 0 (short answer type)


strcat(str3, strcpy(str2, str1)) makes it “daffodills”, hence strcmp(“daffodills”, “daffodills”)=0
Assignment 8 Solution

1. A function prototype is used for


a) Declaring the function logic
b) Calling the function from the main body
c) Telling the compiler, the kind of arguments used in the function
d) Telling the user for proper use of syntax while calling the function
Solution: (c) A function prototype tells the compiler what kind of arguments a function is
looking to receive and what kind of return value a function is going to give back. This
approach helps the compiler ensure that calls to a function are made correctly and that no
erroneous type conversions are taking place.

2. What is the default return type if it is not specified in function definition?


a) void
b) integer
c) double
d) float
Solution: (b) Integer is the default data type if not specified in the function.

3. What will be the output?


#include <stdio.h>
int main()
{
{
int a = 70;
}
{
printf("%d", a);
}
return 0;
}

a) 70
b) Garbage value
c) Compilation error
d) None

Solution: (c) Compilation error.


A Block is a set of statements enclosed within left and right braces ({and} respectively). A
variable declared in a block is accessible in the block and all inner blocks of that block but
not accessible outside the block. Thus the program produces a compiler error.
Assignment 8 Solution

4. How many times will ‘Hello world’ be printed?


#include<stdio.h>
int main()
{
printf("Hello world\n");
main();
return 0;
}

a) Infinite times
b) 32767
c) 65535
d) Till stack overflows

Solution: (d) Till stack overflows

5. How many times ‘Hi’ will be printed in the program given below
#include<stdio.h>
int i;
int fun();

int main()
{
while(i)
{
fun();
main();
}
printf("Hello\n");
return 0;
}
int fun()
{
printf("Hi");
}

a) Only once
b) Zero times
c) Infinite times
d) Compilation error
Solution: (b) The default value of i is ‘0’. Thus, the while loop will never be executed and the
control will not come into the function. Thus, ‘Hi’ will never be printed.
Assignment 8 Solution

6. How many times the function factorial will be executed?


#include<stdio.h>
int factorial(int);
int main()
{
int n=5;
long int f;
f = factorial(n);
printf("%d! = %ld\n", n, f);
return 0;
}
int factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}

Short Answer Type: Answer: 6

As n =5, so the function factorial() calls itself 5 times. In addition, factorial() function is
called once from the main(). So, total 6 times the function factorial() will be executed.

7. What will be the output?


#include<stdio.h>
void func(int n, int sum)
{
int k = 0, j = 0;
if (n == 0) return;
k = n % 10;
j = n / 10;
sum = sum + k;
func (j, sum);
printf ("%d,", k);
}

int main ()
{
int a = 2048, sum = 0;
func (a, sum);
printf ("%d ", sum);
}

a) 8 ,4, 0, 2, 14
b) 8, 4, 0, 2, 0
c) 2, 0, 4, 8, 14
d) 2, 0, 4, 8, 0
Assignment 8 Solution

Solution: (d) 2, 0, 4, 8, 0
sum has no use in func(), it is there just to confuse. Function func() just prints all digits of a
number. In main, there is one more printf statement after func(), so one more 0 is printed after
all digits of n.

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


#include <stdio.h>
int fun(int n)
{
int i, j, sum = 0;
for(i = 1; i<=n; i++)
for(j=i; j<=i; j++)
sum = sum + j;
return(sum);
}
int main()
{
printf("%d", fun(10));
return 0;
}

a) 55
b) 45
c) 66
d) 10

Solution: (a) The program finds the sum of the integer numbers till 10. Thus the output is
55.

9. Consider the function


int find(int x, int y)
{
return((x<y) ? 0 : (x-y));
}
Let a and b be two non-negative integers. The call find(a, find(a, b)) can
be used to find the

a) Maximum of a, b
b) Positive difference between a and b
c) Sum of a and b
d) Minimum of a and b

Solution: (d) Minimum of a and b


The function returns
Assignment 8 Solution

0 if x<y
x-y if x>y
Assume in the first case a>b so find(a,b) will return (a-b). So find(a,find(a,b)) will be
find(a,a-b) which will return (a-(a-b))= b which is the minimum number.
Now if a<b then find(a,b)=0 so find(a,find(a,b))=find(a,0) which will return (a-0)=a
which is the minimum number. So, the code actually returns a minimum of two numbers.
10. What is the output of the C code given below
#include <stdio.h>
float func(float age[ ]);

int main()
{
float result, age[] = { 23.4, 55, 22.6, 3, 40.5, 18 };
result = func(age);
printf("%0.2f", result);
return 0;
}

float func(float age[ ])


{
int i;
float result, sum = 0.0;
for (i = 0; i < 6; ++i) {
sum += age[i];
}
result = (sum / 6);
return result;
}

Ans: 27.08
Week 9: Assignment Solution

1. What is the worst case complexity of selection sort?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)

Solution: (d) O(n2)

2. What is the best case and worst case complexity of ordered linear search?
a) O(nlogn), O(logn)
b) O(logn), O(nlogn)
c) O(n), O(1)
d) O(1), O(n)
Solution: (d)
Although ordered linear search is better than unordered when the element is not present in the array, the best
and worst cases still remain the same, with the key element being found at first position or at last position
respectively.

3. Given an array arr = {12, 34, 47, 62, 85, 92, 95, 99,105} and key = 34; what are the mid values
(corresponding array elements) generated in the first and second iterations?
a) 85 and 12
b) 85 and 34
c) 62 and 34
d) 62 and 47

Solution: (b) 85 and 34


In the first iteration: Mid=arr[(0+8)/2]=arr[4]=85
In the second iteration: Mid=arr[(0+3)/2]=arr[1]=34
So, the mid values are 85 and 34

4. When the Binary search is best applied to an array?


a) For very large size array
b) When the array is sorted
c) When the array elements are mixed data type
d) When the array is unsorted
Solution: (b) Binary search is applied for sorted array.

5. Consider the array A[ ]= {5,4,9,1,3} apply the insertion sort to sort the array. Consider the cost
associated with each sort is 25 rupees, what is the total cost of the insertion sort for sorting the entire
array?
a) 25
b) 50
c) 75
d) 100
Solution: (c)
When the element 1 reaches the first position of the array three comparisons are only required
hence 25 * 3= 75 rupees.

*step 1: 4 5 9 1 3.
Week 9: Assignment Solution

*step 2: 1 4 5 9 3

*step 3: 1 3 4 5 9

6. Select the code snippet which performs unordered linear search iteratively?

a) int unorderedLinearSearch(int arr[], int size, int data)


{
int index;
for(int i = 0; i < size; i++)
{
if(arr[i] == data)
{
index = i;
break;
}
}
return index;
}

b) int unorderedLinearSearch(int arr[], int size, int data)


{
int index;
for(int i = 0; i < size; i++)
{
if(arr[i] == data)
{
break;
}
}
return index;
}

c) int unorderedLinearSearch(int arr[], int size, int data)


{
int index;
for(int i = 0; i <= size; i++)
{
if(arr[i] == data)
{
index = i;
continue;
}
}
return index;
}

d) None of the above

Solution: (a)
Unordered term refers to the given array, that is, the elements need not be ordered. To search for an element
in such an array, we need to loop through the elements until the desired element is found.
Week 9: Assignment Solution

7. What will be the output?


#include<stdio.h>
#define func1(a,b) a > b ? b : a
#define func2(a,b); {temp=a;a=b;b=temp;}
int main()
{
int a=3, b=5,temp;
if((3+func1(a,b)) > b)
func2(a,b);
printf("%d %d", a,b);
return 0;
}

a) 35
b) 30
c) 50
d) 53

Solution: (d)
Here, func1(3,5) (inside the if statement ) returns 3 as the condition given in the macro is false and it
returns a=3.
Next, (3+3 >5 ) this condition is true and the program calls func2(3,5) which swaps the values of a and b.
Therefore the output is 5 3.

8. Consider an array of elements arr[5]= {5,4,3,2,1}, what are the steps of insertions done while doing
insertion sort in the array.

a) 4 5 3 2 1
34521
23451
12345

b) 5 4 3 1 2
54123
51234
12345

c) 4 3 2 1 5
32154
21543
15432

d) 4 5 3 2 1
23451
34521
12345

Solution: (a)
Iteration 1: 4 is placed in its appropriate location. arr={4 5 3 2 1}
Week 9: Assignment Solution

Iteration 2: 3 is placed in its appropriate location. arr={3 4 5 2 1}


Iteration 3: 2 is placed in its appropriate location. arr={2 3 4 5 1}
Iteration 4: 1 is placed in its appropriate location. arr={1 2 3 4 5}

9. What will be the output of the following C code?


#include <stdio.h>
#if A == 1
#define B 0
#else
#define B 1
#endif
int main()
{
printf("%d", B);
return 0;
}

a) 0
b) 1
c) 01
d) None of the above
Solution: (b)
Here, A is not initialized, so its default value is 0. The condition in the #if macro is FALSE and #define B 1
is executed. Therefore, B stores 1.
10. What will be the output?
#include <stdio.h>
#define a 10
int main()
{
printf("%d ", a);
int a=50;
printf("%d ", a);
return 0;
}

a) 10 10
b) 10 50
c) 50 50
d) Compilation error
Solution: (d)
If a is defined in macro, its value will be same throughput the program. The value assigned to that macro
cannot be changed inside the program. This is the reason, it will show compilation error at the step
int a=50.
Week 10 Assignment Solution

1. Bisection method is used to find


a) Derivative of a function at a given point
b) Numerical integration of a function within a range
c) The root of a function
d) None of the above

Solution: (c) The root of a function

2. In ……………, the search starts at the beginning of the list and checks every element in
the list.
a) Linear search
b) Binary search
c) Hash search
d) Binary tree search
Solution: (a) Linear search

3. What is the worst-case time complexity of Linear Search?


a) O(1)
b) O(logn)
c) O(n)
d) O(n2)

Answer: c) O(n)

Explanation: In the worst-case scenario, Linear Search may require O(n) time to find an
element in an array of n elements.

4. What is the worst-case complexity of bubble sort?


a) O(N log N)
b) O(log N)
c) O(N)
d) O(N2)

Solution: (d)
Explanation: Bubble sort works by starting from the first element and swapping the elements
if required in each iteration. Therefore, in the worst case, the complexity is O(N^2).

5. What maximum number of comparisons can occur when a bubble sort is implemented?
Assume there are n elements in the array.
a) (1/2) (n-1)
b) (1/2) n(n-1)
c) (1/4) n(n-1)
d) None of the above

Solution: (b)(½)n(n-1)
Week 10 Assignment Solution

The total number of comparisons, therefore, is (n - 1) + (n - 2) ... (2) + (1) = n (n - 1)/2

6. What are the correct intermediate steps of the following data set when it is being sorted
with the bubble sort? 7,4,1,8,2
a) 4,7,1,8,24,1,7,2,84,1,2,7,81,4,2,7,81,2,4,7,8
b) 4,7,1,8,24,1,7,8,24,1,7,2,81,4,7,2,81,4,2,7,81,2,4,7,8
c) 4,7,1,8,21,4,7,8,21,4,2,7,81,2,4,7,8
d) 4,7,1,8,24,7,1,2,81,4,7,2,81,4,2,7,81,2,4,7,8

Solution: (b) Bubble sort works by starting from the first element and swapping the elements
if required in each iteration. Therefore, the order when the changes happen are as shown in
the steps of the answer.

7. What is the main disadvantage of the Bisection Method?


a) It is computationally expensive
b) It cannot find complex roots
c) It requires the function to be differentiable
d) It is not guaranteed to converge

Answer: b) It cannot find complex roots

Explanation: The Bisection Method is only applicable for real roots and cannot find complex
roots of a function.

8. What will be the output of the following snippet?


int arr[] = {10, 20, 30, 40, 50};
int *ptr1 = arr;
int *ptr2 = ptr1 + 3;
printf("%d", *ptr2 - *ptr1);

Solution: 30
ptr1 points to the first element (10), and ptr2 points to the fourth element (40). *ptr2 - *ptr1
will be 40−10=40−10=30.

9. What is the solution of the equation given below using the Bisection Method up to four
decimal places? (Consider the root lying on positive quadrant only and compute the root
till five iterations only)
𝑓(𝑥) = 𝑥𝑒 − 3𝑥 − 5

Solution: 1.0312 (short answer type)


The root lies between 1 and 2. Using bisection method, we can find the root as 1.0312 (up to
three decimal accuracy)
Week 10 Assignment Solution

10. What will be the output?


#include <stdio.h>
int main(void)
{
int a[] = {10, 12, 6, 7, 2};
int i, *p;
p=a+4;
for(i=0; i<5; i++)
printf("%d ", p[-i]);
return 0;
}

a) 10 12 6 7 2
b) 10 12 6 7
c) 2 7 6 12
d) 2 7 6 12 10
Solution: (d) The pointer p after the operation p = a + 4, essentially points the last element of
the array. Therefore, when the for loop iterates, it keeps printing from the last element to the
beginning.
Week 11 Assignment Solution

1. Interpolation provides a mean for estimating functions


a) At the beginning points
b) At the ending points
c) At the intermediate points
d) None of the mentioned

Solution: (c) At the intermediate points


Explanation: Interpolation provides a mean for estimating the function at the intermediate points.
2. To solve a differential equation using Runge-Kutta method, necessary inputs from user to the
algorithm is/are
a) the differential equation dy/dx in the form x and y
b) the step size based on which the iterations are executed.
c) the initial value of y.
d) all the above

Solution: (d) The differential equation, step size and the initial value of y are required to solve differential
equation using Runge-Kutta method.

3. A Lagrange polynomial passes through three data points as given below


𝑥 5 10 15
𝑓(𝑥) 15.35 9.63 3.74
The polynomial is determined as 𝑓(𝑥) = 𝐿0 (𝑥). (15.35) + 𝐿1 (𝑥). (9.63) + 𝐿2 (𝑥). (3.74)
The value of 𝑓(𝑥) at 𝑥 = 7 is

a) 12.78
b) 13.08
c) 14.12
d) 11.36

Solution: (b)
2
𝑥 − 𝑥𝑗 (7 − 10)(7 − 15) 24
𝐿0 (𝑥) = ∏ = = = 0.48
𝑥0 − 𝑥𝑗 (5 − 10)(5 − 15) 50
𝑗=0
𝑗≠0
2
𝑥 − 𝑥𝑗 (7 − 5)(7 − 15) −16
𝐿1 (𝑥) = ∏ = = = 0.64
𝑥1 − 𝑥𝑗 (10 − 5)(10 − 15) −25
𝑗=0
𝑗≠1

2
𝑥 − 𝑥𝑗 (7 − 5)(7 − 10) −6
𝐿2 (𝑥) = ∏ = = = −0.12
𝑥1 − 𝑥𝑗 (15 − 5)(15 − 10) 50
𝑗=0
𝑗≠2
So 𝑓(7) = 0.48 ∗ 15.35 + 0.64 ∗ 9.63 − 0.12 ∗ 3.74 = 13.08
3.2
4. The value of ∫0 𝑥𝑒 𝑥 𝑑𝑥 by using one segment trapezoidal rule is
a) 172.7
b) 125.6
c) 136.2
d) 142.8

Solution: (b)
𝑏
𝑓(𝑏) − 𝑓(𝑎)
∫ 𝑓(𝑥)𝑑𝑥 = (𝑏 − 𝑎)
𝑎 2
Week 11 Assignment Solution
3.2
Here, 𝑎 = 0, 𝑏 = 3.2, 𝑓(𝑎) = 0 and 𝑓(𝑏) = 78.5. Hence, ∫0 𝑥𝑒 𝑥 𝑑𝑥 = 125.6

5. Accuracy of the trapezoidal rule increases when


a) integration is carried out for sufficiently large range
b) instead of trapezoid, we take rectangular approximation function
c) number of segments are increased
d) integration is performedfor only integer range

Solution: (c)Approximation increases with the increase of the number of segments between the lower and upper
limit.

6. Solve the ordinary differential equation below using Runge-Kutta4th order method.
Step size h=0.2.
𝑑𝑦
5 + 𝑥𝑦 3 = cos(𝑥) , 𝑦(0) = 3
𝑑𝑥
The value of y(0.2) is (upto two decimal points)

a) 2.86
b) 2.93
c) 3.13
d) 3.08
Solution: (b)

7. Match the following


A. Newton Method 1. Integration
B. Lagrange Polynomial 2. Root finding
C. Trapezoidal Method 3. Differential Equation
D. RungeKutta 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)
3
8. The value of ∫1 ex (ln 𝑥) dx calculated using the Trapezoidal rule with five subintervals is (* range
is given in output rather than single value to avoid approximation error)

a) 12.56 to 12.92
b) 13.12 to 13.66
c) 14.24 to 14.58
d) 15.13 to 15.45

Solution: (c) The 14.24 to 14.58


From the formula of trapezoidal rule we get, the following
Δx/2=1/5:
3
∫1 ex (ln 𝑥) =(1/5)(0+2.72892440558099+7.11180421388169+14.2316766420315+25.7295115705906+22
.066217688311)=14.3736269040792
Week 11 Assignment Solution

9. Consider the same recursive C function that takes two arguments

unsignedintfunc(unsigned int n, unsigned int r)


{
if (n > 0) return (n%r + func (n/r, r ));
else return 0;
}
What is the return value of the function foo when it is called as func(513, 2)?

a) 9
b) 8
c) 5
d) 2

Solution: (d) 2
func(513, 2) will return 1 + func(256, 2). All subsequent recursive calls (including func(256, 2)) will return
0 + func(n/2, 2) except the last call func(1, 2) . The last call func(1, 2) returns 1. So, the value returned by
func(513, 2) is 1 + 0 + 0…. + 0 + 1=2.
10. What is the output?
#include <stdio.h>
int fun(int n)
{
if (n == 4)
return n;
else return 2*fun(n+1);
}
int main()
{
printf("%d ", fun(2));
return 0;
}

a) 4
b) 8
c) 16
d) Error

Solution: (c) 16
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