Output of C++ programs | Set 43 (Decision and Control Statements)

Decision and Loops & Control Statements
QUE.1 What is the output of this program ?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
int main ()
{
    int n;
    for (n = 5; n > 0; n--)
    {
        cout << n;
        if (n == 3)
            break;
    }
    return 0;
}

chevron_right


OPTION
a) 543
b) 54
c) 5432
d) 53

Answer: a

Explanation : In this program, we are printing the numbers in reverse order and by using break statement we stopped printing on 3.



QUE.2 What is the output of this program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
int main()
{
    int a = 10;
    if (a < 15)
    {
        time:
        cout << a;
        goto time;
    }
    break;
    return 0;
}

chevron_right


OPTION
a) 1010
b) 10
c) infinitely print 10
d) compile time error

Answer:  d

Explanation: Because the break statement needs to be presented inside a loop or a switch statement.

QUE. 3 What is the output of this program ?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
int main()
{
    int n = 15;
    for ( ; ; )
    cout << n;
    return 0;
}

chevron_right


OPTION
a) error
b) 15
c) infinite times of printing n
d) none of the mentioned

Answer: c

Explanation: There is no condition in the for loop, So it will loop continuously.

QUE.4 What is the output?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
int main()
{
    int i;
    for (i = 0; i < 10; i++);
    {
        cout << i;
    }
    return 0;
}

chevron_right


OPTION
a) 0123456789
b) 10
c) 012345678910
d) compile time error

Answer: b

Explanation: for loop with a semicolon is called as body less for loop. It is used only for incrementing the variable values. So, in this program the value is incremented and printed as 10.

QUE.5 what is output of this program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
int i = 30;
using namespace std;
int main()
{
    int i = 10;
    for (i = 0; i < 5; i++)
    {
        int i = 20;
        cout << i <<" ";
    }
    return 0;
}

chevron_right


OPTION
a) 0 1 2 3 4
b) 10 10 10 10 10
c) 20 20 20 20 20
d) Compile Errors

Answer: c

Explanation: int i = 20 is initialized in the body of loop so it considers the value i = 20 inside loop only. For loop control, the i declared outside is used and loop runs 5 times and print 20 five times.

This article is contributed by Ajay Puri. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.