Question

c++) explain your answer. What will be printed on the screen? explain your answer. int main()...

c++) explain your answer.

What will be printed on the screen? explain your answer.

int main()

{ int i = 3, j = 4; int x = 0;

try

{ if(i < j) throw “Oops!”; }

catch(int i)

{ x = 1; }

catch(double d)

{ x = 3; }

catch(...)

{ x = 4; }

cout << "x = " << x << endl; return 0; }

0 0
Add a comment Improve this question Transcribed image text
Answer #1


x = 4




int main() {
    int i = 3, j = 4;
    int x = 0;

    try {
        if (i < j)  // i < j is true, so, below throw statement is executed. throw throws a string.
            throw "Oops!";
    }
    catch (int i) { x = 1; }    // this is not executed because throw didn't throw an int
    catch (double d) { x = 3; } // this is not executed because throw didn't throw an double
    catch (...) { x = 4; }  // this is executed because none of the above throws were executed. this set's x to 4

    cout << "x = " << x << endl;    // this prints 4
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
c++) explain your answer. What will be printed on the screen? explain your answer. int main()...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • can someone please explain why 333 22 1 is printed on the screen? i have an...

    can someone please explain why 333 22 1 is printed on the screen? i have an idea as to why but it would help if an expert could explain why. int main() {    for (int i = 3; i > 0; i--) {        for (int j = 1; j <= i; j++) {            cout << i;        }        cout << endl;    }    system("pause"); }

  • Id: 40100885 in c++ output text please thanks! what is the output values printed by the...

    Id: 40100885 in c++ output text please thanks! what is the output values printed by the following code? You need to explain step by step how each printed value is calculated. #include <iostream> using namespace std; int m = 0; void SampleMethod (int); int SampleMethod(); void increase(); int main() { int j = 9; SampleMethod(j); cout << j<<endl; return 0; w Y == void SampleMethod(int i) { if (j%2 1) cout << SampleMethod() <<endl; else cout << j << "...

  • what is the output of the following code segment? C++ g. int arr[3][4]; for (int i...

    what is the output of the following code segment? C++ g. int arr[3][4]; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) arr[i][j] =i*4 + j; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) cout << arr[i][j] << " "; h. int next(int & x) { x= x + 1; return (x + 1); int main() { int y = 10;...

  • Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1,...

    Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1, int j=2,int k=3, double product =1.0) { i+=2; j*=2; k/=2; product=i*j*k; } void Magic(int& i, int& j, double& product) { i+=2; j=j*2+2; product=i*j; } void Magic(int* i,int* j) { double product; *i+=2; *j=*j*2+2; product=*i * *j; } int main() { double product; int i=0,j=0,k=0; product=i*j*k;    Magic(); cout<<"i, j, k and product in main () after 1st round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;    Magic(2,4); cout<<"i, j, k and...

  • 1. What is wrong with the following C++ program? #include <iostream> int main() { a =...

    1. What is wrong with the following C++ program? #include <iostream> int main() { a = 4; b = 6; cout << a << "+" << b << "=" << a+b; return 0; 2. What is wrong with the following C++ program? What was its intended output? #include <iostream> using namespace std; int main() { cout << "What is larger? e pi or pi e?" << endl; double ans1 = exp(pi); double ans2 = pi exp(1.); cout << "epi is...

  • In addition to including an exceptions library, you can also create exceptions by using the throw...

    In addition to including an exceptions library, you can also create exceptions by using the throw keyword. (1pt) Create a function called int throwsInt() that will throw an integer (1pt) Create a function called double throwsDouble() that will throw a double (2pt) Using try/catch statements, expand the code stubs to call each function in it's own try block, and catch and report the thrown value. Provided code: #include <iostream> using namespace std; int throwsInt(); double throwsDouble(); int main() { cout...

  • Hello, please EXPLAIN how you got your answer. Thank you! int x = 1 int j;...

    Hello, please EXPLAIN how you got your answer. Thank you! int x = 1 int j; for (j=0; j <=2  ; j++) x = x * j ; cout << x << endl; What is the output of the C++ code above? Answer: A) 0 B) 1 C) 2 D) 3

  • How many Iterations will this while loop perform? int ico), j(10); cout << "i = "...

    How many Iterations will this while loop perform? int ico), j(10); cout << "i = " << i << endl; cout << "j = " << j << endl; while (i > j) { cout << "j-" « j << endl; j += 2; cout << "i = " << i << endl; } cout << "i = << i << endl; cout << "j = " << j << endl; 5 6 C 8 10 Infinite times Does the...

  • What is printed when the following code is executed? #include <stdio.h> int main() { for (int...

    What is printed when the following code is executed? #include <stdio.h> int main() { for (int i=3, j=5; i>=0; i-=2, j+=j*i) { printf("%d", j); منم }

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT