C Decision Control Structures Set 1

Ques: 1 What will be the output of the following program?
#include<stdio.h>
int main()
{
    if(false)
     printf("hello");
    else
     printf("hi");
    return 0;
}
A. hi
B. hellohi
C. Compile time error
D. None of these
Description
Answer: C
This program will work in C++ not in C.

Ques: 2 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int x=0;
    if(x++)
     printf("true %d",x);
    else
     printf("false %d",x);
    printf("%d",x);
    return 0;
}
A. true 1
B. false 1
C. true 11
D. false 11
Description
Answer: D
First condition will be executed then post increment will work. Last printf() will work either condition is true or false.

Ques: 3 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int i=10,j=20;
    if(j>i)
    {
        {
            i=30;
            printf("%d",i);
        }
        printf("%d",i);
    }
    printf("%d",i);
    return 0;

}
A. 301010
B. 303010
C. 303030
D. None of these
Description
Answer: C
Inside the block value has been changed for the i which is only one in whole program.

Ques: 4 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int i=10,j=20;
    if(j>i)
    {
        {
            int i=30;
            printf("%d",i);
        }
        printf("%d",i);
    }
    printf("%d",i);
    return 0;

}
A. 301010
B. 303010
C. 303030
D. None of these
Description
Answer: A
Here a separate i is being declared inside the block so printf() will go for the nearest i.

Ques: 5 What will be the output of the following program?
#include<stdio.h>
int main()
{
    float x=10.0;
    if(10.0==10.0)
        printf("Yes");
    else
        printf("No");
return 0;
}
A. Yes
B. No
C. Compile time error
D. None of these
Description
Answer: A
Very simple program.

Ques: 6 What will be the output of the following program?
#include<stdio.h>
int main()
{
    float x=10.0;
    if(x==10.0)
        printf("Yes");
    else
        printf("No");
    return 0;
;
}
A. Yes
B. No
C. Compile time error
D. None of these
Description
Answer: A
Again simple comparison inside if().

Ques: 7 What will be the output of the following program?
#include<stdio.h>
int main()
{
    float x =10.1;
    if(x == 10.1)
        printf("Yes");
    else
        printf("No");
    return 0;
}
A. Yes
B. No
C. Compile time error
D. None of these
Description
Answer: B
This time it is not so simple. Yes it will print No because float variables with some decimal values does not stored internally exactly as they are.

Ques: 8 What will be the output of the following program?
#include<stdio.h>
int main()
{
    double x=10.1;
    if(x == 10.1)
        printf("Yes");
    else
        printf("No");	
return 0;
}
A. Yes
B. No
C. Compile time error
D. None of these
Description
Answer: A
Again it will print “Yes�. Because only float variables with some decimal values does not stored internally exactly as they are but here x is double not float.

Ques: 9 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int a=6,b=6;
    if(a==b);
        printf("Yes");
    else
        printf("No");
    return 0;
}
A. Yes
B. No
C. Error
D. No output
Description
Answer: C
Error else without if see ; after if();.

Ques: 10 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int a=6,b=6;
    if(a==b);
    else
        printf("No");
    return 0;
}
A. Yes
B. No
C. Error
D. No output
Description
Answer: D
No output because conditions is true but it does not have body to execute.

comments powered by Disqus