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

Control Instructions Questions and Answers

41.

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.

42.

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.

43.

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.

44.

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.

45.

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.

47.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int i=0;
    for(; i<=5; i++);
        printf("%d", i);
    return 0;
}

 

Answer: D

Correct answer: 6

The semicolon immediately after the for header is an empty body, so the loop does nothing but count. It exits when the test i <= 5 first fails, which is when i has reached 6 - and that is the value the printf, which is outside the loop, then prints once.

Please log in or register to report this question.

48.

Comment on the code below

#include <stdio.h>
    void main()
    {
        int x = 5;
        if (true);
            printf("hello");
    }

 

Answer: B

Correct answer: It will throw an error

true is not a keyword in C. It is a macro defined in <stdbool.h>, which this program does not include, so the identifier is undeclared and the program fails to compile. (Had it compiled, the semicolon after the if would have been the empty statement forming its branch, and hello would have printed unconditionally.)

Please log in or register to report this question.

49.

Consider the following program fragment i=6720; j=4; while (i%j)==0 { i=i/j; j=j+1; } On termination j will have the value

Answer: C

Correct answer: 9

Each pass divides i by j and then increases j:

i672016803365681
j456789

At each step the division is exact, so the loop continues - until i is 1 and j is 9, where \(1 \bmod 9 = 1\) and the loop stops. j is then 9. (Note that 6720 = 4 × 5 × 6 × 7 × 8, which is what makes every division exact.)

Please log in or register to report this question.

Answer: D

Correct answer: “break” and “continue” can be used in “for”, “while” and “do-while” loop body. But only “break” can be used in “switch” body

Both statements work in all three loops. The difference is the switch: break is what ends a switch, while continue has no meaning there, since a switch has no iterations. A continue written inside a switch applies to the loop enclosing the switch, and fails to compile if there is none.

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