Test your knowledge of key Python interview topics including data types, object-oriented programming, string manipulations, and foundational Python concepts. This quiz helps you assess your Python skills with medium-difficulty questions tailored for technical interviews.
Which of the following best describes a tuple in Python?
Explanation: A tuple is an ordered, immutable sequence of elements, meaning its contents cannot change after creation. A mutable mapping of keys to values describes a dictionary, not a tuple. A string is a sequence of characters, and boolean values can only be True or False.
Given the variable s = '123', which code correctly converts it to an integer?
Explanation: 'int(s)' converts the string s to an integer. Using 'str(s)' turns it into a string (which it already is), 'float(s)' converts it to a floating point number, and 'list(s)' breaks the string into a list of characters, not a number.
What is the purpose of the type() function in Python?
Explanation: The type() function returns the type of the object it is given. It does not create objects, compare numbers, or add elements to a list—those purposes belong to other functions such as object creation, comparison operators, or the list's append() method.
Which of the following statements creates an empty dictionary in Python?
Explanation: 'empty = {}' creates an empty dictionary. '[]' creates an empty list, '()' creates an empty tuple, and '' sets up an empty string. Only curly braces without key-value pairs denote an empty dictionary.
Which OOPS principle allows one class to inherit the properties and methods of another?
Explanation: Inheritance refers to a class inheriting features from a parent class. Encapsulation means bundling data and functions, abstraction hides unnecessary details, and composition means building complex objects out of simpler ones. Only inheritance fits the description.
How many main Object-Oriented Programming principles are traditionally highlighted in Python?
Explanation: The main four OOPS principles are encapsulation, inheritance, polymorphism, and abstraction. Three is incorrect as it omits one, and five or six add more than the traditional core principles.
What happens when a subclass in Python defines a method with the same name and signature as one in its parent class?
Explanation: When a subclass defines a method matching its parent in name and signature, it overrides the parent version. This does not cause an error, create a new data type, or overload the method, as Python treats this specifically as overriding.
Which method type is defined with the @classmethod decorator and can access only class-level attributes?
Explanation: @classmethod is used to define a class method, which can access class-level attributes but not instance-specific attributes. Instance methods use self and can access instance data, static methods have no access to either by default, and local method is not a recognized term.
If str1 = 'Hello' and str2 = 'World', which operation produces 'HelloWorld'?
Explanation: The plus (+) operator concatenates strings in Python, producing 'HelloWorld' in this case. The multiplication and division operators are not valid for string to string, and subtraction is not defined for strings.
Which string method returns the uppercase version of a string in Python?
Explanation: upper() returns the entire string in uppercase letters. capitalize() turns only the first character uppercase, replace() substitutes substrings, and lower() returns the lowercase version.
What is the result of 'apple,banana,cherry'.split(',') in Python?
Explanation: The split(',') method breaks the string into a list of its pieces, splitting wherever a comma is found. Leaving the string unchanged or adding spaces is not what split does, and not providing a delimiter splits by spaces by default.
Suppose you have x = 9.7. What would type(x) return?
Explanation: Since 9.7 is a floating point value, type(x) yields u003Cclass 'float'u003E. Integers, strings, or lists are all incorrect for this variable's type.
After creating a variable as my_tuple = (1, 2, 3), what happens if you try to assign my_tuple[0] = 5?
Explanation: Tuples can't be changed after creation, so trying to assign a value by index causes an error. Changing the value, appending, or sorting are not permitted on immutable tuples in this way.
What is a correct way to initialize an empty list in Python?
Explanation: An empty list is denoted by square brackets. Curly braces initiate an empty dictionary, parentheses create an empty tuple, and two single quotes result in an empty string.
Which function do you use to get the number of characters in the string 'hello'?
Explanation: len() returns the length of a string. str() tries to make a string, int() would cause an error unless it's a digit string, and size() is not a built-in Python function.
Given the list items = [1, 2, 3, 4], what does items[2] return?
Explanation: Python lists are zero-indexed, so items[2] accesses the third element, which is 3. Choosing 2, 4, or 1 would correspond to different indices.