Sunday, February 23, 2020

C Programming for loop predict output MCQs Set - 1 Answers

C Programming for loop predict output (MCQs) Set - 1 Answers

1. Predict output of following C program:
#include <stdio.h>
void main()
{
    int c = 0;
    for (c)
        printf("Top C Practicals");
}
A. Compile time error
B. Top C Practicals
C. no output
D. none of these

ANSWER: A
Explanation: C programming for loop needs three expressions/statements inside for loop.

2. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i = 0;
    for ( ; i<5 ; i++)
        printf("%d ", i);
    return 0;
}
A. 0 1 2 3 4
B. 1 2 3 4 5
C. Compile time error
D. none of these

ANSWER: A
Explanation: Initial value of i will be considered as 0. Hence output will be 0 1 2 3 4

3. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i=0, j=5;
    printf("Hello ");
    for (i; i>j; i++)
        printf("%d ", i);
    printf("Students ");
    return 0;
}
A. Hello 0 1 2 3 4 Students
B. Hello Students
C. Hello 0 1 2 3 4
D. 0 1 2 3 4 5

ANSWER: B
Explanation: Initial value of i is 0 and j is 5; Hence i>j will be evaluated to false. Hence for loop will be executed zero times; thus answer is Hello Students.

4. Predict output of following C program:
#include <stdio.h>
int main()
{
   int i;
   for (i=0; i<10; i++)
       printf("%d ", ++i);
   return 0;
}
A. 1 3 5 7 9
B. 0 1 2 3 4 5 6 7 8 9
C. 2 4 6 8
D. 2 4 6 8 10

ANSWER: A
Explanation: Due to ++i (pre-increment) inside loop, the output will be 1 3 5 7 9

5. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i;
    for (i=65; i<70; i++)
        printf("%c ", i);
    return 0;
}
A. 65 66 67 68 69
B. A B C D E
C. Compile time error
D. Run time error

ANSWER: B
Explanation: %c in the printf will print character associated with the ASCII value of 65 to 69. 

* * * * *



Read more interesting C Programming MCQs here.


No comments:

Post a Comment