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

Khang Lê

New member
What will be the output of the following C++ code?
Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        double a = 10, b = 5, res;
        char Operator = '/';
        try 
        {
            if (b == 0)
                throw "Division by zero not allowed";
            res = a / b;
            cout << a << " / " << b << " = " << res;
        }
        catch(const char* Str)
        {
            cout << "\n Bad Operator: " << Str;
        }
        return 0;
    }
A. 10
B. 2
C. Bad Operator
D. 10 / 5 = 2
 
Answer: d
Explanation: In this program, We are dividing the two variables and printing the result. If any one of the operator is zero means, it will arise a exception.
Output:
Code:
$ g++ gex.cpp
$ a.out
10 / 5 =2
 

Tìm kiếm

Back
Top