Week 5 July 2023 Solution
Week 5 July 2023 Solution
1. The statement that transfers control to the beginning of the loop is called
a) break
b) continue
c) goto
d) None of the above
Solution: (b) continue
a) 5
b) 10
c) No output
d) Compilation error
Week 5 Assignment Solution
Solution: (c) 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) 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.
a) Error
b) I love C - will be printed 3 times
c) I love C - will be printed 6 times
d) I love C - will be printed 5 times
Solution: (b) I love C will be printed 3 times
a) 1
b) 2
c) 3
d) Error
Solution: (c) 3
exp1? exp2: exp3
(4 < 8) ? (5!= 1 < 5 == 0)? 1: 2 : 3;
9. The following program is used to find the reverse of a number using C language. Find the missing
condition inside while statement (indicated as ‘xxxx’).
#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;
while(xxxx)
Week 5 Assignment Solution
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
return 0;
}
a) n!=0
b) n==0
c) n%10==0
d) n/10==0
Solution: (a) The loop should be continued till the value of n becomes zero. Thus, the right option is n!=0.
10. Compute the printed value of i & j of the C program given below
#include <stdio.h>
int main()
{
int i = 0, j = 15;
while (i<8, j >9)
{
i++;
j--;
}
printf("%d, %d\n", i, j);
return 0;
}
a) 8,10
b) 8,9
c) 6, 9
d) 7, 10
Solution: (c) The while condition checks the last condition (i.e. j>9) and till the condition is satisfied the block inside
the loop is executed. Thus the loop is run for 6 times. i will be incremented by 6 and j will be decremented by 6. The
final values of i and j will be i=6 and j=9.