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
1 times
B
10 times
C
Infinite times
D
0 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
Greater
B
100
C
Smaller
D
Compile Error
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,300
B
100,300,300
C
100,200,400
D
100,300,400
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
x and y are equal
D
Compile Error
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
0, 2, 2
B
2,2,2
C
1,1,2
D
0,1,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.