Quiz: Python Strings and Lists — 44 questions

Detailed questions and answers

1. Which kind of data does a string represent in Python?

Textual data such as a name or message
Collection data containing key-value pairs
Boolean data representing true or false
Numeric data used in arithmetic calculations

Textual data such as a name or message

Explanation

A string is a sequence of characters used to represent textual data, including names, locations, and messages. Numeric data is represented by numbers rather than strings.

2. Which expression is a string literal written directly in source code?

input()
"MARY"
len(MARY)
str(MARY)

"MARY"

Explanation

A string literal is text enclosed in single or double quotation marks in the source code. The input() function obtains a value during program execution rather than defining quoted text directly.

3. If word is the string "PYTHON", which character does word[1] access?

N
Y
P
T

Y

Explanation

String indexing starts at 0, so index 0 contains P and index 1 contains Y. The character T is at index 2, while N is at index 5.

4. What value does len("coding") return?

6
5
7
0

6

Explanation

The len() function returns the number of characters in the string, and "coding" contains six characters. It does not count indexes, which would run from 0 through 5.

5. What does string immutability mean in Python?

A string cannot be stored in a variable
A string cannot be combined with another string
A string cannot be accessed through indexing
A string cannot have a character changed in place

A string cannot have a character changed in place

Explanation

String immutability means that an existing string object cannot have one of its characters changed in place. Strings can still be indexed, combined, and assigned to variables.

6. What happens when Python evaluates name[2] = "B" for a string variable named name?

It creates an empty replacement string
It appends B to the string
It raises a TypeError
It changes the character at index 2

It raises a TypeError

Explanation

Indexed assignment attempts to modify a character in place, which strings do not support, so Python raises a TypeError. Indexing can read a character, but it cannot assign a new one.

7. How can a program replace the value of a string variable after a character change is needed?

Assign a character through the string index
Call len() on the original string
Assign a new string object to the variable
Convert the string into an index expression

Assign a new string object to the variable

Explanation

Because strings cannot be modified in place, replacement is done by assigning a newly constructed string to the variable. Indexed assignment is invalid for strings.

8. What string results from evaluating "New" + "York"?

"YorkNew"
"New York"
"NewYork"
"New+York"

"NewYork"

Explanation

The + operator concatenates the two strings in their given order, producing "NewYork". It does not insert a space or reverse the operands.

9. Which feature distinguishes an f-string from an ordinary quoted string?

It prevents variables from being used in expressions
It converts every character into a numeric value
It evaluates expressions placed inside curly braces
It treats all curly-brace contents as literal text

It evaluates expressions placed inside curly braces

Explanation

An f-string begins with f and evaluates expressions inside its curly-brace replacement fields. An ordinary quoted string treats such contents as text rather than evaluating them.

10. If num_items is 3 and cost_taco is 1.25, what does f"{num_items} tacos cost {cost_taco * num_items}" produce?

"3 tacos cost 1.25"
"3 tacos cost 3.75"
"1.25 tacos cost 3"
"num_items tacos cost cost_taco * num_items"

"3 tacos cost 3.75"

Explanation

The f-string evaluates both replacement fields: num_items becomes 3 and cost_taco * num_items becomes 3.75. Ordinary quoted text would display the expression names instead of calculating the product.

11. What output does the format specification in f"{4:.2f}" produce?

"4.2"
"4"
"4.02"
"4.00"

"4.00"

Explanation

The colon introduces a format specification, and .2f represents the value as fixed-point notation with two digits after the decimal point. Thus the value 4 is displayed as 4.00.

12. Which statement correctly describes a Python list?

It is a fixed-size sequence written with parentheses.
It is an immutable, ordered sequence written with quotation marks.
It is a mutable, ordered collection written with square brackets.
It is an unordered collection that requires curly braces.

It is a mutable, ordered collection written with square brackets.

Explanation

A list is an ordered collection whose elements can be changed, and it is created with square brackets. A string is also ordered but is immutable, so it cannot be edited in place.

13. What index refers to the first element of a nonempty Python list?

The list length
1
0
-1

0

Explanation

Python list indexing begins at 0, so the first element has index 0. Index 1 refers to the second element, while the list length is not an element index.

14. Which expression uses a valid index to access the first element of my_list?

my_list[0.0]
my_list[0]
my_list[1.5]
my_list["0"]

my_list[0]

Explanation

List indices must be integers, so 0 is valid for the first element. Floating-point and string representations of zero are different types and cause an indexing error.

15. How does bracket notation access an element at a particular position in my_list?

my_list[i]
my_list(i)
my_list.value(i)
my_list{i}

my_list[i]

Explanation

Bracket notation places the integer index inside square brackets, as in my_list[i]. Parentheses, dot notation, and braces do not perform standard list indexing.

16. What is the result of executing prices[0] = 2.50 when prices is a list?

A new list is created while prices remains unchanged.
The first element is removed and the list becomes shorter.
A TypeError occurs because list elements cannot be assigned.
The element at index 0 is replaced with 2.50.

The element at index 0 is replaced with 2.50.

Explanation

Lists are mutable, so indexed assignment replaces the value at the specified position. The restriction on indexed assignment applies to immutable strings, not lists.

17. Which pairing correctly describes pop() and remove() for lists?

pop() counts matches, while remove() returns the list length.
pop() removes by value, while remove() adds a value.
pop() removes by index, while remove() removes the first matching value.
pop() adds a value, while remove() removes by index.

pop() removes by index, while remove() removes the first matching value.

Explanation

pop() selects an element using its index, whereas remove() searches for and deletes the first matching value. The other pairings assign insertion, counting, or length operations to the wrong methods.

18. Which expression returns the number of occurrences of value in my_list?

my_list.count(value)
sum(my_list)
my_list.index(value)
len(my_list)

my_list.count(value)

Explanation

The count() method reports how many times a specified value occurs in a list. index() returns the first matching position, while len() counts all elements and sum() adds numeric values.

19. What requirement applies when using min() or max() on a list?

The list must contain exactly two elements.
All elements must be strings.
All elements must have the same type.
Every element must have a different value.

All elements must have the same type.

Explanation

The min() and max() functions require comparable elements of the same type. A list may contain more than two elements, and its elements need not be strings or distinct.

20. Which characteristic distinguishes a tuple from a list?

A tuple is immutable after it is created.
A tuple automatically sorts its elements.
A tuple permits indexed replacement of its elements.
A tuple requires every element to have the same type.

A tuple is immutable after it is created.

Explanation

A tuple is an immutable sequence, so its stored elements cannot be changed after creation. Lists, in contrast, are mutable and support indexed updates.

21. How are fields in a named tuple typically accessed?

By using names with dot notation.
By searching values with the remove() method.
By modifying fields through indexed assignment.
By referring to fields through dictionary keys only.

By using names with dot notation.

Explanation

Named tuples provide immutable fields that can be accessed through descriptive names and dot notation. They do not support changing fields by index, and their named access is distinct from list removal or dictionary-key access.

22. Which characteristic distinguishes a Python set from a list?

A set stores repeated elements in a fixed positional order.
A set associates each element with a separate lookup key.
A set stores unique elements without meaningful positional indexes.
A set preserves insertion positions for numeric access.

A set stores unique elements without meaningful positional indexes.

Explanation

A set is mutable, unordered, and contains unique elements, so positional indexing has no meaningful role. The ordered positional behavior described by the other choices belongs more closely to lists.

23. What is the result of evaluating set([2,2,4,5,5])set([2, 2, 4, 5, 5])?

A set containing 22, 44, and 55
A list containing 22, 44, and 55
A set containing five elements in list order
A set containing each duplicate as a separate element

A set containing $$2$$, $$4$$, and $$5$$

Explanation

Converting a list to a set removes duplicate values, leaving the unique elements 22, 44, and 55. The operation produces a set rather than preserving the original list structure or repeated entries.

24. Which operation adds every element from another collection to an existing set?

Using remove()remove() with the other collection
Using update()update() with the other collection
Using pop()pop() with the other collection
Using add()add() with the other collection

Using $$update()$$ with the other collection

Explanation

The update()update() method adds elements from another collection to a set. The add()add() method is intended for one value, while remove()remove() deletes a specified value and pop()pop() removes a random element.

25. Given sets A={1,2,3}A = \{1,2,3\} and B={3,4,5}B = \{3,4,5\}, which operation produces {3}\{3\}?

The symmetric difference of AA and BB
The difference of AA and BB
The union of AA and BB
The intersection of AA and BB

The intersection of $$A$$ and $$B$$

Explanation

The intersection contains elements shared by both sets, so the result is {3}\{3\}. The union would contain all five unique values, while the two difference operations would exclude the shared value.

26. How does a Python dictionary differ from a list when retrieving stored data?

A dictionary retrieves values through keys, whereas a list uses numeric indexes.
A dictionary retrieves values randomly, whereas a list uses key-value pairs.
A dictionary retrieves values by duplicate entries, whereas a list uses mappings.
A dictionary retrieves values through positions, whereas a list uses unique keys.

A dictionary retrieves values through keys, whereas a list uses numeric indexes.

Explanation

A dictionary is a mutable mapping that associates keys with values, so its entries are accessed through keys. Lists are ordered collections whose elements are accessed by numeric indexes.

27. What happens when code accesses a dictionary with a key that is not present?

Python raises a KeyError.
Python returns an empty dictionary.
Python returns the nearest existing value.
Python creates the missing key automatically.

Python raises a KeyError.

Explanation

Bracket access with a nonexistent dictionary key raises a KeyError. It does not create a new entry or substitute an empty or nearby value.

28. What does assigning a value to an existing dictionary key do?

It replaces the value associated with that key.
It adds a second entry with the same key.
It removes the key and its previous value.
It converts the key into a list index.

It replaces the value associated with that key.

Explanation

Dictionary assignment updates an existing key by replacing its associated value. A key does not receive a duplicate entry, and assignment does not remove the key or change it into an index.

29. Which Python type is best characterized as an unordered collection of unique data?

A string
A tuple
A set
A list

A set

Explanation

A set represents unordered data in which elements are unique. Strings represent text, while lists and tuples are ordered sequences with different mutability properties.

30. A program must store an ordered collection that can be edited after creation. Which type is appropriate?

A dictionary
A list
A set
A tuple

A list

Explanation

Lists are ordered and mutable, making them suitable for collections that may be edited. Tuples are ordered but immutable, while sets and dictionaries serve different organizational purposes.

31. Which data type is most appropriate for storing relationships between identifiers and their associated information?

A tuple
A dictionary
A list
A string

A dictionary

Explanation

A dictionary is designed to represent relationships between keys and values, such as identifiers and associated information. Lists and tuples are ordered sequences, whereas a string represents text.

32. What distinguishes implicit type conversion from explicit type conversion?

Implicit conversion changes numbers to strings, while explicit conversion changes strings to numbers
Implicit conversion applies to input, while explicit conversion applies to arithmetic
Implicit conversion uses a function, while explicit conversion occurs automatically
Implicit conversion occurs automatically, while explicit conversion uses a conversion function

Implicit conversion occurs automatically, while explicit conversion uses a conversion function

Explanation

Implicit conversion is performed automatically by the language, whereas explicit conversion requires the programmer to call a conversion function. The option reversing these roles confuses the two conversion methods.

33. What type of result does the expression 1+2.01 + 2.0 produce?

An integer value
A floating-point value
A Boolean value
A string value

A floating-point value

Explanation

An arithmetic operation produces a float when either operand is a float, so 1+2.01 + 2.0 produces a floating-point result. The integer rule applies when both operands are integers, as in 1+21 + 2.

34. What value does int(4.9)\text{int}(4.9) produce?

4.9
5
4
0

4

Explanation

Converting a float to an integer with int()\text{int}() truncates the fractional part, yielding 4. Rounding to the nearest whole number would produce 5, but truncation does not round upward.

35. A program receives a number through input()\text{input}() and needs to use it in arithmetic; what should happen first?

Multiply the returned string by a decimal value
Use the returned string as an integer automatically
Convert the returned string to a numeric type
Convert the returned number to a string type

Convert the returned string to a numeric type

Explanation

The input()\text{input}() function initially returns a string, so the program must convert it with a function such as int()\text{int}() or float()\text{float}() before numeric use. Arithmetic does not automatically make the input suitable as a numeric operand.

36. Which description correctly defines a binary number?

A base-10 value using the digits 0 through 9
A base-8 value using the digits 0 through 7
A base-16 value using digits 0 through 9 and letters
A base-2 value using the digits 0 and 1

A base-2 value using the digits 0 and 1

Explanation

Binary representation uses base 2 and therefore has two possible digits, 0 and 1. Decimal uses base 10, while octal and hexadecimal use different bases and digit sets.

37. What determines the place value of a digit in a binary number?

An increasing power of 10
A fixed value shared by every position
An increasing power of 2
A decreasing power of 2

An increasing power of 2

Explanation

Each binary position is weighted by an increasing power of 2, such as 20,21,222^0, 2^1, 2^2, and so on. Powers of 10 describe decimal place values rather than binary ones.

38. What is the largest integer representable by an 8-bit unsigned binary number?

255
128
511
256

255

Explanation

An 8-bit unsigned value ranges from 0 through 255, so 255 is the largest representable integer. The value 256 requires a ninth position because the maximum is 28−12^8 - 1.

39. How can the binary number 101121011_2 be converted to decimal?

Compute 1×23+0×22+1×21+1×201\times2^3 + 0\times2^2 + 1\times2^1 + 1\times2^0
Add the binary digits and multiply the sum by 2
Read the four binary digits as the decimal number 1011
Compute 1×103+0×102+1×101+1×1001\times10^3 + 0\times10^2 + 1\times10^1 + 1\times10^0

Compute $$1\times2^3 + 0\times2^2 + 1\times2^1 + 1\times2^0$$

Explanation

Binary-to-decimal conversion multiplies each digit by its corresponding power of 2 and adds the results, giving 8+0+2+1=118 + 0 + 2 + 1 = 11. Using powers of 10 would treat the number as decimal rather than binary.

40. Using the program's estimate of 365 days per year, how many days are estimated for a person aged 3 years?

1,095 days
1,080 days
365 days
3,650 days

1,095 days

Explanation

The estimate uses age in days=age in years×365\text{age in days} = \text{age in years} \times 365, so 3 years gives 3×365=1,0953\times365=1{,}095 days. This estimate does not add extra days for leap years.

41. Which heart-rate assumption is used when estimating a person's lifetime heartbeats in the health-data practice?

An average rate of 100 beats per minute
An average rate of 60 beats per minute
An average rate of 72 beats per minute
An average rate researched separately for each person

An average rate of 72 beats per minute

Explanation

The practice estimates lifetime heartbeats using an average rate of 72 beats per minute. Other health statistics, such as sneezes or calories, involve separate estimates or additional research rather than this specific heart-rate assumption.

42. What does the expression `my_list + your_list` produce when both variables are lists?

A new list containing your_list's elements followed by my_list's elements
A new list containing my_list's elements followed by your_list's elements
A single nested list containing my_list and your_list as two elements
An existing list with one additional item appended at its end

A new list containing my_list's elements followed by your_list's elements

Explanation

The `+` operator concatenates the two lists in order, placing the elements of `my_list` before those of `your_list`. Appending differs because it adds one item to an existing list rather than combining two lists element by element.

43. In a list program, which operation changes `my_flower2` into `their_flower` while preserving the list position?

Concatenating a one-item list containing `their_flower`
Replacing the existing element at the position of `my_flower2`
Removing the first `their_flower` and then adding it at the front
Appending `their_flower` after the list's current final element

Replacing the existing element at the position of `my_flower2`

Explanation

Replacing assigns a new value to an existing position, so `my_flower2` becomes `their_flower` in place. Appending would add another item at the end and would not alter the element currently stored at that position.

44. A set already contains `your_fruit1`, and the program adds `your_fruit1` again before removing `my_fruit1`. What effect does the repeated addition have?

It creates a second copy of the fruit in the set
It moves the fruit to a new list-style position
It leaves the set's contents unchanged before the removal
It replaces another fruit with the repeated fruit

It leaves the set's contents unchanged before the removal

Explanation

Adding an element that is already present does not create a duplicate, so the set remains unchanged by that operation. The later removal of `my_fruit1` is a separate operation and does not make the repeated addition produce another copy.

Review with flashcards

Memorize the answers with 89 flashcards on Python Strings and Lists.

What is a string in programming?

A sequence of characters representing textual data.

How is a string literal defined in source code?

By surrounding text with single or double quotes.

What index does the first character of a string have?

Index 0.

See flashcards →

Read the study sheet

Read the complete study sheet on Python Strings and Lists.

See study sheet →

Similar courses

Create your own quizzes

Import your course and AI generates quizzes with corrections in 30 seconds.

Quiz generator