Correct Answer: A) Using try-except blocks for errors
Explanation: In Python, exceptions are handled using try
blocks to test code and except
blocks to handle errors that occur.
A) Defining a function only once in code
B) Reusing a variable name across modules
C) Using the same method in multiple classes
D) Creating a class without initialization
Correct Answer: C) Using the same method in multiple classes
Explanation: Polymorphism allows methods in different classes to have the same name but behave differently based on the class.
A) Using try-except blocks for errors
B) Using if-else conditions for logic
C) Using while loops for conditions
D) Using variables to catch issues
Correct Answer: A) Using try-except blocks for errors
Explanation: In Python, exceptions are handled using try
blocks to test code and except
blocks to handle errors that occur.
A) 10 as the character count
B) 11 for all characters
C) 12 for spaces included
D) Error due to syntax issue
Correct Answer: B) 11 for all characters
Explanation: The len()
function counts all characters in a string, including spaces, so the result is 11
.
A) Use range() for indices
B) Use keys() for iteration
C) Use items() method directly
D) Use dict() to access all
Correct Answer: C) Use items() method directly
Explanation: The items()
method returns key-value pairs, allowing iteration over both keys and values in a dictionary.
A) is
checks identity, ==
checks equality
B) is
compares values, ==
compares types
C) is
handles errors, ==
evaluates logic
D) is
defines conditions, ==
assigns values
Correct Answer: A) is
checks identity, ==
checks equality
Explanation: The is
operator checks whether two variables point to the same object, while ==
checks if their values are equal.
A) {key: value for key in range}
B) (x for x in sequence)
C) {x for x in sequence}
D) [x for x in sequence]
Correct Answer: D) [x for x in sequence]
Explanation: List comprehensions in Python are enclosed in square brackets and provide a concise way to create lists.
A) pass statement logic
B) def function blocks
C) try-except blocks
D) break statements
Correct Answer: C) try-except blocks
Explanation: The try-except
structure is used to handle runtime errors by catching exceptions and executing alternative code.
A) open(“file.txt”, “r”)
B) open(“file.txt”, “w”)
C) open(“file.txt”, “a”)
D) open(“file.txt”, “x”)
Correct Answer: A) open(“file.txt”, “r”)
Explanation: The r
mode opens a file for reading. If the file doesn’t exist, an error is raised.
A) file.save() for changes
B) file.close() for closure
C) file.flush() for buffer
D) file.quit() to terminate
Correct Answer: B) file.close() for closure
Explanation: The close()
method ensures the file is properly closed and resources are released.
A) Ensure code runs only if no errors
B) Execute code after the try block
C) Terminate the program execution
D) Skip remaining lines in code
Correct Answer: B) Execute code after the try block
Explanation: The finally
block runs regardless of whether an exception occurs, often used for cleanup operations.
A) return for functions
B) break in loops
C) execute for calls
D) continue in conditions
Correct Answer: C) execute for calls
Explanation: The word execute
is not a reserved keyword in Python, unlike return
, break
, and continue
.
A) Use random.randint()
B) Use math.random()
C) Use random.randint()
D) Use seed.random()
Correct Answer: C) Use random.randint()
Explanation: The random.randint()
function generates a random integer within a specified range.
A) reduce() for outputs
B) any() for conditions
C) map() for functions
D) all() for all elements
Correct Answer: D) all() for all elements
Explanation: The all()
function returns True
if all elements in a list evaluate to True
.
A) [0, 1, 4] in result
B) [1, 4, 9] output
C) Error in syntax logic
D) Null due to no operation
Correct Answer: A) [0, 1, 4] in result
Explanation: The list comprehension squares each number from 0
to 2
(range(3)
), resulting in [0, 1, 4]
.
A) Use isinstance(type)
B) Use type(variable)
C) Use len(variable)
D) Use str(variable)
Correct Answer: B) Use type(variable)
Explanation: The type()
function returns the data type of a variable, such as int
, str
, or list
.
A) list.sort() directly
B) sorted(list) function
C) range() for elements
D) map() for iteration
Correct Answer: A) list.sort() directly
Explanation: The sort()
method modifies a list in place to arrange its elements in ascending order.
A) 15.5 for numeric addition
B) Error in conversion syntax
C) 10.5 for combined values
D) Null due to type mismatch
Correct Answer: A) 15.5 for numeric addition
Explanation: The string '10'
is converted to an integer, '5.5'
to a float, and their sum is 15.5
.
A) str.match(‘char’)
B) str.contains(‘char’)
C) str.startswith(‘char’)
D) str.prefix(‘char’)
Correct Answer: C) str.startswith(‘char’)
Explanation: The startswith()
method checks if a string begins with the specified substring.
A) Create lists from sequences
B) Sort elements by order
C) Map keys to values directly
D) Iterate with index pairs
Correct Answer: D) Iterate with index pairs
Explanation: The enumerate()
function adds an index to each item in a sequence during iteration.
A) Use yield in functions
B) Use return in loops
C) Use map for iteration
D) Use break in recursion
Correct Answer: A) Use yield in functions
Explanation: The yield
keyword creates a generator, which returns values one at a time, pausing execution between each.
& stay ahead of your competitors!
* Subscribe to our email newsletter to get the latest posts delivered right to your email.