Test Series - cpp

Test Number 54/102

Q: What operation can be performed by destructor?
A. Abort the program
B. Resource cleanup
C. Exit from the current block
D. Terminate the program
Solution: It will be used to free all the resources that are used by the block of code during execution.
Q: What is the main purpose of the constructor?
A. Begin the execution of class
B. Include the macros for the program
C. Establish the class invariant
D. Terminate the program
Solution: The purpose of a constructor is to establish the class invariant. To do that, it often needs to acquire system resources or in general perform an operation that may fail.
Q: Why is it expensive to use objects for the exception?
A. Exception object is created only if an error actually happens
B. Because of execution time
C. Memory space involved in creating an exception object
D. Because of time and space
Solution: If an error occurs in the program, then only exception object is created otherwise, It will not be created. since throwing an exception triggers a bunch of actions during the stack unrolling, like invoking the the destructor of all the objects that has been created up to the point in which we are able to catch the exception, and invoking the destructor methods can imply flushing streams and freeing memory which can be expensive as well. Therefore, it’s expensive to use in the program.
Q: What is an exception in C++ program?
A. A problem that arises during the execution of a program
B. A problem that arises during compilation
C. Also known as the syntax error
D. Also known as semantic error
Solution: An exception is defined as the problem in C++ program that arises during the execution of the program for example divide by zero error.
Q: By default, what a program does when it detects an exception?
A. Continue running
B. Results in the termination of the program
C. Calls other functions of the program
D. Removes the exception and tells the programmer about an exception
Solution: By default, whenever a program detects an exception the program crashes as it does not know how to handle it hence results in the termination of the program.
Q: Why do we need to handle exceptions?
A. To avoid unexpected behaviour of a program during run-time
B. To let compiler remove all exceptions by itself
C. To successfully compile the program
D. To get correct output
Solution: We need to handle exceptions in a program to avoid any unexpected behaviour during run-time because that behaviour may affect other parts of the program. Also, an exception is detected during run-time, therefore, a program may compile successfully even with some exceptions cases in your program.
Q: How Exception handling is implemented in the C++ program?
A. Using Exception keyword
B. Using try-catch block
C. Using Exception block
D. Using Error handling schedules
Solution: C++ provides a try-catch block to handle exceptions in your program.
Q: What is an error in C++?
A. Violation of syntactic and semantic rules of a languages
B. Missing of Semicolon
C. Missing of double quotes
D. Violation of program interface
Solution: An error occurs when rules and laws of a language is violated while writing programs in that language.
Q: What is the difference between error and exception?
A. Both are the same
B. Errors can be handled at the run-time but the exceptions cannot
C. Exceptions can be handled at the run-time but the errors cannot
D. Both can be handled during run-time
Solution: Exceptions can be handled during run-time whereas errors cannot be because exceptions occur due to some unexpected conditions during run-time whereas about errors compiler is sure and tells about them during compile-time.
Q: What are the different types of exceptions?
A. 1
B. 2
C. 3
D. 4
Solution: There are two types of exceptions: Synchronous and asynchronous exceptions. Synchronous exceptions that are caused by the event which can be controlled by the program whereas Asynchronous exceptions are those which are beyond the control of the program.

You Have Score    /10