python

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Which of the following is a bad Python variable name?

#spam

Which of the following tuples is greater than x in the following Python sequence? x = (5, 1, 3) if ??? > x : ...

(6, 0, 0)

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?

-1

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)

-1

What would the following Python code print out? stuff = dict() print(stuff.get('candy',-1))

-1 not The program would fail with a traceback

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])

.ma

Python scripts (files) have names that end with:

.py

In 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)

1

What will the following Python code print out? ef func(x) : print(x) func(10) func(20)

10 20

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)

2

What is the value of the following expression: 42 % 10 Hint - the "%" is the remainder operator

2 the remainder since 4*10=40

What will the following program print out: >>> x = 15 >>> x = x + 5 >>> print(x)

20

What will end up in the variable y after this code is executed? x , y = 3, 4

4

What does the following Python code print out? print(len('banana')*7)

42

What does the following Python program print out? x = '40' y = int(x) + 2 print(y)

42

What does the following Python program print out? tot = 0 for i in [5, 4, 3, 2, 1] : tot = tot + 1 print(tot)

5

What will be the value of x after the following statement executes: x = 1 + 2 * 3 - 8 / 4

5.0

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

6

What will be the value of x when the following statement is executed: x = int(98.6)

98

Which of these operators is not a comparison / logical operator?

=

In the following code, print(98.6) What is 98.6?

A constant

What type of data is produced when you call the range() function? x = range(5)

A list of integers

In the following Python code, what will end up in the variable y? x = { 'chuck' : 1 , 'fred' : 42, 'jan': 100} y = x.items()

A list of tuples

What is "code" in the context of this course?

A sequence of instructions in a programming language

What is a term commonly used to describe the Python dictionary feature in other programming languages?

Associative arrays

What is the most important benefit of writing your own functions?

Avoiding writing the same non-trivial code more than once in your program

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

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')

Bonjour Michael

What is a common use of Python dictionaries in a program?

Building a histogram counting the occurrences of various strings in a file

Which of the parts of a computer actually executes the program instructions?

Central Processing Unit (CPU)

How are "collection" variables different from normal variables?

Collection variables can store multiple values in a single variable

What is the purpose of the following Python code? fhand = open('mbox.txt') x = 0 for line in fhand: x = x + 1 print(x)

Count the lines in the file 'mbox.txt'

What is true about the following code segment: if x == 5 : print('Is 5') print('Is Still 5') print('Third 5')

Depending on the value of x, either all three of the print statements will execute or none of the statements will execute

What does the break statement do?

Exits the currently executing loop

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

False

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

Glenn

What does the following Python Program print out? str1 = "Hello" str2 = 'there' bob = str1 + str2 print(bob)

Hellothere

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?

Indent the line below the if statement

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) )

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

What is the purpose of the newline character in text files?

It indicates the end of one line of text and the beginning of another line of text

In the following Python, what does the for loop iterate through? x = dict() ... for y in x : ...

It loops through the keys in the dictionary

What does the continue statement do?

Jumps to the "top" of the loop and starts the next iteration

What would the following Python code print out? fruit = 'Banana' fruit[0] = 'b' print(fruit)

Nothing would print - the program fails with a traceback

Which of the following elements of a mathematical expression in Python is evaluated first?

Parentheses ( )

What does the Python input() function do?

Pause the program and read data from the user

How are Python dictionaries different from Python lists?

Python lists are indexed using integers and dictionaries can use strings as indexes

Which of the following is not one of the programming patterns covered in Chapter 1?

Random steps

What does the following Python code do? fhand = open('mbox-short.txt') inp = fhand.read()

Reads the entire file into the variable inp as a string

Assume the variable x has been initialized to an integer value (e.g., x = 3). What does the following statement do? x = x + 2

Retrieve the current value for x, add two to it and put the sum back into x

A USB memory stick is an example of which of the following components of computer architecture?

Secondary Memory

Given the architecture and terminology we introduced in Chapter 1, where are files stored?

Secondary memory

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')

Small All done

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)

Sum all the elements of a list

What is the best way to think about a "Syntax Error" while programming?

The computer did not understand the statement that you entered

What is stored in a "file handle" that is returned from a successful open() call?

The handle is a connection to the file's data

What would the following Python code print out? stuff = dict() print(stuff['candy'])

The program would fail with a traceback notcandy

What does the following code print out? def thing(): print('Hello') print('There')

There

For the following code, if x < 2 : print('Below 2') elif x >= 2 : print('Two or more') else : print('Something else')

This code will never print 'Something else' regardless of the value for 'x'

What is wrong with this Python loop: n = 5 while n > 0 : print(n) print('All done')

This loop will run forever

What is the purpose of the second parameter of the get() method for Python dictionaries?

To provide a default value if the key is not found

When Python is running in the interactive mode and displaying the chevron prompt (>>>) - what question is Python asking you?

What would you like to do?

What do we use the second parameter of the open() call to indicate?

Whether we want to read data from the file or write data to the file

In Python, how do you indicate the end of the block of code that makes up the function?

You de-indent a line of code to the same indent level as the def keyword

When you have multiple lines in an if block, how do you indicate the end of the if block?

You de-indent the next line past the if block to the same level of indent as the original if statement

You look at the following text: if x == 6 : print('Is 6') print('Is Still 6') print('Third 6')

You have mixed tabs and spaces in the file (it needs to be three spaces not tab depending on your texteditor, atom will atomatically convert tab to the proper spaces.)

In Python what is the input() feature best described as?

a built in function

In the following code, x=42 What is x?

a variable

What list method adds a new item to the end of an existing list?

append()

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

If the variable data is a Python list, how do we sort it in reverse order?

data.sort(reverse=True)

Which Python keyword indicates the start of a function definition?

def

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?

else

What are the Python keywords used to construct a loop to iterate through a list?

for / in

If we open a file as follows: xfile = open('mbox.txt')

for line in xfile:

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!')

friend

Which of these words are reserved words in Python ?

if, break, while

Which of the following methods work both in Python lists and Python tuples?

index()

What Python function would you use if you wanted to prompt the user for a file name to open?

input( )

What is the iteration variable in the following Python code? for letter in 'banana' : print(letter)

letter

What is the difference between a Python tuple and Python list?

lists are mutable, tuples are immutable

What is a good statement to describe the is operator as used in the following if statement: if smallest is None : smallest = value

not Looks up 'None' in the smallest variable if it is a string orifThe if statement is a syntax error

How many times will the body of the following loop be executed? n = 0 while n > 0 : print('Lather') print('Rinse') print('Dry off!')

not this is an indefinite loop

Which line of the following Python program will never execute? def stuff(): print('Hello') return print('World') stuff()

print('World')

Using the following tuple, how would you print 'Wed'? days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')

print(days[2])

For the following list, how would you print out 'Sally'? friends = [ 'Joseph', 'Glenn', 'Sally' ]

print(friends[2])

How would you print out the following variable in all upper case in Python? greet = 'Hello Bob'

print(greet.upper())

Which of the following Python statements would print out the length of a list stored in the variable data?

print(len(data))

How would you use string slicing [:] to print out 'uct' from the following string? x = 'From [email protected]'

print(x[14:17])

How would you use the index operator [ ] to print out the letter q from the following string? x = 'From [email protected]'

print(x[8])

What is the proper way to say "good-bye" to Python?

quit()

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] ...

rstrip( )

Which of the following is not a Python reserved word?

speed, spam

Which of the following string methods removes whitespace from both the beginning and end of a string?

strip()

Which of the following slicing operations will produce the list [12, 3]? t = [9, 41, 12, 3, 74, 15]

t[2:4]

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)

try / except

Which of the following is not a valid string method in Python?

twist() shout()

Which method in a dictionary object gives you a list of the values in the dictionary?

values()

Which reserved word indicates the start of an "indefinite" loop in Python?

while

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

x


संबंधित स्टडी सेट्स

Western Europe in Early Middle Ages Review

View Set

Chapter 20 SB: Global NutritionAssignment

View Set

Spinal Cord Injury Review Questions

View Set

Chapter 2 - Operating System Overview (Review Questions)

View Set