C++ Interview Questions

Bjarne-stroustrup
 
Prev14 of 17Next

According to the C++11 standard, what is the output of this program?

#include <iostream>
#include <exception>

int x = 0;

class A {
public:
	A() {
		std::cout << 'a';
		if (x++ == 0) {
			throw std::exception();
		}
	}
	~A() { std::cout << 'A'; }
};

class B {
public:
	B() { std::cout << 'b'; }
	~B() { std::cout << 'B'; }
	A a;
};

void foo() { static B b; }

int main() {
	try {
		foo();
	}
	catch (std::exception &) {
		std::cout << 'c';
		foo();
	}
}

Answer