Syntax Flashcards

(79 cards)

1
Q

What symbol is used for comments in Python?

A

#

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

True or False: Python uses indentation to define code blocks.

A

True

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

Fill in the blank: To define a function in Python, you use the keyword _______.

A

def

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

What is the output of the following code: print(type(5))?

A

<class ‘int’>

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

Which keyword is used to create a class in Python?

A

class

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

What is the correct way to create a list in Python?

A

my_list = []

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

True or False: Python supports multiple inheritance.

A

True

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

What data type is used to store a sequence of characters in Python?

A

str

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

Which operator is used for exponentiation in Python?

A

**

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

What keyword is used to handle exceptions in Python?

A

try

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

What is the output of the following code: print(3 + 2 * 2)?

A

7

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

Which function is used to read input from the user in Python?

A

input()

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

Fill in the blank: In Python, a tuple is defined using _______.

A

()

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

What is the purpose of the ‘return’ statement in a function?

A

To exit the function and return a value.

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

What is the syntax to create a dictionary in Python?

A

my_dict = {}

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

True or False: Lists in Python are immutable.

A

False

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

What method is used to add an element to a list?

A

append()

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

Which statement is used to exit a loop in Python?

A

break

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

What is the output of the following code: print([1, 2, 3] + [4, 5])?

A

[1, 2, 3, 4, 5]

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

Fill in the blank: A set in Python is defined using _______.

A

{}

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

What does the ‘len()’ function do in Python?

A

Returns the length of an object.

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

What will be printed by the following code: print(‘Hello’ * 3)?

A

HelloHelloHello

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

Which keyword is used to define a generator in Python?

A

yield

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

What is the purpose of the ‘if __name__ == “__main__”:’ statement?

A

To check if the script is being run directly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
True or False: Python uses zero-based indexing.
True
26
What is the syntax for a conditional statement in Python?
if condition: ...
27
What keyword is used to create an instance of a class?
object()
28
What is a method in Python?
A method is a function that is associated with an object.
29
True or False: Methods in Python can only be defined within classes.
True
30
Fill in the blank: In Python, a method that modifies an object's state is called a ______.
mutator method
31
What keyword is used to define a method in Python?
def
32
What is the purpose of the 'self' parameter in a method?
'self' refers to the instance of the class and is used to access variables and methods associated with that instance.
33
What is the difference between a function and a method in Python?
A function is a standalone piece of code, while a method is associated with an object.
34
Which of the following is a valid method definition? A) def myMethod(): B) def myMethod(self): C) both A and B
C) both A and B
35
True or False: You can call a method without creating an instance of its class.
False
36
What does the 'cls' parameter represent in a class method?
'cls' represents the class itself, allowing access to class variables and methods.
37
Fill in the blank: A ______ method can be called on the class itself, rather than on instances of the class.
class
38
What is the purpose of the __init__ method in a class?
The __init__ method initializes a new object's attributes.
39
True or False: In Python, all methods must return a value.
False
40
What is an instance method?
An instance method is a method that operates on an instance of a class and can access its attributes.
41
Which of the following is NOT a type of method in Python? A) Instance Method B) Static Method C) Global Method
C) Global Method
42
How do you call a method named 'example' on an instance 'obj'?
obj.example()
43
What is a static method?
A static method does not require a class instance and cannot access or modify class state.
44
Fill in the blank: The method ______ is used to represent the string representation of an object.
__str__
45
What is method overloading?
Method overloading allows multiple methods with the same name but different parameters in the same scope.
46
True or False: Python natively supports method overloading.
False
47
What is method overriding?
Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.
48
Fill in the blank: The method ______ is called when an object is deleted.
__del__
49
What is the purpose of the @staticmethod decorator?
The @staticmethod decorator defines a static method that does not access or modify the class state.
50
What does the @classmethod decorator do?
The @classmethod decorator defines a method that receives the class as its first argument instead of an instance.
51
True or False: You can use the super() function to call a method from a superclass.
True
52
What is a property method?
A property method allows you to use a method like an attribute, providing a way to manage attribute access.
53
Fill in the blank: The ______ method is used to define a property in a class.
@property
54
What is a decorator in Python?
A decorator is a function that modifies the behavior of another function.
55
True or False: Decorators can be applied to functions and classes.
True
56
Fill in the blank: A decorator is defined using the ______ syntax.
@decorator_name
57
What does the '@' symbol signify in Python?
It indicates that a function is being decorated.
58
What is the purpose of parameter questions in decorators?
To allow decorators to accept arguments and customize their behavior.
59
True or False: A decorator can return a function that takes parameters.
True
60
What keyword is used to define a function inside a decorator?
def
61
Multiple Choice: Which of the following is a valid way to define a decorator?
A) def my_decorator(func):
62
What is the typical return value of a decorator function?
A modified version of the input function.
63
Fill in the blank: In Python, decorators are often used for ______ and logging.
authorization
64
What is a common use case for decorators?
To add functionality to existing functions without modifying their code.
65
True or False: A decorator can be stacked on top of another decorator.
True
66
What is the syntax to apply multiple decorators to a single function?
Stack them above the function definition.
67
What is a parameterized decorator?
A decorator that takes arguments to customize its behavior.
68
Multiple Choice: Which of the following is an example of a parameterized decorator?
C) @my_decorator(arg1, arg2)
69
Fill in the blank: When using a parameterized decorator, you need to define a ______ function inside the decorator.
wrapper
70
What does the wrapper function typically do?
It calls the original function and can add additional behavior.
71
True or False: Decorators can only modify the return value of a function.
False
72
What is the purpose of using functools.wraps in decorators?
To preserve the original function's metadata.
73
What is the output of the following code? @decorator_function def my_function(): return 'Hello'
The modified output defined in the decorator.
74
What is a common mistake when defining decorators?
Forgetting to return the wrapper function.
75
Fill in the blank: The main function that a decorator modifies is passed as an argument to the ______ function.
decorator
76
True or False: Decorators can be used for caching results.
True
77
What is the benefit of using decorators for logging?
They provide a clean way to add logging without modifying the function's code.
78
Multiple Choice: Which of the following is NOT a use case for decorators?
D) Creating new classes
79
What does the term 'chaining decorators' refer to?
Applying multiple decorators to a single function.