Python Formatting and Decorators MCQ Questions and Answers

1. How do you embed a variable in a string using f-strings in Python 3.6+?

a) "Hello, {name}!"
b) "Hello, %name%!"
c) f"Hello, {name}!"
d) "Hello, + name + !"

Answer:

c) f"Hello, {name}!"

Explanation:

F-strings, introduced in Python 3.6, allow embedding expressions inside string literals using curly braces {}.

2. What is the correct way to format a string using the .format() method?

a) "Hello, {0}!".format(name)
b) "Hello, %s!" % name
c) "Hello, {name}!".format(name)
d) "Hello, $name$!".format(name)

Answer:

a) "Hello, {0}!".format(name)

Explanation:

The .format() method replaces placeholders with positional or keyword arguments.

3. How do you create a decorator in Python?

a) Using the @ symbol before a function
b) By defining a class
c) Using the decorator keyword
d) By importing the decorator module

Answer:

a) Using the @ symbol before a function

Explanation:

A decorator in Python is created using the @ symbol before a function that modifies another function.

4. What is the purpose of decorators in Python?

a) To add new syntax features
b) To modify the behavior of functions or methods
c) To perform debugging
d) To define classes

Answer:

b) To modify the behavior of functions or methods

Explanation:

Decorators are used to modify or extend the behavior of functions or methods without changing their code.

5. How do you right-align text using f-strings?

a) f"{text:>width}"
b) f"{text:<width}"
c) f"{text:^width}"
d) f"{text:width}"

Answer:

a) f"{text:>width}"

Explanation:

In f-strings, ':>' is used for right alignment with the specified width.

6. Which of the following is used to create a function decorator?

a) A lambda function
b) A generator
c) A higher-order function
d) A tuple

Answer:

c) A higher-order function

Explanation:

A decorator is a higher-order function that takes a function as an argument and returns a new function.

7. How do you center-align text using the .format() method?

a) "{:^width}".format(text)
b) "{:width}".format(text)
c) "{:<width}".format(text)
d) "{:>width}".format(text)

Answer:

a) "{:^width}".format(text)

Explanation:

In the .format() method, ':^' is used to center-align text within the specified width.

8. What does the '@staticmethod' decorator do?

a) Makes a method static, not requiring an instance
b) Converts a function into a static method
c) Makes a static method dynamic
d) Decorates a static function

Answer:

a) Makes a method static, not requiring an instance

Explanation:

The '@staticmethod' decorator is used to declare a method as static, meaning it can be called without an instance of the class.

9. How do you left-align text in a string with a specific width using f-strings?

a) f"{text:<width}"
b) f"{text:>width}"
c) f"{text:^width}"
d) f"{text:width}"

Answer:

a) f"{text:<width}"

Explanation:

In f-strings, ':<' is used for left alignment with the specified width.

10. What is the result of using the '@property' decorator?

a) It turns a method into a class property
b) It makes a property private
c) It creates a new property dynamically
d) It sets a property to be read-only

Answer:

a) It turns a method into a class property

Explanation:

The '@property' decorator is used to define a method that can be accessed like an attribute.

11. How do you format a number as a percentage using f-strings?

a) f"{value:%}"
b) f"{value:.%}"
c) f"{value:.2%}"
d) f"{value}%"

Answer:

c) f"{value:.2%}"

Explanation:

In f-strings, ':.2%' formats a number as a percentage with two decimal places.

12. What is a common use of decorators in Python?

a) Memory management
b) Error handling
c) Modifying function behavior without changing its code
d) Iterating over collections

Answer:

c) Modifying function behavior without changing its code

Explanation:

Decorators are commonly used to extend or modify the behavior of functions or methods without altering their source code.

13. How do you use placeholders for positional formatting with .format()?

a) "Hello, {1}, {0}!".format(name, age)
b) "Hello, {name}, {age}!".format(name, age)
c) "Hello, %s, %d!" % (name, age)
d) "Hello, ?, ?!".format(name, age)

Answer:

a) "Hello, {1}, {0}!".format(name, age)

Explanation:

The .format() method uses curly braces with positional indexes to specify the order of arguments.

14. How do you pad a number with zeros using f-strings?

a) f"{number:0d}"
b) f"{number:0>width}"
c) f"{number:0^width}"
d) f"{number:0<width}"

Answer:

b) f"{number:0>width}"

Explanation:

In f-strings, ':0>' pads the number with zeros on the left side up to the specified width.

15. What does the '@classmethod' decorator do?

a) Makes a method a class method that can modify class state
b) Converts a method into a static method
c) Creates a new class dynamically
d) Decorates a class constructor

Answer:

a) Makes a method a class method that can modify class state

Explanation:

The '@classmethod' decorator is used to create a class method that can access and modify the class state.


Comments