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

Control Instructions Questions and Answers

31.

What is the output of this C code?

int main()

    {

        int i = 0;

        for (foo(); i == 1; i = 2)

            printf("In for loop\n");

            printf("After loop\n");

    }

    int foo()

    {

        return 1;

    }

 

Answer: A

Correct answer: After loop

The initialisation foo() is evaluated once and its value discarded, so the 1 it returns changes nothing. The condition i == 1 is then tested with i still 0, which is false, so the body never runs - only After loop is printed.

Please log in or register to report this question.

32.

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.

33.

What is the output of the below program?

#include <stdio.h>
int main()
{
    int i = 0;
    switch (i)
    {
        case '0': printf("Geeks");
                break;
        case '1': printf("Quiz");
                break;
        default: printf("GeeksQuiz");
    }
    return 0;
} 

 

Answer: C

Correct answer: GeeksQuiz

'0' is a character constant, and its value is the ASCII code 48, not the number 0. Likewise '1' is 49. The controlling expression is 0, which matches neither, so control falls to default.

Please log in or register to report this question.

34.

#include <stdio.h>
int main()
{
    int i = 3;
    switch (i)
    {
        case 0+1: printf("Geeks");
                break;
        case 1+2: printf("Quiz");
                break;
        default: printf("GeeksQuiz");
    }
    return 0;
}

 

Answer: B

Correct answer: Quiz

A case label may be any constant expression, and the compiler evaluates it: 0+1 is 1 and 1+2 is 3. With i equal to 3 the second label matches, so Quiz is printed and the break ends the switch.

Please log in or register to report this question.

35.

Predict the output of the below program:

#include <stdio.h>
#define EVEN 0
#define ODD 1
int main()
{
    int i = 3;
    switch (i & 1)
    {
        case EVEN: printf("Even");
                break;
        case ODD: printf("Odd");
                break;
        default: printf("Default");
    }
    return 0;
}

 

Answer: B

Correct answer: Odd

i & 1 isolates the lowest bit, which is 1 for an odd number and 0 for an even one. With i equal to 3 the expression is 1, matching ODD, so Odd is printed. This bitwise test is a common alternative to i % 2.

Please log in or register to report this question.

36.

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

 

Answer: C

Correct answer: 03

printf returns the number of characters written. It prints 0 - that is one character - and returns 1, which is true, so i becomes 3. The output is therefore the 0 from the condition followed by the 3: 03.

Please log in or register to report this question.

37.

#include <stdio.h>
int i;
int main()
{
    if (i);
    else
        printf("Else");
    return 0;
}

What is correct about the above program?

Answer: B

Correct answer: Else block is executed

i is a global, so it is zero-initialised - not indeterminate, which is what makes the outcome predictable. The condition is therefore false. The semicolon after if (i) is the empty statement that forms the if branch, and the else attaches to it perfectly legally, so the else branch runs.

Please log in or register to report this question.

38.

What is the output?

#include <stdio.h>
int main()
{
   int n;
   for (n = 9; n!=0; n--)
     printf("n = %d", n--);
   return 0;
}

 

Answer: C

Correct answer: Infinite Loop

n is decremented twice on every pass - once by n-- inside the printf and once by the loop’s own n--. Starting from 9 it therefore takes only odd values: 9, 7, 5, 3, 1, -1, -3, … and never equals 0, so the test n != 0 never fails. It runs until n underflows, which is undefined behaviour.

Please log in or register to report this question.

39.

Output?

#include <stdio.h>
int main()
{
    int c = 5, no = 10;
    do {
        no /= c;
    } while(c--);
  
    printf ("%d\n", no);
    return 0;
}

 

Answer: B

Correct answer: Runtime Error

The loop divides no by c and then tests c--, so c falls 5, 4, 3, 2, 1, 0. On the pass after the test that saw c equal to 1, c is 0 - and no /= 0 is integer division by zero, which the hardware traps. The compiler cannot see it, so it is a run-time fault.

Please log in or register to report this question.

Answer: B

Correct answer: GeeksQuiz GeeksQuiz GeeksQuiz .... infinite times

i starts at 1 and rises by 2, so it takes only the odd values 1, 3, 5, 7, 9, 11, … and never equals 10. The test i != 10 is therefore never false. This is why < is safer than != in a loop condition whose variable moves by more than one.

Please log in or register to report this question.

Loading…
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