Machine-translated by Google. Questions are authored in English.
Top

Control Instructions Questions and Answers

41.

Point out the error, if any in the while loop.

#include <stdio.h>
int main()
{
    int i=1;
    while()
    {
        printf("%d\n", i++);
        if(i>10)
           break;
    }
    return 0;
}

 

Answer: A

Correct answer: There should be a condition in the while loop

Unlike for, a while requires a controlling expression - while() is a syntax error. The equivalent infinite loop is written while(1), or for(;;), where an omitted condition is defined to be true.

Please log in or register to report this question.

42.

A labelled statement consists of an identifier followed by

Answer: B

Correct answer: :

A label is an identifier followed by a colon - here: - marking a point that goto can jump to. The same colon form is used by case and default labels inside a switch. Labels have function scope, so a goto can only reach one in the same function.

Please log in or register to report this question.

43.

goto can be used to jump from main into another function.

Answer: B

Correct answer: FALSE

goto can only reach a label in the same function; labels have function scope and are invisible outside it. Jumping between functions would leave the stack in an inconsistent state. Where a non-local jump really is needed, setjmp and longjmp provide a disciplined version of it.

Please log in or register to report this question.

44.

What is the output of this C code?

int main()

    {

        int *p = NULL;

        for (foo(); p; p = 0)

            printf("In for loop\n");

            printf("After loop\n");

    }

 

Answer: B

Correct answer: Compile time error

foo is never declared or defined anywhere in this program. Under the current C rules an undeclared function is an error, and even where it is only a warning the missing definition would fail at link time. So the program does not run at all - the value of p is beside the point.

Please log in or register to report this question.

45.

In the loop construct for(exp1; exp2; exp3), which part is always executed exactly once?

Answer: A

Correct answer: exp1

exp1, the initialisation, is evaluated once before the loop starts and never again. exp2, the condition, is evaluated before every pass - at least once, and once more than the body runs. exp3, the update, runs after each pass, so it may not run at all if the condition fails immediately.

Please log in or register to report this question.

46.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int x = 3;
    float y = 3.0;
    if(x == y)
        printf("x and y are equal");
    else
        printf("x and y are not equal");
    return 0;
}

 

Answer: A

Correct answer: x and y are equal

When an int and a float are compared, the int is converted to floating point first. Both values are then exactly 3.0 - and 3 is one of the values a float can represent exactly - so the comparison succeeds. It is only fractional values such as 0.1 that make such comparisons unreliable.

Please log in or register to report this question.

47.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int a=0, b=1, c=3;
    *((a) ? &b : &a) = a ? b : c;
    printf("%d, %d, %d\n", a, b, c);
    return 0;
}

 

Answer: C

Correct answer: 3, 1, 3

Both conditionals test a, which is 0 and therefore false. The left-hand side selects &a, so the assignment targets a; the right-hand side selects c, which is 3. So a becomes 3 while b and c are untouched, giving 3, 1, 3. Note that a conditional expression can be dereferenced to form an lvalue like this only because both branches are pointers.

Please log in or register to report this question.

48.

Which of the following statements are correct about the below program?

#include <stdio.h>
int main()
{
    int i = 10, j = 20;
    if(i = 5) && if(j = 10)
        printf("Have a nice day");
    return 0;
}

 

Answer: C

Correct answer: Error: Expression syntax

if is a statement, not an expression, so it cannot be an operand of &&. The line if(i = 5) && if(j = 10) is simply not C. The intended test would be written if((i = 5) && (j = 10)).

Please log in or register to report this question.

49.

What will be the output of the program?

#include <stdio.h>
int main()
{
    unsigned int i = 65535; /* Assume 2 byte integer*/
    while(i++ != 0)
        printf("%d",++i);
    printf("\n");
    return 0;
}

 

Answer: A

Correct answer: Infinite loop

i++ tests the old value and then increments, and the body increments again with ++i. Starting from 65535, the test takes i to 0, the body takes it to 1, and thereafter the value seen by the test is always odd - 1, 3, 5, … Wrapping round at 65536 lands on 1 again, never on 0, so the condition is never false.

Please log in or register to report this question.

Answer: B

Correct answer: for (i = 0, j = 0;i < n, j < n; i++, j += 5)

The comma operator lets a single for carry more than one initialisation and more than one update, so both variables advance together in one loop. Note the trap in the condition: i < n, j < n evaluates both but yields only the last, so it is really j < n that controls the loop - a genuine condition on both would need &&.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Control Instructions Questions and Answers || Control Instructions MCQ Questions and Answers || Control Instructions GK Questions and Answers || Control Instructions GK MCQ Questions || Control Instructions Multiple Choice Questions and Answers || Control Instructions GK || GK on Control Instructions || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming