Sunday 18 July 2010

Interview Questions on for loop part 2

What is the output of this C code?

    #include <stdio.h>
    void main()
    {
        double k = 0;
        for (k = 0.0; k < 3.0; k++);
            printf("%lf", k);
    }
a) 2.000000
b) 4.000000
c) 3.000000
d) Run time error

Answer:c

What is the output of this C code?

    #include <stdio.h>
    void main()
    {
        int k;
        for (k = -3; k < -5; k++)
            printf("Hello");
    }
a) Hello
b) Infinite hello
c) Run time error
d) Nothing

Answer:d
What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int i = 0;
        for (; ; ;)
            printf("In for loop\n");
            printf("After loop\n");
    }


a) Compile time error
b) Infinite loop
c) After loop
d) Undefined behaviour

Answer:a
What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int i = 0;
        for (i++; i == 1; i = 2)
            printf("In for loop ");
            printf("After loop\n");
    }
a) In for loop after loop
b) After loop
c) Compile time error
d) Undefined behaviour

Answer:a
What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int i = 0;
        for (foo(); i == 1; i = 2)
            printf("In for loop\n");
            printf("After loop\n");
    }
    int foo()
    {
        return 1;
    }
a) After loop
b) In for loop after loop
c) Compile time error
d) Infinite loop

Answer:a
What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int *p = NULL;
        for (foo(); p; p = 0)
            printf("In for loop\n");
            printf("After loop\n");
    }


a) In for loop after loop
b) Compile time error
c) Infinite loop
d) Depends on the value of NULL

Answer:b
What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        for (int i = 0;i < 1; i++)
            printf("In for loop\n");
    }
a) Compile time error
b) In for loop
c) Depends on the standard compiler implements
d) Depends on the compiler

Answer:c


Back
Next: Interview Questions on while loop

No comments:

Post a Comment