LearnFrenzy
Sign in Take a free test

Exception Handling · Exercise 2 of 4

Exception Handling Predict the Output

  • 20 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each C++ program and choose what it prints. Every program was compiled and run on a 64-bit system, so the answers are the real output.

Question 1 Easy

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <stdexcept>

int main() {
    int step = 0;
    try {
        step = 1;
        throw std::runtime_error("x");
        step = 2;
    } catch (const std::exception&) {
        step += 10;
    }
    step += 100;
    std::cout << step << '\n';
    return 0;
}
A 111
B 112
C 101
D 11
Question page & discussion
Question 2 Medium

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <stdexcept>

int main() {
    try {
        throw std::out_of_range("bad index");
    } catch (const std::logic_error& e) {
        std::cout << "logic: " << e.what() << '\n';
    } catch (const std::out_of_range& e) {
        std::cout << "range: " << e.what() << '\n';
    } catch (...) {
        std::cout << "other\n";
    }
    return 0;
}
A logic: bad index
B range: bad index
C other
D logic: bad index and range: bad index
Question page & discussion
Question 3 Hard

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <stdexcept>
#include <string>

struct Base { virtual std::string name() const { return "Base"; } virtual ~Base() = default; };
struct Derived : Base { std::string name() const override { return "Derived"; } };

int main() {
    try { throw Derived(); }
    catch (Base b) { std::cout << b.name() << ' '; }           // by value
    try { throw Derived(); }
    catch (const Base& b) { std::cout << b.name() << '\n'; }   // by reference
    return 0;
}
A Base Derived
B Derived Derived
C Base Base
D Derived Base
Question page & discussion
Question 4 Medium

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <stdexcept>

struct T {
    char c;
    explicit T(char x) : c(x) { std::cout << '+' << c; }
    ~T() { std::cout << '-' << c; }
};

void inner() { T x('x'); T y('y'); throw std::runtime_error("e"); }
void outer() { T a('a'); inner(); T b('b'); }

int main() {
    try { outer(); }
    catch (const std::exception& e) { std::cout << " caught " << e.what(); }
    std::cout << '\n';
    return 0;
}
A +a+x+y-y-x-a caught e
B +a+x+y-a-x-y caught e
C +a+x+y caught e-y-x-a
D +a+x+y+b-b-y-x-a caught e
Question page & discussion
Question 5 Hard

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <stdexcept>

struct NotFound : std::runtime_error { using std::runtime_error::runtime_error; };

void relay(bool plain) {
    try { throw NotFound("user 7"); }
    catch (const std::exception& e) {
        if (plain) throw;          // original object
        throw e;                   // copy of the static type: std::exception
    }
}

int main() {
    for (bool plain : {true, false}) {
        try { relay(plain); }
        catch (const NotFound& e) { std::cout << "NotFound:" << e.what() << ' '; }
        catch (const std::exception&) { std::cout << "std::exception "; }
    }
    std::cout << '\n';
    return 0;
}
A NotFound:user 7 std::exception
B NotFound:user 7 NotFound:user 7
C std::exception std::exception
D std::exception NotFound:user 7
Question page & discussion
Question 6 Medium

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <stdexcept>

int main() {
    try {
        try {
            throw std::runtime_error("deep");
        } catch (const std::logic_error&) {
            std::cout << "inner logic ";
        }
        std::cout << "after inner ";
    } catch (const std::runtime_error& e) {
        std::cout << "outer " << e.what();
    }
    std::cout << '\n';
    return 0;
}
A outer deep
B inner logic after inner
C after inner outer deep
D inner logic outer deep
Question page & discussion
Question 7 Hard

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <stdexcept>

struct M {
    int id;
    explicit M(int i) : id(i) { if (i == 3) throw std::runtime_error("m3"); std::cout << "+M" << id << ' '; }
    ~M() { std::cout << "-M" << id << ' '; }
};

struct Owner {
    M a{1}, b{2}, c{3}, d{4};
    Owner() { std::cout << "Owner body "; }
    ~Owner() { std::cout << "~Owner "; }
};

int main() {
    try { Owner o; }
    catch (const std::exception& e) { std::cout << "| " << e.what(); }
    std::cout << '\n';
    return 0;
}
A +M1 +M2 -M2 -M1 | m3
B +M1 +M2 ~Owner -M2 -M1 | m3
C +M1 +M2 | m3
D +M1 +M2 -M1 -M2 | m3
Question page & discussion
Question 8 Easy

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <string>

int check(int x) {
    if (x < 0) throw -1;
    if (x == 0) throw std::string("zero");
    if (x > 100) throw 'B';
    return x;
}

int main() {
    for (int v : {5, -3, 0, 500}) {
        try { std::cout << check(v) << ' '; }
        catch (int code) { std::cout << "int" << code << ' '; }
        catch (const std::string& s) { std::cout << s << ' '; }
        catch (...) { std::cout << "other "; }
    }
    std::cout << '\n';
    return 0;
}
A 5 int-1 zero other
B 5 int-1 zero int66
C 5 -1 zero B
D 5 int-1 other other
Question page & discussion
Question 9 Medium

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <stdexcept>
#include <string>

int main() {
    int sum = 0;
    for (std::string s : {"12", "7abc", "x9", "99999999999", " 4"}) {
        try { sum += std::stoi(s); }
        catch (const std::invalid_argument&) { std::cout << "invalid "; }
        catch (const std::out_of_range&) { std::cout << "range "; }
    }
    std::cout << sum << '\n';
    return 0;
}
A invalid range 23
B invalid invalid range invalid 12
C invalid range 19
D invalid invalid range 16
Question page & discussion
Question 10 Easy

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <stdexcept>
#include <vector>

int main() {
    std::vector<int> v{10, 20, 30};
    int found = 0, errors = 0;
    for (std::size_t i : {0u, 2u, 3u, 7u}) {
        try { found += v.at(i); }
        catch (const std::out_of_range&) { ++errors; }
    }
    std::cout << found << ' ' << errors << '\n';
    return 0;
}
A 40 2
B 60 1
C 40 0
D 30 2
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links