QCM : Python Data Handling and String Formatting — 10 questions

Questions et réponses du QCM

1. How do you create a Python dictionary from a list of key-value pair tuples?

Use the dict() constructor with the list of tuples as an argument
Use the create() method on the list of tuples
Use the new() function with the list of tuples as an argument
Use the list() function with the list of tuples as an argument

Use the dict() constructor with the list of tuples as an argument

Explication

The correct way to create a dictionary from a list of key-value pair tuples is to use the dict() constructor with the list as an argument, as explicitly stated in the context.

2. What is the purpose of the 'cmp()' function in Python 2 when used with dictionaries?

It retrieves all keys from a dictionary as a list.
It raises a KeyError if a specified key does not exist in the dictionary.
It compares two dictionaries and returns an integer indicating their relative order or equality.
It creates a new dictionary from a sequence of key-value pairs.

It compares two dictionaries and returns an integer indicating their relative order or equality.

Explication

The 'cmp()' function in Python 2 compares two dictionaries and returns an integer indicating whether they are equal (0), or which one is greater or lesser, based on their contents. It is specifically used for dictionary comparison in Python 2.

3. Who is credited with describing the `format()` method and format specifiers `!r` and `!s` in Python string formatting?

The Python Software Foundation, 2001
Guido van Rossum, 1991
Unspecified author, unspecified date
Unspecified author, unspecified date

Unspecified author, unspecified date

Explication

The content states that the `format()` method and format specifiers `!r` and `!s` are described with the phrase 'AUTHOR' and 'date,' indicating an unspecified author and date. Since the options should include a specific author or date, the most accurate choice reflecting the content is the placeholder 'Unspecified author, unspecified date,' which matches the style used in the content for references. The other options mention known Python authors or dates but are not explicitly linked to this particular description in the provided content.

4. What is the effect of using the 'break' statement inside a loop in Python?

It exits the current function.
It causes the loop to restart from the beginning.
It skips the current iteration and continues.
It causes the loop to terminate immediately.

It causes the loop to terminate immediately.

Explication

Using 'break' inside a loop causes the loop to terminate immediately, exiting the loop as soon as the 'break' statement is executed.

5. What is a Python module?

A file containing Python code that can be reused in other programs
A variable used to store data temporarily during program execution
A function that performs a specific task in Python
A class that defines a blueprint for objects in Python

A file containing Python code that can be reused in other programs

Explication

A Python module is a file containing Python definitions and statements, which can be imported and reused in other programs, promoting modular programming.

6. How do Python's arithmetic operators differ from relational operators?

Arithmetic operators perform mathematical calculations, while relational operators compare values and return Boolean results.
Arithmetic operators compare values and return Boolean results, whereas relational operators perform calculations and return numerical results.
Arithmetic operators are used only with integers, whereas relational operators are used only with strings.
Both arithmetic and relational operators perform mathematical calculations, but relational operators also support string concatenation.

Arithmetic operators perform mathematical calculations, while relational operators compare values and return Boolean results.

Explication

Python's arithmetic operators are used for mathematical calculations like addition and multiplication, and they return numerical results. Relational operators are used to compare two values, such as greater than or equal to, and they return Boolean results (True or False). Therefore, they differ in purpose and output type, with arithmetic operators producing numbers and relational operators producing Boolean values.

7. Who is credited with creating the concept of functions in the Python programming language?

Guido van Rossum
James Gosling
Dennis Ritchie
Bjarne Stroustrup

Guido van Rossum

Explication

Guido van Rossum, the creator of Python, is credited with designing the language's features, including the use of 'def' to define functions. The other options are creators of different programming languages or are unrelated to Python's development.

8. What is the primary role of the open() function in Python file handling?

To read data from a file
To open a file for reading or writing operations
To close an open file after processing
To delete a file from the filesystem

To open a file for reading or writing operations

Explication

The open() function in Python is used to open a file with a specified mode, such as 'r' for reading or 'w' for writing, enabling subsequent file operations. It does not close, read, or delete files directly.

9. What is a key feature of Python's string formatting with the `format()` method regarding format specifiers?

`!r` calls `repr()` on the object, providing a detailed string representation.
`!s` converts the object to uppercase during formatting.
`!r` and `!s` are used to specify string concatenation within the format string.
`!s` calls `repr()` on the object, providing a detailed string representation.

`!r` calls `repr()` on the object, providing a detailed string representation.

Explication

The `!r` format specifier in Python's `format()` method calls `repr()` on the object, providing a detailed, unambiguous string suitable for debugging. The `!s` specifier calls `str()` on the object, providing a user-friendly string. These specifiers are key features that control how objects are represented as strings during formatting.

10. When was Python, the programming language that introduced data types such as strings, lists, and dictionaries, first released to the public?

2000
1985
2010
1991

1991

Explication

Python was first released by Guido van Rossum in 1991, establishing its core data types such as strings, lists, and dictionaries. This date marks the official establishment of Python as a programming language with these fundamental data types.

Révisez avec les flashcards

Mémorisez les réponses avec 20 flashcards sur Python Data Handling and String Formatting.

Dictionary comparison — method?

Use `==` for equality; `cmp()` in Python 2.

keys() — returns?

A view object of all dictionary keys.

dict() — creates?

A dictionary from sequences of key-value pairs.

Voir les flashcards →

Approfondir avec la fiche

Consultez la fiche de révision complète sur Python Data Handling and String Formatting.

Voir la fiche →

Cours similaires

Crée tes propres QCM

Importe ton cours et l'IA génère des QCM avec corrections en 30 secondes.

Générateur de QCM