-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexam_revision.py
689 lines (348 loc) · 11 KB
/
exam_revision.py
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# List of all questions to be revised:
#Q1 You're going to negate the __venomous property of the version_2 object. How will you do this?
class Python:
population = 1
victims = 0
def __init__(self):
self.length_ft = 3
self.__venomous = False
version_2 = Python()
# Answer: version_2._Python__venomous = not version_2._Python__venomous
'''
#Q2 What is the output of the following expression?
'mike' > 'Mike'
Hint:
ASCII 65(A) to 90(Z)
ASCII 97(a) to 122(z)
Python string comparison ends at the first difference; the string with the higher ASCII value at that point is greater.
'''
# Answer: True
# Q3 What is the expected output of the following code?
try:
print("5"/0)
except ArithmeticError:
print("arith")
except ZeroDivisionError:
print("zero")
except:
print("some")
# Answer: Prints "some"
# Q4 The following code does what?
print(float("1, 3"))
# Answer: ValueError
''' Q5
Which of the following are examples of Python built-in concrete exceptions? (Select two answers):
ArithmeticError
ImportError
BaseException
IndexError
'''
# Answer: IndexError, ImportError
''' Q6 what is the output of each?:
Q6 Questions:
a) print(math.floor(-29) + math.trunc(10.6))
b) print([i for i in range(1, math.ceil(11))])
c) print([i for i in range(5) if i % 2 == 0 and i % 1 == i])
Q6 Answers:
a)
b)
c)
'''
# Q7 What is BOM?
# Answer: Byte Order Mark
# Q8 What is the length of the following string assuming there is no whitespaces between the quotes?
"""
"""
# Answer: 1
#Q9 What is the expected output of the following code?
s = 'It is either easy or impossible'
s = s.replace('easy', 'hard').replace('im', '')
print(s)
# Answer: It is either hard or possible.
# Q10 Which of the following lines describe a true condition?
1. 'smith' > 'Smith'
2. 'Smiths' < 'Smith'
3. 'Smith' > '1000'
4. '11' < '8'
# Answer: 1, 4
# Q11 What is the expected output of the following code?
s1 = 'Where are the snows of yesteryear?'
s2 = s1.split()
s3 = sorted(s2)
print(s3[1])
# Answer: 'are'
# Q12What is the expected result of the following code?
s1 = '12.8'
i = int(s1)
s2 = str(i)
f = float(s2)
print(s1 == s2)
# Answer: ValueError
# Q13 What is the expected value of the result variable after the following code is executed?
import math
result = math.e == math.exp(1)
# Answer: True
# Q14 (Complete the sentence) Setting the generator's seed with the same value each time your program is run guarantees that...
# Answer: You'll get the same output.
# Q15 Which of the platform module's functions will you use to determine the name of the CPU running inside your computer?
# Answer: platform.processor()
# Q15 What is the expected output of the following snippet?
import platform
print(len(platform.python_version_tuple()))
# Answer: 3
# Q16 You want to prevent your module's user from running your code as an ordinary script. How will you achieve such an effect?
# Answer:
import sys
if __name__ == '__main__':
sys.exit()
# Q17 Some additional and necessary packages are stored inside the D:\Python\Project\Modules directory. Write a code ensuring that the directory is traversed by Python in order to find all requested modules.
# Answer:
import sys
sys.path.append('D:\\Python\\Project\\Modules')
# Q18 The directory mentioned in the previous exercise contains a sub-tree of the following structure: abc/def/mymodule.py.
# Assuming that D:\Python\Project\Modules has been successfully appended to the sys.path list, write an import directive letting you use all the mymodule entities.
# Answer:
import abc.def.mymodule
# Q19 What is __pycache__ and __init__.py ?
'''
__pychache__ is a folder containing semi-compiled code (bytecode) stored in the same folder as your scripts. It simply allows subsequent program runs to execute faster.
The folder is usually hidden by vscode to prevent clutter, except when creating directories with user created modules.
__init__.py is an often empty file that indicates to Python that its containing directory is a Package. Prior to Python 3.3, this was required.
'''
# Answer:
# __pycache__ is a directory that stores semi-compiled code (bytecode) of modules the first time they're imported so that subsequent runs are faster
# __init__.py is placed in directories of packages (and subpackages) to let Python know they are packages.
''' Q20
# Explain how to:
1. Check pip version?
2. Install pip package?
3. Install pipo package only for a specific user ?
'''
# Answer:
1. pip3 --version
2. pip3 install package_name
3. pip3 install --user package_name
'''
Q21 : When you use pip to install a package that requires one or more dependencies, then:
-you'll have to install all the dependencies by yourself before you install the desired package
-pip will take care of everything by itself
-you'll have to install all the dependencies by yourself after you install the desired package
-the package will install all the dependencies during its first run
'''
# Answer: -pip will take care of everything by itself
'''
Q22: A list of package's dependencies can be obtained from pip using its command named:
-dir
-show
-list
-deps
'''
# Answer: -show
'''
Q23: What is true about the pip search command? (Select three answers)
1. it needs working Internet connection to work
2. all its searches are limited to locally installed packages
3. it searches through package names and summaries
4. it searches through all PyPI packages
'''
# Answer: 1, 3, 4
'''
Q24: During the first import of a module, Python deploys the pyc files in which of the below directories?
(Note: Although bytecode (pyc files) are created on a script's first execution, they aren't necessarily stored in the __pycache__ folder).
1. __pycache__
2. mymodules
3. hashbang
4. __init__
'''
# Answer: 1
'''Q25 For each print statement, what are the possible outputs?
from random import randrange, randint
1. print(randrange(1), end= ' ')
2. print(randrange(0, 1), end= ' ' )
3. print(randrange(0, 20, 5), end= ' ')
4. print(randint(0, 2), '\n')
Answers:
1.
2.
3.
4.
'''
'''
Q26:
A) How to use pip to remove an installed package?
1. pip remove package
2. pip --uninstall package
3. pip uninstall package
4. pip install --uninstall package
B) What is the command to a) i) view all installed packages? ii) view a package's dependencies? iii) search for packages?
Answers:
A) 3. pip uninstall package
B) pip list
'''
'''
Q27: Choose the true statements. (Select two answers)
1. The version function from the platform module returns a string with your Python version
2. The processor function from the platform module returns an integer with the number of processes currently running in your OS
3. The version function from the platform module returns a string with your OS version
4. The system function from the platform module returns a string with your OS name
'''
# Answer: 3, 4
'''
Q28: When a module is imported, its contents:
1. are ignored
2. may be executed (explicitly)
3. are executed once (implicitly)
4. are executed as many times as they are imported
'''
# Answer: 3
# Q29 What is the output of: factorial(4)
# Answer: 24
# Q30 What is the output of math.log10(100)
# Answer: 2
# Q31 What is the output of a) math.ceil(2.5) and b) math.floor(2.5)?
# Answer: a) 3.0 b) 2.0
''' Q32 Python's math module includes a function that calculates the exponential of a given number, effectively computing Euler's number
(approximately 2.71828). Can you name this function and demonstrate how to use it to calculate the exponential of 1?
Answer:
import math
math.exp(1)
'''
# Q33 What is the result of: a) math.floor(-55.19) and b) math.ceil(-3.7)?
# Answer:
# a)
# b)
# Q34 What is the result of:
s = '123456789'
for i in range(len(s)):
print(math.floor(int(s[i])) and math.ceil(int(s[i])))
# Answer:
# Q35 What is the output of print(math.radians(90))?
# Answer: 1.57
''' Q36
a) What is the output of print("5"/0) and b) what is the output of int("1, 2")
Answer:
a) TypeError
b) ValueError
'''
''' Q37
What is the expected output of the following code?
import math
try:
print(math.sqrt(-9))
except ValueError:
print("inf")
else:
print("fine")
finally:
print("the end")
Answer:
inf
the end
'''
# Q38:
class Bird:
aviary = 0
def __init__(self, species):
self.species = species
Bird.aviary += 1
def __str__(self):
return self.species + " chirps."
class SongBird(Bird):
def __str__(self):
return super().__str__() + " Singing a melody."
class HuntingBird(Bird):
def __str__(self):
return super().__str__() + " Watching for prey."
'''
Your Task:
Define a HuntingBird subclass named ForestHuntingBird, and equip it with an __str__() method overriding an inherited method of the same name.
The new bird's __str__() method should return the string "<species> chirps. Watching for prey. Loves the forest!"
'''
# Answer:
''' Q39:
True or False: In Python, the method str.upper() creates a new string and does not modify the original string.
Choose True or False.
'''
# Answer: True
''' Q40:
Write a Python code snippet to concatenate the following two strings with a space between them: "Hello" and "World". Use a string method.
Answer:
'''
# Q41: What will be the output of the following code?
s = "abcd"
print(s[0:1:-1])
# Answer:
# Q42: Write a snippet to convert "This is a string" to "string a is This"
'''Answer:
print(' '.join("This is a string".split()[::-1]))
# or
s = "This is a string".split()
s.reverse()
print(' '.join(s))
'''
# Q43: What is the output of the following snippet?
s = "Coffee is black"
print(s[::-3])
'''
A) Cf a
B) Coffee is black
C) Ca fc
D) klsef
'''
# Answer: D
# Q44 a): Name four list methods that return a value and seven that don't.
'''
Answer:
'''
# Q44 b): Which string methods don't return a value?
# Answer:
# Q45: How many arguments does the string class's 'join' method take?
# Answer: 1
# Q46: What is the result of math.hypot(7, 0)
# Answer: 7
''' Q47: What is the result of math.hypot(1, 2)
a) 6.92
b) 4
c) 2
d) 2.23
'''
# Answer: 2.23
''' Q48: Write three snippets to iterate through the dictionary:
a) its key-value pairs
b) only its keys
c) only its values
'''
dic = {1: 'one', 2: 'two', 3: 'three'}
''' Answer:
a) for v in dic.items()
b) for v in dic.keys()
c) for v in dic.values()
'''
# Q49: What is the output?
s = '123456'
even_sum = 0
odd_product = 1
for i in range(len(s)):
num = int(s[i])
if i % 2 == 0:
even_sum += num
else:
odd_product *= num
print("Sum of numbers at even indices:", even_sum)
print("Product of numbers at odd indices:", odd_product)
# Answer:
# 6
# 15
# Q50: What is the output?
s = '13579'
total = 0
for i in range(1, len(s)):
if i % 2 == 0:
total += int(s[i])
print("Total of even-indexed numbers:", total)
# Answer:
# Q51: What is the ouput of this?
lst_obj = (-1, 11, 0)
lst_obj[0] += 1
obj = range(1, 11, 0)
# Answer: TypeError - tuple's cannot be modified. The range's step of zero is invalid too.