Practice writing

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

Which of the following is not true of file access codes? "w" - opens a file for writing, generates an error if the file cannot be found "r" - open a file for reading, generates an error if the file cannot be found "w" - opens a file for writing, overwrites existing data in the file None of the above

"w" - opens a file for writing, generates an error if the file cannot be found

Which of the following are true concerning file access code? "w"means write to a file, adding to the contents that were already there "a" means append to a file, overwriting the contents that were already there "w" means write to a file, overwriting the contents that were already there "a" means append to a file, adding no the contents that were already there None of the above

"w" means write to a file, overwriting the contents that were already there "a" means append to a file, adding no the contents that were already there

File access modes in Python include (check all that apply): 'a' for append 'u' for update 'r' for read 'w' for write 'd' for delete All the above

'a' for append 'r' for read 'w' for write

What is the outcome of the following code? words = ['jazz', 'rock', 'country'] for i in range(len(words)): print(words[i]) 'jazz', 'rock', 'country' 0, 1, 2 'jazz', 'rock' 1,2,3

'jazz', 'rock', 'country'

What will be the outcome of the following code? i = 0 while i < 5: print(i) i += 1 0,1,2,3,4,5 0,1,2,3,4 0,1,2,3 0,1,2

0,1,2,3,4

What is the outcome of the following code? for i in range(10): print(i,end = ',') 1,2,3,4... 10 0,1,2,3,4...10 0,1,2,3...9 0,1,2,3, ... 11

0,1,2,3...9

What is the execution order of the following code block ? try: print('1' + 2) except: print('Invalid') else: print('Valid') finally: print('Thanks') #unindent part of code

1. try 2. except 3. finally 4.unindent

What is the outcome of the following code where data = 50? Write exact outcome. try: data = data/5 except: print('Invalid') else: print(data)

10

What is the outcome of the following code? for i in range(2, 10, 2): print(i, end = ',') 2,3,4,... 10 2,4,6,...10 2,4,6,8 1,2,3,4,...100

2,4,6,8

What is the output displayed by the following code? def addNums (a, b, c = 7): s = a + b + c + d if s > 25: return s, 'big' else: return s, 'small' d = 9 print (addNums (5, 10)) 22 small 24 small 54 big 31 big

31 big

What is the output displayed by the following code? def addNums (a, b, c = 7): s = a + b + c + d if s > 25: return s, 'big' else: return s, 'small' d = 10 print (addNums(5, 10, c = 20)) 22 small 25 small 45 big none of the above

45 big

How many times the following print statement will execute? for i in range(2): for j in range(3): print('Hi')

6

What will the following code display? def addnums (a = 3, b = 4): s = a + b return s result = addnums ( ) print (result) nothing (None) 3 4 7

7

What will the following code display? def addnums (a = 3, b = 4): s = a + b return s result = addnums (b = 6, a = 2) print (result) 8 7 9 None of the above

8

What will the following code display? def sumNums (a, b, c): d = a + b return d, c x, y = sumNums (3, 5, 7) print (x, y) 8, 7 15, 7 3, 12 None of the above

8, 7

What will the following code display? def sumNums (a, b, c): if a > b: return a + b elif b > c: return b + c else: return a + b + c x = sumNums (1, 3, 5) print (x) 4 5 9 None of the above

9

How to find full name of 'CA' from the given dictionary? states = {'VA': 'Virginia', 'CA': 'California', 'WV': 'West Virginia'} >>> states['California'] >>> states[1] >>> states['CA'] >>> states['VA']

>>> states['CA']

Which is true about function parameters and return statements? A function has at least one parameter and one return statement A function has zero or more parameters and at least one return statement A function has zero or more parameters and zero or more return statements A function has at least one parameter and zero or more return statements

A function has zero or more parameters and zero or more return statements

How to prepare your code for testing? Organize code into modules, functions Code runs before its ready prepare set of expected results All of the above

All of the above

How to remove an item from the dictionary? del d[key] d.pop(key) d.pop(key, 'Not found') All of the above

All of the above

Which of the following is true about code testing? Compare a set of pre-calculated value with program generated value Testing is done to validate the code Your code needs to run before you are ready to test All of the above

All of the above

What will the following code display? def displayreceipt(items): " " " Display a list of input items and their price in a receipt-format column. " " " for i in items: print(i[0], '\t', i[1]) help(displayreceipt) a column of items and their prices Display a list of input items and their price in a receipt-format column. all the above none of the above

Display a list of input items and their price in a receipt-format column

What is the resolution of the following expression? True and False

False

When you open a file with access code "w" and begin writing records, the data is immediately written to the file and can be viewed using Notepad or other software tool. True False

False

What does the following code generate? print('Hello there') for x in range(10): print(x) SyntaxError TypeError IndentationError None of the above

IndentationError

What is the outcome of the following code? try: print(10/0) except ZeroDivisionError: print('Invalid') else: print('Valid') Valid Invalid Raise an error None of the above

Invalid

Suppose I have a dictionary d = {2: 'two', 3: 'Three'} If I try to access d[0] what will be the outcome? Key Error: 0 Index Error (2: 'two') 'Zero'

Key Error: 0

What will be the outcome of the code? if 1!=2: print('Label') elif 2!=1: print('Button') else: print('Entry') Label Button Entry Label and then Button

Label

What's the outcome of the following code? if 1 > 10: print('Button') elif 10 != 9: print ('Label') elif 2 > 6: print('Entry') else: print('Radio Button') Button Label Entry Radio Button

Label

When you issue this file open: fp = open('infile.txt', 'w') and then do the following operations: fp.write('Line 1') fp.write('Line 2') fp.write('Line 3') What will the contents of 'infile.txt' be when viewing it using Notepad? a. Line 3 b. Line 1 Line2 Line3 c. Line 1Line 2Line 3 d. Line 3 Line 2 Line 1 e. None of the above

Line 1Line 2Line 3

Fill in the blank) What is the outcome of the following code? Write OK if test pass otherwise write Fail. def fun(a): ''' >>> fun(2) 4 >>> fun(3) 9 ''' return a*a

OK

What is the outcome of the following code? s = 'HELLO' for i in range(len(s)-1, - 1, -1): print(s[i]) HELLO OLLEH OLLEHOLLEH HELL

OLLEH

What is the outcome of the following code? try: print(10/0) except TypeError: print('Invalid') else: print('Valid') invalid valid Python raises an error None of the above

Python raises an error

Python has two types of errors. They are ______ and _____. Syntax and Exception Syntax and Type error Exception and while loop Syntax error and Account not found error

Syntax and Exception

(True/False) List comprehension is faster than regular list creation.

True

Breakpoint is an intentional pause in to debug certain part of a large code. True False

True

Python considers data that is stored in files to be "off-line" True False

True

Reading data from or writing data to a file are generally referred to as "I/O", which stands for Input/Output True False

True

You need at least on except block with a try block True False

True

What is the content of the following list 'numbers'? numbers = [x**2 for x in range(4)] a. [0,1,4,9] b. [0] c. [0,1,4,9,16]

[0,1,4,9]

What will be the outcome of the following code? >>> d = {1: 'One', 2: 'Two', 3: 'Three'} >>> print(d.keys()) ['One', 'Two', 'Three'] [1,2,3] [(1, 'One'), (2, 'Two'), (3, 'Three')] (1,2,3,4)

[1,2,3]

Given: thislist = ["apple", "banana", "cherry"] Write a program with short hand for loop that will print all items in a list and get the output: apple banana cherry

[print(x) for x in thislist]

A function paramater: is a way to pass a value from a calling program to a function can be a variable, literal, or data structure is specified in parentheses on the same line as the function definition all the above

all the above

What does the values() method return? all the values of the dictionary all the keys of the dictionary all the items of the dictionary all the key-value pairs of the dictionary

all the values of the dictionary

Given: a = "Hello" b="World" Write a program that will get the output: Hello World

c = a + " " + b print(c)

Given: a = "Hello" b="World" Write a program that will get the output: HelloWorld

c = a + b print(c)

Which is the correct syntax to define a dictionary in python? d = (1:1, 2:2, 3:3) d = {1:1, 2:2, 3:3} d = [1:1, 2:2, 3:3] All of the above

d = {1:1, 2:2, 3:3}

How to add a key-value pair to the dictionary? d[key] = value d.append(key, value) d.add(key, value) d [value] = key

d[key] = value

Which of the following is a correct definition of a function called 'addnums'? function addnums ( ): addnums def def: addnums( ) def addnums ( ): addnums def ( ):

def addnums ( ):

Given: thislist = ["apple", "banana", "cherry"] Write a program that will get the output: ["banana", "cherry"]

del thislist[0] print(thislist)

nested_list = ['string', {1:1, 2:2}, [1,2,3]] What is the data type of nested_list[1]? Write exact answer.

dict or dictionary or dic

Write exact syntax to delete a breakpoint from debugging environment. Let's assume the breakpoint number is 1.

disable 1

Which of the following are correct formats of the Python 'open' statement (check all that apply)? f = open(infile, "r") f = open("C:/Data/datafile.txt") f = open("C:/Data/datafile.txt", "w") infile = "C:/Data/datafile.txt" f = open(infile, "a")

f = open ("C:/Data/datafile.txt", "r") f = open("C:/Data/datafile.txt") f = open("C:/Data/datafile.txt", "w") f = open(infile, "a")

Given: thislist = ["apple", "banana", "cherry"] The iterable created in the example above is [0, 1, 2]. Write a program that will loop through the list items by using the index number; use range( ) and len( ) and get the output: apple banana cherry

for i in range(len(thislist)): print(thislist[i])

What is the most suitable type of loop can be used for the following pseudocode? define a list of numbers define a result list to be empty repeat until end of the list take number from numbers list multiply with 2 add to result list while loop for loop no looping is needed control structure

for loop

Write a For Loop program to get an output: b a n a n a

for x in "banana": print(x)

Given: thislist = ["apple", "banana", "cherry"] Write a program that will loop and print one by one and get the output: apple banana cherry

for x in thislist: print(x)

Given: thislist = ["apple", "banana", "cherry"] Write a program with while loop through the index numbers and get the output: apple banana cherry

i = 0 while i < len(thislist): print(thislist[i]) i = i + 1

What is the import statement to import doctest into current module? Write the exact syntax.

import doctest or from doctest import *

Write exact syntax in python to import the pdb debugger in current module

import pdb or from pdb import *

Which of the following are true about a Python function (check all that apply): it is a portion of Python code that can only be executed once in a program it is a portion of code that must be defined before being used after it is defined it can be used multiple times in a program it only applies to Python code in a built-in or external library

it is a portion of code that must be defined before being used after it is defined it can be used multiple times in a program

Which of the following is the command to list lines in a debugger? l m n s

l

For the variables in the following code, which namespaces (scope) do they belong to? def addNums (a, b): return a + b + z x = 5 y = 10 z = 15 addNums (x, y) local namespace: a and b; global namespace: x, y, z local namespace: a, b, and z; global namespace: x and y global namespace: a and b; local namespace: x, y, and z none of the above

local namespace: a and b; global namespace: x, y, z

Which of the following reads a comma-separated line of text from a text file, opened with symbol "tf", into a list of strings with variable name 'los' in which each comma-separated datum is a separate string in 'los'? tf = los.readline( ).split (',') los = tf.readline( ).split (',') los = tf.readline.split( ) tf = readline(los).split (',') None of the above

los = tf.readline( ).split (',')

How to update the value of key 'G123' to 'Adam'? Write exact syntax. my_list = ['STRING', {'G123': 'Bob', 'G444': 'Alan'}]

my_list[1]['G123'] = 'Adam'

Given: quantity = 3 itemnum = 567 price = 49.95 Write a program that will get the output: I want 3 pieces of item 567 for 49.95 dollars.

myorder = "I want {} pieces of item {} for {} dollars." print(myorder.format(quantity, itemnum, price))

Given: fruits = ["apple", "banana", "cherry", "kiwi", "mango"] Write a program that will get the output: ['apple', 'banana', 'mango']

newlist = [ ] for x in fruits: if "a" in x: newlist.append(x) print(newlist)

Given: fruits = ["apple", "banana", "cherry", "kiwi", "mango"] Write a program to set all values in the new list to 'hello'

newlist = ['hello' for x in fruits] print(newlist)

Given: fruits = ["apple", "banana", "cherry", "kiwi", "mango"] Based on a list of fruits, you want a new list, containing only the fruits with the letter "a" in the name. Write a program with list comprehension to write one line of code and get the output: ['apple', 'banana', 'mango']

newlist = [x for x in fruits if "a" in x] print(newlist)

Given: fruits = ["apple", "banana", "cherry", "kiwi", "mango"] Write a program to Return "orange" instead of "banana"

newlist = [x if x != "banana" else "orange" for x in fruits]

Given a 2D list: numbers = [['1,2,3], ['A', 'B', 'C']] How to access 'B'? Write exact answer using proper index notation

numbers[1][1] or numbers[-1][-2] or numbers[-1][1] or numbers[1][-2]

The basic steps in file processing are: read, process, open, close open, close,process, read open, read, process, close process, open, close, read None of the above

open, close, process, read

Which of the following are types of parameters (check all that apply)? keyword or default implicit positional visual keyword or default All the above

positional keyword or default

Given: txt = "The best things in life are free!" Write a program that will get the output: False

print("expensive" not in txt)

Given: txt = "The best things in life are free!" Write a program that will get the output: True

print("free" in txt)

Given: a = "Hello, World!" Write a program that will get the output: hello, world!

print(a.lower())

Given: a = "Hello, World!" Write a program that will get the output: Jello, World!

print(a.replace("H", "J"))

Given: a = "Hello, World!" Write a program that will get the output: ['Hello', ' World!']

print(a.split(","))

Given: a = "Hello, World!" Write a program that will remove white space and get the output: Hello, World!

print(a.strip())

Given: a = "Hello, World!" Write a program that will get the output: HELLO, WORLD!

print(a.upper())

Given : a = "Hello, World!" Write a program to get an output: e

print(a[1])

Given: b = "Hello, World!" Write a program that will slice from the end and get the output: orl

print(b[-5:-2])

Given: b = "Hello, World!" Write a program that will get the output: llo

print(b[2:5])

Given: b = "Hello, World!" Write a program that will get the output: llo, World!

print(b[2:])

Given: b = "Hello, World!" Write a program that will get the output: Hello

print(b[:5])

Given : a = "Hello, World!" Write a program that will get the output: 13

print(len(a))

Which of the following is used to access inside a function? l n s none of the above

s

What is list comprehension?

shorter syntax when you want to create a new list based on the values of an existing list.

Which of the following reads an entire text file, assigned symbol "tf", into a string variable "sv"? sv = tf.readline( ) tf = sv.readlines( ) sv = tf.read(all) sv = tf.read( ) None of the above

sv = tf.read( )

The area that contains variables a function controls is called: the functional area the local namespace the global scope the builtin namespace

the local namespace

Given: thislist = ["apple", "banana", "cherry"] Write a program that will get the output: [ ]

thislist.clear()print(thislist)

Given: thislist = ["apple", "banana", "cherry"] thistuple = ("kiwi", "orange") Write a program that will get the output: ["apple", "banana", "cherry", "kiwi", "orange" ]

thislist.extend(thistuple) print(thislist)

Given: thislist = ["apple", "banana", "cherry"] tropical = ["mango", "pineapple", "papaya"] Write a program that will get the output: ["apple", "banana", "cherry", "mango", "pineapple", "papaya"]

thislist.extend(tropical) print(thislist)

Given: thislist = ["apple", "banana", "cherry"] Write a program that will get the output: ['apple', 'banana', 'watermelon', 'cherry']

thislist.insert(2, "watermelon") print(thislist)

Given: thislist = ["apple", "banana", "cherry"] Write a program that will get the output: ["apple", "banana"]

thislist.pop() print(thislist)

Given: thislist = ["apple", "banana", "cherry"] Write a program that will get the output: ["apple", "cherry"]

thislist.pop(1) print(thislist)

Given: thislist = ["apple", "banana", "cherry"] Write a program using remove( ) that will get the output: ["apple", "cherry"]

thislist.remove("banana") print(thislist)

Given: thislist = ["orange", "mango", "kiwi", "pineapple", "banana"] Write a program to Sort the list alphabetically and get an output: ['banana', 'kiwi', 'mango', 'orange', 'pineapple']

thislist.sort() print(thislist)

Given: age = 36 Write a program that will concatenate a str and int to get the output: My name is John, and I am 36

txt = "My name is John, and I am {}" print(txt.format(age))


Conjuntos de estudio relacionados

Reading 27: Introduction to Corporate Governance

View Set

AZ900 Azure Fundamentals Practice Questions

View Set

Understanding Canadian Business Chapter 2

View Set

CH09: Nutrients Essential to Healthy Tissues

View Set