You are on page 1of 4

Week 4 Assignment Solution

1. The loop which is executed at least once is


a “while” loop
b “do-while” loop
c “for” loop
d None of the above
Solution: (b) do-while loop is executed at least once even though the condition is false.
2. In the C programming language negative numbers when used in if-else conditional
checking, are treated as

a TRUE
b FALSE
c Depends on the implementation
d None of the above

Solution: (a) TRUE, all non-zero values are TRUE in C

3. Choose the correct statement to use “if-else” statement in C Language


a “else if” is compulsory to use with “if” statement.
b “else” is compulsory to use with “if” statement.
c “else” or “else if” is optional with the “if” statement.
d None of the above are correct
Solution: (c) “else” or “else if” is optional with the “if” statement

4. 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’ are printed
d Compilation error

Solution: (c) ‘a--’ post-increments 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
Week 4 Assignment Solution

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.

5. In the following example, tell which statement is correct

if( (condition1==1) && (condition2==1))


printf(“Swayam”);

a Condition1 will be evaluated first, and condition2 will be evaluated second


b Condition2 will be evaluated first, and condition1 will be evaluated second
c Condition1 will be evaluated first, condition2 will be evaluated only if the
condition1 is TRUE
d Condition2 will be evaluated first, and condition1 will be evaluated only if
condition2 is TRUE

Solution: (c) Condition1 will be evaluated first; condition2 will be evaluated only if the
condition1 is TRUE. This is called the Short-circuited evaluation of the operators

6. Which one of the following is the correct syntax for C Ternary Operator?
a) condition ? expression1 : expression2
b) condition : expression1 ? expression2
c) condition ? expression1 < expression2
d) condition < expression1 ? expression2
Solution: (a) If the condition is true, expression 1 is evaluated. If the condition is false, expression
2 is evaluated.

7. The purpose of the following program fragment is to


𝑏𝑏 = 𝑠𝑠 + 𝑏𝑏;
𝑠𝑠 = 𝑏𝑏 − 𝑠𝑠;
𝑏𝑏 = 𝑏𝑏 − 𝑠𝑠;
(where s and b are two integers)

a Transfer the content of s to b


b Transfer the content of b to s
c Exchange (swap) the content of s and b
d Negate the contents of s and b
Solution: (c) Exchange (swap) the content of s and b
Week 4 Assignment Solution

This is a well-known method to swap the two variables without using a third variable.
8. What will be the output?
#include <stdio.h>
int main()
{
int x=0;
x = printf("3");
printf("%d",x);
return 0;
}

a 11
b 33
c 31
d 13
Solution: (c ) 31
x = printf("3");
First printf prints 3. printf() returns 1. Now the variable x=1; So 1 is printed next.

9. What will be the output? (&& is logical AND operation)


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

a 0
12
b 1
11
c 0
00
d 0
11
Solution: (d)
Inside the first printf statement, i++ results in 0 (due to post increment operation). As the left
operand of && operator is zero, the compiler will not evaluate the right operand (i.e., ++j). So, j
is not incremented after the first printf statement. Finally, the result is i=1 and j=1.

10. What will be the value of a, b, and c after the execution of the following
int a = 5, b = 7, c = 111;
c /= ++a * b--;
Week 4 Assignment Solution

a a=5, b=6, c=2;


b a=6, b=7, c=1;
c a=6, b=6, c=2;
d a=5, b=7, c=1;

Solution: (c) ++a * b-- is computed as (a=a+1)*(b)  (6)*(7)=42


c/=42  c=c/42  c=111/42=2 (as c is integer)
Hence the right answer is a=6, b=6 and c=2

You might also like