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

Week 5 Jan 2025 Assignment Solution

The document provides solutions to a series of C programming questions, including topics such as control transfer, loop execution, and output of specific code snippets. Key concepts discussed include the ternary operator, do-while loops, and the behavior of increment and conditional statements. The solutions clarify the expected outputs and explain the reasoning behind each answer.

Uploaded by

DHARSHNI THIRU
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)
5 views

Week 5 Jan 2025 Assignment Solution

The document provides solutions to a series of C programming questions, including topics such as control transfer, loop execution, and output of specific code snippets. Key concepts discussed include the ternary operator, do-while loops, and the behavior of increment and conditional statements. The solutions clarify the expected outputs and explain the reasoning behind each answer.

Uploaded by

DHARSHNI THIRU
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

ASSIGNMENT SOLUTION 5

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


a) Ternary operator
b) Unary operator
c) Logical operator
d) None of the above are true

Solution: (a) Ternary operator


In C, the ternary operator (? :) allows for a three-way transfer of control. It's a
shorthand for an if-else statement, allowing you to choose between two values based
on a condition.

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
In a do-while loop, the statements inside the loop are executed at least once, even if the
condition is false, because the condition is checked after the loop executes.

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

In each iteration, the value of k will be:


ASSIGNMENT SOLUTION 5

Iteration 1: k = 0.5 → prints "I love C"


Iteration 2: k = 1.5 → prints "I love C"
Iteration 3: k = 2.5 → prints "I love C"
Iteration 4: k = 3.5 → fails the condition k < 3, and the loop ends.

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.
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
ASSIGNMENT SOLUTION 5

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);
}
return 0;
}

a) Syntax error
ASSIGNMENT SOLUTION 5

b) 0 1 2 3 4 5 6 7 8 9 10
c) 1 2 3 4 5 6 7 8 9 10
d) 0 1 2 3 4 5 6 7 8 9
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.
Initially, i = 0.
On the first iteration, i is incremented to 1, and 1 is printed.
On the second iteration, i becomes 2, and 2 is printed.
This continues until i becomes 10. At this point, the condition i == 10 becomes true, and the break
statement is executed, exiting the loop.

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;
ASSIGNMENT SOLUTION 5

j+=1;
}
printf("%d, %d", k,j);
return 0;
}

a) 5,4
b) 7,4
c) 5,6
d) 6,4
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy