Self Studies

Computer Science Test - 3

Result Self Studies

Computer Science Test - 3
  • Score

    -

    out of -
  • Rank

    -

    out of -
TIME Taken - -
Self Studies

SHARING IS CARING

If our Website helped you a little, then kindly spread our voice using Social Networks. Spread our word to your readers, friends, teachers, students & all those close ones who deserve to know what you know now.

Self Studies Self Studies
Weekly Quiz Competition
  • Question 1
    5 / -1
    When is the finally block executed?
    Solution

    The correct answer is option 4.

    Concept:

    The finally block is always executed after the try block has terminated normally or after the try block has terminated due to an exception.

    Syntax:
    try:
           # Some Code here exception will occur

    except:
           # optional block Handling of exceptions (if required)

    finally:
          # Some code .....(always executed)      

    Key points for finally block:

    • After exiting the try statement, the finally block is always executed. If an exception is not handled by the except block, it is raised again after the finally block is executed.
    • Finally-block is used to deallocate system resources.
    • Finally can be used directly after a try without using an except block, but no exceptions are handled.

    Hence the correct answer is always.

  • Question 2
    5 / -1
    How many except statements can a try-except block have to handle any exception?
    Solution

    The correct answer is option 4.

    Concept:

    try-except:

    • The idea of the try-except clause is to handle exceptions (errors at runtime).
    • There has to be at least one except statement because in main exceptions will occur in the try black only occurred exceptions will be handled in the except block. Hence at least one or more than zero try-except blocks are required. 

    Syntax of the try-except block is:

    try:
       
    except Exception:
       

    try-except block is this:

    try:

    The code that will catch the exception(s). If an exception is thrown, it proceeds directly to the unless block.

    except:

    This code is only performed if an exception occurs in the try block. The unless the block is necessary with a try block, even if it merely includes the pass statement.

    Hence the correct answer is more than zero.

  • Question 3
    5 / -1
    When will the else part of try-except-else be executed? 
    Solution

    The correct answer is option 3.

    Concept:

    An exception interrupts the flow of the program without interrupting its execution.

    Clauses are defined as:

    Try:  Use to contain the statement of code where an exception can raise.

    Except:  This clause contains the way to handle a particular exception.

    Else: If no exception raises then the else block is used to execute the certain statements.  It means else part is executed when no exception occurs. The else block always need to be present after the except block.

    Finally:  Whether an exception is triggered or not, the finally-block is always performed.

    Syntax:
    try:
           # Some Code... 
    except:
           # optional block Handling of exceptions (if required)
    else:
           # execute if no exception
    finally:
          # Some code .....(always executed)

  • Question 4
    5 / -1
    Can one block of except statements handle multiple exception?
    Solution

    The correct answer is option 1.

    Exception handling clause:

    Try:

    This block will check for the expected error.

    Except: 

    We can deal with the problem here.

    Else:

    This block will be run if there are no exceptions. It means else part is executed when no exception occurs.

    Finally:

    Whether an exception is triggered or not, the finally-block is always performed.

    Syntax:
    try:
           # Some Codes or some operations are performed.
    except (error_1 name, erro_2 name, .......):
           # optional block Handling of multiple exceptions (if required) or some operations are performed.
    else:
           # execute if no exception
    finally:
          # Some code  (always executed) or some operations are performed.

    Explanation:

    A program can reply to several exceptions without being terminated by handling multiple exceptions. Try-except blocks in Python can be used to capture and handle one or more exceptions. When a process raises more than one possible exception, all of them can be handled with a single except clause. As a result, each type of exception may be explicitly specified. It is unnecessary to include it in a list.

    Hence the correct answer is yes, like except TypeError, SyntaxError [,…]

  • Question 5
    5 / -1

    What will be the output of the following Python code:

    a = [4, 5, 6, 7]
    try:
        print ("First element is %d" %(a[2]))
        print ("Fifth element is %d" %(a[5]))
     
    except:
        print ("Error")

    Solution

    Correct option: 

    First element is 6

    Error

    Concept:

    Try and Except statement is used to handle errors within code in Python. The code inside the try block will execute when there is no error in the program.

    Whereas the code inside the except block will execute whenever the program encounters some error in the execution of the try block.

     

    Syntax: 

    try:

        # Code

    except:

         # Executed if there is an error in the try block

    Explanation:

    Here the first statement in the try block is executed successfully as it is trying to access the 2nd index of the list. This statement will print "First element is 6".

    But the second statement in the try block is trying to access the 5th index which will throw the error "List index out of range", as the length of the array is 4 (i.e index range 0-3) and there is no 5th index in the list.

    This will lead to the execution of except block to handle the exception. This will print "Error" in the output.

     
  • Question 6
    5 / -1
    The process of executing a suitable handler is known as _______________.
    Solution

    The correct answer is option 4.

    Concept:

    • Exception handling ensures that the flow of the program doesn't break when an exception occurs.
    • The runtime system transmits the exception to the handler when an appropriate handler is identified. If the type of the exception object thrown matches the type that the handler can handle, the handler is regarded as suitable.
    • The chosen exception handler is said to catch the error. So the process of executing a suitable handler is known as catching the exception.

    ​Pseudocode:

    try:
       # do something
       pass

    except ValueError:
       # handle ValueError exception
       pass

    except (TypeError, ZeroDivisionError):
       # handle multiple exceptions
       # TypeError and ZeroDivisionError
       pass

    except:
       # handle all other exceptions
       pass

    ​Hence the correct answer is catching the exception.

  • Question 7
    5 / -1
    If the file specified in a program statement of a Python code is unable to be opened then the interpreter raise which type of error?
    Solution

    The correct answer is option 3.

    Concept:

    Exception:

    An exception is an occurrence that happens during program execution that disturbs the regular flow of the program's instructions. When a Python script finds a condition that it cannot handle, it throws an exception. A Python object that reflects an error is known as an exception.

    • IOError is an error that occurs when an input/output action fails, such as when using the print statement or the open() function to open a file that does not exist.
    • IOError is also raised for problems in the operating system.

    Example:

    import sys
    def whatever():
    try:
    f = open ( "foo.txt", 'r' )
    except IOError, e:
    print e
    print sys.exc_type
    whatever()

    Output:

    [Errno 2] No such file or directory:

    'foo.txt'

    Hence the correct answer is IOError.

    Additional Information

    • EOFError exception is thrown when methods like input() and raw input() return end-of-file (EOF) without reading any input.
    • IndexError is a sort of Python exception produced by the system when the index supplied as subscript does not fall within the range of indices of boundaries of a list.
    • NameError is a form of Python error that happens when running a function, variable, library, or string that has been entered in the code without any prior Declaration.
  • Question 8
    5 / -1
    The exceptions are need to be handled by ______________.
    Solution

    The correct answer is option 3.

    Concept:

    Exception:

    An exception is an occurrence that happens during program execution that disturbs the regular flow of the program's instructions. When a Python script finds a condition that it cannot handle, it throws an exception. A Python object that reflects an error is known as an exception.

    • Unchecked Exceptions are another name for Runtime Exceptions.
    • These exceptions are not verified at compile-time, thus the compiler does not know whether the programmer handled them or not, but it is the programmer's responsibility to handle these exceptions and offer a safe exit.
    • In the handle exception, some specific clauses are followed in Python like try-exception, try-except-else, etc.

    ​Hence the correct answer is the programmer.

    Additional Information

    • Editors, often known as text editors, are software applications that allow users to create and edit text files. In the realm of programming, the term editor mainly refers to source code editors, which offer various particular functions for creating and modifying code.
    • A compiler is a language processor that reads the entire source program written in the high-level language in one go and converts it into an equivalent program in machine code. C, C++, C#, and Java are a few examples.
  • Question 9
    5 / -1
    Which of the following is the right syntax to write try-except clause.
    Solution

    Concept: The try-except clause is used to handle the exception that can be raised during the runtime of code.

    The try block is used to contain the code which can raise the exception, and the except block contains the handler that handles the exception.

    The correct syntax for the try-except clause is:

    try:

    [ code that can raise exception]

    except [exception-name]:

    [code to handle exception]

    Example:

    try:
      print(a)
    except NameError:
      print("Variable a is undefined")

    • In the following code, the variable a is undefined, hence it raises a NameError. The try block raises the NameError and the except block act according to that error.

     

    Output:

    Variable a is undefined

    Additional Information

    • The semicolons ; can be used in the try-except clause
    • The parentheses () can not be used with the try clause.
    • The except code cannot be used inside the parentheses ().
  • Question 10
    5 / -1
    When mismatched or inappropriate values but of the right data type are passed to a built-in method or operation as an argument, it can create which kind of exception?
    Solution

    The correct answer is option 2.

    Concept:

    Exception:
     An exception is an occurrence that happens during program execution that disturbs the regular flow of the program's instructions. When a Python script finds a condition that it cannot handle, it throws an exception. A Python object that reflects an error is known as an exception.

    • In Python, a ValueError is thrown when a user passes an erroneous value to a function while passing a valid input.
    • It generally happens in mathematical processes that need a certain type of value, even if the value is the proper argument. Consider instructing Python to compute the square root of a negative number.
    • When mismatched or inappropriate values of the right data type are passed to a built-in method or operation as an argument, it can create a ValueError kind of exception.
    • The syntax to handle such exceptions is like this:

    ​try: 

    // The code in which exceptions can take place

    except ValueError:

    // Code to handle the following error

    ​Hence the correct answer is ValueError.

    Additional Information

    • IOError stands for Input/Output Error. It happens when the file, file path, or OS activity we're looking for doesn't exist.
    • EOFError is a Python exception that is thrown when methods like input() and raw input() return end-of-file (EOF) without reading any input.
    • IndexError is a sort of Python exception produced by the system when the index supplied as subscript does not fall within the range of indices of boundaries of a list.
Self Studies
User
Question Analysis
  • Correct -

  • Wrong -

  • Skipped -

My Perfomance
  • Score

    -

    out of -
  • Rank

    -

    out of -
Re-Attempt Weekly Quiz Competition
Self Studies Get latest Exam Updates
& Study Material Alerts!
No, Thanks
Self Studies
Click on Allow to receive notifications
Allow Notification
Self Studies
Self Studies Self Studies
To enable notifications follow this 2 steps:
  • First Click on Secure Icon Self Studies
  • Second click on the toggle icon
Allow Notification
Get latest Exam Updates & FREE Study Material Alerts!
Self Studies ×
Open Now