Exceptions CPPE2 M3 Test

  1. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    #include <stdexcept>
    using namespace std;
    
    class X : public domain_error  {
    public:
        X() : domain_error("0") {};
    };
    
    void z() throw(X) {
        X x;
        throw x;
        cout << 1;
    }
    
    int main()
    {
        X x;
        try {
           z();
        } catch(X &i) {
          cout << 1;
        }
        catch(domain_error &i) {
           cout << 0;
        }
        return 0;
    }
    • Execution fails
    • 1
    • 0
    • Compilation fails
  2. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    #include <stdexcept>
    using namespace std;
    
    class Int {
    public:
        int v;
        Int(int a) : v(a) {}
    };
    
    void a() {
        throw Int(1);
    }
    
    void b() {
        try {
           a();
        }catch(Int &i) {
             throw Int(i.v + 1);
        }
    }
    
    void c() {
        try {
             b();
         } catch(...) {
             throw;
         }
    }
    
    int main()
    {
       try {
            c();
        } catch(Int &i) {
            cout<<i.v;
        }
        return 0;
    }
    • 0
    • 1
    • 2
    • Compilation fails
  3. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    #include <stdexcept>
    using namespace std;
    
    class X : public logic_error {
    public:
        X() : logic_error("0") {};
    };
    
    void z() throw(logic_error) {
        X x;
        throw x;
        cout << 1;
    }
    
    int main()
    {
        X x;
        try {
             z();
         } catch(logic_error &i) {
             cout << i.what();
         }
         return 0;
    }
    • Compilation fails
    • Execution fails
    • 0
    • 1
  4. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    using namespace std;
    
    int main()
    {
        throw 2/4;
        catch(int i) {
           cout << i;
        }
        return 0;
    }
    • 0.5
    • Execution fails
    • 0
    • Compilation fails
  5. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    using namespace std;
    
    int main()
    {
        try {
           throw 2./4;
        }
        catch(int i) {
           cout << i;
        }
        return 0;
    }
    • 0
    • Compilation fails
    • Execution fails
    • 0.5
  6. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    #include <stdexcept>
    using namespace std;
    
    class E {};
    
    class X {
        static int c;
    public:
        X() { if(c++ > 2) throw new E;}
        ~X() { if(c++ > 2) throw new E;}
    };
    
    int X::c = 0;
    
    void f(int i) {
        X a,b;
        cout << i;
    }
    
    int main()
    {
       try {
          f(0);
          f(1);
        } catch(...) {
           cout << 1;
        }
        return 0;
    }
    • 0
    • Execution fails
    • Compilation fails
    • 01
  7. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    #include <stdexcept>
    using namespace std;
    
    class E {};
    
    class X {
    public:
        static int c;
        X(int a) { c = a; }
        ~X() { if(c++ > 2) throw new E; }
    };
    
    int X::c = 0;
    
    void f(int i) {
         X* t[2];
         for(int j = 0; j < i; j++)
            t[j] = new X(i+1);
         for(int j = 0; j < i; j++)
            delete t[j];
    }
    
    int main(void)
    {
        try {
            f(2);
        } catch(...) {
             cout << X::c;
        }
        return 0;
    }
    • 2
    • 3
    • 2
    • Compilation fails
  8. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class X {
    public:
        X(void) throw(int) { cout << 1; }
        ~X(void) throw(int) { cout << 2; }
        void exec() { throw string("0"); }
    };
    
    void exec( X x) {
        x.exec() ;
    }
    
    int main(void) 
    {
       X x;
       try {
           exec(x);
       } catch(int &i) {
           cout << i;
       }
       return 0;
    }
    • Compilation fails
    • 102
    • 120
    • Execution fails
  9. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    #include <stdexcept>
    using namespace std;
    
    class X : public logic_error {
    public:
        X() : logic_error("0") {};
    };
    
    void z() throw(X) {
        throw new logic_error("0");
    }
    
    int main(void)
    {
        X x;
        try {
           z();
        } catch(X &i) {
           cout << i.what();
       }
        return 0;
    }
    • 120
    • 102
    • Compilation fails
    • Execution fails
  10. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    using namespace std;
    
    int main()
    {
        try {
           throw exception();
        }
        catch(exception &x) {
           cout << x.what();
        }
        return 0;
    }
    • It prints a non-empty string 
    • Compilation fails
    • It prints an empty string
    • Execution fails
  11. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class X {
    public:
        X(void) throw(int) { cout << 1; }
        ~X(void) throw(int) { cout << 2; }
        void exec() { throw 0; }
    };
    
    void exec( X &x) {
        x.exec() ;
    }
    
    int main(void) 
    {
       X x;
       try {
           exec(x);
       } catch(int &i) {
           cout << i;
       }
       return 0;
    }
    • Compilation fails
    • 120
    • 102
    • Execution fails
  12. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class X {
    public:
        X(void) { cout << 1; }
        ~X(void) { cout << 2; }
    };
    
    X *exec() {
        X *x = new X();
        throw string("0");
        return x;
    }
    
    int main(void) 
    {
       X *x;
       try {
           delete exec();
       } catch(string &s) {
           cout << s;
       }
       return 0;
    }
    • 12
    • Compilation fails
    • 10
    • Execution fails
  13. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    #include <stdexcept>
    using namespace std;
    class X : public runtime_error {
    public:
        X() : domain_error("0") {};
    };
    
    void z() throw(X) {
        X x;
        throw x;
        cout << 1;
    }
    
    int main(void) {
        X x;
        try {
            z();
        } catch(X &i) {
            cout << 1;
        }
        catch(domain_error &i) {
            cout << 0;
        }
        return 0;
    }
    • 0
    • Compilation fails
    • Execution fails
    • 1
  14. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    #include <stdexcept>
    using namespace std;
    class Int {
    public:
        int v;
        Int(int a) : v(a) {}
    };
    
    void a() {
        throw 0;
    }
    
    void b() {
        try {
            a();
        }catch(int i) {
            throw Int(i + 1);
        }
    }
    
    void c() {
        try {
            b();
         }catch(...) {
            throw;
         }
    }
    
    int main(void) 
    {
       try {
           c();
       } catch(int &i) {
           cout << i.v;
       }
       return 0;
    }
    • 2
    • 1
    • 0
    • Compilation fails
  15. What is the output of the following snippet?

    #include <iostream>
    #include <string>
    using namespace std;
    class X {
    public:
        X(void) { cout << 1; }
        ~X(void) { cout << 2; }
    };
    
    void exec() {
        X x; 
        throw string("0");
    }
    
    int main(void) 
    {
       try {
           exec();
       } catch(string &s) {
           cout << s;
       }
       return 0;
    }
    • 102
    • Execution fails
    • 120
    • Compilation fails
  16. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    using namespace std;
    
    int main() 
    {
       try {
           throw 2./4;
       } 
       catch(int i) {
           cout << i;
       }
       return 0;
    }
    • 0
    • Compilation fails
    • Execution fails
    • 0.5
  17. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    #include <stdexcept>
    using namespace std;
    class E {};
    
    void f(int i) {
        E e;
        switch(i) {
        case 0: throw e;
        case 1: throw &e;
        }
        cout << 0;
    }
    
    int main(void) 
    {
       try {
           f(2);
       } catch(E*) {
           cout << 1;
       }catch(E) {
           cout << 2;
       }
       return 0;
    }
    • Compilation fails
    • 1
    • 2
    • 0
  18. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class X {
    public:
        X(void) { cout << 0; }
        ~X(void) { cout << 2; }
    };
    
    int main(void) 
    {
       try {
           X *x = new X();
       throw true; 
           delete x;
       } catch(bool s) {
           cout << s;
       }
       return 0;
    }
    • 01
    • Execution fails
    • 021
    • Compilation fails
  19. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    #include <stdexcept>
    using namespace std;
    class E {};
    
    void f(int i) {
        E e;
        switch(i) {
        case 0: throw e;
        case 1: throw &e;
        }
        cout << 0;
    }
    
    int main(void) 
    {
       try {
           f(1);
       } catch(void *) {
           cout << 2;
       } catch(E*) {
           cout << 1;
       }
       return 0;
    }
    • 0
    • 1
    • 2
    • Compilation fails
  20. What is the output of the following snippet?

    #include <iostream>
    #include <exception>
    using namespace std;
    
    int main(void) 
    {
       try {
           throw 3.14;
       } catch(double x) {
           x *= 2;
       }
       cout << x;
       return 0;
    }
    • Execution fails
    • It prints an empty string 
    • It prints an non-empty string
    • Compilation fails
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments