Programming Test 2

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

What is the output of the code snippet given below? i = 0 while i !=9 : print(i, end = " ") i = i + 2

0 2 4 6 8 10 12 14... (infinite loop)

Given the list below, what are the upper and lower bounds? somelist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

0, 25

What is the output of the following loop? s = 1 n = 1 while s < 10 * n : s = s + n n = n + 1 print(s)

211

What is a list in Python?

A container that stores a collection of elements that are arranged in a linear or sequential order

What is the valid range of index values for a list?

At least zero and less than the number of elements in the list

Which statement correctly identifies the error in this code snippet? scores = [80, 85, 60.5, 95, 70] print(scores[5])

IndexError: list index out of range

What is the output of the following code fragment? i = 1 sum = 0 while i <= 15 : sum = sum + i i = i + 1 print("The value of sum is", sum)

The value of sum is 120

Given a list values that contains the first ten prime numbers, which statement prints the index value of each element in the list?

for i in range(10) : print(i, values[i]

Given a list values that contains the first ten prime numbers, which statement does not print each element in the list?

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

Which statement gets the length of the list values?

numValues = len(values)

Consider the following list: values = [12, 4, 8, 16, 24] Which statement displays the last element in the list?

print(values[4])

What is the output of the following code snippet? i = 1 while i != 9 : print(i, end = " ") i = i + 1 if i == 9 : print("End")

1 2 3 4 5 6 7 8 End

What is the output of the following code snippet? i = 1 while i < 20 : print(i, " ") i = i + 2 if i == 15 : i = 19

1 3 5 7 9 11 13 19

What is the output of the following code snippet? i = 1 while i < 10 : print(i, end = " ") i = i + 2 if i == 5 : i = 9

1 3 9

How many times will the following loop run? i = 0 while i < 10 : print(i) i = i + 1

10

How many times does the code snippet given below display "Loop Execution?" i = 1 while i != 10 : print("Loop Execution") i = i + 1

10 times

Consider the following code segment: values = [4, 12, 0, 1, 5] print(values[1]) What is displayed when it runs?

12

How many times is the text "Let's have fun with Python." printed when this code snippet is run? i = 0 while i < 10 : print("Let's have fun with Python.") i = i + 1 if i % 2 == 0 : i = 10

3

Given the following list what value is at index 5? values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

6

What is the output of the code fragment given below? i = 0 j = 0 while i < 125 : i = i + 2 J = J + 1 print(j)

63

What is the difference between a list and a string?

Lists can hold values of any type, whereas strings are only sequences of characters

Which of the following changes will make the following code snippet display "Let us learn Python" exactly 10 times? i = 0 while i <= 10 : print("let us learn Python") i = i + 1

Replace i = 0 with i = 1

Which statement corrects the off by one error in the following code: # this code prints the first 10 numbers starting with zero i = 0 while i<=10 : print(i) i=i+1

Replace while i <= 10 with while i < 10

What will be the result of running the following code fragment? year = 0 rate = 5 principal = 10000 interest = 0 while year < 10 : interest = (principal * year * rate) / 100 print("Interest ", interest)

The code fragment will continue to display the calculated interest forever because the loop will never end

The code snippet below is supposed to check whether an integer greater than 1 is a prime number. What will be the result of executing it? j = 2 result = 0 number = int(input("Please enter an integer (2 or greater) :")) while j < number : if number % j == 0 : result = 1 j = j + 1 if result == 1 : print("Number:", number, "is Not Prime.") else : print("Number:", number, "is Prime.")

The code snippet displays the desired result

Is the following code snippet legal? b = false while b != b : print("Do you think in Python?")

Yes, it is legal but does not print anything

Consider the following code segmentL values = [1, 2, 3, 4, 5] moreValues = values moreValues[3] = 6 print(values) What is displayed when it runs?

[1, 2, 3, 6, 5]

Consider the following code snippet. What will be stored in the list prices after this code executes? prices = [10.00, 15.50, 13.25, 20.15] for i in range(len(prices)) : prices[i] = prices[i] * 1.06

[10.60, 16.43, 14.31, 21.36]

What is printed after executing the following code snippet? somelist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] for i in range(len(somelist)) : print(somelist[i], end = "")

abcdefghijklmnopqrstuvwxyz

What is printed after executing the following code snippet? somelist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] for i in range(0, len(somelist), 2) : print(somelist[i], end = "")

acegikmoqsuwy

Which of the following code snippets displays the output exactly 10 times?

i = 0 while i < 10 : print("This is example 2.") i = i + 1

What are the values of i and j after the following code snippet is run? i = 10 j = 20 count = 0 while count < 5 : i = i + i i = I + 1 j = j - 1 j = j - j count = count + 1 print("i = ", i, ", j = ", j)

i = 351, j = 0

What is the output of the code fragment given below? i = 0 j = 0 while i <27 : i = i + 2 j = j + 1 print("j =", j)

j = 14

Consider the following line of code: somelist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] What is a valid line of code for displaying the element at the 22nd position in the list?

print(somelist[21])

What is the output of the following code snippet? a = 2 n = 16 r = 1 b = a i = n while i > 0 : if i % 2 == 0 : # n is even b = b * b i = i / 2 else : r = r * b i = i - 1 print("r =", r)

r=65536

For the list: prices = [10.00, 15.50, 13.25, 20.15], what value is contained in prices?

the location of the list

Which of the following statements stores the numbers 1 through 5 into a list named values?

values = [1, 2, 3, 4, 5]

Which statement correctly creates a list that contains four elements?

values = [1, 2, 3, 4]

Given the following code snippet, which statement correctly creates an alias for the list prices? prices = [10.00, 15.50, 13.25, 20.15]

values = prices

Select the statement that correctly completes the loop in this code snippet. years = 20 rate = 0.05 balance = 10000 while years > 0 : # Place code here interest = balance * rate / 100 balance = balance + interest

years = years - 1


Ensembles d'études connexes

Chapter 21 ( 21.4 Control of blood pressure and blood flow)

View Set

1.10 Compare and contrast types of display devices and their features

View Set

PLessy v Ferguson, Crash Course episode 23, US HIS Industrialization

View Set

Math and Graphing Assessment with Tutorials

View Set

Quiz: Transferring a Patient From the Bed to a Chair

View Set

Lesson 3 - Budgeting (Senior Social Studies)

View Set

Care Management 1: Final Practice

View Set