Open In App

Output of C++ programs | Set 40

Improve
Improve
Like Article
Like
Save
Share
Report

Q.1 What is the output of following program? 

CPP




#include
using namespace std;
int max(int& x, int& y, int& z)
{
if (x > y && y > z) {
y++;
z++;
return x++;
} else {
if (y > x)
return y++;
else
return z++;
}
}
int main()
{
int A, B;
int a = 10, b = 13, c = 8;
A = max(a, b, c);
cout << a++ << " " << b-- << " " << ++c << endl; B = max(a, b, c); cout << ++A << " " << --B << " " << c++ << endl; return 0; }


Option a) 3 3 b) 2 0 c) garbage value d) 2 3

Answer : c

Explanation : In this program, in given array we did not assign any value so it will be pointing to any garbage value. Q.2 What is the output of following program? 

CPP




#include <iostream>
using namespace std;
int main()
{
    for (int i = 10; i > 6; i = i - 2)
        cout << i;
    for (int i = -5; i > -7; i--)
        cout << i + 1;
    return 0;
}


Option a) Computer Science, Mathematics b) Mathematics c) wrong choice d) error

Answer : c

Explanation : In this program, m is a variable and it is not assigned any value which means – m is having any garbage value. So, it doesn’t match any case and by default it will be print “wrong choice”. Q.3 What is the output of following program? 

CPP





Option a) 3 4 5 6 7 b) 4 5 6 7 8 c) 2 3 4 5 6 d) none of above

Answer : a

Explanation : In this loop, simply first assign the value of y and then increment so no change. Q.4 What is the output of following program? 

CPP




#include <iostream>
using namespace std;
int max(int& x, int& y, int& z)
{
    if (x > y && y > z) {
        y++;
        z++;
        return x++;
    } else {
        if (y > x)
            return y++;
        else
            return z++;
    }
}
int main()
{
    int A, B;
    int a = 10, b = 13, c = 8;
    A = max(a, b, c);
    cout << a++ << " " << b-- << " " << ++c << endl;
    B = max(a, b, c);
    cout << ++A << " " << --B << " " << c++ << endl;
    return 0;
}


Option a) 10 14 8 or 14 13 8 b) 10 13 8 or 11 14 9 c) 10 14 9 or 14 12 9 d) 11 12 8 or 13 14 8

Answer : c

Explanation : Here, max function is to return max value and at the time of first print, the value of b decrements and remaining two a and c are same and remain same during second call of function. Q.5 What is the output of following program? 

CPP




#include <iostream>
using namespace std;
int main()
{
    for (int i = 10; i > 6; i = i - 2)
        cout << i;
    for (int i = -5; i > -7; i--)
        cout << i + 1;
    return 0;
}


Option a) 10 8 6 -5 -6 b) 10 8 -4 -5 c) 10 8 -5 -6 d) 8 6 -4 -5

Answer : b

Explanation : First loop is executed and prints 10, 8 and then condition gets false, so loop exits. Then second loop is executed and checks the condition and print -4, -5 after that the condition becomes false then it gets terminated.



Last Updated : 18 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads