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

Assignment 5 Jan 2024

Uploaded by

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

Assignment 5 Jan 2024

Uploaded by

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

Week 5 Assignment Solution

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


a) Ternary operator
b) Unary operator
c) Logical operator
d) None of the above
Solution: (a) Ternary operator
2. The loop in which the statements within the loop are executed at least once is called
a) for
b) do-while
c) while
d) goto
Solution: (b) do-while

3. 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 6 times
c) I love C -- will be printed 3 times
d) I love C –will be printed 5 times
Solution: (c) I love C will be printed 3 times

4. What is the output of the following code?


#include <stdio.h>
int main()
{
int i=1;
do
{
printf("while vs do-while\n");
}while(i==1);
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.
Week 5 Assignment Solution

5. Find the output of the following C program


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

a) 0
b) 1
c) No output
d) Compiler error
Solution: (d) Break statement is applicable in loop and switch statements. It is not allowed inside if
statement. Thus the program will show compiler error.

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


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

a) 5
b) 4
c) 1
d) No output
Solution: (d) 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.

7. What will be the output?


#include <stdio.h>
int main()
{
int i=0;
for(;;)
{
if(i==10)
break;
printf("%d ", ++i);
Week 5 Assignment Solution

}
return 0;
}

a) Syntax error
b) 0 1 2 3 4 5 6 7 8 9 10
c) 1 2 3 4 5 6 7 8 9 10
d) 0123456789
Solution: (c)

for(; ;) is possible in c, there is no need to place condition with in the for(), you can place condition within
the body of the loop. The ++i makes it printing from 1 to 10.

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

9. What is the output of the below C program?


#include <stdio.h>
int main()
{
short int k=1, j=1;
while (k <= 4 || j <= 3)
{
k=k+2;
j+=1;
}
printf("%d,%d", k, j);
return 0;
}
a) 5,4
b) 7,4
c) 5,6
d) 6,4
Week 5 Assignment Solution

Solution: (b) The loop will be continued till any of the condition k<=4 or j<=3 is satisfied. So, the loop will be
executed 3 times. Thus, the value of k and j would be 7 and 4.

10. What will be the value of ‘i’ after the execution of the program below
#include <stdio.h>
int main()
{
int i=1,j;
for(j=0;j<=10;j+=i)
{
i=i+j;
}
return 0;
}

a) 10
b) 11
c) 12
d) 13
Solution: (d) The value of j will reach to 8 and i will be 13. In the next iteration, j will become 8+13=21 and the
condition inside for loop will be invalid, thus the compilation will come out of the loop. So, the value of i will be 13.

You might also like