0% found this document useful (0 votes)
44 views5 pages

Quiz On C 3

This document provides solutions to 15 questions related to C programming concepts like loops, conditional statements, operators, etc. Some key points summarized: 1) The '&' operator in scanf indicates the memory location where a variable's value will be stored after input. 2) A program that checks if a variable equals 0 but the variable is initialized to 1 will print nothing, as the if condition is false. 3) Pre-increment operator increments the variable before use, so the code printing 4 is incrementing i to 2, then adding i+i. 4) Both if-else and switch are conditional statements in C.

Uploaded by

Cherry
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)
44 views5 pages

Quiz On C 3

This document provides solutions to 15 questions related to C programming concepts like loops, conditional statements, operators, etc. Some key points summarized: 1) The '&' operator in scanf indicates the memory location where a variable's value will be stored after input. 2) A program that checks if a variable equals 0 but the variable is initialized to 1 will print nothing, as the if condition is false. 3) Pre-increment operator increments the variable before use, so the code printing 4 is incrementing i to 2, then adding i+i. 4) Both if-else and switch are conditional statements in C.

Uploaded by

Cherry
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 4 ASSIGNMENT SOLUTION

1. In C program ‘&’ is used in ‘scanf’ to indicate


a) AND operation
b) Memory location
c) Value of the variable
d) Value at the memory location.
Solution: (b) ‘&’ is used to indicate the memory location where the value of the variable will be stored
after taking input from standard input device.
2. What is the output of the following C code?
#include <stdio.h>
int main()
{
int x = 1;
if (x == 0)
if (x >= 0)
printf("true\n");
else
printf("false\n");
return 0;
}

a) true
b) false
c) Depends on computer
d) Nothing is printed

Solution: (d) x is initialized with 1 and the “if” statement compares it with 0, thus the “if condition” is
“false” and the nested “if” statements do not get executed. Hence, the program do not prints anything.

3. Compute the printed value of i for the C program given below


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

Solution: 4 is printed. Since ++ is pre-increment operator. It increments the operator by one and then
assignment is done. Thus the incremented value of i which becomes 2 and added to it and the result is 4.

4. The control/conditional statements used in C is/are


a) if-else statements
b) switch statements
c) Both (a) and (b)
d) None of these
Solution: (c) Both if-else and switch statements are conditional statements in C.
WEEK 4 ASSIGNMENT SOLUTION

5. 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’ will be printed
b) ‘False’ will be printed
c) Both ‘True’ and ‘False’ will be printed
d) Nothing will be printed

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.

6. Switch statement accepts


a) int
b) char
c) long
d) All of the above
Solution: (d) Integer, character and long constants are accepted in switch statements.

7. What is the output of the following program?


#include <stdio.h>
int main()
{
int k = 1;
for (k)
printf("Hello");
return 0;
}
a) ‘Hello’ will be printed once
b) No output
c) Varies depending on computer
d) Compilation error

Solution: (d) The correct syntax of for loop is not written, thus the compiler will return error.

8. The loop which is executed at least once is


a) while
b) do-while
c) for
d) none of the above
WEEK 4 ASSIGNMENT SOLUTION

Solution: (b) do-while loop is executed at least once even though the condition is false.
9. If multiple conditions are used in a single if statement then the testing of those conditions are done
a) From Right to Left
b) From Left to right
c) Randomly
d) None

Solution: (b) Multiple conditions are tested from Left to right


10. What will be the value of a, b, c after the execution of the followings

int a=10,b=7,c=125;
c /= ++a * --b;
a) a=11, b=7,c=0;
b) a=10, b=6,c=1;
c) a=11, b=6,c=1;
d) a=10, b=7,c=0;
Solution: (c) ++a * --b is computed as (a=a+1)*(b=b-1)  (11)*(6)=66
c/=66  c=c/66  c=125/66=1 (as c is integer)
Hence the right answer is a=11, b=6 and c=1

11. Which statement is correct for the following lines?

switch(month)
{
case < 30:
printf(“It’s February”);
default:
printf(“It’s not February”);
}

a) It’s perfectly fine


b) It will print both statements
c) It will give a compilation error
d) More information required

Solution: (c) Compilation error. Because only equality can be checked in case. Not <,>,<=,>= etc.
12. How many times “Hello” will be printed when the following code is executed?
#include<stdio.h>
int main()
{
int i=6;
while(--i>0)
printf("Hello\t");
return 0;
}
WEEK 4 ASSIGNMENT SOLUTION

Solution: 5
We have declared and initialize the variable i = 6, the condition is (--i > 0). Here i is pre-decremented once
by one until it becomes 1. It will be 5, 4, 3, 2 and 1 for each iteration.
13. What will be the output of the following program?
#include <stdio.h>
int main()
{
int x = 1;
switch (x)
{
case 1: printf("Choice is 1 \n");
default: printf("Choice other than 1 \n");
}
return 0;
}
a) Choice is 1
b) Choice other than 1
c) Both (a) and (b)
d) Syntax error
Solution: (c)
Since “break;” statement is not used after print statement, it will execute the default instruction as well.
14. What will be the output?
#include <stdio.h>
int main () {
int a = 10;
do {
printf("%d ", a);
a++;
}while( a >= 20 );
printf("%d ", a);
return 0;
}
a) 10 10
b) 10 11 12 … 20 20
c) 10 11 12 … 20 21
d) 10 11
Solution: (d)
The loop body is executed once due to false conditional checking. Hence, 10 is printed within the loop. While
exiting the loop the value of a is incremented to 11. So, after the loop 11 will be printed.
15. What will be printed when the following code is executed?
#include <stdio.h>
int main ()
{
int i;
WEEK 4 ASSIGNMENT SOLUTION

for(i=0;i<5;++i);
printf("%d ",i);
return 0;
}
a) 012345
b) 01234
c) 5
d) 0
Solution: (c)
“printf” is a separate instruction as it is not included within the loop. There is a semicolon “;” after the for
loop. After the execution of loop printf statement prints the value of i (as updated in the loop) i.e. 5.

You might also like