1. Home
  2. Explore
  3. Computer Science
  4. Essential Python Concepts

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Essential Python Concepts

  • 40Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Mutability

Which statement best defines an immutable Python object?

40 questions

  1. 1

    Which statement best defines an immutable Python object?

    1. A

      It can be changed only if another variable refers to it.

    2. B

      It cannot be assigned to a different variable after it is created.

    3. C

      It can be changed only inside the function that created it.

    4. D

      It cannot be changed after it is created; an attempted item assignment to it fails.

    Correct answer
    D

    It cannot be changed after it is created; an attempted item assignment to it fails.

    Explanation

    An immutable object cannot be modified after creation. For example, trying to assign to an item of a tuple raises an error.

  2. 2

    Which group contains only types identified as immutable in the content?

    1. A

      Booleans, tuples, sets, dictionaries, and bytes

    2. B

      Lists, sets, dictionaries, strings, and tuples

    3. C

      Strings, integers, floats, booleans, bytes, and tuples

    4. D

      Integers, floats, lists, bytes, and dictionaries

    Correct answer
    C

    Strings, integers, floats, booleans, bytes, and tuples

    Explanation

    The content lists strings, integers, floats, booleans, bytes, and tuples as immutable types.

  3. 3

    Which group contains only built-in mutable types identified in the content?

    1. A

      Tuples, lists, and dictionaries

    2. B

      Lists, integers, and sets

    3. C

      Lists, sets, and dictionaries

    4. D

      Strings, sets, and dictionaries

    Correct answer
    C

    Lists, sets, and dictionaries

    Explanation

    Lists, sets, and dictionaries are the built-in mutable types described in the content.

  4. 4

    A programmer assigns a tuple to variable y with y = x, then reassigns x to a different tuple. What happens to y?

    1. A

      It continues to refer to the original tuple value.

    2. B

      It raises an error because tuples cannot be assigned to variables.

    3. C

      It automatically changes to refer to the new tuple value.

    4. D

      It becomes a mutable copy of the new tuple.

    Correct answer
    A

    It continues to refer to the original tuple value.

    Explanation

    Reassigning x binds x to a different value; it does not change the value that y refers to.

  5. 5

    A programmer sets y = x when x refers to a list, then modifies an item in the list through x. Why does the change also appear through y?

    1. A

      Both variables refer to the same mutable list object.

    2. B

      Python automatically copies every list change to all variables.

    3. C

      The assignment converts the list into a tuple.

    4. D

      The list becomes immutable when assigned to a second variable.

    Correct answer
    A

    Both variables refer to the same mutable list object.

    Explanation

    When a mutable object such as a list is assigned to another variable, both names refer to the same object. Mutating that object is visible through either name.

  6. 6

    What does an attempt to assign to an item of a tuple demonstrate?

    1. A

      The tuple is immutable, so its items cannot be changed in place.

    2. B

      The tuple can be changed only after it is copied to another variable.

    3. C

      The tuple is mutable, but only its final item can be changed.

    4. D

      The tuple is mutable if its items are integers.

    Correct answer
    A

    The tuple is immutable, so its items cannot be changed in place.

    Explanation

    Item assignment to a tuple fails because tuples are immutable.

  7. 7

    A function receives a list and calls numbers.sort(). What is the likely effect on the caller's list?

    1. A

      The same list is sorted in place, so the caller sees the sorted order.

    2. B

      A sorted copy is returned, while the caller's list remains unchanged.

    3. C

      The function raises an error because parameters cannot refer to lists.

    4. D

      The list is converted to a tuple before sorting.

    Correct answer
    A

    The same list is sorted in place, so the caller sees the sorted order.

    Explanation

    The list is mutable, and sort() sorts it in place. The function parameter refers to the same list object supplied by the caller.

  8. 8

    What is a side effect in the function example involving a list?

    1. A

      The function returns a new list but leaves its input unchanged.

    2. B

      The function uses an immutable value as a parameter.

    3. C

      The function modifies an object passed to it, changing state visible outside the function.

    4. D

      The function changes a local variable without affecting any outside object.

    Correct answer
    C

    The function modifies an object passed to it, changing state visible outside the function.

    Explanation

    A side effect occurs when a function modifies a passed-in mutable object, such as sorting the caller's list in place.

  9. 9

    What is the basic purpose of a list comprehension?

    1. A

      To define a function that accepts a variable number of arguments

    2. B

      To make an existing list immutable while preserving its items

    3. C

      To run a block of code only when a module is imported

    4. D

      To create a list by specifying its values and an iteration process in a compact expression

    Correct answer
    D

    To create a list by specifying its values and an iteration process in a compact expression

    Explanation

    A list comprehension expresses the values to place in a list together with the iteration used to generate them.

  10. 10

    What values result from the list comprehension [i for i in range(10)]?

    1. A

      The integers from 1 through 10

    2. B

      The even integers from 0 through 10

    3. C

      A list containing the integer 10 ten times

    4. D

      The integers from 0 through 9

    Correct answer
    D

    The integers from 0 through 9

    Explanation

    range(10) generates integers starting at 0 and stopping before 10, so the comprehension produces 0 through 9.

  11. 11

    In a list comprehension, what does the expression before for determine?

    1. A

      The number of functions called by the comprehension

    2. B

      The value generated for each item added to the list

    3. C

      The condition that must be true before iteration begins

    4. D

      The type of iterator used by every possible comprehension

    Correct answer
    B

    The value generated for each item added to the list

    Explanation

    The expression on the left specifies the value used to populate the list for each iteration.

  12. 12

    What does a nested list comprehension produce when the outer expression creates a list for each outer iteration?

    1. A

      A single flat list containing only the last inner value

    2. B

      A list containing inner lists generated by the nested iteration

    3. C

      A dictionary whose keys are the outer iteration values

    4. D

      A tuple containing the two iteration variables

    Correct answer
    B

    A list containing inner lists generated by the nested iteration

    Explanation

    A comprehension inside another comprehension can generate a list for each outer iteration, resulting in a list of lists.

  13. 13

    What does a condition at the end of a list comprehension do in an expression such as [i for i in range(10) if i % 2 == 0]?

    1. A

      It applies the condition only after the list has been created.

    2. B

      It stops the comprehension at the first item that meets the condition.

    3. C

      It includes an item only when the condition is true.

    4. D

      It changes each item to the result of the condition.

    Correct answer
    C

    It includes an item only when the condition is true.

    Explanation

    The condition filters the generated values: an item is included only if the condition evaluates to true.

  14. 14

    What values are included by [i for i in range(10) if i % 2 == 0]?

    1. A

      All integers from 0 through 10

    2. B

      The even integers from 0 through 8

    3. C

      The even integers from 2 through 10

    4. D

      The odd integers from 1 through 9

    Correct answer
    B

    The even integers from 0 through 8

    Explanation

    The condition selects even values, and range(10) stops before 10, so the result is 0, 2, 4, 6, and 8.

  15. 15

    In a function definition, what is a parameter?

    1. A

      A variable that can be used only in a list comprehension

    2. B

      A value supplied to the function when it is called

    3. C

      A list of values returned from the function

    4. D

      A name listed in the function definition that can receive a value

    Correct answer
    D

    A name listed in the function definition that can receive a value

    Explanation

    Parameters are the names defined in the function signature; values supplied during a call are arguments.

  16. 16

    In a function call, what is an argument?

    1. A

      A name declared in the function definition but never assigned

    2. B

      A value supplied to the function, either positionally or by name

    3. C

      A variable created automatically when a function returns

    4. D

      A statement that determines whether a parameter is optional

    Correct answer
    B

    A value supplied to the function, either positionally or by name

    Explanation

    Arguments are the values passed to a function when it is called.

  17. 17

    A function is defined as def f(x, y):. What does the call f(1, 2) do?

    1. A

      Passes 1 to x and 2 to y by position

    2. B

      Passes 1 to y and 2 to x by position

    3. C

      Passes both values as keyword arguments

    4. D

      Passes one tuple argument containing 1 and 2

    Correct answer
    A

    Passes 1 to x and 2 to y by position

    Explanation

    Positional arguments are assigned to parameters in order, so 1 goes to x and 2 goes to y.

  18. 18

    For a function defined as def f(x, y):, what is the effect of calling f(y=2, x=1)?

    1. A

      It assigns values by parameter name, so x is 1 and y is 2.

    2. B

      It assigns 2 to x and 1 to y because keyword order is reversed.

    3. C

      It raises an error because keyword arguments must follow positional arguments.

    4. D

      It passes a dictionary as the only argument.

    Correct answer
    A

    It assigns values by parameter name, so x is 1 and y is 2.

    Explanation

    Keyword arguments explicitly identify the parameters they set, so their order in the call does not change those assignments.

  19. 19

    Which call follows the rule for combining positional and keyword arguments?

    1. A

      f(x=1, y=3, 2)

    2. B

      f(1, y=3)

    3. C

      f(x=1, 2)

    4. D

      f(y=3, 1)

    Correct answer
    B

    f(1, y=3)

    Explanation

    Positional arguments must come before keyword arguments in a function call. f(1, y=3) follows this rule.

  20. 20

    A function parameter is defined as z=10. What does this default value allow?

    1. A

      The function automatically returns 10 when called.

    2. B

      The caller must pass z as a keyword argument equal to 10.

    3. C

      The function may change z only if it receives a list.

    4. D

      The caller may omit z, in which case it receives the value 10.

    Correct answer
    D

    The caller may omit z, in which case it receives the value 10.

    Explanation

    A parameter with a default value is optional at the call site; if omitted, it receives the default value, here 10.

  21. 21

    What happens if a caller omits an argument for a required parameter that has no default value?

    1. A

      Python removes the parameter from the function call.

    2. B

      Python supplies None automatically in every case.

    3. C

      Python reports an error because a required argument is missing.

    4. D

      Python uses the value of the preceding parameter.

    Correct answer
    C

    Python reports an error because a required argument is missing.

    Explanation

    A parameter without a default is required. Omitting its argument causes a missing-argument error.

  22. 22

    What does *args allow a function to accept?

    1. A

      Any number of additional positional arguments

    2. B

      Exactly one list argument that must contain strings

    3. C

      Only positional arguments that have default values

    4. D

      Any number of additional keyword arguments

    Correct answer
    A

    Any number of additional positional arguments

    Explanation

    *args collects a variable number of positional arguments.

  23. 23

    What data structure contains the values collected by *args?

    1. A

      A set

    2. B

      A tuple

    3. C

      A list comprehension

    4. D

      A dictionary

    Correct answer
    B

    A tuple

    Explanation

    The positional arguments collected by *args are stored in a tuple.

  24. 24

    What does **kwargs allow a function to accept?

    1. A

      Any number of keyword arguments

    2. B

      Any number of positional arguments

    3. C

      A single dictionary that must be modified in place

    4. D

      Only keyword arguments whose values are tuples

    Correct answer
    A

    Any number of keyword arguments

    Explanation

    **kwargs collects a variable number of keyword arguments.

  25. 25

    What data structure contains keyword arguments collected by **kwargs?

    1. A

      A set containing only the argument names

    2. B

      A list containing the values in call order

    3. C

      A tuple containing alternating names and values

    4. D

      A dictionary mapping argument names to values

    Correct answer
    D

    A dictionary mapping argument names to values

    Explanation

    Keyword arguments collected by **kwargs are stored in a dictionary, accessible by their keys.

  26. 26

    In a function that has fixed parameters followed by *args, what does *args collect?

    1. A

      Additional positional arguments beyond the fixed parameters

    2. B

      The default values of all omitted parameters

    3. C

      The return values produced by the function

    4. D

      All keyword arguments supplied before the fixed parameters

    Correct answer
    A

    Additional positional arguments beyond the fixed parameters

    Explanation

    *args collects the extra positional arguments supplied after values have been assigned to the fixed parameters.

  27. 27

    What does placing * before a list in a function call do?

    1. A

      It collects the function's return values into a tuple.

    2. B

      It passes the list as one positional argument without changing it.

    3. C

      It unpacks the list into separate positional arguments.

    4. D

      It converts the list into keyword arguments.

    Correct answer
    C

    It unpacks the list into separate positional arguments.

    Explanation

    Using * to unpack a list in a call passes its elements as separate positional arguments.

  28. 28

    What does placing ** before a dictionary in a function call do?

    1. A

      It converts the dictionary values into positional arguments.

    2. B

      It collects the function's keyword arguments into a dictionary.

    3. C

      It passes the dictionary as a single positional argument.

    4. D

      It unpacks the dictionary entries as keyword arguments.

    Correct answer
    D

    It unpacks the dictionary entries as keyword arguments.

    Explanation

    Using ** with a dictionary in a call passes its keys and values as named keyword arguments.

  29. 29

    What does the condition if __name__ == "__main__": check?

    1. A

      Whether the file is being run directly as the current program

    2. B

      Whether Python is running with multiple threads

    3. C

      Whether the file has imported at least one other module

    4. D

      Whether the function containing it has received keyword arguments

    Correct answer
    A

    Whether the file is being run directly as the current program

    Explanation

    The condition is used to determine whether the current Python file is being run directly rather than merely imported.

  30. 30

    What happens to code inside the __name__ == "__main__" guard when that module is imported by another module?

    1. A

      It does not execute as the guarded direct-run code.

    2. B

      It executes only if the imported module defines a function.

    3. C

      It executes before the importing module's first statement.

    4. D

      It is converted into a function and called automatically.

    Correct answer
    A

    It does not execute as the guarded direct-run code.

    Explanation

    The guard prevents the enclosed code from running just because the module is imported.

  31. 31

    Why place a program's startup actions inside the main guard?

    1. A

      So they run when the file is launched directly but not when it is imported.

    2. B

      So they can be used as positional arguments by other modules.

    3. C

      So they run every time any function in the file is called.

    4. D

      So they run only after another module imports the file.

    Correct answer
    A

    So they run when the file is launched directly but not when it is imported.

    Explanation

    The guard lets a module provide utility functions for imports while reserving startup actions for direct execution.

  32. 32

    A file defines reusable utility functions and also contains code that starts a program. Which design matches the purpose of the main guard?

    1. A

      Put all code outside the guard so imports execute every statement immediately.

    2. B

      Put the startup code inside the guard so importing the file does not start the program.

    3. C

      Put the startup code in a list comprehension so it runs only once.

    4. D

      Put the utility functions inside the guard so imports automatically start the program.

    Correct answer
    B

    Put the startup code inside the guard so importing the file does not start the program.

    Explanation

    The main guard allows reusable definitions to be imported without triggering actions intended only for direct execution.

  33. 33

    If a print statement is placed outside the main guard in a module, what can happen when another file imports that module?

    1. A

      The print statement executes only after the importing file exits.

    2. B

      The print statement can execute during the import.

    3. C

      The print statement is automatically converted into a default parameter.

    4. D

      The print statement is skipped unless a thread acquires the GIL.

    Correct answer
    B

    The print statement can execute during the import.

    Explanation

    Python reads and executes module-level code during import, so unguarded statements can run when another file imports the module.

  34. 34

    What is the Global Interpreter Lock (GIL) described as doing in Python?

    1. A

      Assigning one CPU core permanently to every Python thread

    2. B

      Automatically converting threads into separate processes

    3. C

      Requiring a thread to acquire an interpreter lock before executing

    4. D

      Preventing Python programs from creating more than one thread

    Correct answer
    C

    Requiring a thread to acquire an interpreter lock before executing

    Explanation

    The GIL is an interpreter lock that a thread must acquire to execute, as described in the content.

  35. 35

    According to the content, what is the main execution limitation the GIL imposes on Python threads?

    1. A

      Every thread executes simultaneously, but only on a single CPU core.

    2. B

      Each thread must wait until the entire Python program has finished.

    3. C

      Only threads that use keyword arguments are allowed to execute.

    4. D

      Only one thread can execute at a time, even when multiple CPU cores are available.

    Correct answer
    D

    Only one thread can execute at a time, even when multiple CPU cores are available.

    Explanation

    The content explains that the GIL allows only one thread to execute at a time, even on a computer with multiple cores.

  36. 36

    Why does dividing a CPU-bound calculation among several Python threads not necessarily make it faster under the GIL described in the content?

    1. A

      The threads cannot execute their Python work simultaneously, so the GIL prevents parallel execution.

    2. B

      Multiple threads automatically convert the calculation into a list comprehension.

    3. C

      Python threads cannot pass results back to the main program.

    4. D

      The GIL forces every thread to repeat the entire calculation.

    Correct answer
    A

    The threads cannot execute their Python work simultaneously, so the GIL prevents parallel execution.

    Explanation

    Because only one thread executes at a time under the GIL as described, splitting CPU-bound work among threads does not provide the expected parallel speedup.

  37. 37

    What is a thread in the explanation of multithreading?

    1. A

      A special kind of immutable Python object

    2. B

      A dictionary that stores function keyword arguments

    3. C

      A component of an application that is executed by the CPU

    4. D

      A separate CPU core installed inside a computer

    Correct answer
    C

    A component of an application that is executed by the CPU

    Explanation

    The content describes a thread as a component of an application being executed by the CPU.

  38. 38

    Why can multiple CPU cores be useful in a traditional programming language for a divided calculation?

    1. A

      Every core can execute all program statements in one operation.

    2. B

      Different threads can run on different cores and compute portions at the same time.

    3. C

      Multiple cores make mutable objects immutable while they are calculated.

    4. D

      Each core removes the need to combine partial results.

    Correct answer
    B

    Different threads can run on different cores and compute portions at the same time.

    Explanation

    In the example, separate threads can process different portions on different cores, after which their partial results can be combined.

  39. 39

    Which task is an example of work a multithreaded application might handle as separate activities?

    1. A

      Changing a tuple in place while sorting a dictionary

    2. B

      Processing an image while responding to user input and sending a network request

    3. C

      Running an imported module's main-guard code automatically

    4. D

      Combining all function parameters into one positional argument

    Correct answer
    B

    Processing an image while responding to user input and sending a network request

    Explanation

    The content gives image processing, user input, and network requests as activities an application might perform at the same time.

  40. 40

    What conclusion about Python multithreading is supported by the GIL explanation in the content?

    1. A

      Using more threads always makes CPU-bound Python code proportionally faster.

    2. B

      The GIL allows all threads to execute simultaneously when their work uses lists.

    3. C

      Multiple threads cannot be created in Python if a computer has multiple CPU cores.

    4. D

      Multiple threads can exist, but the GIL limits Python execution to one thread at a time.

    Correct answer
    D

    Multiple threads can exist, but the GIL limits Python execution to one thread at a time.

    Explanation

    Python can use multiple threads, but the content emphasizes that only one can execute at a time under the GIL, limiting CPU-bound speedups.

Question 1 of 40
Question 1 of 40

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent