C Programming [MCQ] : Features of C (Multiple Choice Questions)


Question 1
How many times below for loop will be executed ?
#include<stdio.h>
int main()
{
    int i=0;
    for(;;)
        printf("%d",i);
    return 0;
}
A
0 times
B
Infinite times
C
1 times
D
10 times
Question 2
Find output of the following program ?
#include<stdio.h>
int main()
{
    char str[] = "Smaller";
    int a = 100;
    printf(a > 10 ? "Greater" : "%s", str);
    return 0;
}
A
Compile Error
B
Smaller
C
Greater
D
100
Question 2 Explanation: 
printf(a > 10 ? "Greater" : "%s", str);
is equivalent to -
if(a > 10)
    printf("Greater");
else
    printf("%s", str);
Question 3
Guess the output of the following program ?
#include<stdio.h>
int main()
{
    int a = 100, b = 200, c = 300;
    if(!a >= 500)
        b = 300;
    c = 400;
    printf("%d,%d,%d",a, b, c);
    return 0;
}
A
100,200,400
B
100,300,400
C
100,200,300
D
100,300,300
Question 3 Explanation: 
Condition = if(!a   >= 500)
          = if(!100 >= 500)
          = if(0    >= 500)
          = if(false)
If statement will not be executed and thus value of 'c' will remain unchanged.
Question 4
Guess the output of the following program :
#include<stdio.h>
int main()
{
    int x = 10;
    float y = 10.0;
    if(x == y)
        printf("x and y are equal");
    else
        printf("x and y are not equal");
    return 0;
}
A
x and y are not equal
B
Run Time Error
C
Compile Error
D
x and y are equal
Question 5
Guess the output of the program ?
#include<stdio.h>
int main()
{
    int a=0, b=1, c=2;
    *((a+1 == 1) ? &b : &a) = a ? b : c;
    printf("%d, %d, %d\n", a, b, c);
    return 0;
}
A
1,1,2
B
0, 2, 2
C
0,1,2
D
2,2,2
Question 5 Explanation: 
Step 1 : a,b and c are initialized to 0,1 and 2 Step 2 :
(a+1 == 1)
is true condition hence if part will be executed. i.e expression can be written as -
*(&b) = a ? b : c;
  b   = a ? b : c;
  b   = 0 ? 1 : 2;
  b   = 2
There are 5 questions to complete.