Final Exam

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

suppose listExample is ['h', 'e', 'l', 'l', 'o'], what is len(listExample)?

5

what is the output? aList = [14, 55, 4, 34, 2, -3, 76] index = 0 for i in range(1, len(aList)): -> if ( aList[i] < aList[index]): --> index = i print(index)

5

cnt = 0 while (cnt < 6): -> cnt = cnt + 1 print (cnt, end="")

6

num = 0 sum = 0 while (num < 4): -> sum = sum + num -> num = num + 1 print (sum)

6

which are the advantages of functions in Python? Choose the best answer. reducing duplication of code decomposing complex problems into simpler pieces improving clarity of the code all of the mentioned

all of the mentioned

when reading a file using the file object, which method reads the entire file into a single string?

infile.read()

to read the next line of the file from a file object infile, use___.

infile.readline()

which of the following will read in all the lines from a file at once? assume that the file has been opened into a File variable inputFile

inputFile.readlines()

which of the following statements create empty lists?

li = [] li = ()

which of the following statements insert 7 to the second position in list1?

list1.insert(1, 7)

which of the following statements removes the third element from list1?

list1.pop(2)

which of the following statements removes string "python" from list1?

list1.remove("python")

the following code contains an infinite loop. which is the best explanation for why the loop does not terminate? n = 10 answer = 1 while (n > 0): -> answer = answer + n -> n = n + 1 print(answer)

n starts at 10 and is incremented by 1 each time through the loop, so it will always be positive

what is the scope of a variable?

the range of statements in the code where a variable can be accessed

where is the error? sum the numbers from start to end, both ends being inclusive def main(): -> start = int(input()) -> end = int(input()) -> total = 0 -> for i in range(start, end, 1): --> total = total + i -> print (total) main()

the range should end at end+1, not end

which of the following statements are true regarding the opening modes of a file?

when you open a file for reading, if the file does not exist, an error occurs. when you open a file for writing, if the file does not exist, a new file is created.

which type of loop is more suitable when the number of input is unknown but the last value of the input is known?

while loop

which method should be called after a program is finished with writing a file?

.close()

when reading a file using the file object, what method is best for reading the entire file into a single string?

.read()

when reading a file using the file object, what method is best for reading one line of the file into a single string?

.readline()

what is the result of the following code? res = 0 i = 0 while ( i < 5 ): -> i += 1 -> res += i -> if (res > 6): --> break print (res)

10

what is the output? x = 5 def f1(): -> x = 7 -> print(x) f1() print(x)

7 5

what is printed by this code? n = 1 x = 2 while (n < 5): -> n = n + 1 -> x = x + 1 -> n = n + 2 -> x = x + n print(n, x)

7 15

what will be the output? list1 = [1, 2, 3, 4] list2 = [5, 6, 7, 8] print ( len(list1 + list2))

8

what is the output from the following code segment when the input is (1, 4, 2, 5, -2, 3) cnt = 0 t = 0 while (cnt < 6): -> num = int ( input () ) -> if ( ( num > 0 and ( num % 2 == 1) ): --> t = t + num -> cnt = cnt + 1 print ( t, end="")

9

what is the output? cnt = 0 while (cnt < 8): -> cnt = cnt + 3 print (cnt, end= "")

9

Whenever possible, which of the following ensure that a file object is properly closed after usage?

By using the with statement, and by using the try/finally block

what is a variable defined inside a function referred to as?

a local variable

blist = [alist] * 2 clist = alist * 2

blist = [[4, 2, 8, 6, 5], [4, 2, 8, 6, 5]] clist = [4, 2, 8, 6, 5, 4, 2, 8, 6, 5]

what are the two main types of functions?

built-in functions and user-defined functions

each pass through a loop is called a/an:

iteration

which of the following refer to the last element in a list 'names'?

names[len(names) -1] names[-1]

in a function call, passing the value of the argument to the parameter is referred to as ___.

pass by value

where is the error? sum the numbers from start to end, both ends being inclusive def main(): -> start = int(input()) -> end = int(input()) -> total = end -> for i in range(start, end, 0): --> total = total + i -> print (total) main()

the step in range should be 1, not 0

what is the output? nums = [3, 7, 2, 1] print(len(nums)) print(nums[-1]) print(nums[len(nums)])

there is an IndexError on line 4

what is the output? list1 = ['CS', 'Math', 'EE'] major1, major2, = list1

there is an error because the number of variables on the left hand side is different from the size of the list

a list can have duplicated elements. true or false?

true

given n is a positive integer, do the following two programs display the same? i = 0 while (i < n): -> print (i) -> i = i + 1 for i in range(0, n, 1): -> print(i) -> i = i - 1

true

true or false: you can rewrite any for-loop as a while-loop?

true

which of the following should be as a None function?

write a function that prints integers from 1 to 100

what is wrong with the following function definition: def addEm(x, y, z): -> return x + y + z -> print('the answer is', x + y + z)

you should not have any statements in a function after the return statement. once the function gets to the return statement it will immediately stop executing the function

what is the output of the following code? num = 5 for i in range (num, 15): -> num += i print (num)

100

suppose list1 is [2445, 133, 12454, 123], what is max(list1)?

12454

what is the output? res = 0 i = 0 while (i < 5): -> i += 1 -> res += i -> if (res > 6): --> continue print(res)

15

what is the output of the following code? balance = 20 while (True): -> print (balance) -> if (balance < 10): --> break balance = balance - 10

20 10 0

what is the output? def test(x, y): -> print(x, y) y = 3 x = 4 test (y, x)

3 4

what is the final value of the variable x? x = 0 for i in range (10, 1, -2): --> x += i

30

what's the content of the file after the following statements. the file already has John Smith in it. names = open("names.txt", "a") names.write('\nJames Miller') names.write('\nGary Johnson') names.write('\nSteve Young') names.close()

John Smith James Miller Gary Johnson Steve Young

a function with no return statement returns __.

None

what is the result of the following segment of code: data = ['Mike', '345', 'HELLO', 'Number125'] for index in range(len(data)): -> print(data[index].isalpha())

True False True False

alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[4:])

[[], 3.14, False]

what arguments are passed to the open() function?

a file name and an option as strings

what is a variable defined outside a function referred to as?

a global variable

the readlines() method returns a ___.

a list of lines of strings

to open a file names.txt to add additional names, use ___

nameFile = open("names.txt", "a")

given the file names.txt, which of the following opens the file for reading as a text file?

open('names.txt', 'r')

what is the output? x = 10 y = 10 def change(): -> global y -> x = 45 -> y = 56 change() print(x) print(y)

10 56

what is the output when the inputs are (0, 1, 2, 3, 4) sum = 0 for i in range (1, 6): -> if i % 2 == 0: --> sum += int(input()) -> else: --> sum -= int(input()) print(sum)

-2

suppose list1 is [3, 5, 25, 1, 3], what is min(list1)?

1

what is the output of the following? for i in range (1, 6): -> print (i) else: -> print ("The end.")

1 2 3 4 5 The end.

what is the result of the following code? for i in range (1, 11): -> print (i, end = " ")

1 2 3 4 5 6 7 8 9 10

what is the output? num = 0 while (num < 4): -> num = num + 1 print(num)

4

what is the output? x = 3 def test(): -> x = 4 -> print (x) test () print(x)

4 3

which keyword is used to define a function?

def

every element in a list must be of the same type. true or false?

false

where is the error? def main(): -> start = int(input()) -> end = int(input()) -> prod = 0 -> while (start <= end): --> prod = prod * start --> start += 1 -> print (prod) main()

the initial value of prod should not be 0

what are the elements of C? A = [1, 2, 3, 4] B = [10, 20, 30, 40] C = A + B

[1, 2, 3, 4, 10, 20, 30, 40]

suppose list1 is [1, 3, 2], what is the result of list1 * 2?

[1, 3, 2, 1, 3, 2]

suppose list1 is [2, 33, 222, 14, 25] what is the result for printing list1[-1]?

25

what is the output? li = [4, 5, 2, 6, 6, 3, 6] index = 0 for i in range (1, len(li)): --> if (li[i] > li[index]): ---> index = i print(index)

3

what is the output? i = 3 while ( i < 6 ): -> print (i) -> i += 1

3 4 5

what is the output? def printChar(word, n): -> while (n > 0): --> print(word) --> n-= 1 printChar ('a', 5)

a a a a a

when you define a function, usually define a function header that consists of ___.

function name and parameter list

which of the following is true regarding functions in Python?

functions are reusable pieces of programs

it is a good practice to avoid using ____.

global variables

what is the output? i = 0 while ( i < 3 ): -> print ("hi") -> i = i + 1

hi hi hi

what is the output of the following? y = 4 for i in range (y, 15): -> y = i print (y)

14

suppose list1 is [1, 5, 9], what is sum(list1)?

15

what is the output? def test(x): -> x = x + 5 -> print(x) test(10)

15

what is the result after the breakpoint? sum = 0 num = 0 while (num < 10): -> num = num + 1 -> sum = sum + num -> if (sum > 10): --> break print(sum)

15

what is the output? num = 0 for i in range (0, 10, 3): --> num += i print(num)

18

Floating-point variables can be used as a loop counter. True or false?

False

what is the output? studentsName = ['Alex', 'Smith', 'William', 'Tom'] if ('Smith' in studentsNames): -> print("Found") else: -> print ("Not found")

Found

what's the content of the file after the following statements. the file already has John Smith in it. names = open("names.txt", "w") names.write('\nJames Miller') names.write('\nGary Johnson') names.write('\nSteve Young') names.close()

James Miller Gary Johnson Steve Young


Set pelajaran terkait

fluid and electrolyte imbalances **

View Set

Chapter 6: DNA Replication and Repair

View Set

Introduction to Programming 1-12 Exam

View Set

EC3 Uni 1 ASD 1-3 español a inglés

View Set

Unit 3: Forces and Newton's Laws

View Set

ATI Pedi Book Ch 23 More GI disorders

View Set