Conditional Statement · Programming in C
5 questions · asked in 4 of 27 years (2000–2026)
MCQ 3 · NAT 21-mark 2 · 2-mark 3
2000Questions per year2026
Questions 1–5 of 5
Consider the following ANSI C program.
#include <stdio.h>
int main()
{
int i, j, count;
count=0;
i=0;
for (j=-3; j <=3; j++)
{
if (( j >= 0) && (i++))
count = count + j;
}
count = count +i;
printf("%d", count);
return 0;
}Which one of the following options is correct?Answer: (B)
(The loop goes from j = 3 up to 3, and the program compiles.) When j 0, the && stops early, so i++ is not run.
When j 0, i++ gives the old value of i:
- j = 0: i++ gives 0 (false), then i = 1
- j = 1: it gives 1 (true), count = 1, i = 2
- j = 2: count = 3, i = 3
- j = 3: count = 6, i = 4
Finally, count = 6 + i = 6 + 4 = 10
Answer: The program will compile successfully and output 10 when executed
When j 0, i++ gives the old value of i:
- j = 0: i++ gives 0 (false), then i = 1
- j = 1: it gives 1 (true), count = 1, i = 2
- j = 2: count = 3, i = 3
- j = 3: count = 6, i = 4
Finally, count = 6 + i = 6 + 4 = 10
Answer: The program will compile successfully and output 10 when executed
Consider the C program below.
#include <stdio.h>
int *A, stkTop;
int stkFunc(int opcode, int val)
{
static int size=0, stkTop=0;
switch (opcode) {
case -1: size = val; break;
case 0: if (stkTop < size) A[stkTop++] = val; break;
default: if (stkTop) return A[--stkTop];
}
return -1;
}
int main()
{
int B[20]; A = B; stkTop = -1;
stkFunc (-1, 10);
stkFunc ( 0, 5);
stkFunc ( 0, 10);
printf ("%d\n", stkFunc(1, 0) + stkFunc(1, 0));
} The value printed by the above program is __________.Answer: 15
In stkFunc, the static local stkTop (it starts at 0) hides the global stkTop.
- stkFunc(1, 10) sets size = 10.
- Then 5 is pushed, and then 10 is pushed.
- The two pops return 10 and then 5.
The sum is 10 + 5 = 15.
Answer: 15
- stkFunc(1, 10) sets size = 10.
- Then 5 is pushed, and then 10 is pushed.
- The two pops return 10 and then 5.
The sum is 10 + 5 = 15.
Answer: 15
Consider the following C program:
#include <stdio.h>
int main( )
{
int i, j, k = 0;
j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;
k -= --j;
for(i = 0; i < 5; i++)
{
switch(i + k)
{
case 1:
case 2: printf("\n%d", i+k);
case 3: printf("\n%d", i+k);
default: printf("\n%d", i+k);
}
}
return 0;
} The number of times printf statement is executed is ________.Answer: 10
Find j: 2*3/4 + 2.0/5 + 8/5 = 1 + 0.4 + 1 = 2.4 j = 2. After --j, j = 1, so k = 1.
The values of i + k are 1, 0, 1, 2, 3. There is no break, so the cases fall through:
- 1, 0: only default runs 1 + 1
- 1, 2: case 2, case 3, default 3 + 3
- 3: case 3, default 2
Total = 10
Answer: 10
The values of i + k are 1, 0, 1, 2, 3. There is no break, so the cases fall through:
- 1, 0: only default runs 1 + 1
- 1, 2: case 2, case 3, default 3 + 3
- 3: case 3, default 2
Total = 10
Answer: 10
What will be the output of the following C program segment?
char inChar = 'A' ;
switch ( inChar ) {
case 'A' : printf ("Choice A\n") ;
case 'B' :
case 'C' : printf ("Choice B") ;
case 'D' :
case 'E' :
default : printf ( " No Choice" ) ; } Answer: (C)
No break statement is present, so control falls through from case 'A' into every case after it.
The program prints "Choice A" and a newline, then "Choice B", then " No Choice".
Answer: Choice A / Choice B No Choice
The program prints "Choice A" and a newline, then "Choice B", then " No Choice".
Answer: Choice A / Choice B No Choice
Let x be an integer which can take a value of 0 or 1. The statement
if (x == 0) x = 1; else x = 0; is equivalent to which one of the following ? Answer: (B)
The statement must turn 0 into 1 and 1 into 0. Check each option for and :
(A) : for it gives 2. Wrong.
(B) : for it gives 1, and for it gives 0. Correct.
(C) : for it gives . Wrong.
(D) : for it needs , a remainder on division by zero, which is undefined. Wrong.
Answer: (B)
(A) : for it gives 2. Wrong.
(B) : for it gives 1, and for it gives 0. Correct.
(C) : for it gives . Wrong.
(D) : for it needs , a remainder on division by zero, which is undefined. Wrong.
Answer: (B)
Keep practising