python quiz Flashcards

1
Q

Which one is NOT a legal variable name?

Myvar
my_var
my-var
_myvar

A

my-var

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you create a variable with the numeric value 5?

Both the other answers are correct
x = 5
x = int(5)

A

Both the other answers are correct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a correct syntax to return the first character in a string?

x = “Hello”.sub(0, 1)
x = “Hello”[0]
x = sub(“Hello”, 0, 1)

A

x = “Hello”[0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which method can be used to remove any whitespace from both the beginning and the end of a string?

trim()
ptrim()
len()
strip()

A

strip()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which method can be used to replace parts of a string?

switch()
replace()
replaceString()
repl()

A

replace()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which collection does not allow duplicate members?

TUPLE
SET
LIST

A

SET

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you start writing a while loop in Python?

while x > y {
while (x > y)
x > y while {
while x > y:

A

while x > y:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

for x in y:
for each x in y:
for x > y:

A

for x in y:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Consider the following function definition:

def hello_name(name: str) -> str:
return f”Hello {name}”
What does the -> str part of the function definition indicate?

A

The function is expected to return a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Where are docstrings stored within a Python object?

A

In the .__doc__ property

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the three major categories of docstrings in Python?

A

Class, method, and function docstrings
Package and module docstrings
Script docstrings

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the recommended parts to include in the documentation of a private project?

A

A README and an examples.py file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What’s a good approach to structuring the docs/ folder of a public or open-source Python project?

A

Tutorials
How-To Guides
References
Explanations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How would you express the hexadecimal value a5 as a base-16 integer constant in Python?

A

0xa5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How would you express the constant floating-point value 3.2 × 10-12 in Python:

A

3.2e-12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which of the following are valid ways to specify the string literal foo’bar in Python:

A

”"”foo’bar”””
‘foo'bar’
“foo’bar”

17
Q

Which of the following is not a Python built-in function:

diff()

repr()

isinstance()

round()

map()

A

diff()

18
Q

What’s the primary purpose of decorators in Python?

A

To modify the behavior of a function

19
Q

In Python, what does it mean that functions are considered first-class objects?

A

You can pass functions around and use them as arguments.
You can treat functions like any other object.
You can assign functions to variables.

20
Q

Which of the following statements about functions in Python is true?

A

Functions in Python may have side effects.
The print() function is an example of a function with side effects.

Functions in Python may have side effects, meaning they can perform actions other than just returning a value based on the given arguments. The print() function is an example of a function with side effects, as it outputs something to the console while returning None.

21
Q

What are the characteristics of inner functions in Python?

A

-Python only defines them when you call the parent function.
-You define them inside other functions.
-They’re locally scoped to the parent function.

Inner functions are defined inside other functions and are locally scoped to the parent function. This means they only exist inside the parent function as local variables, and you can’t access them outside the parent function.

The order in which you define them doesn’t affect their execution. They only get defined when you call their parent function.

If you want to diver deeper, then you can read more about what inner functions are good for.

22
Q

def reference():
def inner():
return 42
return inner

def evaluation():
def inner():
return 42
return inner()

referenced = reference()
evaluated = evaluation()
Which statements about the the variables referenced and evaluated are true?

A

The variable evaluated will be the integer 42.

The variable referenced will be a reference to a function object.

23
Q

What’s the purpose of the @ symbol when using decorators?

A

It simplifies applying a decorator to a function.

24
Q

You’ve decorated a function that accepts an argument, but when running it, you get an error:

TypeError: wrapper() takes 0 positional arguments but 1 was given
What might be a good approach to try to fix this error?

A

Using *args and **kwargs in the inner wrapper function

To avoid this error when decorating a function that accepts arguments, use *args and **kwargs in the inner wrapper function. This allows the inner function to accept any number of positional and keyword arguments and then pass them on to the function that it decorates.

25
Q

What happens to the return value of a decorated function if the wrapper function doesn’t explicitly return a value?

A

The decorated function will return none

26
Q

What’s the purpose of using the @functools.wraps decorator in a decorator function?

A

To preserve the original function’s metadata

You’ll use the @functools.wraps decorator in a decorator function to preserve the original function’s metadata, such as its name and documentation. This helps maintain the original function’s identity after decoration.

27
Q

Is it possible to define a decorator without an inner function?

A

yes .

You can create a decorator that takes a function as an argument and only accesses one of its attributes. The code logic in your decorator might save the attribute to another data structure. Then, the decorator returns the function object unmodified.