Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

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

You might also like