PRP201_CÓ LÀM THÌ MỚI CÓ ĂN

¡Supera tus tareas y exámenes ahora con Quizwiz!

(T/F) When you add items to a dictionary they remain in the order in which you added them. a. False b. True

a

Assume the variable x has been initialized to an integer value (e.g., x = 3). What does the following statement do? x = x + 2 a. Retrieve the current value for x, add two to it and put the sum back into x b. This would fail as it is a syntax error c. Create a function called "x" and put the value 2 in the function d. Produce the value "false" because "x" can never equal "x+2"

a

For the following code, if x < 2 : print('Below 2') elif x >= 2 : print('Two or more') else : print('Something else') What value of 'x' will cause 'Something else' to print out? a. This code will never print 'Something else' regardless of the value for 'x' b. x = -2.0 c. x = 22 d. x = 2.0

a

How would you print out the following variable in all upper case in Python? greet = 'Hello Bob' a. print(greet.upper()) b. puts(greet.ucase); c. print(greet.toUpperCase()); d. print(uc($greet));

a

How would you use the index operator [] to print out the letter q from the following string? a. print(x[8]) b. print(x[7]) c. print(x[9]) d. print(x[q]) e. print x[-1]

a

If you write a Python program to read a text file and you see extra blank lines in the output that are not present in the file input as shown below, what Python string function will likely solve the problem? From: [email protected] From: [email protected] From: [email protected] From: [email protected] ... a. rstrip() b. find() c. startswith() d. ljust()

a

In Python what is the input() feature best described as? a. A built-in function b. A conditional statement c. The central processing unit d. A way to retrieve web pages over the network

a

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() : ... a. Because the items() method in dictionaries returns a list of tuples b. Because the keys for the dictionary are strings c. Because for each item we want the previous and current key d. Because there are two items in the dictionary

a

Using the following tuple, how would you print 'Wed'? days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun') a. print(days[2]) b. print(days[1]) c. print[days(2)] d. print(days(2)) e. print(days{2}) f. print(days.get(1,-1))

a

What Python function would you use if you wanted to prompt the user for a file name to open? a. input() b. alert() c. file_input() d. gets()

a

What are the Python keywords used to construct a loop to iterate through a list? a. for / in b. try / except c. foreach / in d. def / return

a

What do we use the second parameter of the open() call to indicate? a. Whether we want to read data from the file or write data to the file b. The list of folders to be searched to find the file we want to open c. What disk drive the file is stored on d. How large we expect the file to be

a

What does the Python input() function do? a. Pause the program and read data from the user b. Connect to the network and retrieve a web page. c. Read the memory of the running program d. Take a screen shot from an area of the screen

a

What does the following Python code print out? a = [1, 2, 3] b = [4, 5, 6] c = a + b print(len(c)) a. 6 b. [1, 2, 3] c. [4, 5, 6] d. [1, 2, 3, 4, 5, 6] e. 15 f. 21

a

What does the following Python code print out? print(len('banana')*7) a. 42 b. banana7 c. -1 d. banana banana banana banana banana banana banana

a

What does the following Python code print out? (Note that this is a bit of a trick question and the code has what many would consider to be a flaw/bug - so read carefully). def addtwo(a, b): added = a + b return a x = addtwo(2, 7) print(x) a. 2 b. 9 c. 14 d. 7

a

What does the following code print out? print("123" + "abc") a. 123abc b. hello world c. 123+abc d. This is a syntax error because you cannot add strings

a

What is the best way to think about a "Syntax Error" while programming? a. The computer did not understand the statement that you entered b. The computer needs to have its software upgraded c. The computer is overheating and just wants you to stop to let it cool down d. The computer has used GPS to find your location and hates everyone from your town

a

What is the difference between a Python tuple and Python list? a. Lists are mutable and tuples are not mutable b. Lists are indexed by integers and tuples are indexed by strings c. Tuples can be expanded after they are created and lists cannot d. Lists maintain the order of the items and tuples do not maintain order

a

What is the iteration variable in the following Python code: friends = ['Joseph', 'Glenn', 'Sally'] for friend in friends : print('Happy New Year:', friend) print('Done!') a. friend b. Glenn c. Sally d. friends

a

What is the iteration variable in the following Python code? for letter in 'banana' : print(letter) a. letter b. for c. 'banana' d. print e. in

a

What is the purpose of the second parameter of the get() method for Python dictionaries? a. To provide a default value if the key is not found b. The value to retrieve c. The key to retrieve d. An alternate key to use if the first key cannot be found

a

What is true about the following code segment: if x == 5 : print('Is 5') print('Is Still 5') print('Third 5') a. Depending on the value of x, either all three of the print statements will execute or none of the statements will execute b. The string 'Is 5' will always print out regardless of the value for x. c. The string 'Is 5' will never print out regardless of the value for x. d. Only two of the three print statements will print out if the value of x is less than zero.

a

What is wrong with this Python loop: n = 5 while n > 0 : print(n) print('All done') a. This loop will run forever b. The print('All done') statement should be indented four spaces c. There should be no colon on the while statement d. while is not a Python reserved word

a

What will be the value of x when the following statement is executed: x = int(98.6) a. 98 b. 6 c. 100 d. 99

a

What will the following Python code print out? friends = [ 'Joseph', 'Glenn', 'Sally' ] friends.sort() print(friends[0]) a. Glenn b. friends c. Sally d. Joseph

a

What will the following Python program print out? def greet(lang): if lang == 'es': return 'Hola' elif lang == 'fr': return 'Bonjour' else: return 'Hello' print(greet('fr'),'Michael') a. Bonjour Michael b. def Hola Bonjour Hello Michael c. Hola Michael d. Hola Bonjour Hello Michael

a

What will the following code print out? smallest_so_far = -1 for the_num in [9, 41, 12, 3, 74, 15] : if the_num < smallest_so_far : smallest_so_far = the_num print(smallest_so_far) Hint: This is a trick question and most would say this code has a bug - so read carefully a. -1 b. 74 c. 42 d. 3

a

What will the following program print out: >>> x = 15 >>> x = x + 5 >>> print(x) a. 20 b. 5 c. 15 d. "print x" e. x + 5

a

When you have multiple lines in an if block, how do you indicate the end of the if block? a. You de-indent the next line past the if block to the same level of indent as the original if statement b. You use a curly brace { after the last line of the if block c. You put the colon : character on a line by itself to indicate we are done with the if block d. You omit the semicolon ; on the last line of the if block

a

Which of the following is not a Python reserved word? a. speed b. else c. for d. if

a

Which of the following is not a valid string method in Python? a. boldface() b. upper() c. lower() d. lstrip() e. startswith()

a

Which of the following methods work both in Python lists and Python tuples? a. index() b. append() c. pop() d. sort() e. reverse()

a

Which of the following tuples is greater than x in the following Python sequence? x = (5, 1, 3) if ??? > x : ... a. (6, 0, 0) b. (4, 100, 200) c. (5, 0, 300) d. (0, 1000, 2000)

a

Which of these words are reserved words in Python ? a. if b. concat c. todo d. make e. break

a e

Given the architecture and terminology we introduced in Chapter 1, where are files stored? a. Main Memory b. Secondary memory c. Motherboard d. Central Processor

b

How many times will the body of the following loop be executed? n = 0 while n > 0 : print('Lather') print('Rinse') print('Dry off!') a. 5 b. 0 c. This is an infinite loop d. 1

b

In the following Python code, which of the following is an "argument" to a function? x = 'banana' y = max(x) print(y) a. max b. x c. print d. 'banana'

b

Python scripts (files) have names that end with: a. .exe b. .py c. .doc d. .png

b

What does the continue statement do? a. Exits the program b. Jumps to the "top" of the loop and starts the next iteration c. Resets the iteration variable to its initial value d. Exits the currently executing loop

b

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

b

What is a good statement to describe the is operator as used in the following if statement: if smallest is None : smallest = value a. The if statement is a syntax error b. matches both type and value c. Looks up 'None' in the smallest variable if it is a string d. Is true if the smallest variable has a value of -1

b

What is a term commonly used to describe the Python dictionary feature in other programming languages? a. Closures b. Associative arrays c. Sequences d. Lambdas

b

What is the proper way to say "good-bye" to Python? a. #EXIT b. quit() c. while d. // stop

b

What is the purpose of the following Python code? fhand = open('mbox.txt') x = 0 for line in fhand: x = x + 1 print(x) a. Reverse the order of the lines in mbox.txt b. Count the lines in the file 'mbox.txt' c. Remove the leading and trailing spaces from each line in mbox.txt d. Convert the lines in mbox.txt to upper case

b

Which Python keyword indicates the start of a function definition? a. sweet b. def c. continue d. return

b

Which of the following Python statements would print out the length of a list stored in the variable data? a. print(strlen(data)) b. print(len(data)) c. print(data.length) d. print(length(data)) e. print(data.length()) f. print(data.Len)

b

Which of the following is a bad Python variable name? a. spam_23 b. #spam c. SPAM23 d. _spam

b

A USB memory stick is an example of which of the following components of computer architecture? a. Central Processing Unit b. Main Memory c. Secondary Memory d. Output Device

c

How are "collection" variables different from normal variables? a. Collection variables merge streams of output into a single stream b. Collection variables can only store a single value c. Collection variables can store multiple values in a single variable d. Collection variables pull multiple network documents together

c

If we open a file as follows: xfile = open('mbox.txt') What statement would we use to read the file one line at a time? a. READ (xfile,*,END=10) line b. while ( getline (xfile,line) ) { c. for line in xfile: d. while (<xfile>) {

c

In Python, how do you indicate the end of the block of code that makes up the function? a. You add the matching curly brace that was used to start the function } b. You put the "END" keyword in column 7 of the line which is to be the last line of the function c. You de-indent a line of code to the same indent level as the def keyword d. You put a # character at the end of the last line of the function

c

The 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 = input('Enter the file name: ') fhand = open(fname) a. signal handlers b. setjmp / longjmp c. try / except d. try / catch / finally

c

What does the break statement do? 1 point a. Exits the program b. Jumps to the "top" of the loop and starts the next iteration c. Exits the currently executing loop d. Resets the iteration variable to its initial value

c

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

c

What does the following Python program print out? tot = 0 for i in [5, 4, 3, 2, 1] : tot = tot + 1 print(tot) a. 0 b. 10 c. 5 d. 15

c

What is stored in a "file handle" that is returned from a successful open() call? a. The handle contains the first 10 lines of a file b. The handle has a list of all of the files in a particular folder on the hard drive c. The handle is a connection to the file's data d. All the data from the file is read into memory and stored in the handle

c

What is the Python reserved word that we use in two-way if tests to indicate the block of code that is to be executed if the logical test is false? a. iterate b. toggle c. else d. otherwise

c

What is the most important benefit of writing your own functions? a. To avoid having more than 10 lines of sequential code without an indent or de-indent b. Following the rule that no function can have more than 10 statements in it c. Avoiding writing the same non-trivial code more than once in your program d. Following the rule that whenever a program is more than 10 lines you must use a function

c

What is the value of the following expression 42 % 10 Hint - the "%" is the remainder operator a. 1042 b. 10 c. 2 d. 420

c

What type of data is produced when you call the range() function? x = list(range(5)) a. A list of characters b. A string c. A list of integers d. A boolean (true/false) value e. A list of words

c

What will the following code print out? x = 0 if x < 2 : print('Small') elif x < 10 : print('Medium') else : print('LARGE') print('All done') a. LARGE All done b. Small c. Small All done d. Small Medium LARGE All done

c

What would the following Python code print out? stuff = dict() print(stuff.get('candy',-1)) a. 0 b. The program would fail with a traceback c. -1 d. 'candy'

c

What would the following Python code print out? stuff = dict() print(stuff['candy']) a. -1 b. candy c. The program would fail with a traceback d. 0

c

Which method in a dictionary object gives you a list of the values in the dictionary? a. items() b. keys() c. values() d. all() e. each()

c

Which of the parts of a computer actually executes the program instructions? a. Main Memory b. Input/Output Devices c. Central Processing Unit d. Secondary Memory

c

Which reserved word indicates the start of an "indefinite" loop in Python? a. indef b. for c. while d. def e. break

c

You look at the following text: if x == 6 : print('Is 6') print('Is Still 6') print('Third 6') It looks perfect but Python is giving you an 'Indentation Error' on the second print statement. What is the most likely reason? a. In order to make humans feel inadequate, Python randomly emits 'Indentation Errors' on perfectly good code - after about an hour the error will just go away without any changes to your program b. Python has reached its limit on the largest Python program that can be run c. You have mixed tabs and spaces in the file d. Python thinks 'Still' is a mis-spelled word in the string

c

For the following code: astr = 'Hello Bob' istr = 0 try: istr = int(astr) except: istr = -1 What will the value be for istr after this code executes? a. false b.It depends on the position in the collating sequence for the letter 'H' c. 9 (the number of characters in 'Hello Bob') d. -1

d

For the following list, how would you print out 'Sally'? friends = [ 'Joseph', 'Glenn', 'Sally' ] a. print friends[3] b. print(friends[2:1]) c. print(friends['Sally']) d. print(friends[2])

d

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

d

How are Python dictionaries different from Python lists? a. Python lists store multiple values and dictionaries store a single value b. Python lists can store strings and dictionaries can only store words c. Python dictionaries are a collection and lists are not a collection d. Python lists are indexed using integers and dictionaries can use strings as indexes

d

If the variable data is a Python list, how do we sort it in reverse order? a. data = data.sort(-1) b. data = sortrev(data) c. data.sort.reverse() d. data.sort(reverse=True)

d

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

d

In the following Python, what does the for loop iterate through? x = dict() ... for y in x : ... a. It loops through the values in the dictionary b. It loops through all of the dictionaries in the program c. It loops through the integers in the range from zero through the length of the dictionary d. It loops through the keys in the dictionary

d

In the following code, print(98.6) What is "98.6"? a. A variable b. A conditional statement c. An iteration / loop statement d. A constant

d

Question 9 What will the following Python code print out? data = 'From [email protected] Sat Jan 5 09:14:16 2008' pos = data.find('.') print(data[pos:pos+3]) a. 09:14 b. uct c. mar d. .ma

d

What do we do to a Python statement that is immediately after an if statement to indicate that the statement is to be executed only when the if statement is true? a. Start the statement with a "#" character b. Underline all of the conditional code c. Begin the statement with a curly brace { d. Indent the line below the if statement

d

What does the following Python Program print out? str1 = "Hello" str2 = 'there' bob = str1 + str2 print(bob) a. Hello there b. Hello c. Hello there d. Hellothere

d

What does the following Python program print out? x = '40' y = int(x) + 2 print(y) a. int402 b. 402 c. x2 d. 42

d

What does the following code print out? def thing(): print('Hello') print('There') a. thing b. thing Hello There c. def thing d. There

d

What is "code" in the context of this course? a. A set of rules that govern the style of programs b. A password we use to unlock Python features c. A way to encrypt data during World War II d. A sequence of instructions in a programming language

d

What is a common use of Python dictionaries in a program? a. Splitting a line of input into words using a space as a delimiter b. Sorting a list of names into alphabetical order c. Computing an average of a set of numbers d. Building a histogram counting the occurrences of various strings in a file

d

What is a good description of the following bit of Python code? zork = 0 for thing in [9, 41, 12, 3, 74, 15] : zork = zork + thing print('After', zork) a. Find the smallest item in a list b. Find the largest item in a list c. Count all of the elements in a list d. Sum all the elements of a list

d

What is the purpose of the newline character in text files? a. It adds a new network connection to retrieve files from the network b. It allows us to open more than one files and read them in a synchronized manner c. It enables random movement throughout the file d. It indicates the end of one line of text and the beginning of another line of text

d

What list method adds a new item to the end of an existing list? a. forward() b. pop() c. index() d. append() e. push() f. add()

d

What will be the value of x after the following statement executes: x = 1 + 2 * 3 - 8 / 4 a. 2.0 b. 8 c. 4 d. 5.0

d

What will the following Python code print out? def func(x) : print(x) func(10) func(20) a. def x func func b. x 20 c. x x d. 10 20

d

What would the following Python code print out? fruit = 'Banana' fruit[0] = 'b' print(fruit) a. Banana b. banana c. [0] d. Nothing would print - the program fails with a traceback error e. B f. b

d

When Python is running in the interactive mode and displaying the chevron prompt (>>>) - what question is Python asking you? a. What Python script would you like me to run? b. What is your favourite color? c. What is the next machine language instruction to run? d. What Python statement would you like me to run?

d

Which of the following elements of a mathematical expression in Python is evaluated first? a. Multiplication * b. Subtraction - c. Addition + d. Parentheses ( )

d

Which of the following is not one of the programming patterns covered in Chapter 1? a. Sequential Steps b. Conditional Steps c. Repeated Steps d. Random steps

d

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 a. counts[key] = counts.get(key,-1) + 1 b. counts[key] = key + 1 c. counts[key] = (counts[key] * 1) + 1 d. counts[key] = counts.get(key,0) + 1 e. counts[key] = (key in counts) + 1

d

Which of the following string methods removes whitespace from both the beginning and end of a string? a. wsrem() b. split() c. strtrunc() d. strip()

d

n the following code (numbers added) - which will be the last line to execute successfully? (1) astr = 'Hello Bob' (2) istr = int(astr) (3) print('First', istr) (4) astr = '123' (5) istr = int(astr) (6) print('Second', istr) a. 3 b. 2 c. 6 d. 1

d

What will end up in the variable y after this code is executed? x , y = 3, 4 a. A two item tuple b. 3 c. A dictionary with the key 3 mapped to the value 4 d. A two item list e. 4

e

Which line of the following Python program will never execute? def stuff(): print('Hello') return print('World') stuff() a. print('Hello') b. return c. def stuff(): d. stuff() e. print ('World')

e

Which of the following slicing operations will produce the list [12, 3]? t = [9, 41, 12, 3, 74, 15] a. t[1:3] b. t[:] c. t[12:3] d. t[2:2] e. t[2:4]

e

Which of these operators is not a comparison / logical operator? a. >= b. != c. == d. <= e. =

e

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

f


Conjuntos de estudio relacionados

Genetics Exam 2 (Chapter 8)1. List the different types of chromosome mutations and define each one.

View Set

World History Final Superstitions

View Set

AD BANKER AL P&C CH 12 Misc. Commercial Policies

View Set

Chapter 4: Taxes, Retirement, and Other Insurance concepts

View Set

CIW Exam Guide - Lesson 3: Functions, Methods and Events in JavaScript & Lesson 4: Debugging and Troubleshooting JavaScript

View Set

Chapter 39 oxygenation and perfusion

View Set