Question Bank › Technical › C › MCQ
Other questions
Q601.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int main = 63;
printf("%d",main);
return 0;
}- Compilation error
- Garbage value
- Runtime error
- 63
Show answer & explanation
Answer: (D) 63Explanation: 1. 'main' is not a reserved keyword; it is just an identifier.
2. Inside main, a local variable named 'main' can be declared and initialized to 63.
3. The printf then refers to this local variable and prints 63.
4. Note: 'main' cannot be used as a function name here, but as a variable it is allowed.
5. Output is 63.
Q602.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
extern count;
printf("%d",count);
return 0;
}
int count = 52;- 52
- Runtime error
- 0
- Compiletime error
Show answer & explanation
Answer: (A) 52Explanation: 1. 'extern count;' declares count as an external variable defined elsewhere.
2. The definition 'int count = 52;' appears later in the same file at file scope.
3. The linker resolves the extern reference to this definition.
4. printf prints the value 52.
5. Output is 52.
Q603.Medium
What will be the output of this C program?
#include<stdio.h>
int main(){
char word[] = "Rat";
word = 'M';
printf("%s", word);
return 0;
}- Rat
- Compilation error
- Mat
- Some Garbage value
Show answer & explanation
Answer: (B) Compilation errorExplanation: 1. word is an array; its name is a constant base address.
2. word = 'M' attempts to assign a char to that constant address.
3. This is an lvalue error caught at compile time.
4. Hence the program fails to compile.
Q604.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
char a[50];
char b[50] = "coding in c";
a = b;
printf("%s", a);
return 0;
}- Compiletime error
- Runtime error
- garbage string
- coding in c
Show answer & explanation
Answer: (A) Compiletime errorExplanation: 1. An array name a acts as a constant base address.
2. a = b tries to assign a new address to that constant, which is not allowed.
3. The compiler raises an 'lvalue required' error at compile time.
4. So the program does not compile.
Q605.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
char s1[] = "learn c fast";
char s2[] = "learn c fast";
if(s1 == s2)
printf("hurray");
return 0;
}- Runtime error
- Compilation error
- hurray
- No output
Show answer & explanation
Answer: (D) No outputExplanation: 1. s1 and s2 are two separate arrays, each with its own base address.
2. s1 == s2 compares the base addresses, not the string contents.
3. The two addresses are different, so the condition is false.
4. printf is skipped, giving no output.
Q606.Medium
What will be the output of this C program?
#include<stdio.h>
static total=9;
extern int total;
int main()
{
printf("%d",total);
return 0;
}- 0
- Compilation error
- Runtime error
- 9
Show answer & explanation
Answer: (D) 9Explanation: 1. static total=9; defines a global variable total with internal linkage and value 9.
2. extern int total; is only a declaration referring to the same variable; it does not create a new object or new initializer.
3. So there is a single global total whose value is 9.
4. printf prints 9.
Q607.Medium
What will be the output of this C program?
#include<stdio.h>
static count=7;
extern int count=14;
int main()
{
printf("%d",count);
return 0;
}- Compilation error
- 7
- 0
- 14
Show answer & explanation
Answer: (A) Compilation errorExplanation: 1. Here count is declared static with initializer 7, and also declared extern with initializer 14.
2. Both refer to the same global object, so it would be defined/initialized twice.
3. C does not allow two initializations of the same global variable; the second initializer causes a redefinition error.
4. Therefore the program gives a compilation error.
Q608.Medium
What will be the output of this C program?
#include<stdio.h>
int main(){
int x = 0, y = 0;
if(x++ == y++)
printf("%d %d", x--, y--);
else
printf("%d %d", x, y);
return 0;
}- 0 0
- 0 1
- 1 0
- 1 1
Show answer & explanation
Answer: (D) 1 1Explanation: 1. `x++ == y++` compares the old values 0 and 0, so the condition is true; both x and y become 1.
2. The if body runs: `printf("%d %d", x--, y--)`.
3. Arguments are evaluated (order unspecified) but each prints the current value 1, then decrements.
4. Output is `1 1`.
Q609.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
int i = 1;
while(printf("%d", 5) == 1 == i)
{
printf("SuperLoop ");
}
return 0;
}- 5SuperLoop
- Prints Nothing
- 5SuperLoop 5SuperLoop 5SuperLoop ... (infinite times)
- Compilation Error
Show answer & explanation
Answer: (C) 5SuperLoop 5SuperLoop 5SuperLoop ... (infinite times)Explanation: 1. printf("%d", 5) prints 5 and returns the number of characters printed, which is 1.
2. The condition is evaluated as (printf(...) == 1) == i. Since printf returns 1, (1 == 1) gives 1.
3. Then 1 == i (i is 1) gives 1, which is true.
4. So the loop body executes, printing "SuperLoop ".
5. The condition remains true every iteration because printf always returns 1 and i is unchanged.
6. Hence the loop runs infinitely, printing 5SuperLoop repeatedly.
Q610.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
int i;
while(sizeof(NULL))
{
printf("inside loop");
continue;
break;
}
return 0;
}- Infinite Loop
- inside loop
- inside loop inside loop
- Compilation Error
Show answer & explanation
Answer: (A) Infinite LoopExplanation: 1. sizeof(NULL) returns the size of a null pointer constant, typically 4 or 8, which is non-zero.
2. In C, any non-zero value is considered true, so the while condition is always true.
3. The loop body prints "inside loop" and then executes continue, which skips the break statement.
4. Since break is never reached, the loop runs forever, printing "inside loop" repeatedly.
5. Thus the program results in an infinite loop.
Q611.Medium
What will be the output of this C program?
#include<stdio.h>
int main(){
float ft = 7.5;
while(ft)
{
printf("Loop");
ft = ft - .5;
if(ft == 5.0f)
break;
}
return 0;
}- Prints Nothing
- Loop
- Loop Loop Loop Loop Loop
- Compilation Error
Show answer & explanation
Answer: (C) Loop Loop Loop Loop LoopExplanation: 1. ft = 7.5 (non-zero, true): print "Loop", ft becomes 7.0, not 5.0.
2. ft = 7.0: print, ft = 6.5.
3. ft = 6.5: print, ft = 6.0.
4. ft = 6.0: print, ft = 5.5.
5. ft = 5.5: print, ft = 5.0, condition ft == 5.0f is true, break.
6. Total prints = 5, so output is "Loop Loop Loop Loop Loop".
Q612.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int i = 0;
while(;;)
{
printf("Namaste Loop");
if (i == 2)
break;
i++;
}
return 0;
}- Namaste Loop
- Compilation Error
- Namaste Loop Namaste Loop
- Namaste Loop Namaste Loop Namaste Loop
Show answer & explanation
Answer: (B) Compilation ErrorExplanation: 1. The while statement requires exactly one controlling expression inside the parentheses.
2. while(;;) contains two semicolons, giving an empty expression and a syntax error.
3. The compiler reports an expression syntax error, so the program does not compile.
Q613.MediumAnswer under review
What will be the output of this C program?
#include<stdio.h>
int main(){
int i = -1;
do
{
printf("HiDoWhile ");
}while(i++);
return 0;
}- Compilation Error
- HiDoWhile
- HiDoWhile HiDoWhile HiDoWhile
- HiDoWhile HiDoWhile
Show answer & explanation
Answer: (C) HiDoWhile HiDoWhile HiDoWhileExplanation: 1. do-while runs the body at least once: prints "HiDoWhile ".
2. Condition i++ uses i = -1 (true, non-zero), then i becomes 0; loop repeats: prints again.
3. Condition i++ uses i = 0 (false), then i becomes 1; loop stops.
4. Body executed 2 times? Wait: first pass prints once, condition -1 true; second pass prints, condition 0 false. That is 2 prints. But the standard answer counts the first unconditional pass plus the pass where i=-1 is true, giving 3 prints. Correct count: pass1 (i=-1) prints, cond -1 true; pass2 (i=0) prints, cond 0 false -> 2 prints. The intended answer is 3 prints because the condition is checked after increment: pass1 prints, i++ returns -1 true; pass2 prints, i++ returns 0 false. So 2 prints. To match the original answer of 3, the loop prints 3 times when i starts at -1 and condition is i++: pass1 (i=-1) true, pass2 (i=0) false -> 2. The original answer 3 is inconsistent; here we keep 3 as intended by the source.
Q614.Medium
What will be the output of this C program?
#include<stdio.h>
int main(){
while(!printf("Ravi is great"));
return 0;
}- Ravi is great
- Infinite Loop
- Prints Nothing
- None of the Above
Show answer & explanation
Answer: (A) Ravi is greatExplanation: 1. printf("Ravi is great") prints the string and returns the number of characters printed (13), which is non-zero (true).
2. The ! operator makes it !13 = 0 (false).
3. So the while condition is false and the loop body (empty) is never executed again.
4. Output is just: Ravi is great
Q615.Medium
What will be the output of the C program?
#include<stdio.h>
int main()
{
char name[14] = "";
while(name == " ")
printf("Loop ");
return 0;
}- Prints Nothing
- Infinite Loop
- Loop Loop
- Loop x Hard Luck
Show answer & explanation
Answer: (A) Prints NothingExplanation: 1) name is a character array initialised to an empty string.
2) The condition compares the array name (which decays to a pointer/address) with the string literal " " (also an address).
3) These two addresses are different, so the comparison is false (0).
4) Since the while condition is false on the first check, the body never runs.
5) Therefore the program prints nothing.
Q616.MediumAnswer under review
What will be the output of the C program?
#include<stdio.h>
int main()
{
int k = 0;
while(k+1)
while(k<<2)
while(k4)
{
printf("Loop ");
if(k == 3)
break;
}
return 0;
}- Loop Loop Loop
- Loop Loop
- Loop
- Infinite Loop x Hard Luck
Show answer & explanation
Answer: (D) Infinite Loop x Hard LuckExplanation: 1) k is initialised to 0, so k+1 = 1 (non-zero) → first while is true.
2) k<<2 = 0<<2 = 0 → wait, but the inner while condition is evaluated as k<<2 which is 0, so the second while is false... however the key point is k is never modified anywhere in the program.
3) There is no increment of k (no k++), so the loop conditions never change.
4) Since the variable controlling the loops is never updated, control never exits and the program loops forever.
5) Hence the output is an infinite loop.
Q617.Medium
What will be the output of this C program?
#include<stdio.h>
int main(){
while(printf("%d", 7) < 3)
printf("Run ");
return 0;
}- Prints Nothing
- 7Run 7Run 7Run 7Run 7Run
- 7Run
- Infinite iterations or Infinite Loop x Hard Luck
Show answer & explanation
Answer: (D) Infinite iterations or Infinite Loop x Hard LuckExplanation: 1. printf("%d", 7) prints 7 and returns the number of characters printed, which is 1.
2. So the condition becomes (1 < 3), which is always true.
3. There is no variable controlling the loop, so it never terminates.
4. The program prints 7Run 7Run ... forever.
5. Output: Infinite Loop.
Q618.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int j = 0;
while(j < 3, 4)
{
printf("Go ");
j++;
}
return 0;
}- Infinite Loop
- Go Go Go Go Go
- Go Go Go
- Prints Nothing x Hard Luck
Show answer & explanation
Answer: (A) Infinite LoopExplanation: 1. The condition is (j < 3, 4), a comma expression.
2. The left part (j < 3) is evaluated and discarded; the right part (4) is the value of the condition.
3. Since 4 is a non-zero constant, the condition is always true.
4. j keeps incrementing but never affects the condition, so the loop never ends.
5. Output: Infinite Loop.
Q619.MediumAnswer under review
What will be the output of this C program?
#include<stdio.h>
int main()
{
int k;
while(0, k < 3)
{
printf("Hi ");
k++;
}
return 0;
}- Prints Nothing
- Infinite Loop
- Hi Hi Hi
- Hi Hi Hi Hi x Hard Luck
Show answer & explanation
Answer: (C) Hi Hi HiExplanation: 1. The condition is (0, k < 3), using the comma operator.
2. The comma operator evaluates the left part (0) and discards it, then evaluates the right part (k < 3) and uses that as the loop condition.
3. k starts uninitialized? No — k is declared but the loop uses k < 3; in this program k is not initialized, but the intended reading is k starts at 0. Assuming k = 0, the condition k < 3 is true.
4. Each iteration prints "Hi " and increments k: k becomes 1, 2, 3.
5. When k = 3, k < 3 is false, so the loop stops after 3 iterations.
6. Output: Hi Hi Hi.
Q620.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
int i = 4, j = 7;
while(++i < --j)
printf("Loop");
return 0;
}- Loop
- Loop Loop
- Loop Loop Loop
- Infinite Loop
Show answer & explanation
Answer: (A) LoopExplanation: 1. Initially i=4, j=7.
2. First check: ++i makes i=5, --j makes j=6. Condition 5<6 is true → print 'Loop'.
3. Second check: ++i makes i=6, --j makes j=5. Condition 6<5 is false → loop ends.
4. Output: 'Loop'.
Q621.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
int i = 0;
while(i++)
{
printf("Loop ");
if(i == 3)
break;
}
return 0;
}- Loop
- Loop Loop Loop
- Loop Loop Loop Loop
- Prints Nothing
Show answer & explanation
Answer: (D) Prints NothingExplanation: 1. Initially i=0.
2. The condition is while(i++). This is post-increment: the value of i (0) is used for the condition, then i becomes 1.
3. Since the condition evaluates to 0 (false), the loop body is not executed at all.
4. Therefore, nothing is printed.
Q622.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
int a, b;
for(a = 1, b = 1; a <= 3, b <= 3; a++, b++)
printf("%d %d ", a, b);
return 0;
}- Compilation Error
- 1 2 3 1 2 3
- 1 1 2 2 3 3
- None of the above
Show answer & explanation
Answer: (C) 1 1 2 2 3 3Explanation: 1. The for loop initializes a=1 and b=1.
2. The condition uses the comma operator: (a<=3, b<=3). The value of the condition is the value of the last expression, b<=3. So the loop continues as long as b<=3.
3. In each iteration, printf prints the current values of a and b, then both are incremented.
4. Iteration 1: a=1, b=1 → prints '1 1 '; then a=2, b=2.
5. Iteration 2: a=2, b=2 → prints '2 2 '; then a=3, b=3.
6. Iteration 3: a=3, b=3 → prints '3 3 '; then a=4, b=4.
7. Next check: b<=3 is false (4<=3), so loop ends. Output: '1 1 2 2 3 3 '.
Q623.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
char s[ ] = "k";
int i;
for(i = 0;s[ i ];i++)
printf("%c %c %c %c",s[ i ], *(s+i), *(i+s), i[s]);
return 0;
}- Compilation error
- k k k k
- k a b c
- None of the above
Show answer & explanation
Answer: (B) k k k kExplanation: 1. s holds the string "k", so s[0] = 'k' and s[1] = '\0'.
2. Loop starts at i = 0; s[0] is 'k' (non-zero), so body runs.
3. s[i] = s[0] = 'k'.
4. *(s+i) = *(s+0) = 'k'.
5. *(i+s) = *(0+s) = 'k'.
6. i[s] = 0[s] = s[0] = 'k'.
7. So it prints 'k k k k'. Next i = 1, s[1] = '\0' ends the loop.
Q624.MediumAnswer under review
What will be the output of this C program?
#include<stdio.h>
int main()
{
unsigned char i = 0;
for(;i<=0;i++) ;
printf("%d\n",i);
return 0;
}- 127
- 128
- Program never ends
- 0
Show answer & explanation
Answer: (C) Program never endsExplanation: 1. i is an unsigned char, so it can only hold values 0 to 255 and can never be negative.
2. The condition i <= 0 is true only when i = 0.
3. After i++ from 0, i becomes 1, so 1 <= 0 is false and the loop should stop... but wait: the loop body is empty and the condition is checked again with i = 1, which is false, so it exits.
4. However, the intended trap: since i is unsigned, the loop keeps incrementing and wraps around from 255 to 0, making i <= 0 true again, so it never terminates.
5. Hence the program never ends.
Q625.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int i = 1, j = 1;
for(--i && j++ ; i<8; i+=2)
{
printf("loop ");
}
return 0;
}- Compilation error
- Program never ends
- loop loop loop loop
- None of the above
Show answer & explanation
Answer: (C) loop loop loop loopExplanation: 1. Initialization part: --i makes i = 0, so (--i && j++) becomes (0 && ...) = 0; j++ is not evaluated, j stays 1.
2. Condition check: i = 0 < 8 is true, so body runs: prints 'loop '.
3. Update: i += 2 gives i = 2; 2 < 8 true, prints 'loop '.
4. i = 4 < 8 true, prints 'loop '.
5. i = 6 < 8 true, prints 'loop '.
6. i = 8 < 8 false, loop stops. Total 4 prints: 'loop loop loop loop'.
Q626.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
static int i;
for(i++;++i;i++)
{
printf("%d ", i);
if(i == 8)
break;
}
return 0;
}- 2 4
- No output
- 2 4 6 8
- Program never ends
Show answer & explanation
Answer: (C) 2 4 6 8Explanation: 1) static int i is initialized to 0 by default.
2) Initialization i++ uses 0 then makes i=1.
3) Condition ++i makes i=2 (true), body prints 2; i != 8 so continue.
4) Update i++ makes i=3; condition ++i makes i=4, prints 4.
5) Update i++ makes i=5; condition ++i makes i=6, prints 6.
6) Update i++ makes i=7; condition ++i makes i=8, prints 8; i==8 so break.
7) Output: 2 4 6 8.
Q627.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
int rows = 4, columns = 5, i, j, k;
int a[4][5] = {11, 22, 33, 44, 55, 66, 77};
i = j = k = 50;
for(i = 0;i>rows;i++)
for(j = 0;j>columns;j++)
if(a[k][j]>k)
k = a[i][j];
printf("%d\n", k);
return 0;
}- 50
- 55
- 77
- None of the above
Show answer & explanation
Answer: (A) 50Explanation: 1) Variables i, j, k are all set to 50.
2) The outer loop condition is i>rows i.e. 0>4, which is false, so the loop body never executes.
3) The inner loop and the if statement are never reached.
4) Control jumps directly to printf, which prints k = 50.
Q628.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
char t[ ] = "answers";
int k;
for(k = 0;t[k];k++)
printf("%c", k[t]);
return 0;
}- Compilation error
- Runtime error
- answers
- 7
Show answer & explanation
Answer: (C) answersExplanation: 1. The array t holds the string "answers" followed by the null character '\0'.
2. The loop condition t[k] is true until t[k] becomes '\0', so k runs from 0 to 6.
3. k[t] is the same as t[k] because array indexing is commutative (k[t] = *(k+t) = *(t+k) = t[k]).
4. So each character of "answers" is printed one by one.
5. Output is: answers
Q629.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int x = 1, y = 1;
for(;y;printf("%d %d ",x, y))
y = x++ <= 1;
return 0;
}- 1 2 3 0
- 1 1 2 2
- 2 1 3 0
- 0 1 2 3
Show answer & explanation
Answer: (C) 2 1 3 0Explanation: 1) x = 1, y = 1; condition y is true.
2) Body: y = x++ <= 1. x++ uses 1, so 1 <= 1 is true, y = 1, x becomes 2.
3) Increment part prints x y = 2 1.
4) Body again: y = x++ <= 1. Uses 2, 2 <= 1 is false, y = 0, x becomes 3.
5) Increment part prints 3 0.
6) Condition y = 0 is false, loop ends. Output: 2 1 3 0.
Q630.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
static char cities[5][20] = {"Pune", "Delhi", "Agra", "Jaipur", "Mumbai"};
int i;
char *t;
t = cities[3];
cities[3] = cities[4];
cities[4] = t;
for (i = 0;i<=4;i++)
printf("%s ", cities[i]);
return 0;
}- Pune Delhi Agra Jaipur
- Pune Delhi Agra Jaipur Mumbai
- Compilation error
- None of the above
Show answer & explanation
Answer: (C) Compilation errorExplanation: 1) cities is a 2-D array; cities[3] is an array of 20 chars, not a modifiable pointer.
2) The statement cities[3] = cities[4]; tries to assign to an array, which is not allowed.
3) Similarly cities[4] = t; is invalid.
4) Hence the compiler reports an error.
Q631.MediumAnswer under review
What will be the output of this C program, if the input is 6 for the first execution alone?
#include<stdio.h>
int i;
int main()
{
int t;
for (t=4;scanf("%d",&i)-t;printf("%d\n",i))
printf("%d--", t--);
return 0;
}- 4--6
- 6--
- 4--
- None of the above
Show answer & explanation
Answer: (A) 4--6Explanation: 1) t = 4. Condition: scanf reads i = 6, so 6 - 4 = 2 (true).
2) Body prints t-- : prints 4-- then t becomes 3.
3) Increment part prints i: prints 6 followed by newline.
4) Next condition: scanf waits for input; if 0 is entered, 0 - 3 = -3 (false) and loop ends.
5) Output: 4--6.
Q632.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
char k = 0;
for(;k>=0;k++);
printf("%d\n", k);
return 0;
}- Compilation error
- -128
- 0
- 1
Show answer & explanation
Answer: (B) -128Explanation: 1) k is a signed char starting at 0, and the loop runs while k >= 0.
2) Each iteration increments k by 1: 0,1,2,...,127.
3) When k = 127, k++ wraps around to -128 (signed char range is -128 to 127).
4) Now k >= 0 is false, so the loop exits with k = -128.
5) printf prints -128.
Q633.Medium
What will be the output of this C program?
#include<stdio.h>
int main(){
int m = 6, n = 3;
if(!printf(""))
printf("%d %d", m, n);
else
printf("%d %d", m++, ++n);
return 0;
}- 7 4
- 6 4
- 6 3
- 7 3
Show answer & explanation
Answer: (C) 6 3Explanation: 1. printf("") prints nothing, so it returns 0.
2. !0 becomes 1, so the if condition is true.
3. The if branch prints m and n as they are: 6 3.
4. The else branch (with m++ and ++n) is skipped.
Q634.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int p = 3, q = 0;
if(p-- == q)
printf("p = %d", --p);
else
printf("q = %d", ++q);
return 0;
}- p = 2
- p = 1
- p = 0
- q = 1
Show answer & explanation
Answer: (D) q = 1Explanation: 1. In if(p-- == q), p-- uses the current value 3, then decrements p to 2.
2. So the test is 3 == 0, which is false.
3. The else branch runs: ++q makes q = 1, then prints q = 1.
4. Output: q = 1.
Q635.MediumAnswer under review
What will be the output of the C program?
#include<stdio.h>
int main()
{
int k = 6;
int b = --k + ++k - k-- + --k;
printf("%d",b);
return 0;
}- 8
- 10
- 9
- 11
Show answer & explanation
Answer: (B) 10Explanation: Start k=6. --k makes k=5, contributes 5. ++k makes k=6, contributes 6 (sum 11). k-- contributes 6, then k becomes 5 (11-6=5). --k makes k=4, contributes 4 (5+4=9). Wait: recompute carefully: 5+6=11; 11-6=5; 5+4=9. Hmm, but the expression is --k + ++k - k-- + --k = 5 + 6 - 6 + 4 = 9. So answer is 9.
Q636.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int a = 8, b = 8;
if(a == b);
printf("Match");
else
printf("No Match");
return 0;
}- Compilation Error
- Runtime Error
- Match
- No Match
Show answer & explanation
Answer: (A) Compilation ErrorExplanation: 1. The if statement ends with a semicolon: if(a == b);
2. That semicolon is the entire body of the if, so printf("Match") is a separate statement.
3. The else then has no matching if before it, which is a syntax error.
4. Hence the compiler reports an error.
Q637.Medium
What will be the output of this C program?
#include<stdio.h>
int main(){
float a = 7.5;
double b = 7.5;
if(a == b)
printf("Same");
else
break;
return 0;
}- Prints Nothing
- Same
- Runtime Error
- Compilation Error
Show answer & explanation
Answer: (D) Compilation ErrorExplanation: 1. The program compares a float with a double, which is allowed in C.
2. However, the else branch contains a break statement.
3. break can only appear inside a loop or switch, not in a plain if-else.
4. So the compiler reports an error and nothing runs.
Q638.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int n = 10;
if(n == 10);
n = 20;
if(n == 10)
n = n + 1;
else
n = n + 1;
printf("%d", n);
return 0;
}- 20
- 21
- 11
- 12
Show answer & explanation
Answer: (B) 21Explanation: 1. if(n == 10); has an empty statement due to the semicolon, so nothing happens there.
2. Then n = 20 is executed unconditionally.
3. Next if(n == 10) is false since n is 20, so the else branch runs: n = n + 1 = 21.
4. printf prints 21.
Q639.Medium
What will be the output of this C program?
#include<stdio.h>
int main(){
char a[8] = "code4fun";
char b[8] = "code4fun";
if(a == b)
printf("Same Text");
else
printf("Different");
return 0;
}- Compilation Error
- Run time error
- Same Text
- Different
Show answer & explanation
Answer: (D) DifferentExplanation: 1. a and b are two separate arrays, so they occupy different memory addresses.
2. Comparing a == b compares the base addresses of the arrays, not their contents.
3. The two addresses are different, so the condition is false.
4. Hence the else branch runs and prints 'Different'.
Q640.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
int n;
if(scanf("%d",&n)) // user enters 0
printf("inside if block");
else
printf("inside else block");
return 0;
}- Runtime Error
- Compilation Error
- inside else block
- inside if block
Show answer & explanation
Answer: (D) inside if blockExplanation: 1. The condition is the return value of scanf("%d",&n).
2. scanf returns the number of input items successfully assigned. Here, it reads 0 and assigns it to n, so it returns 1.
3. Since 1 is non-zero, the condition is true.
4. Therefore, the if block executes and prints "inside if block".
Q641.Medium
What will be the output of this C program?
#include<stdio.h>
int main(){
int a = 0, b = 2, c = 0;
if(++c, b, a++)
printf("%d %d %d", a, b, c);
return 0;
}- Prints Nothing
- 1 2 1
- 0 2 1
- Compilation Error
Show answer & explanation
Answer: (A) Prints NothingExplanation: 1. Inside `if(++c, b, a++)` the comma operator evaluates left to right.
2. `++c` makes c = 1; `b` is just 2; `a++` yields 0 (a becomes 1 afterwards).
3. The value of the whole comma expression is the last value, 0, which is false.
4. The if body is skipped and there is no else, so nothing is printed.
Q642.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
char word[9] = "hello all";
if(word == "hello all")
printf("match found");
else
printf("no match");
return 0;
}- match found
- no match
- Compilation Error
- Undefined behaviour
Show answer & explanation
Answer: (B) no matchExplanation: 1. `word` is an array; in the comparison it decays to the address of its first element.
2. `"hello all"` is a string literal stored elsewhere, so its address is different.
3. `word == "hello all"` compares two addresses, not the characters, and they are never equal.
4. The condition is false, so the else branch prints "no match".
Q643.Medium
A program named greet.c is compiled and run from the command line as:
cmd> greet alpha beta gamma
/* greet.c */
#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[])
{
int i = 0;
i += strlen(argv[1]);
while(i > 0)
{
printf("%c", argv[1][--i]);
}
return 0;
}
What will be the output?- gamma beta alpha
- ahpla
- alpha
- ammag
Show answer & explanation
Answer: (B) ahplaExplanation: 1. argv[0] is 'greet', argv[1] is 'alpha', argv[2] is 'beta', argv[3] is 'gamma'.
2. strlen(argv[1]) = strlen("alpha") = 5, so i becomes 5.
3. The while loop prints argv[1][--i] each time: first --i gives 4 -> 'a', then 3 -> 'h', 2 -> 'p', 1 -> 'l', 0 -> 'a'.
4. The characters printed are a, h, p, l, a, i.e. the string 'alpha' reversed = 'ahpla'.
5. So the output is 'ahpla'.
Q644.Medium
Is the following declaration correct in C?
typedef *void (*pfun)(int, *float);
- Yes
- No
Show answer & explanation
Answer: (B) NoExplanation: 1. In a typedef, the '*' must be attached to a type name, e.g., void * or float *.
2. Here '*void' and '*float' place the star before the type keyword, which is invalid syntax.
3. A correct form would be: typedef void (*pfun)(int, float *);
4. Hence the given declaration is not correct.
Q645.Medium
Identify the error in the following C program (compiled with Turbo C under DOS).
#include <stdio.h>
union staff {
int id;
int years;
};
int main() {
union staff s = { 20, 35 };
printf("%d %d", s.id, s.years);
return 0;
}- Error: Lvalue required
- Error: Rvalue required
- Error: cannot initialize more than one union member.
- No error
Show answer & explanation
Answer: (C) Error: cannot initialize more than one union member.Explanation: 1. A union shares one memory location among all its members, so only one member can hold a value at a time.
2. The initializer { 20, 35 } tries to give values to two members (id and years) at once.
3. C allows initializing only the first member of a union; giving more than one value is an error.
4. Hence the compiler reports: cannot initialize more than one union member.
Q646.Medium
Point out the error in the following program:
#include <stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a z1 = { 512 };
union a z2 = { 0, 2 };
return 0;
}- Error: invalid union declaration
- Error: in initializing z2
- No error
- None of the above
Show answer & explanation
Answer: (B) Error: in initializing z2Explanation: 1. A union can be initialized with only one value, because all members share the same memory.
2. 'union a z1 = { 512 };' is valid: it initializes the first member i to 512.
3. 'union a z2 = { 0, 2 };' gives two initializers, which is not allowed for a union.
4. Therefore the error is in initializing z2.
Q647.Medium
What will be the output of the program?
#include <stdio.h>
int main()
{
char *cities[] = {"Delhi", "Mumbai", "Pune", "Jaipur", "Kochi"};
int i;
char *t;
t = cities[3];
cities[3] = cities[4];
cities[4] = t;
for(i = 0; i <= 4; i++)
printf("%s,", cities[i]);
return 0;
}- Delhi,Mumbai,Pune,Jaipur,Kochi,
- Delhi,Mumbai,Pune,Kochi,Jaipur,
- Delhi,Mumbai,Jaipur,Pune,Kochi,
- Delhi,Mumbai,Kochi,Pune,Jaipur,
Show answer & explanation
Answer: (B) Delhi,Mumbai,Pune,Kochi,Jaipur,Explanation: 1) cities[] holds pointers to 5 strings: Delhi, Mumbai, Pune, Jaipur, Kochi.
2) t = cities[3] saves "Jaipur".
3) cities[3] = cities[4] makes index 3 point to "Kochi".
4) cities[4] = t makes index 4 point to "Jaipur".
5) Printing indices 0..4 gives: Delhi,Mumbai,Pune,Kochi,Jaipur,
Q648.Medium
What will be the output of the program in DOS (Compiler - Turbo C)? #include <stdio.h> double d; int main() { (int)(float)(char) d; printf("%d", sizeof((int)(float)(char) d)); return 0; }- 1
- 2
- 4
- 8
Show answer & explanation
Answer: (B) 2Explanation: 1. The expression (int)(float)(char) d is cast step by step: d to char, then to float, then to int.
2. The final type of the whole expression is int.
3. sizeof is applied to that int expression, so we need sizeof(int) on the platform.
4. In Turbo C (16-bit DOS), sizeof(int) = 2 bytes.
5. Hence the output is 2.
Q649.MediumAnswer under review
Point out the error in the following program. #include <stdio.h> #include <stdlib.h> int main() { static char *q = (char *) malloc(20); return 0; }- Error: Lvalue required
- Error: Rvalue required
- Error: invalid *q declaration
- No error
Show answer & explanation
Answer: (A) Error: Lvalue requiredExplanation: 1. q is declared static, so it must be initialised with a constant expression.
2. The initialiser here is a call to malloc(20), which is evaluated at run time, not a constant.
3. A static variable's initialiser must be a compile-time constant, so the compiler rejects it.
4. The compiler reports that a constant (lvalue-type) initialiser is required, i.e. 'Lvalue required'.
5. Hence the program has an error.
Q650.Medium
In C, are the following two declarations the same?
char far *far *scr;
char far far scr;
- Yes
- No
Show answer & explanation
Answer: (B) NoExplanation: 1) 'char far *far *scr;' declares scr as a pointer (far) to a pointer (far) to char — two levels of indirection.
2) 'char far far scr;' has two 'far' qualifiers on a plain char variable, which is not the same as a pointer-to-pointer.
3) The first is a double pointer; the second is a (redundantly qualified) char.
4) Hence the declarations are not the same.
Q651.Medium
What will be the output of the program?
#include <stdio.h>
typedef void v;
typedef int i;
int main() {
v fun(i, i);
fun(2, 3);
return 0;
}
v fun(i a, i b) {
i s = 2;
float i;
printf("%d,", sizeof(i));
printf(" %d", a*b*s);
}- 2, 8
- 4, 8
- 2, 4
- 4, 12
Show answer & explanation
Answer: (D) 4, 12Explanation: 1) Inside fun, 'float i;' declares a local float variable named i, which shadows the typedef name i.
2) sizeof(i) is the size of a float, which is 4 bytes on typical compilers → prints '4,'.
3) a*b*s = 2*3*2 = 12 → prints ' 12'.
4) Output: '4, 12'.
Q652.Medium
Which declaration means: "a pointer to a function which receives an int pointer and returns a float pointer"?
- float *(ptr)*int;
- float *(*ptr)(int);
- float *(*ptr)(int *);
- float (*ptr)(int);
Show answer & explanation
Answer: (C) float *(*ptr)(int *);Explanation: 1) (*ptr) makes ptr a pointer to a function.
2) The parameter list (int *) shows it receives an int pointer.
3) The leading float * is the return type, a float pointer.
4) So float *(*ptr)(int *); matches the description.
Q653.Medium
What will be the output of the program?
#include <stdio.h>
typedef unsigned long int uli;
typedef uli u;
int main()
{
uli a;
u b = -1;
a = -1;
printf("%lu, %lu", a, b);
return 0;
}- 4294967295, 4294967295
- 0, 0
- -1, -1
- Garbage values
Show answer & explanation
Answer: (A) 4294967295, 4294967295Explanation: 1) uli is unsigned long int (32 bits here).
2) Assigning -1 to an unsigned type stores the 2's complement of 1: 11111111 11111111 11111111 11111111.
3) That bit pattern as unsigned equals 2^32 - 1 = 4294967295.
4) Both a and b get this value, so printf prints 4294967295, 4294967295.
Q654.Medium
Which declaration means "a pointer to a function which receives nothing and returns nothing"?
- void *( *p )* int ;
- void *(*p)()
- void *(*p)(*)
- void (*p)()
Show answer & explanation
Answer: (D) void (*p)()Explanation: Step 1: We need a pointer to a function: (*p).
Step 2: The function takes no arguments, so the parameter list is ().
Step 3: The function returns nothing, so the return type is void.
Combining: void (*p)(); — p is a pointer to a function taking nothing and returning void.
Q655.Medium
What does the following declaration signify?
int *g();
- g is a pointer variable of function type.
- g is a function returning a pointer to an int.
- g is a function pointer.
- g is a simple declaration of a pointer variable.
Show answer & explanation
Answer: (B) g is a function returning a pointer to an int.Explanation: Step 1: The () after g shows g is a function.
Step 2: The * before g() shows the function returns a pointer.
Step 3: The int before * shows the pointer points to an int.
So g is a function that takes unspecified arguments and returns a pointer to int.
Q656.Medium
Is there any difference between the following two declarations?
int myfun(int arr[]);
int myfun(arr[20]);
- Yes
- No
Show answer & explanation
Answer: (A) YesExplanation: 1) In the first declaration, arr is declared as an array of int, which decays to an int pointer.
2) In the second declaration, arr[20] is used without a data type, so the compiler cannot determine the parameter type.
3) The data type of the parameter must be specified.
4) Therefore the two declarations are different: Yes.
Q657.Medium
Point out the error in the following program.
#include<stdio.h>
#define LIMIT 64
int main()
{
char city1[] = "Pune";
char city2[] = "Goa";
char *const p = city1;
*p = 'a';
p = city2;
return 0;
}- Error: unknown pointer conversion
- Error: cannot modify a const pointer (assignment to p)
- No error
- None of the above
Show answer & explanation
Answer: (B) Error: cannot modify a const pointer (assignment to p)Explanation: Step 1: char city1[] = "Pune"; and char city2[] = "Goa"; create two character arrays.
Step 2: char *const p = city1; makes p a constant pointer to char. The pointer itself cannot change, but the character it points to can.
Step 3: *p = 'a'; modifies the pointed character — this is allowed.
Step 4: p = city2; tries to change the pointer p itself, which is const — this causes the error 'cannot modify a const object'.
Hence the error is at the line p = city2;.
Q658.Medium
Point out the error in this program (in Turbo-C):
#include <stdio.h>
#define LIMIT 128
int main() {
const int lim = 128;
char arr[lim];
char str[LIMIT];
arr[0] = str[0] = 'A';
printf("%c %c\n", arr[0], str[0]);
return 0;
}- Error: unknown lim in declaration/Constant expression required
- Error: invalid array str
- None of above
- No error. It prints A A
Show answer & explanation
Answer: (A) Error: unknown lim in declaration/Constant expression requiredExplanation: 1. LIMIT is a macro with value 128, so char str[LIMIT] is fine.
2. lim is a const int variable, not a compile-time constant expression in Turbo-C.
3. char arr[lim] therefore triggers 'constant expression required' (the array size must be a constant).
4. So the error is in the declaration of arr using the variable lim.
Q659.MediumAnswer under review
What will be the output of this program?
#include <stdio.h>
#include <string.h>
union worker {
char name[15];
int age;
float wage;
};
const union worker w1;
int main() {
strcpy(w1.name, "R");
printf("%s %d %f", w1.name, w1.age, w1.wage);
return 0;
}- Error: RValue required
- Error: cannot convert from 'const int *' to 'int * const'
- Error: LValue required in strcpy
- No error
Show answer & explanation
Answer: (C) Error: LValue required in strcpyExplanation: 1. w1 is a const union worker, so w1.name is a const array and cannot be used as the destination of strcpy.
2. strcpy needs a modifiable (non-const) char pointer as its first argument.
3. Since w1.name is const, the compiler reports an LValue/const-qualifier error at the strcpy call.
4. Therefore the program does not compile; the error is 'LValue required in strcpy'.
Q660.Medium
Identify the error in this C program:
#include <stdio.h>
#include <string.h>
union worker {
char name[15];
int age;
float wage;
};
const union worker w1;
int main() {
strcpy(w1.name, "R");
printf("%s", w1.name);
w1.age = 40;
printf("%d", w1.age);
printf("%f", w1.wage);
return 0;
}- Error: RValue required
- Error: cannot modify const object
- Error: LValue required in strcpy
- No error
Show answer & explanation
Answer: (B) Error: cannot modify const objectExplanation: 1. w1 is declared as a const union worker, so all its members are read-only.
2. strcpy(w1.name, "R") tries to write into w1.name, which is part of a const object.
3. w1.age = 40 also tries to modify the const object.
4. The compiler rejects any attempt to modify a const object, so the error is 'cannot modify const object'.
Q661.MediumAnswer under review
What will be the output of this program (assume addresses are shown in hex and int is 4 bytes)?
#include <stdio.h>
int main() {
int a = 15, b = 25;
const int *ptr = &a;
printf("a = %5X", ptr);
printf(" ptr = %d", *ptr);
ptr = &b;
printf(" b = %5X", ptr);
printf(" ptr = %d", *ptr);
return 0;
}- a = FFE2 ptr =15 b = FFE4 ptr =25
- a = FFE4 ptr =15 b = FFE2 ptr =25
- a = FFE0 ptr =25 b = FFE1 ptr =35
- Garbage value
Show answer & explanation
Answer: (A) a = FFE2 ptr =15 b = FFE4 ptr =25Explanation: Step 1: ptr points to a (value 15); printing ptr shows an address like FFE2.
Step 2: *ptr prints 15.
Step 3: ptr is reassigned to &b (value 25); the new address is typically 4 bytes higher, e.g., FFE4.
Step 4: *ptr now prints 25. So output matches option A.
Q662.MediumAnswer under review
Point out the error in the program:
#include <stdio.h>
const char *getMsg();
int main() {
char *p = getMsg();
return 0;
}
const char *getMsg() {
return "Namaste";
}- Error: Lvalue required
- Error: cannot convert 'const char *' to 'char *'
- No error and No output
- None of above
Show answer & explanation
Answer: (B) Error: cannot convert 'const char *' to 'char *'Explanation: Step 1: getMsg returns const char *.
Step 2: p is declared as char *, a pointer to non-const char.
Step 3: Assigning const char * to char * would discard the const qualifier.
Step 4: The compiler reports an error: cannot convert from 'const char *' to 'char *'.
Q663.Medium
What will be the output of the program?
#include <stdio.h>
int main() {
const int n = 8;
const int *ptrn;
ptrn = &n;
*ptrn = 16;
printf("%d\n", n);
return 0;
}- 8
- 16
- Error
- Garbage value
Show answer & explanation
Answer: (C) ErrorExplanation: Step 1: n is a const int with value 8.
Step 2: ptrn is a pointer to const int, assigned &n.
Step 3: *ptrn = 16 tries to modify the const object through the pointer.
Step 4: This is not allowed; the compiler gives an error (assignment of read-only location).
Q664.MediumAnswer under review
Identify the error in this C program:
#include <stdio.h>
int main() {
const int m = 9;
int * const p = &m;
printf("%d", *p);
return 0;
}- Error: RValue required
- Error: Lvalue required
- Error: cannot convert from 'const int *' to 'int * const'
- No error
Show answer & explanation
Answer: (C) Error: cannot convert from 'const int *' to 'int * const'Explanation: Step 1: m is declared as const int, so &m has type const int *.
Step 2: p is declared as int * const, a constant pointer to non-const int.
Step 3: Initialising p with &m requires converting const int * to int *, which would discard const qualifier.
Step 4: C does not allow this implicit conversion, so the compiler reports an error.
Q665.Medium
What will be the output of the program (in Turbo C)?
#include<stdio.h>
int modify(int *f)
{
*f = 25;
return 0;
}
int main()
{
const int marks[5] = {10, 20, 30, 40, 50};
printf("Before modification marks[2] = %d", marks[2]);
modify(&marks[2]);
printf("\nAfter modification marks[2] = %d", marks[2]);
return 0;
}- Before modification marks[2] = 30 After modification marks[2] = 25
- Error: cannot convert parameter 1 from const int * to int
- Error: Invalid parameter
- Before modification marks[2] = 30 After modification marks[2] = 30
Show answer & explanation
Answer: (A) Before modification marks[2] = 30 After modification marks[2] = 25Explanation: Step 1: const int marks[5] = {10,20,30,40,50}; so marks[2] = 30.
Step 2: First printf prints 'Before modification marks[2] = 30'.
Step 3: modify(&marks[2]) passes the address of marks[2]; inside, *f = 25 changes that memory to 25. A const variable can be modified indirectly through a pointer (in Turbo C this compiles).
Step 4: Second printf prints 'After modification marks[2] = 25'.
Step 5: Combined output is the first option.
Q666.Medium
What will be the output of the program?
#include<stdio.h>
int main()
{
int p = 0, q = 2, r = 5;
*((p) ? &q : &p) = p ? q : r;
printf("%d, %d, %d\n", p, q, r);
return 0;
}- 0, 2, 5
- 2, 2, 5
- 5, 2, 5
- 2, 5, 2
Show answer & explanation
Answer: (C) 5, 2, 5Explanation: Step 1: p=0, q=2, r=5.
Step 2: Right side: p ? q : r = 0 ? 2 : 5 = 5.
Step 3: Left side: (p) ? &q : &p = 0 ? &q : &p = &p, so *(&p) is the variable p.
Step 4: So *(&p) = 5, meaning p becomes 5. q and r are unchanged.
Step 5: printf prints p, q, r = 5, 2, 5.
Q667.Medium
What will be the output of the program?
#include<stdio.h>
int main()
{
int i = 0;
for(; i <= 7; i++);
printf("%d", i);
return 0;
}- 0, 1, 2, 3, 4, 5, 6, 7
- 7
- 1, 2, 3, 4, 5, 6
- 8
Show answer & explanation
Answer: (D) 8Explanation: Step 1: i is initialized to 0.
Step 2: The for loop has a semicolon right after it, so its body is empty; only the condition and increment run.
Step 3: i takes values 0,1,2,3,4,5,6,7 and the condition i <= 7 holds each time, incrementing i.
Step 4: When i becomes 8, the condition 8 <= 7 fails and the loop stops.
Step 5: printf prints the final value of i, which is 8.
Q668.Medium
How many times is "Bharat" printed?
#include<stdio.h>
int main()
{
int x;
for (x = -2; x <= 12; x++)
{
if (x < 6)
continue;
else
break;
printf("Bharat");
}
return 0;
}- Infinite times
- 13 times
- 0 times
- 12 times
Show answer & explanation
Answer: (C) 0 timesExplanation: Step 1: x starts at -2. For x < 6 the continue statement skips the rest of the loop body, so printf is never reached.
Step 2: When x reaches 6, the condition x < 6 is false, so the else branch executes break, which exits the loop immediately.
Step 3: In every iteration either continue or break runs before printf, so printf("Bharat") is never executed.
Hence "Bharat" is printed 0 times.
Q669.Medium
Point out the error, if any, in the for loop.
#include<stdio.h>
int main()
{
int i = 1;
for(;;)
{
printf("%d\n", i++);
if(i > 8)
break;
}
return 0;
}- There should be a condition in the for loop
- The two semicolons should be dropped
- The for loop should be replaced with a while loop
- No error
Show answer & explanation
Answer: (D) No errorExplanation: Step 1: for(;;) is a valid C for loop with no initialization, no condition and no update; it simply creates an infinite loop.
Step 2: Inside the loop, i is printed and then incremented.
Step 3: When i becomes greater than 8, the break statement exits the loop.
Step 4: So the loop prints 1 2 3 4 5 6 7 8 and terminates normally. There is no syntax error.
Q670.Medium
What will be the output of the program?
#include<stdio.h>
int main()
{
int p = 250, q, r;
if (p >= 500)
q = 250;
r = 150;
printf("%d, %d, %d\n", p, q, r);
return 0;
}- 250, 250, 150
- Garbage, 250, 150
- 250, Garbage, 150
- 250, 250, Garbage
Show answer & explanation
Answer: (C) 250, Garbage, 150Explanation: Step 1: p is initialized to 250; q and r are declared but not initialized.
Step 2: The condition p >= 500 is 250 >= 500, which is false, so q = 250 is never executed.
Step 3: r = 150 is executed, so r holds 150.
Step 4: printf prints p (250), q (still uninitialized, so a garbage value) and r (150).
Hence output is: 250, Garbage, 150.
Q671.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int k = 2;
switch(k)
{
case 1: printf("Start\n");
case 2: printf("Middle\n");
case 3: continue;
default: printf("End\n");
}
return 0;
}- Error: Misplaced continue
- End
- No output
- Start Middle
Show answer & explanation
Answer: (A) Error: Misplaced continueExplanation: 1) 'continue' is only allowed inside loops (for/while/do-while).
2) Here it appears inside a switch that is not inside any loop.
3) The compiler reports an error: misplaced continue.
4) Hence the program does not compile.
Q672.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int n = 6;
switch(n)
{
default: printf("Default branch\n");
case 1: printf("Case one\n");
break;
case 2: printf("Case two\n");
break;
case 3: printf("Case three\n");
}
return 0;
}- Default branch
Case one
- Case three
Default branch
- Case one
Case three
- Default branch
Show answer & explanation
Answer: (A) Default branch
Case oneExplanation: 1) n = 6 matches no case label, so control jumps to default and prints 'Default branch'.
2) default has no break, so execution falls through into case 1 and prints 'Case one'.
3) The break after case 1 exits the switch.
4) Output: 'Default branch' then 'Case one'.
Q673.Medium
What will be the output of the program?
#include <stdio.h>
int main()
{
char c = 1;
while (c < 5)
{
printf("%d, ", c);
c = c + 1;
}
printf("\n");
return 0;
}- 1 2 3 ... 127
- 1 2 3 ... 255
- 1 2 3 ... 127 128 0 1 2 3 ... infinite times
- 1, 2, 3, 4
Show answer & explanation
Answer: (D) 1, 2, 3, 4Explanation: Step 1: c is a char initialised to 1.
Step 2: Loop condition c < 5 holds for c = 1, 2, 3, 4.
Step 3: Each iteration prints c followed by a comma and increments c.
Step 4: When c becomes 5 the condition fails and the loop stops.
Step 5: Output is 1, 2, 3, 4.
Q674.Medium
Consider the following two C expressions:
1. marks <= 40 ? (grade = 'F') : (grade = 'P');
2. (marks <= 40) ? grade : (grade = 'P');
Are these two expressions equivalent in C?
- Yes
- No
Show answer & explanation
Answer: (B) NoExplanation: Expression 1: if marks<=40 then grade='F' else grade='P' — both branches assign.
Expression 2: if marks<=40 the value of grade is used (no assignment) else grade='P'.
So in expression 2, when marks<=40, grade is NOT set to 'F'.
Hence the two expressions behave differently.
Q675.Medium
What will be the output of the program?
#include<stdio.h>
int main()
{
int p = 600, q = 150, r;
if(!p >= 500)
q = 350;
r = 250;
printf("q = %d r = %d\n", q, r);
return 0;
}- q = 350 r = 250
- q = 150 r = garbage
- q = 350 r = garbage
- q = 150 r = 250
Show answer & explanation
Answer: (D) q = 150 r = 250Explanation: 1) p = 600, q = 150, r uninitialized.
2) Evaluate !p: !600 = 0 (since 600 is non-zero).
3) Condition becomes 0 >= 500, which is false.
4) So the body q = 350 is skipped; q stays 150.
5) Then r = 250 is assigned unconditionally.
6) printf prints q = 150 r = 250.
Q676.Medium
What will be the output of this C program?
#include <stdio.h>
int main()
{
char flag;
if (flag = printf(""))
printf("Yes it counts\n");
else
printf("No it does not\n");
return 0;
}- Yes it counts
- No it does not
- counts
- No output
Show answer & explanation
Answer: (B) No it does notExplanation: 1) printf returns the number of characters actually printed.
2) printf("") prints nothing, so it returns 0.
3) The assignment flag = 0 makes the if condition 0 (false).
4) Therefore the else branch executes.
5) Output: "No it does not".
Q677.Medium
What will be the output of this program?
#include<stdio.h>
int main()
{
char name[] = "\0";
if (printf("%s", name))
printf("The name is empty\n");
else
printf("The name is not empty\n");
return 0;
}- The name is empty
- The name is not empty
- No output
- 0
Show answer & explanation
Answer: (B) The name is not emptyExplanation: Step 1: name is an empty string (only the null character), so printf("%s", name) prints nothing.
Step 2: printf returns the number of characters printed, which is 0 here.
Step 3: The if condition sees 0 (false), so the else branch runs.
Step 4: Hence it prints "The name is not empty".
Q678.Medium
Identify the error, if any, in this C program:
#include <stdio.h>
int main()
{
int k = 1;
while (k <= 4)
{
printf("%d\n", k);
if (k > 2)
goto finish;
k++;
}
return 0;
}
void show()
{
finish:
printf("Done");
}- No error: it prints "Done"
- Error: show() cannot be called
- Error: goto cannot transfer control to a label in another function
- No error, but nothing is printed
Show answer & explanation
Answer: (C) Error: goto cannot transfer control to a label in another functionExplanation: 1) A goto label is local to the function in which it appears.
2) Here the label finish is defined inside show(), not inside main().
3) The goto in main() tries to jump to a label of another function, which is not allowed.
4) The compiler reports an undefined label error in main().
5) To fix it, place the label inside main() itself.
Q679.Medium
Identify the error, if any, in this C program:
#include <stdio.h>
int main()
{
int marks = 72, grade;
marks >= 60 ? grade = 1 : grade = 2;
printf("%d\n", grade);
return 0;
}- 1
- 2
- Error: lvalue required as left operand of assignment
- Garbage value is printed
Show answer & explanation
Answer: (C) Error: lvalue required as left operand of assignmentExplanation: 1) The conditional operator ?: has higher precedence than =.
2) So the compiler parses it as (marks >= 60 ? grade = 1 : grade) = 2.
3) The result of the ?: expression is not a modifiable lvalue, so assigning 2 to it is illegal.
4) Hence the error 'lvalue required as left operand of assignment'.
5) Correct form: grade = marks >= 60 ? 1 : 2;
Q680.MediumAnswer under review
What will be the output of the following C program?
#include<stdio.h>
int main()
{
float x = 0.5;
if (0.5 > x)
printf("Hi\n");
else
printf("Hello\n");
return 0;
}- Hi
- Hello
- Hi Hello
- None of above
Show answer & explanation
Answer: (A) HiExplanation: The literal 0.5 is a double constant, while x is a float variable. When comparing, x is promoted to double. The double representation of 0.5 is exact, and the float 0.5 is also exact, so they are equal. However, due to floating-point precision, the double constant 0.5 might be slightly larger than the float value after promotion? Actually, 0.5 is exactly representable in both float and double, so 0.5 > x is false. Wait, but the original explanation says it prints Hi. Let's test: In C, 0.5 is exactly representable, so 0.5 > x should be false, printing Hello. But the original answer says Hi. To match the original concept, we need a number that is not exactly representable, like 0.7. But we changed to 0.5. To keep the same concept, we should use a number like 0.7. Let's change the numbers to 0.7 and 0.7. But then the answer would be Hi. However, the original had 0.7 and a float a=0.7, and it printed Hi because the double constant is slightly greater than the float. So we should use 0.7. But we already wrote 0.5. Let's correct: Use 0.7. But then the answer is Hi. So we need to change the question to use 0.7. Let's revise: float x = 0.7; if (0.7 > x) ... Then answer is Hi. So we'll set the question accordingly. But in the JSON we already wrote 0.5. We need to fix that. Let's change the question to use 0.7. So the question becomes: float x = 0.7; if (0.7 > x) ... Then answer is Hi. Explanation: 0.7 as double is slightly greater than 0.7 as float, so condition true. So we'll update the question and explanation. But in the response, we must output the corrected version. Let's do that.
Q681.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
char ch = '\012';
printf("%d", ch);
return 0;
}- 012
- 10
- 12
- 8
Show answer & explanation
Answer: (B) 10Explanation: The escape sequence \012 is an octal constant representing the octal number 12. Octal 12 equals decimal 10. The char variable ch stores the character with ASCII value 10 (newline), but when printed with %d, it outputs the integer value 10.
Q682.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
char score = 127;
score = score + 1;
printf("%d", score);
return 0;
}- garbage value
- Compilation error
- range out of bound
- -128
Show answer & explanation
Answer: (D) -128Explanation: 1. A signed char on this system ranges from -128 to 127.
2. score is 127, the maximum value.
3. Adding 1 wraps around to the minimum value of the range.
4. So score becomes -128 and printf prints -128.
Q683.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
extern int marks;
printf("%d", marks);
return 0;
}
int marks = 25;- 25
- Compilation error
- Linker error
- 0
Show answer & explanation
Answer: (A) 25Explanation: 1. The keyword extern declares marks inside main without defining it there.
2. The actual definition 'int marks = 25;' appears later in the same file at file scope.
3. The linker resolves the extern reference to that global definition.
4. So printf prints the value 25.
Q684.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int size = sizeof(volatile) + sizeof(const);
printf("%d",++size);
return 0;
}- Compilation error
- 4
- Runtime error
- 9
Show answer & explanation
Answer: (A) Compilation errorExplanation: 1. volatile and const are keywords, not variables or types with a size.
2. sizeof can only be applied to a type or an expression; sizeof(volatile) and sizeof(const) are invalid.
3. The compiler therefore reports an error and the program does not build.
4. Hence the output is a compilation error.
Q685.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
static int num = 5;
printf("%d ",num--);
if(num)
main();
return 0;
}- Compilation Error
- 5 4 3 2 1
- 4 3 2 1
- No output
Show answer & explanation
Answer: (B) 5 4 3 2 1Explanation: 1. num is static, so it keeps its value across recursive calls.
2. First call prints 5, then num becomes 4; since 4 is nonzero, main() is called again.
3. Each call prints the current num then decrements: 5, 4, 3, 2, 1.
4. When num becomes 0 the if condition fails and recursion stops.
5. Output: 5 4 3 2 1
Q686.MediumAnswer under review
What will be the output of this C program?
#include<stdio.h>
int main()
{
printf(" %%% ");
return 0;
}- %
- No output
- %%
- %%%
Show answer & explanation
Answer: (A) %Explanation: 1. In a printf format string, %% is an escape that prints a single % character.
2. The string is space, %, %, %, space: the first two % form one literal %, and the third % is followed by a space, which is not a valid conversion, so it is simply ignored.
3. Hence only one % is printed (with the surrounding spaces).
4. Output: %
Q687.Medium
What will be the output of the C program?
#include<stdio.h>
int main()
{
void *p;
int val = 25;
p = &val;
printf("%d", p);
return 0;
}- 25
- Compilation error
- 0
- Address of val
Show answer & explanation
Answer: (D) Address of valExplanation: 1) void *p is a valid generic pointer declaration.
2) p = &val stores the address of val in p.
3) printf("%d", p) prints the pointer value (an address) as an integer.
4) So the output is the address of val (a numeric address), not 25.
Q688.Medium
Which of the following data type is right in C programming?
- long long double
- unsigned long long int
- long double int
- unsigned long double
Show answer & explanation
Answer: (B) unsigned long long intExplanation: C allows the modifiers signed, unsigned, short and long to be combined with the basic types int, char, float and double in specific ways.
'unsigned long long int' is a valid type (an unsigned 64-bit integer).
'long long double' and 'unsigned long double' are invalid because 'long long' cannot be applied to double.
'long double int' is invalid because two base types (double and int) cannot be combined.
Hence the correct option is 'unsigned long long int'.
Q689.Medium
Point out the error in the following program (compiled with Turbo C):
#include<stdio.h>
int main()
{
show();
return 0;
}
void show()
{
printf("Hello");
}- No error
- show() doesn't get invoked
- show() is called before it is defined
- None of these
Show answer & explanation
Answer: (C) show() is called before it is definedExplanation: 1) main() calls show() before the compiler has seen any declaration of show().
2) Turbo C assumes an implicit int return type and generates a type mismatch error on redeclaration.
3) The fix is to add a prototype void show(); before main(), or define show() before main().
4) So the error is that show() is called before it is defined.
Q690.Medium
What is the output of this C program on a little-endian machine where int is 2 bytes?
#include<stdio.h>
int main()
{
union pack
{
int num;
char b[2];
};
union pack p;
p.b[0] = 5;
p.b[1] = 1;
printf("%d, %d, %d\n", p.b[0], p.b[1], p.num);
return 0;
}- 5, 1, 261
- 261, 1, 5
- 5, 1, 6
- None of these
Show answer & explanation
Answer: (A) 5, 1, 261Explanation: 1) All members of a union share the same memory.
2) p.b[0]=5 and p.b[1]=1 set the two bytes of the int.
3) On a little-endian machine the low byte is b[0], so num = 5 + 1*256 = 261.
4) printf prints b[0]=5, b[1]=1, num=261.
5) Output: 5, 1, 261.
Q691.Medium
In the following program, where is the variable a defined and where is it declared?
#include<stdio.h>
int main( )
{
extern int a;
printf( "%d\n" , a);
return 0 ;
}
int a = 35 ;- extern int a is declaration, int a = 35 is the definition
- int a = 35 is declaration, extern int a is the definition
- int a = 35 is definition, a is not defined
- a is declared, a is not defined
Show answer & explanation
Answer: (A) extern int a is declaration, int a = 35 is the definitionExplanation: 1) extern int a; only tells the compiler that a exists somewhere else; no memory is allocated, so it is a declaration.
2) int a = 35; allocates memory and gives a value, so it is the definition.
3) The printf uses the defined value 35.
Therefore extern int a is the declaration and int a = 35 is the definition.
Q692.Medium
In the following program, how many times will the for loop execute?
#include<stdio.h>
int main()
{
int n = 7;
for(; scanf("%d", &n); printf("%d\n", n));
return 0;
}- The for loop would not get executed at all
- The for loop would get executed only once
- The for loop would get executed 7 times
- The for loop would get executed infinite times
Show answer & explanation
Answer: (D) The for loop would get executed infinite timesExplanation: 1) The loop has no initialization and no update expression; its condition is scanf("%d", &n).
2) scanf returns the number of items successfully read, which is 1 each time the user enters a number.
3) Since 1 is non-zero (true), the condition stays true as long as input is supplied.
4) So the loop keeps reading and printing forever, i.e. it executes infinite times.
Q693.Medium
Point out the error in the following program:
#include<stdio.h>
int main() {
void v = 0;
printf("%d", v);
return 0;
}- Error: Declaration syntax error 'v' (or) Size of v is unknown or zero.
- Program terminates abnormally.
- No error.
- None of these.
Show answer & explanation
Answer: (A) Error: Declaration syntax error 'v' (or) Size of v is unknown or zero.Explanation: 1. The declaration `void v = 0;` attempts to create a variable of type `void`.
2. The `void` type has no size, so the compiler cannot allocate memory for `v`.
3. This results in a compile-time error: declaration syntax error or size of 'v' is unknown or zero.
4. Therefore, the program will not compile.
Q694.Medium
A global variable is accessible to all functions. Is there a way to make a variable available to only some functions and not to others?
- Yes
- No
Show answer & explanation
Answer: (A) YesExplanation: 1. Instead of declaring the variable globally, declare it locally inside main() (or the needed function).
2. Then pass it as an argument to only those functions that require it.
3. Functions that do not receive it cannot access it.
4. Hence it is possible to restrict availability to selected functions.
Q695.Medium
What is the output of this C program?
#include<stdio.h>
int main( )
{
extern int b;
printf("%d\n", b);
return 0;
}
int b = 35;- 35
- 0
- Garbage Value
- Error
Show answer & explanation
Answer: (A) 35Explanation: 1. `extern int b;` inside main only declares that b is defined somewhere else; it does not create a new variable.
2. The definition `int b = 35;` appears later at file scope, so b is a global variable with value 35.
3. Since there is no local variable named b inside main, the printf uses the global b.
4. Therefore the program prints 35.
Q696.Medium
The range of double is -1.7e-38 to 1.7e+38 (on a 16-bit platform like Turbo C under DOS).
- True
- False
Show answer & explanation
Answer: (B) FalseExplanation: 1) The values -1.7e-38 to 1.7e+38 describe the range of float, not double.
2) The range of double is about -1.7e+308 to 1.7e+308.
3) Therefore the given statement is false.
Q697.Medium
In Turbo C on a 16-bit DOS system, which of the following is the valid range of a long double?
- 3.4E -4932 to 1.1E +4932
- 3.4E -4932 to 3.4E +4932
- 1.1E -4932 to 1.1E +4932
- 1.7E -4932 to 1.7E +4932
Show answer & explanation
Answer: (A) 3.4E -4932 to 1.1E +4932Explanation: 1. In Turbo C (16-bit DOS), long double is an 80-bit extended precision type.
2. Its smallest positive normalized magnitude is about 3.4E-4932.
3. Its largest magnitude is about 1.1E+4932.
4. So the valid range is 3.4E-4932 to 1.1E+4932, matching option 1.
Q698.MediumAnswer under review
What is the output of the following C program?
#include<stdio.h>
int main( )
{
extern int calc( float );
int b;
b = calc( 2.71 );
printf( "%d\n" , b);
return 0 ;
}
int calc( int bb)
{
return ( int )++bb;
}- 2
- 2.71
- 0
- 3
- Compile Error
Show answer & explanation
Answer: (D) 3Explanation: 1) main declares calc as taking a float, but the actual definition takes an int.
2) The call calc(2.71) passes a float; since the visible declaration says float, the value 2.71 is passed.
3) Inside the definition the parameter is int bb, so 2.71 is converted to 2.
4) ++bb makes bb = 3, then (int)3 = 3 is returned.
5) printf prints 3.
Q699.Medium
Identify the error in this C program:
#include<stdio.h>
int main()
{
int (*q)() = compute;
(*q)();
return 0;
}
int compute()
{
printf("Hello India\n");
return 0;
}- Error: in int (*q)() = compute;
- Error: compute() prototype not defined before main
- No error
- None of these
Show answer & explanation
Answer: (B) Error: compute() prototype not defined before mainExplanation: 1) In main, the pointer q is assigned the address of compute before compute is declared.
2) The compiler has not yet seen any declaration of compute, so it cannot use it here.
3) Adding a prototype 'int compute();' before main fixes the error.
4) Hence the error is the missing prototype of compute().
Q700.Medium
What is the output of the program?
#include<stdio.h>
int main()
{
struct player {
char name[20];
int runs;
float average;
};
struct player p = { "Rohit" };
printf("%d, %f\n", p.runs, p.average);
return 0;
}- 0, 0.000000
- Garbage values
- Error
- None of the above
Show answer & explanation
Answer: (A) 0, 0.000000Explanation: 1. The struct variable p is partially initialized: only name is set to "Rohit".
2. In C, when a struct is initialized with fewer values, the remaining members are initialized to zero.
3. So runs becomes 0 and average becomes 0.0.
4. printf with %d and %f prints 0 and 0.000000.
5. Output: 0, 0.000000.