Test Series - python

Test Number 113/108

Q: What will be the output of the following Python code?

lst = [1, 2, 3]
lst[3]
A. NameError
B. ValueError
C. IndexError
D. TypeError
Solution: The snippet of code shown above throws an index error. This is because the index of the list given in the code, that is, 3 is out of range. The maximum index of this list is 2.
Q: What will be the output of the following Python code?

t[5]
A. IndexError
B. NameError
C. TypeError
D. ValeError
Solution: The expression shown above results in a name error. This is because the name ‘t’ is not defined.
Q: Compare the following two Python codes shown below and state the output if the input entered in each case is -6?

CODE 1
import math
num=int(input("Enter a number of whose factorial you want to find"))
print(math.factorial(num))
 
CODE 2
num=int(input("Enter a number of whose factorial you want to find"))
print(math.factorial(num))
A. ValueError, NameError
B. AttributeError, ValueError
C. NameError, TypeError
D. TypeError, ValueError
Solution: The first code results in a ValueError. This is because when we enter the input as -6, we are trying to find the factorial of a negative number, which is not possible. The second code results in a NameError. This is because we have not imported the math module. Hence the name ‘math’ is undefined.
Q: What will be the output of the following Python code?

def getMonth(m):
    if m<1 or m>12:
        raise ValueError("Invalid")
    print(m)
getMonth(6)
A. ValueError
B. Invalid
C. 6
D. ValueError(“Invalid”)
Solution: In the code shown above, since the value passed as an argument to the function is between 1 and 12 (both included), hence the output is the value itself, that is 6. If the value had been above 12 and less than 1, a ValueError would have been thrown.
Q: What will be the output of the following Python code if the input entered is 6?

valid = False
while not valid:
    try:
        n=int(input("Enter a number"))
        while n%2==0:
            print("Bye")
        valid = True
    except ValueError:
        print("Invalid")
A. Bye (printed once)
B. No output
C. Invalid (printed once)
D. Bye (printed infinite number of times)
Solution: The code shown above results in the word “Bye” being printed infinite number of times. This is because an even number has been given as input. If an odd number had been given as input, then there would have been no output.
Q:  Identify the type of error in the following Python codes?

Print(“Good Morning”)
print(“Good night)
A. Syntax, Syntax
B. Semantic, Syntax
C. Semantic, Semantic
D. Syntax, Semantic
Solution: The first code shows an error detected during execution. This might occur occasionally. The second line of code represents a syntax error. When there is deviation from the rules of a language, a syntax error is thrown.
Q: Which of the following statements is true?
A. The standard exceptions are automatically imported into Python programs
B. All raised standard exceptions must be handled in Python
C. When there is a deviation from the rules of a programming language, a semantic error is thrown
D. If any exception is thrown in try block, else block is executed
Solution: When any exception is thrown in try block, except block is executed. If exception in not thrown in try block, else block is executed. When there is a deviation from the rules of a programming language, a syntax error is thrown. The only true statement above is: The standard exceptions are automatically imported into Python programs.

You Have Score    /7