Python Basics and Interview Concepts Quiz Quiz

Challenge your understanding of essential Python concepts commonly featured in interviews. Improve your grasp on programming fundamentals and data structures with these carefully selected questions.

  1. Question 1

    In Python, what is the purpose of the __init__ method within a class?

    1. It defines global variables.
    2. It is used only for inheritance.
    3. It deletes objects from memory.
    4. It initializes the class's attributes when a new object is created.

    Explanation: The __init__ method is called automatically when a new instance of a class is made and is used to initialize that object's attributes. It does not delete objects (that is typically handled by other methods), nor is it solely used for inheritance. It also has no relation to defining global variables.

  2. Question 2

    What is the main difference between a Python list and a Python tuple?

    1. Lists cannot be iterated, but tuples can.
    2. Lists are mutable, while tuples are immutable.
    3. Lists can only store integers, while tuples can store strings.
    4. Tuples require more memory than lists.

    Explanation: Lists can have their contents changed after creation, making them mutable, whereas tuples cannot be changed (immutable). Both lists and tuples can store any data type, tuples do not always require more memory, and both types can be iterated over.

  3. Question 3

    Which keyword in Python is used when you want to exit a loop immediately?

    1. pass
    2. continue
    3. break
    4. exit

    Explanation: The 'break' keyword is used to exit a loop before it would normally finish. 'continue' skips to the next loop iteration, 'pass' does nothing and acts as a placeholder, and 'exit' is not a loop control keyword.

  4. Question 4

    In Python, what is a docstring?

    1. A syntax error in string formatting.
    2. A string used to document a module, function, class, or method.
    3. A type of comment ignored by the interpreter.
    4. A string used to name a variable.

    Explanation: Docstrings are special strings at the beginning of modules, functions, classes, or methods that explain their purpose. They are not variable names, they are not ignored comments (they can be accessed), and they are not related to string formatting errors.

  5. Question 5

    How can you make a Python script executable on Unix systems?

    1. Compile the script to bytecode first.
    2. Add a shebang line at the top and give the file execute permissions.
    3. Write the script in uppercase letters.
    4. Only save the file with a .py extension.

    Explanation: To make a Python script executable on Unix, you must add a shebang line (e.g., #!/usr/bin/env python3) and set execute permissions. The .py extension alone and uppercase text do nothing for executability, and compilation to bytecode is not required for execution as a script.