-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCEP-30-02 4.2 section, covering pa.txt
227 lines (192 loc) · 6.13 KB
/
PCEP-30-02 4.2 section, covering pa.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
PCEP-30-02 4.2 section, covering parameters vs. arguments, positional, keyword, and mixed argument passing, default parameter values, name scopes, name hiding (shadowing), and the global keyword:
1. What is the difference between parameters and arguments in Python functions?
A) Parameters are defined in the function, arguments are passed when calling the function
B) Arguments are defined in the function, parameters are passed when calling the function
C) They are the same thing
D) Parameters are for built-in functions, arguments are for user-defined functions
2. Which of the following is an example of positional argument passing?
A) func(x=5, y=10)
B) func(5, 10)
C) func(*args)
D) func(**kwargs)
3. What is the output of this code?
```python
def greet(name, greeting="Hello"):
print(f"{greeting}, {name}!")
greet("Alice")
```
A) Hello, Alice!
B) Alice, Hello!
C) Error
D) None
4. Which of the following is an example of keyword argument passing?
A) func(5, 10)
B) func(x=5, y=10)
C) func(5, y=10)
D) func(*[5, 10])
5. What is the output of this code?
```python
def func(a, b, c=3):
print(a, b, c)
func(1, c=5, b=2)
```
A) 1 2 5
B) 1 5 2
C) Error
D) 1 2 3
6. Which of the following allows a function to accept any number of keyword arguments?
A) def func(**kwargs):
B) def func(*args):
C) def func(args):
D) def func(...):
7. What is the purpose of default parameter values?
A) To make parameters optional
B) To increase function performance
C) To prevent errors
D) To override global variables
8. What is the output of this code?
```python
x = 10
def func():
x = 20
print(x)
func()
print(x)
```
A) 20, 20
B) 20, 10
C) 10, 10
D) Error
9. Which of the following is an example of mixed argument passing?
A) func(1, 2, 3)
B) func(a=1, b=2, c=3)
C) func(1, b=2, c=3)
D) func(*args)
10. What happens if you provide fewer arguments than parameters in a function call?
A) The function uses default values for missing arguments if available
B) The function always raises a TypeError
C) The function ignores the missing arguments
D) The function returns None
11. What is name shadowing in Python?
A) When a local variable has the same name as a global variable
B) When a function has the same name as a variable
C) When two functions have the same name
D) When a variable is not defined
12. Which keyword is used to modify a global variable from within a function?
A) global
B) nonlocal
C) extern
D) public
13. What is the output of this code?
```python
def func(a, b, *, c):
print(a, b, c)
func(1, 2, c=3)
```
A) 1 2 3
B) Error
C) 1 2
D) None
14. What does the * operator do when used in a function definition?
A) It multiplies the parameters
B) It allows the function to accept any number of positional arguments
C) It makes all parameters optional
D) It raises the parameters to a power
15. What is the output of this code?
```python
x = 10
def func():
global x
x = 20
print(x)
func()
print(x)
```
A) 20, 20
B) 20, 10
C) 10, 10
D) Error
16. Which of the following is true about default parameter values?
A) They must be immutable objects
B) They are evaluated every time the function is called
C) They can be any Python expression
D) They can only be used with keyword arguments
17. What is the output of this code?
```python
def func(a, b=2, *args, **kwargs):
print(a, b, args, kwargs)
func(1, 3, 4, 5, x=6, y=7)
```
A) 1 3 (4, 5) {'x': 6, 'y': 7}
B) 1 2 (3, 4, 5) {'x': 6, 'y': 7}
C) Error
D) 1 3 4 5 {'x': 6, 'y': 7}
18. What happens if you try to modify a mutable default argument in a function?
A) It raises an error
B) The change is reflected in subsequent function calls
C) The change is only local to that function call
D) It creates a new copy of the argument
19. What is the purpose of the nonlocal keyword?
A) To access global variables
B) To create a new global variable
C) To modify a variable in the nearest enclosing scope
D) To prevent variable shadowing
20. What is the output of this code?
```python
def outer():
x = "local"
def inner():
nonlocal x
x = "nonlocal"
inner()
print(x)
outer()
```
A) local
B) nonlocal
C) Error
D) None
21. Which of the following is true about positional-only parameters?
A) They must be the first parameters in a function definition
B) They can be mixed with keyword parameters
C) They are indicated by / in the function definition
D) They cannot have default values
22. What is the output of this code?
```python
def func(a, b, **kwargs):
print(a, b, kwargs.get('c', 3))
func(1, 2)
```
A) 1 2 3
B) 1 2 None
C) Error
D) 1 2
23. What happens if you provide more positional arguments than parameters in a function call?
A) The extra arguments are ignored
B) The function raises a TypeError
C) The extra arguments are packed into a tuple if *args is used
D) The function returns None
24. What is the purpose of the * in a function call?
A) To unpack a list or tuple into separate arguments
B) To indicate variable-length arguments
C) To multiply arguments
D) To make arguments optional
25. What is the output of this code?
```python
x = 10
def func():
x = 20
def inner():
global x
x = 30
inner()
print(x)
func()
print(x)
```
A) 20, 30
B) 20, 10
C) 30, 30
D) 20, 20
Answers:
1. A, 2. B, 3. A, 4. B, 5. A, 6. A, 7. A, 8. B, 9. C, 10. A, 11. A, 12. A, 13. A, 14. B, 15. A, 16. A, 17. A, 18. B, 19. C, 20. B, 21. C, 22. A, 23. B, 24. A, 25. C