Open In App

Output of C programs | Set 61 (Loops)

Last Updated : 04 Oct, 2017
Comments
Improve
Suggest changes
Like Article
Like
Report
Prerequisite : Loops in C Q.1 What is the output of this program? CPP
#include <iostream>
using namespace std;
int main()
{
    int i, j, var = 'A';

    for (i = 5; i >= 1; i--) {
        for (j = 0; j < i; j++)
            printf("%c ", (var + j));
        printf("\n");
    }
    return 0;
}
Options a)A B C D E A B C D E A B C D E A B C D E A B C D E b)A B C D A B C D A B C D A B C D c)A B C D A B C A B A d)A B C D E A B C D A B C A B A
ans:- d 
Explanation :- inner loop iterates for value less than equal to i, thus printing A B C D E A B C D A B C A B A Q.2 What is the output of this program? CPP
#include <iostream>
using namespace std;
int main()
{
    int counter = 1;
    do {
        printf("%d, ", counter);
        counter += 1;
    } while (counter >= 10);
    printf("\nAfter loop counter=%d", counter);
    printf("\n");
    return 0;
}
Options a) After loop counter=1 b) 1, After loop counter=2 c) 1, After loop counter=1 d) After loop counter=2
ans:- b
Explanation :- do while is an exit controlled loop, here loop body executed first, then condition will be checked. Q.3 What is the output of this program? CPP
#include <iostream>
using namespace std;
int main()
{
    int counter = 1;
    while (counter >= 10) {
        printf("%d, ", counter);
        counter += 1;
    }
    printf("\nAfter loop counter=%d", counter);
    printf("\n");
    return 0;
}
Options a)After loop counter=1 b)1, After loop counter=2 c)1, After loop counter=1 d)After loop counter=2
ans:- a
Explanation :- since while is an entry controlled loop so, here condition will be checked first. Q.4 What is the output of this program? CPP
#include <iostream>
using namespace std;
int main()
{
    int loopvar = 10;
    while (printf("Hello ") && loopvar--)
        ;
    return 0;
}
Options a)Hello b)Hello Hello Hello Hello ....... c)Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello d)Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
ans:- d
Explanation :- since post decrement operator is used so value is decremented after expression loopvar - - is evaluated. Thus, Hello is printed 11 times. Q.5 What is the output of this program? CPP
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    int counter = 1;
    while (counter <= 10 && 1 ++)
        printf("Hello");
    return 0;
}
Options a)compilation error b)HelloHello ... 10 times c)HelloHello ... 11 times d)Hello
ans:- a
Explanation :- Error: lvalue required as increment operand. It is a compile time error.

Next Article

Similar Reads