Here is a listing of C++ programming interview questions on “Error Handling Alternatives” along with answers, explanations and/or solutions:
1. Which alternative can replace the throw statement?
a) for
b) break
c) return
d) exit
Answer: c
Clarification: throw and return does the same job as return a value. So it can be replaced.
2. What are the disadvantages if use return keyword to return error codes?
a) You have to handle all exceptional cases explicitly
b) Your code size increases dramatically
c) The code becomes more difficult to read
d) All of the mentioned
Answer: d
Clarification: As we are using return for each and every exception, It will definitely increase the code size.
3. What is most suitable for returning the logical errors in the program?
a) Use constructor and destructor
b) Set a global error indicator
c) Use break keyword
d) Use final keyword
Answer: b
Clarification: Set a global error indicator is most suitable for returning the logical errors in the program.
4. What will be the output of the following C++ code?
-
#include -
#include -
using namespace std;
-
class A -
{ -
};
-
int main()
-
{ -
char c; float x;
-
if (typeid(c) != typeid(x))
-
cout << typeid(c).name() << endl;
-
cout << typeid(A).name();
-
return 0;
-
}
a)
c 1A
b) x
c) Both c & x
d) c
Answer: a
Clarification: We are checking the type id of char and float as they are not equal, We are printing c.
Output:
$ g++ eal.cpp $ a.out c 1A
5. What will be the output of the following C++ code?
-
#include -
using namespace std;
-
void Division(const double a, const double b);
-
int main()
-
{ -
double op1=0, op2=10;
-
try -
{ -
Division(op1, op2);
-
} -
catch (const char* Str)
-
{ -
cout << "nBad Operator: " << Str;
-
} -
return 0;
-
} -
void Division(const double a, const double b)
-
{ -
double res;
-
if (b == 0)
-
throw "Division by zero not allowed";
-
res = a / b;
-
cout << res;
-
}
a) 0
b) Bad operator
c) 10
d) 15
Answer: a
Clarification: We are dividing 0 and 10 in this program and we are using the throw statement in the function block.
Output:
