What will be the output of the following C++ code? [code] #include <iostream> using namespace std; int f(int p, int q)

Trung Minh

New member
What will be the output of the following C++ code?
Code:
    #include <iostream>
    using namespace std;
    int f(int p, int q)
    {
        if (p > q)
            return p;
        else
            return q;
    }
    main()
    {
        int a = 5, b = 10;
        int k;
        bool x = true;
        bool y = f(a, b);
        k =((a * b) + (x + y));
        cout << k;
    }
A. 55
B. 62
C. 52
D. 75
 
Answer: c
Explanation: In this question, value of x = true and value of y will be also true as f(a,b) will return a non-zero value. Now when adding these values with integers, the implicit type conversion takes place hence converting both x and y to 1(integer equivalent of bool true value). So expression (a*b) + (x+y) is evaluated to 52.
 

Tìm kiếm

Back
Top