Test Series - cpp

Test Number 50/102

Q: Which of the following is used for generic programming?
A. Virtual functions
B. Modules
C. Templates
D. Abstract Classes
Solution: Templates are used for generic programming. They help in making generic functions and classes hence achieving the generic codes.
Q: Which of the following is correct about templates?
A. It is a type of compile time polymorphism
B. It allows the programmer to write one code for all data types
C. Helps in generic programming
D. All of the mentioned
Solution: Templates are used for generic programming hence allowing to write a single function for all data types. It is a type of compile time polymorphism.
Q: Which keyword is used to handle the expection?
A. try
B. throw
C. catch
D. handler
Solution: When we found a exception in the program, We need to throw that and we handle that by using the catch keyword.
Q: Which is used to throw a exception?
A. throw
B. try
C. catch
D. handler
Solution: throw keyword is used to throw an exception.
eg:
if(divisor == 0)
{
    throw "Divide by zero error";
}
Q: What is the use of the ‘finally’ keyword?
A. It used to execute at the starting of the program
B. It will be executed at the end of the program even if the exception arised
C. It will be executed at the starting of the program even if the exception arised
D. It will be executed at the middle of the program even if the exception arised
Solution: finally keyword will be executed at the end of all the exception.
Q: How do define the user-defined exceptions?
A. inheriting and overriding exception class functionality
B. overriding class functionality
C. inheriting class functionality
D. delting and adding class member
Solution: User defined exceptions can be done by inheriting and overriding the exception class functionality.
Q: Which exception is thrown by dynamic_cast?
A. bad_cast
B. bad_typeid
C. bad_exception
D. bad_alloc
Solution: bad_cast exception is thrown by dynamic_cast.
Q: How many types of exception handling are there in c++?
A. 1
B. 2
C. 3
D. 4
Solution: There are two types of exception handling in c++. They are synchronous exception handling and asynchronous exception handling.
Q: How many runtime error messages associated with exception?
A. 2
B. 4
C. 5
D. infinite
Solution: There are four runtime error messages associated with exceptions. They are overflow_error, range_error, system_error and underflow_error.
Q: Which block should be placed after try block?
A. catch
B. throw
C. either catch or throw
D. handler
Solution: Syntax of try catch block:
try{
    // do something
}
catch{
   // catch respective error.
}
finally{
   // do something after trying or catching error i.e. run this block in both cases.
}

You Have Score    /10