Week 5 Jan 2025 Assignment Solution
Week 5 Jan 2025 Assignment Solution
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.
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
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.
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.
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.
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.
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.