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

Assignment 5 July 2022 Solution

The document contains solutions to 10 questions about C programming concepts like loops, conditions, and output. For question 1, the code would produce no output as the loop condition is invalid. Question 10 is answered that the code would not print anything due to the continue statement skipping the print.

Uploaded by

Subanandhini S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Assignment 5 July 2022 Solution

The document contains solutions to 10 questions about C programming concepts like loops, conditions, and output. For question 1, the code would produce no output as the loop condition is invalid. Question 10 is answered that the code would not print anything due to the continue statement skipping the print.

Uploaded by

Subanandhini S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
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 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.

You might also like