Exam 98-381: Introduction to Programming Using Python-2 50q

1. What does the following code print to the console?

a, b, c = (True, 1, 'True')
print(type(a), type(b), type(c))

Question 1 of 50

2. What does the following code print to the console?

1: string1 = 'hello'
2: string2 = "hello"
3: string3 = """hello"""

4: print(string1  == string2 == string3)

Question 2 of 50

3. The following code has an error, which line is it on?

1: a, b, c = 0, 0, 0
2: a, b, c = (0) * 3 
3: a, b, c = '0' * 3
4: a, b, c = ['0'] * 3

Question 3 of 50

4. What does the following code print to the console? (Hint: 2**64 == 18446744073709551616)

a = 2 ** 64
a = a + 1
print(a)

Question 4 of 50

5. What does the following code print to the console?

s = 'The quick brown fox'

a = s
a[-3:] = 'dog'

print(a)

Question 5 of 50

6. What does the following code print to the console?

animals = ['cat', 'dog', 'hamster', 'pony']

my_anymals =[animals[-2:-1][0], animals[2], animals[2:3][0], animals[1:3][1]]

my_anymals = set(my_anymals)

print(my_anymals)

Question 6 of 50

7. The following code has an error, which line is it on?

1: jennifer  = 'Jennifer'
2: joe = 'Joe'
3: mike_and_partners  = ['Mike', 'Lana',  'Abrams']
4: team = {}
5: team[jennifer] = 'Python Developer'
6: team[joe] = 'C++ Developer'
7: team['Developers Team'] = [joe, jennifer]
8: team[mike_and_partners] = 'SMM Team'

Question 7 of 50

8. What does the following code print to the console?

a = [True, True, False, True]
b = [False, True, False, True]

c = a + b
e = c - a
print(e)

Question 8 of 50

9. What does the following code print to the console?

a = False
b = True
c = a & b
e =  0
d = [True, False]

print("My name is ...")
if a | b or c == c and c is not d:
    print("Jon!")
elif c is not in d or b:
    print("Jessica")
else:
    print("Ralph!")

Question 9 of 50

10. What does the following code print to the console?

color = "Red"
number = 16

if (color == "Red" or color == "Black") and number <= 2:
    print("1")
    number = []
    if number:
        print('3')
    else:
        print('4')
else:
    print("2")
    number = []
    if number:
        print('5')
    else:
        print('6')

Question 10 of 50

11. What does the following code print to the console?

a = {'a': 2, 'b': 3, 'c': [2, 3, 4]}
if 5 in a.values() or len(a) > 3:
    print('1')
    if a['c'][2] in range(-5, 5):
        print('2')
    else:
        print('3')
else:
    print('4')
    if a['c'][-1] in range(3):
        print('5')
    else:
        print('6')

Question 11 of 50

12. What does the following code print to the console?

a = ('1', 2, '3', '4')
if type(a) == 'tuple'  and list(a)[2] == '2':
    print('1')
elif type(a[2])  == 'str':
    print('2')
elif int(a[0]*a[1]) == 11:
    print('3')
else:
    print('4')

Question 12 of 50

13. Which of the listed operators should we use  here (a [operator] b [operator] a) to get the largest number?

a = 1
b = 3
print(a [operator] b [operator] a)

Question 13 of 50

14. Which of the listed operators should we use here (a [operator1] b [operator2] a) to get the smallest number?

a = -3
b = -4

print(a [operator1] b [operator2] a)

Question 14 of 50

15. What does the following code print to the console?

a = ['1']
b = ['1']
if a[0] is b[0]:
    print("1")
    a = b
    if a is b:
        print("3")
else:
    print("2")
    a = b
    if a is b:
        print("4")

Question 15 of 50

16. What does the following code print to the console?

a = 10
b = 15

if a is (b-5):
    print(a>>b)
else:
    print(a%b)

Question 16 of 50

17. What does the following code print to the console?

a = 8
if a > 3:
    a+=10
elif a > 18:
    a += 10
if a > 8:
    a *= 10
elif a > 38:
    a += 10
if a > 48:
    a += 10
if a > 58:
    a += 10
print(a)

Question 17 of 50

18. What does the following code print to the console?

i = 19
for i in range(5, -1):
    if i < 7:
        i+=1
print(i)

Question 18 of 50

19. What does the following code print to the console?

a = 3
b =  3

switch(a%b):
    case 1:
        print('1')
    case 3:
        print('2')
    case 0:
        print('0')
    default:
        print('default')

Question 19 of 50

20. What does the following code print to the console?

i = 30
for i in range(50):
    while True:
        if i == 25:
            break
        i += 2
        continue
print(i)

Question 20 of 50

21. What does the following code print to the console?

d = {'tomato':1, 'potato': 2, 'carrot':3}

for i in d.keys():
    for item in ['tomato', 'potato', 'carrot']:
        if 'r' in item:
            continue
        if item == i:
            d[item] *= 2
        break
print(d)

Question 21 of 50

22. What does the following code print to the console?

for i in range(10, 50, 2):
    if i % 2:
        break
    else:
        i +=1

print(i)

Question 22 of 50

23. What does the following code print to the console?

items = ['a', 'b', 'c', 'd']

for i in range(len(items)):
    for item in items:
        item = '*'
    if items[i] != '*':
        items[i] = str(i)

print(items)

Question 23 of 50

24. What does the following code print to the console?

def func(a, b, c):
    return a + b + c

c = 15
for i in range(1, 5):
    func(c, i, c)
print(c)

Question 24 of 50

25. The following code has an error, which line is it on?

1: f = open("demofile.txt", "w")
2: a = f.readline()
3: b = f.read()
4: f.write('Hello!')
5: f.close()

Question 25 of 50

26. What does the following code print to the console?

file = open('demofile.txt', 'wb')
a = file.read()
file.close()
a = a[2:4]

file = open('demofile.txt', 'rb')
b = file.read(4)
c = file.read(2)
file.close()

print(a==c)

Question 26 of 50

27. You need to write list items to file line by line, how do you do it?

list = ['a', 'b', 'c']

Question 27 of 50

28. You need to read from file to list line by line, how do you do it? (file exists and not empty)

Question 28 of 50

29. Which function contains an inaccuracy in the docstring?

Question 29 of 50

30. Python script 'script.py' was launched with syntax:

python3 script.py '1' '2' '3'

What does the following code print to the console?

import sys
result  = sys.argv[0]*int(sys.argv[1])*int(sys.argv[2])
print(result)

Question 30 of 50

31. You are asking the user to enter an 21 or more alphabetical symbols (no spaces, numbers, etc).
What code validates this input correctly?

Question 31 of 50

32. You need to get the following output from the code above:

a = 1.0544363
b  = 2.43545

Integer: 1
2-digit number: 2.44

how do you do it?

Question 32 of 50

33. What does the following code print to the console?

def func1():
    """doc string"""
    pass

def func2():
    #doc string
    pass

doc_string1 =  func1.__doc__
doc_string2 =  func2.__doc__

print(doc_string1)
print(doc_string2)

Question 33 of 50

34. What does the following code print to the console?

def func1(a, b):
    """
    :param a:
    :param b:
    :return:
    """

    for i in range(5):
        # increment a
        a += b
        """end  of  iteration"""
    """next steps"""
    a = a * 2
    #    """return a"""
    return a


c = func1(1, 2)
print(c)

Question 34 of 50

35. What does the following code print to the console?

def func1(a, b=4, c=8):
    return a * b * c


a = func1(6, 3)
print(a)

Question 35 of 50

36. What does the following code print to the console?

def func1(**kwargs):
    return kwargs

a = func1(param1 = 'a', param2  = 'c')
print(a)

Question 36 of 50

37. What does the following code print to the console?

def func1(a, b, c=6, d=5):
    a = a+b*c
    return c*d


a = func1(1,4)
b = func1(3,8)
print(a == b)
print(a is b)

Question 37 of 50

38. What does the following code print to the console?

def func1(a, b, *args):
    a = (a+b)*args[1]/args[3]
    return a

a = func1(1,2,3,4,5,6,7)
print(a)

Question 38 of 50

39. Which exception will the following code raise?

def func1(a, b, c=6):
    a = a-c
    b = c/(a+2)
    return b

def func2(a, b, c=6):
    c = a*b*b


a = 6
b = 2
c =  func1(a, b)*func2(b,a)

Question 39 of 50

40. Which exception will the following code raise?

items = [1, '2', '34', [2, 3, 4]]
dict_items = {}
for i in range(len(items)):
    dict_items[items[i]] = items[-1]
print(dict_items)

Question 40 of 50

41. Which exception will the following code raise?

items1 = ['1', '2', '3', '4', '5', '6']
items2 = ('a', 'b', 'c', 'd', 'e', 'f')

for i in range(len(items1)):
    value = int(items1[i]) * items2[i]
    items2[i] = value
print(items2)

Question 41 of 50

42. Which exception will the following code raise?

dict1 = {'a':'1', 'c':'2'}
int = 5
dict['a'] = str(int(dict['a'])*int)
print(dict[a])

Question 42 of 50

43. What does the following code print to the console?

def check_exception(a):
    try:
        c = items[len(items)+1]
    except ZeroDivisionError:
        print("1")
    except Exception:
        print("2")
    else:
        print('3')
        c = '1'
    finally:
        c = '2'
        print('4')
    print('5')
    return c
items = ['1', '2', '3']
a = check_exception(5)
print(a)

Question 43 of 50

44. Which exception will the following code raise?

def func2(a):
    return func1(a)


def func1(a):
    return func2(a)

a = 5
func1(a)

Question 44 of 50

45. What does the following code print to the console?

def check_exception():
    try:
        print('1')
        raise ValueError('1')
    except ValueError as e:
        try:
            print('2')
            a = 0/5
        except ZeroDivisionError as e:
            print(str(e))
            print('3')
    finally:
        print('4')

check_exception()

Question 45 of 50

46.

Which of the pieces of code below causes Exception?

Question 46 of 50

47. What is the correct way to get the last folder from path - folderD

import os
path = 'C:/folderA/folderB/folderC/folderD/myfile.zip'

Question 47 of 50

48. What does the following code print to the console?

import os
path = 'C:/folderA/folderB/folderC/folderD/myfile.zip'
file = os.path.basename(path)
folder_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(path))))
print(os.path.join(folder_path, file))

Question 48 of 50

49. What is the lowest and highest possible value of 'result'?

import random
result = random.randint(1, 3)*random.randint(2, 4)*random.randint(1, 2)/random.randint(1, 2)
print(result)

Question 49 of 50

50. You need to safely remove all files from some folder, how do you do it?

Question 50 of 50


 

Leave a comment