Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
162 views

Assignment Solutions-Problem Solving Through Programming in C

The document provides solutions to a week 1 assignment in C programming. It includes 10 multiple choice questions about topics like data types, operators, control flow, and functions. The solutions explain the reasoning for each answer.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views

Assignment Solutions-Problem Solving Through Programming in C

The document provides solutions to a week 1 assignment in C programming. It includes 10 multiple choice questions about topics like data types, operators, control flow, and functions. The solutions explain the reasoning for each answer.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Week 1 Assignment Solution

1. The input given from keyboard is converted to computer understandable unit (bit) by the standard
a) ISO
b) ANSI
c) ASCII
d) EBCDIC

Solution: (c) ASCII is the standard for character to equivalent number (in bits) conversion.

2. The execution nature of C program is


a) Event-based
b) Concurrent
c) Multi-threaded
d) Sequential

Solution: (d) Sequential

3. Choose the correct statements from the following


i) In high-level language, testing and debugging a program is difficult than assembly language.
ii) C programs are highly portable on any type of operating system platform.
iii) A flowchart is a visual representation of the sequence of steps for solving a problem.
iv) The role of a compiler is to translate source program statements to decimal codes.

a) (i) and (ii)


b) (ii) and (iii)
c) (i), (ii), and (iii)
d) (ii), (iii), and (iv)

Solution: (b) (ii) and (iii) are correct.

In high-level language, testing and debugging a program is easier than assembly language.
The role of a compiler is to translate source program statements to object codes.

4. When we write X=Y in C, which of the following statements is valid?


a) X and Y both have same values but different locations.
b) X and Y both have same location and same values.
c) X and Y have different values and same location.
d) X and Y have different values and different locations.

Solution: (a) X and Y both have same values but different locations.

5. What will be the output of the flowchart given below?


Week 1 Assignment Solution

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

Solution: (c) b=a+b=5,b=a*b+ b/5=3*5 + 5/5=15 +1= 16

6. The output of the following algorithm is

a) 21
b) 28
c) 30
Week 1 Assignment Solution

d) 40

Solution: (b) The flowchart finds the sum of first 7 natural numbers. Hence, the right answer is 28.

7. The print values of ‘a’ and ‘b’ of the flowchart below are

a) a=4,b=6
b) a=6,b=4
c) a=10,b=2
d) a=2,b=10

Solution: (b) The algorithm finds the swap of two numbers. Hence, the output is a=6,b=4

8. The program which translates high level program into its equivalent machine language program is
called

a) a translator
b) a language processor
c) a converter
d) None of the above

Solution: (a) translator. Generally, there are three types of translator-compilers, interpreters, assemblers.

9. An interpreter reads the source code of a program


Week 1 Assignment Solution

a) one line at a time


b) two line at a time
c) complete program in one stroke
d) None of these

Solution: (a) one line at a time.

10. The C language has been developed at


a) IBM, USA
b) Borland International, USA
c) Sun Microsystems
d) AT & T Bell Labs, USA

Solution: (d) AT & T Bell Labs, USA


Week 2 Assignment Solution

1. A function is

a) Block of statements to perform some specific task


b) It is a fundamental modular unit to perform some task
c) It has a name and can be used multiple times
d) All of the above

Solution: (d) All are true

2. If an integer needs two bytes of storage, then the minimum value of a signed integer in C
would be
a) −(216 − 1)

b) 0

c) −(215 − 1)

d) −215

Solution: (d) The first bit is used to indicate whether it is a signed or unsigned integer.

3. Which of the following statements is correct?


I. Keywords are those words whose meaning is already defined by Compiler.
II. Keywords cannot be used as variable names.
III. There are 32 keywords in C
IV. C keywords are also called reserved words.

a) I and II
b) II and III
c) I, II and IV
d) All of the above

Solution: (d) All of the above are correct.

4. 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;
}
Week 2 Assignment Solution

a)13
b)31
c)11
d)33
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.

5. 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.

6. Which of the following are not standard header files in C?


a) stdio.h
b) conio.h
c) string.h
d) All the above are standard header file

Solution: (d) all are valid c headers.

7. What is the output of the following code?


#include<stdio.h>
#define fun(x) (x*x-x)
void main()
{
float i;
i = 37.0/fun(2);
printf("%.2f", i);
}

Solution: 18.50

The pre-processing replaces fun(2) with (2*2-2). Thus fun(2)=2, so, i=37.0/2=18.50

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


a) Var123
b) Var_123
c) 123Var
Week 2 Assignment Solution

d) X_123_Var

Solution: (c) Variable name must not begin with a digit. So, ‘123Var’ is an invalid variable
declaration in C.

9. What is the output of the following program?


#include <stdio.h>
#define a 6
int main()
{
int a =3;
a = a+1;
printf("%d", a);
return 0;
}

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

Solution: (d) #define is a pre-processor and 6 is stored in a. Thus ‘a’ cannot be declared as a
variable. Thus, the compiler will return a compilation error.

10. The following C program swaps the value of two numbers without using any third
variable. What are the correct operations that need to be inserted inside the blanks?
#include <stdio.h>
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;
Week 3: Assignment Solution

1. Find the output of the following C program

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

a) 120, 120
b) 120, 130
c) 130, 120
d) 130, 130

Solution:(b) The eqation z= x*y++ gives the result 120 because ‘y’ does not get incremented. After
multiplication ‘y’ incremented to 13. So the second equation gives the result 130.

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

#include<stdio.h>
int main()
{
int p=2;
int m=10;
int k;
k= !((p<2)&&(m>2));
printf("\n%d", k);
return 0;
}

a) 1
b) 0
c) -1
d) 2
Solution: (a) As (2<2) and (10>2) both condition are false, so the AND (&& ) operation will be zero. Hence
k=!0=1.

3. Find the output of the following C code.


#include<stdio.h>
#include<math.h>
int main()
{
int a=5, b=2, c=6;
float x1, x2;
if(b*b>4*a*c)
{
x1= -b+sqrt(b*b-4*a*c)/2*a;
x2= -b-sqrt(b*b-4*a*c)/2*a;
printf("\n x1=%f, x2=%f",x1,x2);
Week 3: Assignment Solution

}
else
printf("\n Roots are imaginary");
return 0;
}

a) x1=4, x2=3
b) x1=-5, x2=-4
c) x1=2.5, x2=4.2
d) Roots are imaginary

Solution: (d) Roots are imaginary. b*b=4, 4*a*c=4*5*6=120 So, b*b<4*a*c. Hence ‘else’ part will be
executed.

4. Find the output of the following code.

#include<stdio.h>
int main()
{
int p=6, q=4, r=10;
if(p>q)
{
if(p>r)
printf("%d", p);
else
printf("%d", r);
}
else
{
if(r>q)
printf("%d", r+q);
else
printf("%d", q);
}
return 0;
}

a) 6
b) 4
c) 10
d) 14

Solution: (c) 10. Here p>q as p=6, q=4 so the first if condition is true. But the condition p>r is not true. So
the else condition is satisfied of the first if block.

5. Which of the following statements are correct?


I. The ‘else’ block is executed when condition inside ‘if’ statement is false.
II. One ‘if’ statement can have multiple ‘else’ statement.

a) Only I
b) Only II
c) Both I and II
Week 3: Assignment Solution

d) None of the above is correct

Solution: (a) The ‘else’ block is executed when condition is false. One ‘if’ statement have only one ‘else’
statement.

6. C modulo division operator ‘ %’ can be applied on

a) only float variables


b) only int variables
c) int and float combination
d) any data types in C

Solution: (b) modulo division operator ‘ %’ can be applied only on int variables.

7. The output of the following program will be

#include<stdio.h>
int main()
{
if(0)
printf(" C programming\n");
if(0.5)
printf("Java \n");
if(-0.5)
printf("Python \n");
return 0;
}

a) C programming
b) Java
Python
c) C programming
Java
d) Compilation error

Solution: (b) Any non-zero number inside the if condition is always true. So if(0.5) and if(-0.5) are true. Thus the
printf corresponding to 0.5 and -0.5 will be printed. But if(0) is false.

8. What will be the output?

#include<stdio.h>
int main()
{
int p=2;
int k;
k= p>10?2:10;
printf("%d",k);
return 0;
}

a) 2
b) 8
c) 10
d) 12
Week 3: Assignment Solution

Solution: (c)10. It is a ternary operator. Here condition is false. So the second expression will execute.

9. What will be the output?

#include<stdio.h>
int main()
{
int s=20;
if(s=19)
printf("Right\n");
else
printf("Wrong\n");
return 0;
}

a) Right
b) Wrong
c) 0
d) No output

Solution: (a) s=19 is an assignment not comparison. If (non zero) is always true.

10. What will be the output of the program ?

#include<stdio.h>
int main()
{
int a=2, b=3, c=5, d=8, ans;
ans=a-b+c*a/d%b;
printf(" The answer will be %d", ans);
return 0;
}

a) The answer will be 15


b) The answer will be 0
c) The answer will be 1
d) Compilation error

Solution: (b)
Following the precedence rule, we can conclude that the operation steps are
 ans=2-3+5*2/8%3
 ans=-1+10/8%3
 ans=-1+1%3
 ans=-1+1
 ans=0
Week 4 Assignment Solution

1. The loop which is executed at least once is


a “while” loop
b “do-while” loop
c “for” loop
d None of the above
Solution: (b) do-while loop is executed at least once even though the condition is false.
2. In the C programming language negative numbers when used in if-else conditional
checking, are treated as

a TRUE
b FALSE
c Depends on the implementation
d None of the above

Solution: (a) TRUE, all non-zero values are TRUE in C

3. Choose the correct statement to use “if-else” statement in C Language


a “else if” is compulsory to use with “if” statement.
b “else” is compulsory to use with “if” statement.
c “else” or “else if” is optional with the “if” statement.
d None of the above are correct
Solution: (c) “else” or “else if” is optional with the “if” statement

4. 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’ are printed
d Compilation error

Solution: (c) ‘a--’ post-increments 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
Week 4 Assignment Solution

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.

5. In the following example, tell which statement is correct

if( (condition1==1) && (condition2==1))


printf(“Swayam”);

a Condition1 will be evaluated first, and condition2 will be evaluated second


b Condition2 will be evaluated first, and condition1 will be evaluated second
c Condition1 will be evaluated first, condition2 will be evaluated only if the
condition1 is TRUE
d Condition2 will be evaluated first, and condition1 will be evaluated only if
condition2 is TRUE

Solution: (c) Condition1 will be evaluated first; condition2 will be evaluated only if the
condition1 is TRUE. This is called the Short-circuited evaluation of the operators

6. Which one of the following is the correct syntax for C Ternary Operator?
a) condition ? expression1 : expression2
b) condition : expression1 ? expression2
c) condition ? expression1 < expression2
d) condition < expression1 ? expression2
Solution: (a) If the condition is true, expression 1 is evaluated. If the condition is false, expression
2 is evaluated.

7. The purpose of the following program fragment is to


𝑏𝑏 = 𝑠𝑠 + 𝑏𝑏;
𝑠𝑠 = 𝑏𝑏 − 𝑠𝑠;
𝑏𝑏 = 𝑏𝑏 − 𝑠𝑠;
(where s and b are two integers)

a Transfer the content of s to b


b Transfer the content of b to s
c Exchange (swap) the content of s and b
d Negate the contents of s and b
Solution: (c) Exchange (swap) the content of s and b
Week 4 Assignment Solution

This is a well-known method to swap the two variables without using a third variable.
8. What will be the output?
#include <stdio.h>
int main()
{
int x=0;
x = printf("3");
printf("%d",x);
return 0;
}

a 11
b 33
c 31
d 13
Solution: (c ) 31
x = printf("3");
First printf prints 3. printf() returns 1. Now the variable x=1; So 1 is printed next.

9. What will be the output? (&& is logical AND operation)


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

a 0
12
b 1
11
c 0
00
d 0
11
Solution: (d)
Inside the first printf statement, i++ results in 0 (due to post increment operation). As the left
operand of && operator is zero, the compiler will not evaluate the right operand (i.e., ++j). So, j
is not incremented after the first printf statement. Finally, the result is i=1 and j=1.

10. What will be the value of a, b, and c after the execution of the following
int a = 5, b = 7, c = 111;
c /= ++a * b--;
Week 4 Assignment Solution

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
Week 5 Assignment Solution
1. What will be the output ?
#include <stdio.h>
int main()
{
int n;
for(n=10;n<10;n--)
printf(" %d", n);
return 0;
}

a)10 9 8 7 6 5 4 3 2 1
b) 1 2 3 4 5 6 7 8 9 10
c) No output
d) Error
Solution: (c)The test condition is not proper at the beginning. So, loop will not
execute.

2. Which of the following is not an infinite loop?


a) for(; ;)
b) for(x=0; ;x<=10)
c) while(1)
d) while(0)
Solution: (d)
3. Consider the following and identify the false statement(s)?
i) ‘do-while’ loop must be terminated by a semi colon.
ii) ‘do-while’ loop is always an infinite loop.
iii) Even if the condition is false, the ‘do-while’ loop executes once.
iv) ‘do-while’ loop is an entry-controlled loop.
a) (i) and (ii)
b) (i), (ii), and (iv)
c) (ii)
d) (ii) and (iv)

Solution: (d) ‘do-while’ loop is not always an infinite loop. It depends on the
condition mentioned inside the while statement.
‘do-while’ loop is an exit-controlled loop.

4. Compute the printed value of ‘m’ and ‘n’ of the C program given below
Week 5 Assignment Solution
#include <stdio.h>
int main()
{
int m= 0, n = 0;
while (m<5, n <7)
{
m++;
n++;
}
printf("%d, %d\n", m, n);
return 0;
}

a) 5,7
b) 5,5
c) 7,7
d) 0,0
Solution: (c) The while condition checks the last condition (i.e. n<7) and till the
condition is satisfied the block inside the loop is executed. Thus the loop is run for
7 times and both the values of m and n are incremented by 7.

5. What should be in the place of ****** so that except i=8, rest of the values of i
(as defined in the ‘for’ loop: i=0 to i=19) will be printed?

#include <stdio.h>
int main()
{
int i = 0;
for (i = 0;i< 20; i++)
{
if(i==8)
{
**********;
}
printf("i=%d\n",i);
}
return 0;
}

a) break
b) continue
c) switch
d) exit
Week 5 Assignment Solution
Solution: (b) continue forces the next iteration of the loop to take place skipping
any code in between.

6. What will be the output?


#include <stdio.h>
int main()
{
int x = 0;
switch (x)
{
case 0: printf("NPTEL");
case 1: printf("SWAWAM");
default: printf("IIT/IISc");
}
return 0;
}

a) NPTEL
b) IIT/IISc
c) NPTELSWAWAMIIT/IISc
d) Compilation error

Solution: (c) Here no ‘break’ statement is given. When a match is found, the
program executes the statements following that case, and all subsequent case and
default statements as well.
7. What will be the output?
#include <stdio.h>
int main()
{
int k,j;
for (k=1,j=3;k<=3,j>=1;k++,j--)
{
printf("%d,%d\n",k,j);

}
return 0;
}

a) 1,3
b) 1,3
3,1
c) 1,3
2,2
3,1
d) 0,0
Week 5 Assignment Solution

Solution: (c) Multiple variables can be initialized as well as multiple condition ,and
propagation can be made in a single ‘for’ loop just by using a comma ‘,’.So k value
will be increased upto 3 and j value will be decreased to 1.

8. What will be the output of the program?


#include <stdio.h>
int main()
{
int p;
for (p=0;p<3;p++)
{
int p=4;
printf("%d,",p);
}
return 0;
}

a) 4 will print 1 times


b) 4 will print 3 times
c) 4 will print 4 times
d) No output

Solution: (b) for loop will be continue for 3 times(p=0,1,2) and 4 will be print three
times. Here, the variable ‘p’ inside the for loop is a local variable. So, this local
variable does not change the condition of the for loop.

9.
For the C program given below, if the input given by the user is 7. What will be
shown on the output window?
#include <stdio.h>
int main()
{
int n,i=2;
scanf("%d",&n);
do
{
if(n%i==0)
{
printf("The number is odd");
}
i++;
}
while(i<n);
printf("The number is prime");
return 0;
}
Week 5 Assignment Solution
a) The number is odd
b) The number is prime
c) The number is odd The number is prime
d) Syntax Error

Solution: (b) The number is prime.

10. What will be the output? (Answer according to the output you get on the output window and the
options provided, though it prints some values previously.)

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

a) 0 1 2 3 4 5 6 7 8 9 11 12…..infinite times
b) 1 2 3 4 5 6 7 8 9 11 12….infinite times
c) Won’t print anything
d) Error

Solution: (c) Won’t print anything


Due to the continue, the printf will always be skipped and loop will iterate
without printing anything.
Week 6 Assignment Solution

1. What is the right way to initialise an array in C?


a) int arr{}= {1,2, 5,6,9}
b) int arr[5] = {1,2, 5,6,9}
c) int arr{5} = {1,2, 5,6,9}
d) int arr() = {1,2, 5,6,9}

Solution: (b)
2. An integer array of dimension 10 is declared in a C program. The memory location
of the first byte of the array is 1000. What will be the location of the 9th element of
the array? (Assume integer takes 4 bytes of memory and the element stored at 1000
is identified as 1st element)
a) 1028
b) 1032
c) 1024
d) 1036

Solution: (b) Integer takes four bytes of memory. As the memory assignment to the
elements are consecutive and the index starts from 0, the 9th element will be located at
1000+(8×4)

3. 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: (a) The program finds the minimum element of an array. Hence, the output
is 1.

4. Which of the statements is/are correct?


a) An array may contain more than one element
b) All elements of array have to be of same data type
c) The size of array has to be declared upfront
d) All of the above
Week 6 Assignment Solution

Solution: (d) All of the above

5. What actually gets passed when you pass an array as an argument to a function

a) Value of elements in array


b) First element of the array
c) Base address of the array
d) Address of the last element of array

Solution: (c) The base address of the array gets passed.

6. 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

7. What will be the output?


#include <stdio.h>
int main()
{
int p;
int arr[10]={1,2,3,4,5,6,9,10};
p=(arr+1)[5];
printf("%d", p);
return 0;
}
Week 6 Assignment Solution

a) 5
b) 6
c) 9
d) 10

Solution: (c)
x[i] is equivalent to *(x + i),
so (buf + 1)[5] is *(buf + 1 + 5), i.e. buf[6]=9.

8. An array of the void data type


a) can store any data-type
b) only stores element of similar data type to first element
c) acquires the data type with the highest precision in it
d) It is not possible have an array of void data type

Solution: (d) It is not possible to have an array of void datatype.


9. What will be the output?
#include<stdio.h>
int main()
{
int n = 3;
int sum = 4;
switch(n)
{
case 2: sum = sum-2;
case 3: sum*=5;
break;
default:
sum =0;
}
printf("%d", sum);
return 0;
}

Solution: 20 ( Short answer type)


N=3 therefore switch(3) i.e. case 3 will be executed. Inside case 3 sum becomes sum=
sum*5 = 20. So, finally 20 is printed.

10. How many ‘a’ will be printed when the following code is executed?
#include <stdio.h>
int main()
{
int i = 0;
char c = 'a';
while (i < 5)
{
i++;
Week 6 Assignment Solution

switch (c)
{
case 'a':
printf("%c ", c);
break;
}
}
printf("a\n");
return 0;
}

Solution: 6 (short answer type)


Initially, i=0, which satisfies the while condition. Case ‘a’ is always executed inside
the while loop for i=1 to i=5 i.e., 5 times. Finally, another ‘a’ will be printed that is
outside of the while loop. Therefore, a total of 6 times ‘a’ is printed.
Assignment 7 July 2022 Solution

1. Which of the following statement/s are false?


I. Array elements are stored in memory in contiguous locations.
II. An integer array always terminates with ‘\0’ (NULL).

a) I
b) II
c) Both I and II
d) None
Solution: (b)

2. If two strings are identical, then strcmp() function returns


a) 1
b) 0
c) -1
d) None of these
Solution: (b)

3. Which of the following function is more appropriate for reading in a multi word
string?
a) scanf()
b) gets()
c) printf()
d) puts()

Solution: (b)

4. What will be printed after execution of the following code?


#include<stdio.h>
int main()
{
int a[20] = {10, 20, 30, 40,50,60};
printf("%d", 3[a]);
return 0;
}

Solution: 40
3[a] denotes the 4th element of the array.

5. What will be the output of the program?


#include<stdio.h>
int main()
{
char str[] = "Array\0String";
printf("%s", str);
return 0;
}
Assignment 7 July 2022 Solution

a) Array
b) Array String
c) Array\0String
d) Compilation error

Solution:(a)

6. What will be the output?


#include<stdio.h>
#include<string.h>
int main()
{
char str1[20] = "Programming", str2[20] = "Language";
printf("%s", strcpy(str2, strcat(str1, str2)));
return 0;
}

a) Programming
b) Language
c) ProgrammingLanguage
d) LanguageProgramming

Solution: (c) ProgrammingLanguage


str1=programming, str2=Language.
After strcat(str1,str2), str1= ProgrammingLanguage . And strcpy makes
str2=ProgrammingLanguage .

7. What will be the output?


#include <stdio.h>
int main()
{
char str1[] = "I-LOVE-C";
char str2[] = {'I', '-', 'L', 'O', 'V', 'E', '-', 'C'};
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= 8, n2=8
b) n1=9, n2=9
c) n1=8, n2=9
d) n1=9, n2=8

Solution: (d)
Assignment 7 July 2022 Solution

8. What will be the output?


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

Solution: 720 (short answer type)


This is a factorial program. 6!=720.

9. What will be the output?


#include<stdio.h>
#include<string.h>
int main()
{
char p[] = "assignment";
char t;
int i, j;
for(i=0, j=strlen(p); i<j; i++)
{
t = p[i];
p[i] = p[j-i];
p[j-i] = t;
}
printf("%s", p);
return 0;
}

a) assignment
b) tnemngissa
c) nothing will be printed
d) tttttttttt
Solution: (c) nothing will be printed as the string termination character ‘\0’ is assigned to first
element of array p[].

10. What will be the output?


#include<stdio.h>
#include<string.h>
int main()
{
char p[] = "welcome", q[]="welcome";
Assignment 7 July 2022 Solution

if(p==q)
{
printf("Two strings are equal");
}
return 0;
}

a) Two strings are equal


b) Two strings are not equal
c) Would not print anything
d) Compilation error

Solution: (c) No output, as we are comparing both base addresses and they are not same.
Assignment 8 July 2022 Solution

1. 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.
2. Which of the following statements are correct in C?
a) A function with the same name cannot have different signatures
b) A function with the same name cannot have different return types
c) A function with the same name cannot have a different number of parameters
d) All of the mentioned

Solution: (d) All of the statements mentioned are correct in C.

3. What will the function return?


int func(int x, int y)
{
if (y==0) return 0;
if (y ==1) return x;
return x+func(x, y-1);
}

a) x * y where x and y are integers


b) x * y where x and y are non-negative integers
c) x + y where x and y are integers
d) x + y where x and y are non-negative integers

Solution: (b) x * y where x and y are non-negative integers


Assignment 8 July 2022 Solution

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


#include <stdio.h>
void foo(), f();
int main()
{
f();
return 0;
}
void foo()
{
printf("2 ");
}
void f()
{
printf("1 ");
foo();
}

a) Compiler error as foo() is not declared in main


b) 12
c) 21
d) Compile time error due to declaration of functions inside main

Solution: (b) The function f() is called from the main. After printing ‘1’, foo() will be
called, and ‘2’ will be printed. This is an example of a nested function.

5. What will be the output?


#include <stdio.h>
int main()
{
switch(printf("C"))
{
default:
printf("Default");
case 1: printf("Choice1");
break;
case 2: printf("Choice2");
break;
}
return 0;
}

a) Choice1
b) CChoice1
Assignment 8 July 2022 Solution

c) DefaultChoice1
d) CChoice1Choice2

Solution: (b)

Solution: The first printf statement inside the switch prints ‘C’ and returns 1 (i.e. the
number of elements/characters inside printf) to the switch i.e. switch(1), therefore the
case 1 is invoked and Choice1 is printed. So, the whole answer is CChoice1.

6. What will be the output of the C code?


#include <stdio.h>
int main()
{
char x=0;
for(x=0; x<=127; x++)
{
printf("%d ", x);
}
return 0;
}

a) Compilation error
b) 0, 1, 2 …….., 127
c) 0, 1, 2, …….., 127, -128, -127,…infinite loop
d) 1, 2, 3…….,127

Solution: (c)

Because the character data type range varies from -128 to 127, after reaching the value of
127, the value of x changes to -128 by incrementing and therefore the loop runs all over
again, and the process continues.

7. 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));
Assignment 8 July 2022 Solution

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.

8. Consider the function


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

9. int fibonacci (int n)


{
switch (n)
{
default:
return (fibonacci(n - 1) + fibonacci(n - 2));
case 1:
case 2:
}
Assignment 8 July 2022 Solution

return 1;
}

The function above has a flaw that may result in a serious error during some
invocations.
Which one of the following describes the deficiency illustrated above?

(a) For some values of n, the environment will almost certainly exhaust its stack
space before the calculation completes.
(b) An error in the algorithm causes unbounded recursion for all values of n.
(c) A break statement should be inserted after each case. Fall-through is not
desirable here.
(d) The fibonacci() function includes calls to itself. This is not directly supported by
Standard C due to its unreliability.

Solution (a) For some values of n, the environment will almost certainly exhaust its stack
space before the calculation completes.

The function calls itself. So it's an example of recursion. As there is no proper terminating
condition for the function, for a large value of n, it will exhaust the storage space (stack),
which will stop the execution without a final result.

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("Result is=%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
Assignment 8 July 2022 Solution

Solution: The code essentially calculates the average of the provided numbers.
Assignment 9 July 2022 Solution

1. What is the best case complexity of ordered linear search and worst case complexity of
selection sort respectively?
a) O(1), O(n2)
b) O(logn), O(1),
c) O(n), O(logn)
d) O(n2), O(nlogn)

Solution: (a) O(1), O(n2)

2.Which of the following is/are correct?


I. Binary search is applied when elements are sorted.
II. Linear search can be applied when elements are in random order.
III. Binary search can be categorized into divide and conquer rule.

a) I & II
b) Only I
c) I and III
d) I, II & III

Solution: (d) I, II&III

3. What is the recurrence relation for the linear search recursive algorithm?

a) T(n-2)+c
b) 2T(n-1)+c
c) T(n-1)+c
d) T(n+1)+c

Solution: (c) T(n-1)+c

4. Given an array arr = {20, 45, 77, 89, 91, 94, 98,100} and key = 45; what are the mid values
(corresponding array elements) generated in the first and second iterations?
a) 91 and 98
b) 89 and 45
c) 89 and 77
d) 91 and 94

Solution: (b) 89 and 45


In the first iteration: Mid = arr[(0+7)/2] = arr[3] = 89
In the second iteration: Mid = arr[(0+2)/2] = arr[1] = 45
So, the mid values are 89 and 45
5. Consider an array of elements a[7]= {10,4,7,23,67,12,5}, what will be the resultant
array a after third pass of insertion sort.

a) 67, 12, 10, 5, 4, 7


b) 4, 7, 10, 23, 67, 12, 5
c) 4, 5, 7, 67, 10, 12, 23
Assignment 9 July 2022 Solution

d) 10, 7, 4, 67, 23, 12, 5

Solution: (b) 4, 7, 10, 23, 67, 12, 5


Array: 10, 4, 7, 23, 67, 12, 5

After 1st pass: 4, 10, 7, 23, 67, 12, 5 – 4 and 10 got swapped or 4 got inserted at its appropriate
position.

After 2nd pass: 4, 7, 10, 23, 67, 12, 5 – 7 got inserted at its appropriate position.

After 3rd pass: 4, 7, 10, 23, 67, 12, 5 – No change as the value 10 is placed at its appropriate
position already.

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

a) intunorderedLinearSearch(intarr[], int size, int data)


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

b) intunorderedLinearSearch(intarr[], int size, int data)


{
int index;
for(inti = 0; i< size; i++)
{
if(arr[i] == data)
{
break;
}
}
return index;
}
c) intunorderedLinearSearch(intarr[], int size, int data)
{
int index;
for(inti = 0; i<= size; i++)
{
if(arr[i] == data)
{
index = i;
Assignment 9 July 2022 Solution

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.

7. Which of the following input will give worst case time complexity for selection sort to sort
an array in ascending order?
I. 1, 2, 3, 4, 5, 6, 7, 8
II. 8, 7, 6, 5, 4, 3, 2, 1,
III. 8, 7, 5, 6, 3, 2, 1, 4

a) I
b) II
c) II and III
d) I,II and III

Ans : (d)
Selection sort has the same time complexity for best, average and worst cases. Therefore, all
the three arrays will be sorted at the same time complexity of O(n2).

8. 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.

*step 2: 1 4 5 9 3

*step 3: 1 3 4 5 9

9. A sorting technique is called stable if:


Assignment 9 July 2022 Solution

a) It takes O(nlog n)time


b) It maintains the relative order of occurrence of non-distinct elements
c) It uses divide and conquer paradigm
d) It takes O(n) space

Solution: (b) A sorting algorithm is called stable if it keeps elements with equal keys in the
same relative order in the output as they were in the input.

10. The average case occurs in the Linear Search Algorithm when
a) The item to be searched is in some where middle of the Array
b) The item to be searched is not in the array
c) The item to be searched is in the last of the array
d) The item to be searched is either in the last or not in the array
Solution: (a)

 The average case occurs in the Linear Search Algorithm when the item to be searched
is in some where middle of the Array.
 The best case occurs in the Linear Search Algorithm when the item to be searched is
in starting of the Array.
 The worst case occurs in the Linear Search Algorithm when the item to be searched is
in end of the Array.
So, option (A) is correct.
Week 10 Assignment Solutions

1. What is the output of the C program given below?


#include <stdio.h>
int main()
{
char str1[ ] = "NPTEL";
char str2[ ] = {'P', 'R', 'O', 'G', 'R', 'A', 'M', 'M', 'I', 'N', 'G'};
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 = 9, n2 = 10
b) n1 = 6, n2 = 11
c) n1 = 7, n2 = 12
d) n1 = 6, n2 = 10
Solution: (b) NPTEL when taken as a string, ends with a null character (\0). The Null
character in the C programming language is used to terminate the character strings. Therefore
the size of the str1 is 6 (NPTEL + Null). Whereas when PROGRAMMING is stored in str2,
they are taken as individual characters, as a result there is no null character at the end. Hence,
size of str2 is 11 (PROGRAMMING).

2. 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

3. 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

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)
Week 10 Assignment Solutions

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)
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. Which of the following statement is correct for the 2 arrays with respect to A and B.
int *x[5];
int *(y[5]);

A. Array of pointers
B. Pointer to an array

a) x is A, y is B
b) x is A, y is A
c) x is B, y is A
d) y is B, y is B

Solution: (b) They are both array of pointers.


Week 10 Assignment Solutions

8. Find the output of the following program


#include <stdio.h>
int main()
{
int *ptr, a = 5;
ptr = &a;
*ptr =*ptr - 3;
printf("%d,%d ", *ptr, a);
return 0;
}

Solution: 2,2 (short answer type)


The pointer variable ptr contains the address of the variable a. Incrementing the value at
address also modifies the content of a. Thus, both will be containing 2.

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)
𝑓(𝑥) = 𝑥𝑒 2𝑥 − 3𝑥 2 − 5

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

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. Which are true about interpolation?


I. It is a process for finding a value between two points on a line or curve.
II. Interpolation provides a mean for estimating functions at the intermediate points.

a) Only I
b) Only II
c) Both I & II
d) None of the above

Solution: (c) Both

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


𝑥 10 15 20
𝑓(𝑥) 19.45 10.63 7.82
The polynomial is determined as 𝑓 (𝑥 ) = 𝐿 (𝑥 ). (19.45) + 𝐿 (𝑥 ). (10.63) +
𝐿 (𝑥 ). (7.82)
The value of 𝑓(𝑥) at 𝑥 = 12 is

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

Solution: (c)
𝑥−𝑥 (12 − 15)(12 − 20) 24
𝐿 (𝑥 ) = = = = 0.48
𝑥 −𝑥 (10 − 15)(10 − 20) 50

𝑥−𝑥 (12 − 10)(12 − 20) −16


𝐿 (𝑥 ) = = = = 0.64
𝑥 −𝑥 (15 − 10)(15 − 20) −25

𝑥−𝑥 (12 − 10)(12 − 15) −6


𝐿 (𝑥 ) = = = = −0.12
𝑥 −𝑥 (20 − 10)(20 − 15) 50

So 𝑓(12) = 0.48 ∗ 19.45 + 0.64 ∗ 10.63 − 0.12 ∗ 7.82 = 15.2008

3. The value of ∫ 𝑥 𝑒 𝑑𝑥 by using one segment trapezoidal rule is

a) 5446.3
b) 5336.2
c) 4986.5
d) 5278.4

Solution: (a)
Week 11 Assignment Solution

𝑓 (𝑏) − 𝑓(𝑎)
𝑓(𝑥)𝑑𝑥 = (𝑏 − 𝑎)
2
Here, 𝑎 = 0, 𝑏 = 3, 𝑓(𝑎) = 0 and 𝑓(𝑏) = 3630.859. Hence, ∫ 𝑥 𝑒 𝑑𝑥 = 5446.3

4. To solve the ordinary differential equation


𝑑𝑦
10 + 𝑥 𝑒 = ycos(𝑥 ) + 𝑥𝑠𝑖𝑛(𝑦) , 𝑦(0) = 5
𝑑𝑥

using Runge-Kutta 4th order method, the equation is re-written as

a) = ycos(𝑥) + 𝑥𝑠𝑖𝑛(𝑦) , 𝑦(0) = 5

b) = (ycos(𝑥) + 𝑥𝑠𝑖𝑛(𝑦)) , 𝑦(0) = 5

c) = (ycos(𝑥) + 𝑥𝑠𝑖𝑛(𝑦) − 𝑥 𝑒 ), 𝑦(0) = 5

d) = 𝑥𝑠𝑖𝑛(𝑦) − 𝑥 𝑒 , 𝑦(0) = 5

Solution: (c) The LHS contains the derivative part. All other terms are shifted to right
side.

5. Given 4 + 𝑥 = 𝑦 , y(0.5)=2, and using a step size of h  0.2 , Find the value of
𝑦(0.7) using Runge-Kutta 4th order method is

a) 2.8634
b) 2.5546
c) 2.1865
d) 1.9856

Solution: (b) 2.5546

6. What will be the area under the curve using the Trapezoidal Rule

x 1.4 1.6 1.8 2.0 2.2


y 4.3215 4.7428 5.5205 6.0525 6.8762

a) 4.3829
b) 5.4863
c) 6.3427
d) 3.2857

Solution: (a) 4.3829


Week 11 Assignment Solution

Using Trapezoidal Rule


∫ydx=h2[y0+y4+2(y1+y2+y3)]

∫ydx=0.22[4.3215+6.8762+2×(4.7428+5.5205+6.0525)]

∫ydx=0.22[4.3215+6.8762+2×(16.3158)]

∫ydx=4.3829
Solution by Trapezoidal Rule is 4.3829

7. The real root of the equation 5x − 2cosx −1 = 0 (up to two decimal accuracy) is

[You can use any method known to you. A range is given in output rather than single
value to avoid approximation error]

a) 0.45 to 0.47
b) 0.35 to 0.37
c) 0.41 to 0.43
d) 0.53 to 0.56

Solution: (d) 0.53 to 0.56


8. Which is/are false?

I. The bisection method is guaranteed to work for finding roots of all continuous
functions.
II. Trapezoidal rule is a technique for approximating the indefinite integral.
III. Lagrange polynomial is used for Polynomial Interpolation.

a) Only I
b) Only II
c) II and III
d) None

Solution: (b) Trapezoidal rule is a technique for approximating the definite integral.

9. Find the root of 𝑥 − 𝑥 − 10 = 0 approximately upto 5 iterations using Bisection


Method. Let a = 1.5 and b = 2.

a) 1.68
b) 1.92
c) 1.86
d) 1.66

Solution: (c) 1.86


Week 11 Assignment Solution

10. 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)
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 above 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

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. The program will allocate ……. bytes to ptr. Assume sizeof(int) = 4.


#include<stdio.h>
#include<stdlib.h>

int main()
{
int *ptr;
ptr = (int*)malloc(sizeof(int)*4);
ptr = realloc(ptr,sizeof(int)*2);
return 0;
}

Solution: 8
First, using malloc 16 bytes were allocated. Thereafter, applying calloc the size is adjusted and
made 8.
Week 12 Assignment Solution

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;
++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. What is the output?


#include<stdio.h>
int main()
{
struct xyz{ int a;};
struct xyz obj1={1};
struct xyz obj2 = obj1;
printf("%d", obj2.a);
obj2.a = 100;
printf("%d", obj1.a);
printf("%d", obj2.a);
return 0;
}

a) 11100
b) 11
c) 11001
d) 11000

Solution: (a) 11100


7.
Week 12 Assignment Solution

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

(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. What does fp point to in the program?


#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen("hello", "r");
return 0;
}

a) The first character in the file


b) A structure which contains a char pointer which points to the first character of
a file.
c) The name of the file.
d) The last character in the file
Solution: (b) The fp is a structure which contains a char pointer which points to the first
character of a file.

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;
Week 12 Assignment Solution

int x = (sizeof(p1) / 3);


if (x == sizeof(int) + sizeof(char))
printf("True");
else
printf("False");
return 0;
}

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