MIS 15 Final

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

6. What will the following Python code print out? data = 'From stephen, [email protected]. za Sat Jan 5 09:14:16 2008' pos = data.find('. ') print(data[pos:pos+3]) mar Sat .ma 09:14

.ma

9. how many times will the body of the loop be executed? n = 0 while n > 0: print('Lather') print('Rinse') print('Dry off') 1 0 5 This is an infinite loop

0

13. What is the output? L = ['None'] *10 Print(len(L)) 10 0 Syntax error

10

35. What would this code print out? Ist = [ ] Ist.append(4) Ist.append(10) Ist.append(21) Ist.append(6) print(Ist[2]) 21 10 4 0

21

6. What is the output of the following code? var1 = 1 var2 = 2 var3 = "3" print(var1 + var2 + var3) 6 33 123 Error. mixing operators between numbers and strings are not supported

6

22. what is the purpose of the following python code fhand= open('mbox.text') x = 0 for line in fhand: x = x + 1 print(x) Count the lines in the file 'mbox.txt' Reverse the order of the lines in mbox.txt Remove the leading and trailing spaces from each line in mbox.txt Convert the lines in mbox.txt to upper case

Count the lines in the file 'mbox.txt'

20. what would the following Python code print out? fruit = 'banana' fruit[0]= 'b' print(fruit) b banana B Nothing would print - the program fails with a traceback

Nothing would print - the program fails with a traceback

4. What is the output of the following code? var = "James" * 2 * 3 print(var) JamesJamesJamesJamesJamesJames JamesJamesJamesJamesJames Error invalid syntax

JamesJamesJamesJamesJamesJames

18. following code sequence fails with a traceback when the user enters a file that does not exist. How would you avoid the traceback and make it so you could print out your own error message when a bad file name was entered? fname = raw input ('Enter the file name: ' ) fhand = open (fname) Single handlers Try/Except try/catch/finally on error resume next

Try/Except

26. What is the output of this code? tuple1 = (1120, 'a') print(max(tuple1)) TypeError 1120 'a'

TypeError

32. What is the output of the following code? aTuple = (100, 200, 300, 400, 500) aTuple[1] = 800 print(aTuple) TypeError (100, 800, 200, 300, 400, 500) (800, 100, 200, 300, 400, 500)

TypeError

15. What is the output of the following list operation aList = [10, 20, 30, 40, 50, 60, 70, 80] print (aList[2:5]) print (aList[:4]) print (aList[3:]) A. [20, 30, 40, 50] [10, 20, 30, 40] [30, 40, 50, 60, 70, 80] b. [30, 40, 50] [10, 20, 30, 40] [40, 50, 60, 70, 80]

b. [30, 40, 50] [10, 20, 30, 40] [40, 50, 60, 70, 80]

8. What would the following code print out? stx = 'hello there bob how are you" wds = stx.split() print(wds[2]) bob are E how

bob

23. Which of the following lines of Python is equivalent to the following sequence of statements assuming that counts is a dictionary? if key in counts: counts[ key ] = counts [key] + 1 else: counts[key] = 1 counts[key] = counts.get (key, 0) + 1 counts[key] = counts.get (key, -1) + 1 counts[key] = (key in counts) + 1 counts[ key] = (counts[ key] * 1) + 1

counts[key] = counts.get (key, 0) + 1

29. if the variable data is a python list, how do we sort in the reverse order? data = data.sort(-1) data.sort(reverse=True) data.sort.reverse() data = sortrev(data)

data.sort(reverse=True)

21. What is the output of the code dict1 = {"name" : "Mike" , "salary": 8000} temp = dict1.pop("age") print(temp) key error: 'age' none

key error: 'age'

10. Select the option to join two lists in python listOne = ['a' , 'b' , 'c' , 'd'] listTwo = ['e' , 'f' , 'g'] newList = listOne + listTwo newList = extend(listone, listTwo) newList. extend(listone, listTwo)

newList = listOne + listTwo

16. Which of the following Python statements would print out the length of a list stored in the variable data? print(data.length()) print (len(data)) print (data.Len) print (length (data))

print (len(data))

5. How would you print out the following variable in all upper case in Python? greet = "Hello Bob' puts greet.ucase; console.log(greet.toUpperCase()); print(greet.upper()) print(uc($greet);)

print(greet.upper())

3. How would you use string slicing [:] to print out 'uct' from the following string? X = 'From [email protected]' print(x[14:3]) print(x[14+17]) print(x[15:18]) print(x[14:17])

print(x[14:17])

24. Select the correct ways to create an empty dictonary (Select all that apply) sampleDict = { } sampleDict = dict( ) sampleDict = dict { }

sampleDict = { } sampleDict = dict( )

2. You are reading Python code, and these statements appear scattered in different locations throughout the code: employeenumber = 4398 EmployeeNumber = 4398 employeeNumber =4398 these statements refer to different variables these statements refer to the same variable

these statements refer to different variables

3. What is the output of the following code? valueOne = 5**2 valuetwo = 5**3 print(valueOne) print(valueTwo) 10 15 25 125 error invalid syntax

25 125

1. What does the following Python program print out? x = '40' y = int(x) + 2 print(y) 42 X2 402 int402

42

What is the output of this code? grade = ('mahdi": 20 , "amin': 15 , 'keyvan' : 19 , "behzad' : 11) z = list(grade,.items( )) print(type(z)) print(type(z[2]) ) ) <class 'list'> <class 'tuple'> <class 'tuple'> <class 'list'>

<class 'list'> <class 'tuple'>

1.What signifies the end of a statement block or suite in python? A comment End A line that is indented less than the previous line ]

A line that is indented less than the previous line

28. in the following Python code, what will end up in the variable y? x={ "chuck' : 1, "fred' 42, 'jan': 100} x. items () A list of intergers A list of strings A list of tuples a tuple with three intergers

A list of tuples

Select which is true for Python tuple (Select all that apply) A tuple maintains the order of items A tuple is unordered We cannot change the tuple once created We can change the tuple once created

A tuple maintains the order of items We cannot change the tuple once created

31. In the following Python loop, why are there two iteration variables (k and v)? c = {'a':10, 'b':1, 'c':22} for k, v in c.items() : Because for each item we want the previous and current key Because the items() method in dictionaries returns a list of tuples Because there are two items in the dictionary Because the keys for the dictionary are strings

Because the items() method in dictionaries returns a list of tuples

7. 'Boolean' refers to anything that Can take on any textual value Can take on any numerical value Can only take on one of two values: True or False Can only take on one of three values: True, False, or Undefined

Can only take on one of two values: True or False

30. Given that python lists and python tuples are quite similar - when might you prefer to use a tuple over a list? For a list of items that want to use strings as key values instead of integers For a list of items that will be extended as new items are found For a temporary variable that you will use and discard without modifying For a list of items you intend to sort in place

For a temporary variable that you will use and discard without modifying

12. What is output of the following code? my_list = "Hello" "python"] print("-" .Join(my_list)) HelloPython- Hello-Python -HelloPython

Hello-Python

10. What is the purpose of the break statement in python (Select all that apply) It allows you to break out of a for-loop It allows you to break out of a while-loop It allows you to abort program execution It allows you to break out of an if-statement

It allows you to break out of a for-loop It allows you to break out of a while-loop

27. What does the following Python code accomplish assuming c is non empty dictionary? tmp = list( ) for k, v in c.items( ) : tmp.append((v,k)) It sorts the dictionary based on its key values It creates a list of tuples where each tuple is a value, key pair It computes the largest of all of the values in the dictionary It computes the average of all of the values in the dictionary

It creates a list of tuples where each tuple is a value, key pair

17. What does the following Python code do? fhand = open ('mbox-short.txt') inp = fhand.read() Checks to see if the file exists and can be written Turns the text in the file into a graphic image like a PNG or JPG Reads the entire file into the variable inp as a string Prompts the user for a file name

Reads the entire file into the variable inp as a string

8. What is the output salary = 8000 def printSalary(): salary = 12000 print ("Salary:" , salary) printSalary() print ("Salary:", salary) Salary: 12000 Salary: 8000 Salary: 8000 Salary: 12000 The program failed with errors

Salary: 12000 Salary: 8000

33. a Python tuple can be also created without using parentheses? True False

True

4. Strings are immutable in Python, which means a string cannot be modified. True False

True

5. What is the output of the following code? listOne = [20, 40, 60, 80] listTwo = [20, 40, 60, 80] print(listOne==listTwo) print(listOne is listTwo) True True True False False True

True False

7. Str1= 'Welcome' print (str1[:6] + 'PYnative') Welcome PYnative WelcomPYnative Welcom PYnative WelcomePYnative

Welcom PYnative

9. Choose the correct output of the following string operations str1 = 'Welcome' print(str1*2) WelcomeWelcome Type Error unsupported operand type(s)

WelcomeWelcome

2. str1 "pynative" print (str1[1:4], str1[:5], str1[4:], str[:7] Yna PYnat tive PYnativ PYn PYnat ive vitanYP Yna PYnat tive PvitanYP

Yna PYnat tive PYnativ

11. What is the output of the following code? sampleList = [10, 20, 30, 40] del sampleList[0:4] print(sampleList) [ ] list index out of range [10,40]

[ ]

14. What is the output of the following list assignment? aList = [4, 8, 12, 16] aList [1:4] = [20, 24 , 28] print(aList) [4, 20, 24, 28, 8, 12, 16] [4, 20, 24, 28]

[4, 20, 24, 28]

25. What is the output of this code? ( 'first' , 1) , ( 'second', 2 ) , ( 'third' , 3 ) ] ) print(sampleDict) [('first', 100), ('second', 200), ('third', 300) ] Options: SyntaxError: invalid syntax {'first': 1, 'second': 2, 'third': 3}

{'first': 1, 'second': 2, 'third': 3}


Ensembles d'études connexes

Chapter 39 & 40 Pathophysiology Quizzes

View Set

Rosetta Stone Arabic Unit 1 Lesson 1

View Set