-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCEP-30-02 4.1 working with functio.txt
224 lines (189 loc) · 5.84 KB
/
PCEP-30-02 4.1 working with functio.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
PCEP-30-02 4.1 working with functions in Python, including defining and invoking user-defined functions, using the return keyword, understanding the None keyword, and working with recursion:
1. What is the correct way to define a function in Python?
A) function my_function():
B) def my_function():
C) define my_function():
D) func my_function():
2. What is the output of the following code?
```python
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
```
A) Hello, Alice!
B) Alice
C) Nothing (no output)
D) Error
3. Which keyword is used to return a value from a function?
A) give
B) return
C) yield
D) send
4. What is the output of this code?
```python
def add(a, b):
return a + b
result = add(3, 4)
print(result)
```
A) 3
B) 4
C) 7
D) None
5. What is a generator function in Python?
A) A function that generates random numbers
B) A function that uses the 'yield' keyword to return values
C) A function that creates other functions
D) A function that only returns None
6. What is the purpose of the 'None' keyword in Python?
A) To represent a null or empty value
B) To stop a function execution
C) To define a variable without initializing it
D) To create an empty list
7. What is recursion in programming?
A) A loop that runs infinitely
B) A function that calls itself
C) A method to sort data
D) A way to define multiple functions at once
8. What is the output of this recursive function?
```python
def countdown(n):
if n <= 0:
return
print(n)
countdown(n-1)
countdown(3)
```
A) 3 2 1
B) 1 2 3
C) 3 3 3
D) Error (infinite recursion)
9. Which of the following is true about Python functions?
A) They must always return a value
B) They can have multiple return statements
C) They can only have one parameter
D) They must be defined before the main program
10. What happens if a function doesn't have a return statement?
A) It returns 0
B) It returns None
C) It raises an error
D) It returns an empty string
11. How do you call a function named 'my_function' with no arguments?
A) call my_function
B) my_function
C) my_function()
D) run my_function
12. What is the output of this code?
```python
def multiply(a, b=2):
return a * b
print(multiply(3))
```
A) 3
B) 2
C) 6
D) Error
13. What is a benefit of using functions in programming?
A) They make the code run faster
B) They reduce code duplication
C) They increase memory usage
D) They always improve algorithm efficiency
14. What is the difference between a function and a generator?
A) Functions use 'return', generators use 'yield'
B) Functions can have parameters, generators cannot
C) Generators are faster than functions
D) Functions can be recursive, generators cannot
15. What is the output of this code?
```python
def greet():
return "Hello"
print("World")
print(greet())
```
A) Hello
B) World
C) Hello World
D) None
16. How can you make a function return multiple values?
A) Use multiple return statements
B) Return a tuple, list, or dictionary
C) Use the yield keyword multiple times
D) It's not possible in Python
17. What is the purpose of the 'global' keyword in a function?
A) To create a new global variable
B) To use a global variable inside the function
C) To prevent the function from accessing global variables
D) To make all variables in the function global
18. What is the output of this recursive function?
```python
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
print(factorial(3))
```
A) 3
B) 6
C) 9
D) Error (infinite recursion)
19. Which of the following is not a valid function definition?
A) def f():
B) def f(a, b, c):
C) def f(*args):
D) def f(a, b, c,):
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. What is a lambda function in Python?
A) A function defined using the 'lambda' keyword
B) A function that always returns True or False
C) A function that can only have one argument
D) A function that is automatically called at program start
22. What is the output of this code?
```python
def increment(n):
return n + 1
result = increment(5)
print(result)
```
A) 5
B) 6
C) None
D) Error
23. Which of the following is true about function arguments in Python?
A) All arguments must have default values
B) Functions can have a variable number of arguments
C) Arguments are always passed by reference
D) Functions can have a maximum of 5 arguments
24. What does the asterisk (*) do in a function definition?
A) It makes the function recursive
B) It allows the function to accept any number of positional arguments
C) It multiplies the return value by 2
D) It makes all arguments optional
25. What is the output of this generator function?
```python
def countdown(n):
while n > 0:
yield n
n -= 1
for i in countdown(3):
print(i, end=' ')
```
A) 1 2 3
B) 3 2 1
C) 3 3 3
D) Error
Answers:
1. B, 2. A, 3. B, 4. C, 5. B, 6. A, 7. B, 8. A, 9. B, 10. B, 11. C, 12. C, 13. B, 14. A, 15. A, 16. B, 17. B, 18. B, 19. D, 20. B, 21. A, 22. B, 23. B, 24. B, 25. B