Prerequisite : Loops in C
Q.1 What is the output of this program?
#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?
#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?
#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?
#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?
#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.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Oct, 2017
Like Article
Save Article