Sunday, 18 July 2010

Interview Questions on if else

What is the output of this code?

    #include <stdio.h>
    int main()
    {
        int x = 1;
        if (x > 0)
            printf("inside if\n");
        else if (x > 0)
            printf("inside elseif\n");
    }
a) inside if
b) inside elseif
c) inside if
    inside elseif
d) Compile time error

Answer:a

What is the output of this code?

    #include <stdio.h>
    int main()
    {
        int x = 0;
        if (x++)
            printf("true\n");
        else if (x == 1)
            printf("false\n");
    }
a) true
b) false
c) Compile time error
d) Undefined behaviour

Answer:b

What is the output of this code?

    #include <stdio.h>
    int main()
    {
        int x = 0;
        if (x == 1)
            if (x == 0)
                printf("inside if\n");
            else
                printf("inside else if\n");
        else
            printf("inside else\n");
    }
a) inside if
b) inside else if
c) inside else
d) Compile time error

Answer:c

What is the output of this code?
    #include <stdio.h>
    int main()
    {
        int x = 0;
        if (x == 0)
            printf("true, ");
        else if (x = 10)
            printf("false, ");
        printf("%d\n", x);
    }
a) false, 0
b) true, 0
c) true, 10
d) Compile time error

Answer:b
What is the output of this code?

    #include <stdio.h>
    int main()
    {
        int x = 0;
        if (x == 1)
            if (x >= 0)
                printf("true\n");
            else
                printf("false\n");
    }
a) true
b) false
c) Depends on the compiler
d) No print statement

Answer:d

if (a == 1||b == 2)
{

can be written as:
a) if (a == 1)
    if (b == 2)
    {
     }
b) if (a == 1)
    {
     }
    if (b == 2)
    {
    }
c) if (a == 1)
    {
     }
    else if (b == 2)
    {
    }
d) None of the mentioned

Answer:d

Which of the following is an invalid if-else statement?
a) if (if (a == 1)){}
b) if (func1 (a)){}
c) if (a){}
d) if ((char) a){}

Answer:a
What is the output of this code?

    #include <stdio.h>
    int main()
    {
        int a = 1;
        if (a--)
            printf("True");
            if (a++)
                printf("False");
    }

a) True
b) False
c) True False
d) No Output

Answer:a
What is the output of this code?

    #include <stdio.h>
    int main()
    {
        int a = 1;
        if (a)
            printf("Sindhu ");
            printf("Ganga\n");
        else
            printf("Saraswathi\n");
    }
a) Sindhu Ganga
b) Ganga Saraswathi
c) Ganga
d) Compile time errors during compilation

Answer:d
What is the output of this code?

    #include <stdio.h>
    int main()
    {
        if (printf("%d", printf(")))
            printf("Kashmir");
        else if (printf("1"))
            printf("JaiHind");
    }
a) 0Kashmir
b) 1Kashmir
c) 1JaiHind
d) Compile time error

Answer:d

Back
Next Interview Questions on switch statements

No comments:

Post a Comment