w3resource

PCEP Certification Practice: TypeError Questions & Explanations

PCEP Certification Practice Test - Questions, Answers and Explanations

Below are 25 PCEP questions focused on TypeError in Python. These questions cover various aspects of how and why TypeError is raised, its handling, and Python's exception hierarchy, following different formats such as single and multiple-select, "Place" & "Gap fill", sorting, code fill, and code insertion.

Question 1: Which of the following best describes the TypeError exception in Python?

  1. Raised when attempting to divide by zero.
  2. Raised when an operation or function is applied to an object of an inappropriate type.
  3. Raised when accessing an out-of-range index in a list.
  4. Raised when a key is not found in a dictionary.

Answer: B) Raised when an operation or function is applied to an object of an inappropriate type.

Explanation: TypeError occurs when an operation or function is applied to an object of a type that it does not support, such as adding a string and an integer.

Question 2: Which of the following operations can raise a TypeError in Python? (Select all that apply.)

  • Adding a string to an integer.
  • Concatenating two lists.
  • Calling a non-callable object.
  • Using a string as a list index.

Answer: A) Adding a string to an integer.
C) Calling a non-callable object.
D) Using a string as a list index.

Explanation: TypeError is raised in situations where the type of the object does not support the operation, such as adding a string and an integer, using a non-callable object like a string as a function, or using a string instead of an integer as a list index.

Question 3: The _________ exception is raised when an operation is applied to an object of inappropriate type.

  1. ValueError
  2. IndexError
  3. TypeError

Answer: C) TypeError

Explanation: TypeError is raised when an operation is applied to an object of a type that is not suitable for that operation.

Question 4: The __________ exception is raised when a function is called with an argument of an incorrect type.

Answer: TypeError

Explanation: TypeError is raised when a function is provided with an argument of an inappropriate type, such as passing a string where an integer is expected.

Question 5: Fill in the blank to catch the TypeError exception raised by adding a string to an integer:

try:
    result = "10" + 5
except __________:
    print("Type mismatch error occurred")

Answer: except TypeError:

Explanation: A TypeError is raised when attempting to add a string and an integer. The except TypeError block catches this exception.

Question 6: Which of the following will raise a TypeError?

  1. len([1, 2, 3])
  2. 'hello' + 'world'
  3. 5 + '10'
  4. 2 * 3

Answer: C) 5 + '10'

Explanation: Attempting to add an integer (5) and a string ('10') raises a TypeError because these types are incompatible for the addition operation.

Question 7: Insert the correct exception handling code to catch a TypeError when adding incompatible types:

try:
    result = [1, 2] + "34"
except __________:
    print("Incompatible types for addition")

Answer: except TypeError:

Explanation: Adding a list and a string will raise a TypeError because these types are incompatible. The except TypeError block catches this exception.

Question 8: Which of the following scenarios can raise a TypeError? (Select all that apply.)

  1. Using a string as a list index.
  2. Concatenating a string and a list.
  3. Dividing a float by an integer.
  4. Calling an integer as if it were a function.

Answer: A) Using a string as a list index.
B) Concatenating a string and a list.
D) Calling an integer as if it were a function.

Explanation: A TypeError is raised when a string is used as an index for a list, when trying to concatenate a list and a string, or when calling an integer as a function.

Question 9: The _________ exception is raised when attempting to call a non-callable object, such as an integer.

  • TypeError
  • KeyError
  • IndexError

Answer: A) TypeError

Explanation: Calling a non-callable object, such as an integer or string, raises a TypeError because only callable objects like functions or methods can be invoked.

Question 10: When trying to call an integer as a function, Python raises a __________ exception.

Answer: TypeError

Explanation: TypeError is raised when a non-callable object, such as an integer or string, is called as if it were a function.

Question 11: Arrange the following exceptions in the correct order, from the highest level to the lowest in the Python exception hierarchy.

  1. TypeError
  2. Exception
  3. BaseException

Answer: C) BaseException
B) Exception
A) TypeError

Explanation: BaseException is the root of Python’s exception hierarchy. Exception is a subclass of BaseException, and TypeError is a subclass of Exception.

Question 12: Which of the following are true about TypeError in Python? (Select all that apply.)

  1. It is raised when an operation is applied to an object of inappropriate type.
  2. It is a subclass of Exception.
  3. It is raised when an index is out of range in a list.
  4. It is raised when two compatible types are used together in an operation.

Answer: A) It is raised when an operation is applied to an object of inappropriate type.
B) It is a subclass of Exception.

Explanation: TypeError occurs when an operation is applied to an object of the wrong type, such as adding a string to an integer, and it is a subclass of Exception.

Question 13: The abstract exception _________ is the base class for exceptions raised when an operation or function is applied to an object of inappropriate type.

  1. TypeError
  2. ValueError
  3. LookupError

Answer: A) TypeError

Explanation: TypeError is the base class for exceptions that are raised when operations or functions are applied to objects of incorrect types.

Question 14: Adding a string and an integer will raise a __________ exception in Python.

Answer: TypeError

Explanation: Attempting to add a string and an integer raises a TypeError because these types are incompatible for this operation.

Question 15: Fill in the blank to handle a TypeError raised by attempting to multiply a list by a string:

my_list = [1, 2, 3]
try:
    result = my_list * "hello"
except __________:
    print("Incompatible types for multiplication")

Answer: except TypeError:

Explanation: Multiplying a list by a string raises a TypeError because this operation is not supported between these types.

Question 16: Which of the following will raise a TypeError?

  1. 10 + '10'
  2. len([1, 2, 3])
  3. 'hello' + 'world'
  4. 4 * 2

Answer: A) 10 + '10'

Explanation: Attempting to add an integer (10) and a string ('10') raises a TypeError because the types are incompatible for the addition operation.

Question 17: Insert the correct code to handle a TypeError when a string is used as a list index:

my_list = [10, 20, 30]
try:
    # INSERT CODE HERE
except TypeError:
    print("Cannot use string as a list index")

Answer: print(my_list["0"])

Explanation: Using a string ("0") as a list index raises a TypeError because list indices must be integers.

Question 18: Which of the following scenarios will raise a TypeError? (Select all that apply.)

  1. Using a float as a list index.
  2. Concatenating a string and an integer.
  3. Calling a string as if it were a function.
  4. Dividing two integers.

Answer: A) Using a float as a list index.
B) Concatenating a string and an integer.
C) Calling a string as if it were a function.

Explanation: Using a float as a list index, concatenating a string and an integer, and calling a string as a function all raise TypeError. Dividing two integers does not raise an exception.

Question 19: In Python, the _________ exception is raised when an operation is performed on an object of an inappropriate type.

  1. TypeError
  2. IndexError
  3. KeyError

Answer: A) TypeError

Explanation: TypeError is raised when an operation is applied to an object of an inappropriate type, such as adding a string to an integer.

Question 20: Attempting to call a string as if it were a function will raise a __________ exception.

Answer: TypeError

Explanation: A TypeError is raised when trying to call a non-callable object, such as a string, as a function.

Question 21: Sort the following exceptions in the correct order from the most general to the most specific.

  1. BaseException
  2. TypeError
  3. Exception

Answer: A) BaseException
C) Exception
B) TypeError

Explanation: BaseException is the most general, followed by Exception. TypeError is a more specific exception, which is a subclass of Exception.

Question 22: Which of the following operations can raise a TypeError in Python? (Select all that apply.)

  1. Using a list as a function.
  2. Adding a string and an integer.
  3. Multiplying an integer by a string.
  4. Accessing an out-of-range index in a list.

Answer: A) Using a list as a function.
B) Adding a string and an integer.

Explanation: Using a list as a function or adding a string and an integer will raise TypeError. Multiplying an integer by a string (e.g., 2 * 'abc') will not raise an error because Python repeats the string.

Question 23: The exception _________ is raised when you try to use a non-callable object, such as a string or integer, as if it were a function.

  1. IndexError
  2. KeyError
  3. TypeError

Answer: C) TypeError

Explanation: TypeError is raised when trying to call a non-callable object, such as a string or integer, as a function.

Question 24: Fill in the blank to catch a TypeError when trying to call a list as a function:

my_list = [1, 2, 3]
try:
    my_list()
except __________:
    print("Cannot call a list like a function")

Answer: except TypeError:

Explanation: Calling a list like a function raises a TypeError because lists are not callable objects.

Question 25: Which exception is raised when trying to add an incompatible type, such as an integer and a string?

  1. IndexError
  2. TypeError
  3. ValueError
  4. KeyError

Answer: B) TypeError

Explanation: A TypeError is raised when incompatible types, such as an integer and a string, are used in an operation that is not supported for those types.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python/certificate/functions-and-exceptions-typeerror.php