Python Concepts and Interview Essentials Quiz Quiz

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.

  1. Recognizing Data Types

    Which of the following best describes a tuple in Python?

    1. A mutable mapping of keys to values
    2. A boolean value
    3. A string of characters
    4. An immutable sequence of elements

    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.

  2. Converting Data Types

    Given the variable s = '123', which code correctly converts it to an integer?

    1. str(s)
    2. int(s)
    3. list(s)
    4. float(s)

    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.

  3. Checking Data Type

    What is the purpose of the type() function in Python?

    1. To check the variable's data type
    2. To create a new object
    3. To compare numbers
    4. To add elements to a list

    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.

  4. Creating Empty Structures

    Which of the following statements creates an empty dictionary in Python?

    1. empty = []
    2. empty = {}
    3. empty = ()
    4. empty = ''

    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.

  5. OOPS Fundamentals

    Which OOPS principle allows one class to inherit the properties and methods of another?

    1. Composition
    2. Encapsulation
    3. Inheritance
    4. Abstraction

    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.

  6. OOPS Principles Count

    How many main Object-Oriented Programming principles are traditionally highlighted in Python?

    1. Six
    2. Three
    3. Five
    4. Four

    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.

  7. Method Overriding vs Overloading

    What happens when a subclass in Python defines a method with the same name and signature as one in its parent class?

    1. It causes an error
    2. It overloads the method
    3. It overrides the parent method
    4. It creates a new data type

    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.

  8. Class vs Instance Methods

    Which method type is defined with the @classmethod decorator and can access only class-level attributes?

    1. Instance method
    2. Class method
    3. Local method
    4. Static method

    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.

  9. String Concatenation

    If str1 = 'Hello' and str2 = 'World', which operation produces 'HelloWorld'?

    1. str1 + str2
    2. str1 / str2
    3. str1 * str2
    4. str1 - str2

    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.

  10. String Methods

    Which string method returns the uppercase version of a string in Python?

    1. replace()
    2. upper()
    3. capitalize()
    4. lower()

    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.

  11. Splitting Strings

    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.

  12. Data Type Checking Example

    Suppose you have x = 9.7. What would type(x) return?

    1. u003Cclass 'float'u003E
    2. u003Cclass 'int'u003E
    3. u003Cclass 'list'u003E
    4. u003Cclass 'str'u003E

    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.

  13. Lists vs Tuples

    After creating a variable as my_tuple = (1, 2, 3), what happens if you try to assign my_tuple[0] = 5?

    1. It appends 5 to the tuple
    2. It sorts the tuple
    3. It changes the first value to 5
    4. It raises a TypeError because tuples are immutable

    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.

  14. Creating an Empty List

    What is a correct way to initialize an empty list in Python?

    1. my_list = []
    2. my_list = ''
    3. my_list = ()
    4. my_list = {}

    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.

  15. String Length Function

    Which function do you use to get the number of characters in the string 'hello'?

    1. int('hello')
    2. len('hello')
    3. str('hello')
    4. size('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.

  16. Accessing List Elements

    Given the list items = [1, 2, 3, 4], what does items[2] return?

    1. 4
    2. 1
    3. 3
    4. 2

    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.