|
| 1 | +# Parallel Programming Exercises |
| 2 | + |
| 3 | +## 🔴 Basic Concepts I |
| 4 | + |
| 5 | +### 1. What do you call a command-line interpreter which lets you interact with your OS and execute Python commands and scripts? |
| 6 | + |
| 7 | +- An editor |
| 8 | +- Jython |
| 9 | +- A console |
| 10 | +- A compiler |
| 11 | + |
| 12 | +### 2. What is the expected output of the following code? |
| 13 | + |
| 14 | +```python |
| 15 | +prin("Goodbye!") |
| 16 | +``` |
| 17 | + |
| 18 | +- The program will generate an error message to the screen |
| 19 | +- The program will output **("Goodbye!")** to the screen |
| 20 | +- The program will output **"Goodbye!"** to the screen |
| 21 | +- The program will output **Goodbye!** to the screen |
| 22 | + |
| 23 | +### 3. What is the expected output of the following code? |
| 24 | + |
| 25 | +```python |
| 26 | +print("Hello!") |
| 27 | +``` |
| 28 | + |
| 29 | +- The program will output **Hello!** to the screen |
| 30 | +- The program will generate an error message to the screen |
| 31 | +- The program will output **"Hello!"** to the screen |
| 32 | +- The program will output **("Hello!")** to the screen |
| 33 | + |
| 34 | +### 4. What is CPython? |
| 35 | + |
| 36 | +- It's a programming language that is a superset of the C language, designed to produce Python-like performance with code written in C |
| 37 | +- It's the default, reference implementation of the C language, written in Python |
| 38 | +- It's a programming language that is a superset of Python, designed to produce C-like performance with code written in Python |
| 39 | +- It's the default, reference implementation of Python, written in the C language |
| 40 | + |
| 41 | +### 5. What is true about compilation? |
| 42 | + |
| 43 | +**I.** The code is converted directly into machine code executable by the processor |
| 44 | +**II.** It tends to be slower that interpretation |
| 45 | +**III.** Both you and the end user must have the compiler to run your code |
| 46 | +**IV.** It tends to be faster that interpretation |
| 47 | + |
| 48 | +- I and II |
| 49 | +- I and III |
| 50 | +- II and III |
| 51 | +- I and IV |
| 52 | + |
| 53 | +## 🔴 Basic Concepts II |
| 54 | + |
| 55 | +### 1. The print() function can output values of |
| 56 | + |
| 57 | +- any number of arguments (including zero) |
| 58 | +- just one argument |
| 59 | +- any number of arguments (excluding zero) |
| 60 | +- not more than five arguments |
| 61 | + |
| 62 | +### 2. What is the output of the following snippet if the user enters two lines containing 2 and 4 respectively? |
| 63 | + |
| 64 | +```python |
| 65 | +x = input() |
| 66 | +y = input() |
| 67 | +print(x + y) |
| 68 | +``` |
| 69 | + |
| 70 | +- 24 |
| 71 | +- 2 |
| 72 | +- 4 |
| 73 | +- 6 |
| 74 | + |
| 75 | +### 3. What is the output of the following snippet? |
| 76 | + |
| 77 | +```python |
| 78 | +y = 2 + 3 * 5. |
| 79 | +print(Y) |
| 80 | +``` |
| 81 | + |
| 82 | +- 25 |
| 83 | +- the program will generate an error message to the screen |
| 84 | +- 17 |
| 85 | +- 17.0 |
| 86 | + |
| 87 | +### 4. The ** operator |
| 88 | + |
| 89 | +- does not exist |
| 90 | +- performs duplicated multiplication |
| 91 | +- performs floating-point multiplication |
| 92 | +- performs exponentiation |
| 93 | + |
| 94 | +### 5. The meaning of the keyword parameter is determined by |
| 95 | + |
| 96 | +- its position within the argument list |
| 97 | +- its value |
| 98 | +- the argument's name specified along with its value |
| 99 | +- its connection with existing variables |
| 100 | + |
| 101 | +## 🔴 Loops and Sequences |
| 102 | + |
| 103 | +### 1. What is the output of the following code? |
| 104 | + |
| 105 | +```python |
| 106 | +my_list = [1, 2, 3] |
| 107 | +for v in range(len(my_list)): |
| 108 | + my_list.insert(1, my_list[v]) |
| 109 | +print(my_list) |
| 110 | +``` |
| 111 | + |
| 112 | +- [1, 2, 3, 1, 2, 3] |
| 113 | +- [1, 2, 3, 3, 2, 1] |
| 114 | +- [3, 2, 1, 1, 2, 3] |
| 115 | +- [1, 1, 1, 1, 2, 3] |
| 116 | + |
| 117 | +### 2. How many stars will the following code snippet print? |
| 118 | + |
| 119 | +```python |
| 120 | +i = 0 |
| 121 | +while i <= 5: |
| 122 | + i += 1 |
| 123 | + if i % 2 == 0: |
| 124 | + break |
| 125 | + print("*") |
| 126 | +``` |
| 127 | + |
| 128 | +- three |
| 129 | +- one |
| 130 | +- two |
| 131 | +- zero |
| 132 | + |
| 133 | +### 3. How many elements does the my_list contain after the following code is executed? |
| 134 | + |
| 135 | +```python |
| 136 | +my_list = [i for i in range(-1, 2)] |
| 137 | +``` |
| 138 | + |
| 139 | +- four |
| 140 | +- one |
| 141 | +- two |
| 142 | +- three |
| 143 | + |
| 144 | +### 4. What is the output of the following code? |
| 145 | + |
| 146 | +```python |
| 147 | +my_list = [3, 1, -2] |
| 148 | +print(my_list[my_list[-1]]) |
| 149 | +``` |
| 150 | + |
| 151 | +- -2 |
| 152 | +- 3 |
| 153 | +- 1 |
| 154 | +- -1 |
| 155 | + |
| 156 | +### 5. The value eventually assigned to the variable x is |
| 157 | + |
| 158 | +```python |
| 159 | +x = 1 |
| 160 | +x = x == x |
| 161 | +``` |
| 162 | + |
| 163 | +- False |
| 164 | +- 1 |
| 165 | +- True |
| 166 | +- 0 |
| 167 | + |
| 168 | +## 🔴 Functions and Decorators |
| 169 | + |
| 170 | +### 1. What is the output of the following code? |
| 171 | + |
| 172 | +```python |
| 173 | +def func(a, b): |
| 174 | + return a ** a |
| 175 | + |
| 176 | +print(func(2)) |
| 177 | +``` |
| 178 | + |
| 179 | +- 4 |
| 180 | +- 2 |
| 181 | +- None |
| 182 | +- error |
| 183 | + |
| 184 | +### 2. What is the output of the following code? |
| 185 | + |
| 186 | +```python |
| 187 | +def func(inp=2, out=3): |
| 188 | + return inp * out |
| 189 | + |
| 190 | +print(func(out=2)) |
| 191 | +``` |
| 192 | + |
| 193 | +- 2 |
| 194 | +- 4 |
| 195 | +- 6 |
| 196 | +- error |
| 197 | + |
| 198 | +### 3. What is the output of the following code? |
| 199 | + |
| 200 | +```python |
| 201 | +def decorator(func): |
| 202 | + def wrapper(): |
| 203 | + print("Before function call", end=" >> ") |
| 204 | + result = func() |
| 205 | + print("After function call", end=" >> ") |
| 206 | + return result |
| 207 | + return wrapper |
| 208 | + |
| 209 | + |
| 210 | +@decorator |
| 211 | +def say_hello(): |
| 212 | + print("Hello!", end=" >> ") |
| 213 | + |
| 214 | +say_hello() |
| 215 | +``` |
| 216 | + |
| 217 | +- Before function call >> Hello! |
| 218 | +- Hello! >> Before function call |
| 219 | +- Before function call >> Hello! >> After function call >> |
| 220 | +- The program will generate an error message to the screen |
| 221 | + |
| 222 | +### 4. What is the output of the following code? |
| 223 | + |
| 224 | +```python |
| 225 | +def func(x): |
| 226 | + global y |
| 227 | + y = x * x |
| 228 | + return y |
| 229 | + |
| 230 | + |
| 231 | +func(2) |
| 232 | +print(y) |
| 233 | +``` |
| 234 | + |
| 235 | +- 2 |
| 236 | +- None |
| 237 | +- 4 |
| 238 | +- error |
| 239 | + |
| 240 | +### 5. What is the output of the following code? |
| 241 | + |
| 242 | +```python |
| 243 | +def func(x, y, z): |
| 244 | + return x + 2 * y + 3 * z |
| 245 | + |
| 246 | + |
| 247 | +print(func(0, z=1, y=3)) |
| 248 | +``` |
| 249 | + |
| 250 | +- 3 |
| 251 | +- error |
| 252 | +- 0 |
| 253 | +- 9 |
| 254 | + |
| 255 | +## 🔴 Asynchronous Programming |
| 256 | + |
| 257 | +### 1. Which method starts an event loop in asyncio module? |
| 258 | + |
| 259 | +- asyncio.start() |
| 260 | +- asyncio.run() |
| 261 | +- asyncio.loop() |
| 262 | +- asyncio.call() |
| 263 | + |
| 264 | +### 2. How does asynchronous programming handle multiple tasks? |
| 265 | + |
| 266 | +- By running them on different threads |
| 267 | +- By switching between tasks using the event loop |
| 268 | +- By running them on multiple CPU cores |
| 269 | +- By starting a new event loop for each task |
| 270 | + |
| 271 | +### 3. What will happen if you forget to await a coroutine? |
| 272 | + |
| 273 | +- The coroutine will run twice |
| 274 | +- The coroutine will throw a runtime error |
| 275 | +- The coroutine will return a coroutine object instead of running |
| 276 | +- The coroutine will block the event loop |
| 277 | + |
| 278 | +### 4. Which of the following correctly declares an asynchronous function? |
| 279 | + |
| 280 | +- def async_function(): |
| 281 | +- async def async_function(): |
| 282 | +- def async async_function(): |
| 283 | +- def async_function async(): |
| 284 | + |
| 285 | +### 5. What is the function of asyncio.gather()? |
| 286 | + |
| 287 | +- It creates a new event loop for each coroutine |
| 288 | +- It ensures coroutines run sequentially |
| 289 | +- It schedules multiple coroutines to run concurrently |
| 290 | +- It waits for each coroutine to finish before starting the next one |
0 commit comments