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

Assignment Solution 5 Jan 2020

The document provides solutions to 10 coding questions related to C programming concepts like loops, conditional statements, operators, etc. Some key points summarized: 1) Question 1's code prints numbers from 1 to 10 using a for loop. 2) Question 2's solution is that continue statement is used to stop the current iteration and begin the next from the start. 3) Question 3's code prints 5 for both i and j after running a while loop 5 times and incrementing both variables each iteration.

Uploaded by

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

Assignment Solution 5 Jan 2020

The document provides solutions to 10 coding questions related to C programming concepts like loops, conditional statements, operators, etc. Some key points summarized: 1) Question 1's code prints numbers from 1 to 10 using a for loop. 2) Question 2's solution is that continue statement is used to stop the current iteration and begin the next from the start. 3) Question 3's code prints 5 for both i and j after running a while loop 5 times and incrementing both variables each iteration.

Uploaded by

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

WEEK 5 ASSIGNMENT SOLUTION

1. What will be printed when the following code is executed?


#include<stdio.h>
int main()
{
int i=0;
for(;i<=9;)
{
i++;
printf("%d “, i);
}
return 0;
}
a) 0 1 2 … 9
b) 0 1 2 … 10
c) 1 2 3 … 9
d) 1 2 3 … 10
Solution: (d) initially i =0. So, the for condition is TRUE till i=9. Therefore, every time the incremented
value of i starting from 1 to 10 will be printed.

2. Continue statement used


a) to continue to the next line of code
b) to debug
c) to stop the current iteration and begin the next iteration from the beginning
d) None of the above
Solution: (c)

3. Compute the printed value of i of the C code given below


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

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

4. The following program takes n, a positive integer as input.


What is the purpose of the program?
#include <stdio.h>
int main()
{
int n, i;
unsigned long long result = 1;
printf("Enter an integer: ");
scanf("%d", &n);

for(i=1; i<=n; ++i)


{
result*= i;
}
printf("The output of the program is %llu", result);
return 0;
}

a) n multiplied n times
b) factorial of n
c) display factors of n
d) display Fibonacci series upto n.

Solution: (b) In the for loop, 1 to n is multiplied. This computes the factorial of the number n.

5. What will be the output?


#include <stdio.h>
int main()
{
switch(printf("IIT"))
{
default:
printf(" Guwahati");
case 1: printf(" Delhi");
break;
case 2: printf(" Kharagpur");
break;
case 3: printf(" Madras");
break;
}
return 0;
}

a) IIT Delhi
b) IIT Kharagpur
c) IIT Madras
d) IIT Guwahati

Solution: (c)
WEEK 5 ASSIGNMENT SOLUTION

printf(“IIT”) prints IIT and counts the number of characters inside it which is 3 here. Therefore, the case 3
i.e. Madras will be printed next.
6. What will be the output?
#include <stdio.h>
int main()
{
if((0 && 1)||(1 && -1))
printf("Condition is true.");
else
printf("Condition is false.");
return 0;
}

a) Condition is true
b) Condition is false
c) Error
d) No output possible

Solution: (a)
(0 && 1) is 0, (1 && -1) is 1. (0 OR 1) is 1 hence if conditions is true and "Condition is true." is printed.

7. What will be the output of the following code?


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

a) 1 2 3 4 5
b) 1 2 4 5
c) 1 2
d) 4 5
Solution: (c) Initially, c=1 which satisfies the while condition. It prints the value till c = 2. When c becomes
3 the if condition satisfies and the break the while loop. Therefore, 1 and 2 are printed.

8. What will be output of the C code?


#include <stdio.h>
int main()
{
WEEK 5 ASSIGNMENT SOLUTION

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,……., -2, -1, 0, 1, ……. infinite loop
d) 1, 2, 3…….,127

Solution: (c) The range of character variable is from -128 to 127. It is due to 1 Byte of memory allocation
i.e. -2^7 to 2^7-1. When x= 127, in the next iteration of the for loop, x is incremented to -128 that satisfies
the condition again. Therefore, the value of x runs in an infinite loop.

9. The following if-block inside a function is intended to check whether n


is a leap year. The expression in the blank is _________________________.
if(n%100 == 0){
if (________){
printf ("%d is a leap year.\n”, n);
return 0;
}
}
if(n%4 == 0){
printf ("%d is a leap year.\n”, n);
return 0;
}

a) n==4
b) n%400 != 0
c) n>0
d) n%400 == 0
Solution: (d) This is the logic to decide whether a year is leap year or not.
10. 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
WEEK 5 ASSIGNMENT SOLUTION

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.

You might also like