Assignment 3 July 2022 Solution
Assignment 3 July 2022 Solution
#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.
#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.
}
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.
#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.
a) Only I
b) Only II
c) Both I and II
Week 3: Assignment Solution
Solution: (a) The ‘else’ block is executed when condition is false. One ‘if’ statement have only one ‘else’
statement.
Solution: (b) modulo division operator ‘ %’ can be applied only on int variables.
#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.
#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.
#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.
#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;
}
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