COMP560 W2 Zych4_loops

1) How many times will the following loop run?

i = 0

while i < 10 :

   print(i)

   i = i + 1

A) 10

2) How many times does the code snippet given below display “Loop Execution”?

i = 1

while i != 10 :

   print(“Loop Execution”)

   i = i + 1

A) 9 times

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

A) 1 3 9

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

A) 63

5) What is the output of the following loop?

s  = 1

n  = 1

while  s < 10 * n :

   s = s + n

   n = n + 1

print(s)

A) 211

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

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

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

A) i = 0

while i < 10 :

   print(“This is example 2.”)

   i = i + 1

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

A) 1 2 3 4 5 6 7 8 End

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

A) 3

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

A) years = years – 1

11) Is the following code snippet legal?

b = False

while b != b :

   print(“Do you think in Python?”)

A) Yes, it is legal but does not print anything.

12) What is the output of the following code snippet?

i = 1

while i  < 20 :

   print(i , ” “)

   i = i + 2

   if i == 15 :

      i = 19

A) 1 3 5 7 9 11 13 19

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

A) i = 351, j = 0

14) How many times does the following code fragment display “Hi”?

i = 10

while i >= 0 :

  print(“Hi”)

  i = i –  1

A) 11 times

15) What is the output of the code snippet given below?

n = 0

while n * n < 100 :

   print(n * n,  end =  ”  “)

   n = n + 1

A) 0 1 4 9 16 25 36 49 64 81

16) What is the output of the code snippet given below?

s = “abcde”

length = len(s)

i = 1

while i < length :

   print(s[i])

   i = i + 1

A) bcde

17) What is the output of the code snippet given below?

s = “abcde”

i = 1

while i < 5 :

   if i > 1 :

      print(s[i])

A) No output (infinite loop)

18) What is the output of the code snippet given below?

s = “12345”

i = 0

while i < 5 :

   print(s[i])

   i = i + 1

A) 12345

19) What is the output of the code snippet given below?

s = “12345”

i = 1

while i < 5 :

   if i > 1 :

      print(s[i])

A) No output (infinite loop)

20) How many times does the code snippet below display “Hello”?

i = 0

while i != 15 :

   print(“Hello”)

   i = i + 1

A) 15 times

21) How many times does the following loop run?

i = 0

j = 1

while j >= 1 :

   print(i , “;” , j)

   i = i + 1

   if i % 3 == 0 :

      j = j –  1

A) 3 times

22) What is the output of the following code snippet?

i = 1

while i <= 10 :

   print(“Inside the while loop”)

   i = i + 10

A) “Inside the while loop” will be displayed only once.

23) How many copies of the letter A are printed by the following loop?

i = 0

while i < 5 :

   print(“A”)

   i = i + 1

A) 5

24) How many copies of the letter B are printed by the following loop?

i = 0

while i == 5 :

   print(“B”)

   i = i + 1

A) 0

25) How many copies of the letter C are printed by the following loop?

i = 0

while i < 5 :

   print(“C”)

   i = i – 1

A) Infinity

26) What is the value of i at the end of the following code segment?

i = 1

while i < 32 :

   i = i * 2

A) 32

27) The following while loop should continue to run as long as the user does not enter a negative number. What condition should be used to achieve this behavior?

x = int(input(“Enter an integer: “))

while ____________ :

   x = int(input(“Enter an integer: “))

A) x >= 0

28) What term is used to describe a loop where the number of times that the loop will execute is known before the body of the loop executes for the first time?

A) Definite

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

A) Replace i = 0 with i = 1

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

A) Replace while i <= 10 with while i < 10

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

A) The value of sum is 120

32) What are the values of i and j after the following code fragment runs?

i = 60

j = 50

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)

A) i = 1951, j = 0

33) Which type of error could be reported by Python when the program contains an “off-by-one” error?

A) Run-time error

34) What is the output of the code snippet given below?

i = 0

while i !=  11 :

   print(i, end=” “)

   i = i + 3

A) 0 3 6 9 12 … (infinite loop)

35) What is the output of the following code fragment?

i = 1

sum = 0

while i <= 11 :

   sum = sum + i

   i = i + 1

print(“The value of sum is”, sum)

A) The value of sum is 66

36) What is the last line of output produced by the code snippet below?

i = 0

total = 0

while total < 0 :

   i = i + 1

   total = total – i

   print(i, total)

A) No output

37) How many times does the following loop run?

i = 0

j = 1

while j >= 1 :

   print(“” , i , “;” , j)

   i = j + 1

   if i % 2 == 0 :

      j = j – 1

A) 1 time

38) What is the output of the code snippet given below?

s = “abcde”

j = len(s) – 1

while j >= 0 :

   print(s[j])

   j = j –  1

A) edcba

39) What is the output of the code snippet given below?

i = 0

while i != 11 :    

   print(” “, i)

   i = i + 2

A) 0 2 4 6 8 … (infinite loop)

40) What will be the output of the following code snippet?

token = False

while token :

   print(“Hello”)

A) No output after successful compilation.

41) What is the output of the code snippet given below?

i = 0

while i != 9 :

   print(i, end = ” “)

   i = i + 2

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

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

A) j = 14

43) 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.”)

A) The code snippet displays the desired result.

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

A) r = 65536

45) What are the final values of the variables i, j, and n at the end of this loop?

i = 0

j = 12

n = 0

while i != j :

   i = i + 2

   j = j – 2

   n = n + 1

A) 6 6 3

46) When hand-tracing the loop in the code snippet below, which variables are important to evaluate?

i  = 10

j  = 5

k  = -10

sum  = 0

while  i > 0 :

   sum = sum + i + j

   i = i –  1

   print(“Iteration: “, i)

A) The variables i and sum

47) When hand tracing, drawing a line through the value stored in a variable means that

A) The value stored there has changed to something new

48) When hand-tracing a portion of code, which statement about Boolean conditions is true?

A) They are crucial to evaluate since they determine if-statement conditions and looping.

49) What is the output of this code snippet?

s = 1

n = 1

while s  < 3 * n :

   s = s + n

   print(s , end = ” “)

   n = n + 1

A) 2 4 7 11 16 22

50) What are the values of i and j after the following code snippet executes?

i  = 20

j  = 70

count  = 0

while  count < 5 :

   i = i + i 

   i = i + 1

   j = j – 1

   j = j – j

   count = count + 1

print(i)

print(j)

A) i = 671, j = 0

51) The process of hand-tracing code is valuable because

A) It gives valuable insight that you do not get by running the code.

52) What is the output of the code snippet given below?

s = “aeiou”

i = 0

while i < 5 :

   print(s[i], s[i + 1], end = ” “)

   i = i + 1

   if i >= 3 :

      i = 5

A) a e e i i o

53) What is the sentinel value in the following code segment?

value = 15

x = int(input(“Enter an integer: “))

while x != 0 :

   value = value * 2

   print(value + 3)

   x = int(input(“Enter an integer: “))

A) 0

54) Of the following options, what should the user enter to cause the following while loop to terminate?

done = False

while not done :

   x = float(input(“Enter a number: “))

   if x > 5.0 :

      print(x)

   elif x > 0.0 :

      done = False

   elif x < -5.0 :

      print(-x)

   else :

      done = True

A) -2.5

55) Which of the following statements is correct about a sentinel?

A) A sentinel is a value that indicates the end of an input sequence.

56) What will be the output of the following code snippet?

token1 = True

while token1 :

   for i in range(0,10) :

      print(“Hello”)

   token1 = False

A) Hello will be displayed 10 times.

57) Insert a statement that will correctly terminate this loop when the end of input is reached.

done = False

while done != True :

   x = input(“Enter a value”)

   if x == “Q” :

      _________________

A) done = True

58) Which of the following loops executes exactly 10 times?

A) for i in range(1, 11) :

   i =  1

59) When will the loop in the following code snippet stop?

sum = 0

count = 1

str = input(“Enter values, Q to quit: “)

while count < 100 and str != “Q” :

   value = float(str)

   sum = sum + value

   count = count + 1

   str = input(“Enter values, Q to quit: “)

I. When the user enters an integerII. When the user enters the character QIII. After the user enters 100 numbers

A) II or III

60) The value that denotes the end of an input sequence is known as a:

A) Sentinel value

61) Which of the following command lines starts the python program sum.py so that it will read its input from values.txt instead of the keyboard?

A) python sum.py < values.txt

62) What happens when the following loop is executed?

val1 = True

val2 = False

while val1 :

   if val1 :

      print(“Hello”)

   val1 = val2

A) “Hello” will be displayed only once.

63) Which statement is correct about the execution of the loop in the following code fragment?

num = int(input(“Please enter a number (0 when done): “))

incr = 0

while num != 0 :

   incr = incr + 1

   num = int(input(“Please enter a number (0 when done): “))

print(incr)

A) The program prints the count of inputs not equal to zero.

64) What is the sentinel value in the following code snippet?

age = 0

sumOfAges = 0

stop = 1

age = int(input(“Enter  an age (-1 to stop):”))

while age != -1 :

   sumOfAges =  sumOfAges + age

   age = input(“Enter an age (-1 to stop):”)

print(“Sum  of ages “, sumOfAges)

A) -1

65) What will be the final output of the following code snippet when a user enters input values in the order 10, 20, 30, 40, 50, and -1?

sum = 0

count = 0

salary = 0

average = 0

while salary != -1 :

   salary = float(input(“Enter  salaries (-1 to stop): “))

   if  salary != -1 :

      sum = sum + salary

      count = count + 1

if count > 0 :

   average = sum / count

   print(“The average salary:  “, average)

else :

   print(“No data!”)

A) The average salary: 30.0

66) Storyboards are a helpful part of the design process because the storyboard develops

A) The information needed to solve the problem, and how to present that information

67) When designing storyboards, it is a good idea to use different colors to

A) Make it easy to distinguish between user input and program output.

68) Suppose you must design a program to calculate the roll-out (number of inches traveled in one revolution of the pedals of a bicycle based on its gear combinations). The user must provide the gear sizes, which must be converted into roll-out for all different gear combinations. How can the flow of user interaction for this problem be designed?

A) A storyboard can be used.

69) Which statement about storyboards is true?

A) A storyboard can help prevent potential user confusion early in the design process.

70) What will be printed by the statements below?

a = 10

while a  > 5 :

   print(a , end = ” “)

   a = a – 2

A) 10 8 6

71) What will be printed by the statements below?

val = 1

sum = 0

while val < 5 :

   sum = sum + val

   val = val + 1

print(sum)

A) 10

72) What will be printed by the statements below?

val = 1

sum = 0

while val  < 5 :

   sum = 0

   sum = sum + val

   val = val + 1

print(sum)

A) 4

73) What will be printed by the statements below?

for ctr in range(0, 10) :

   print(ctr, end = ” “)

A) 0 1 2 3 4 5 6 7 8 9

74) What will be printed by the statements below?

for ctr in range(10, 5, -1) :

   print(ctr, end = ” “)

A) 10 9 8 7 6

75) Which of the following loops will print the odd numbers between 0 and 20?

A) num = 1

while num < 20 :

   print(num, ” “)

   num = num + 2

76) Which of the following loops will print the odd numbers between 0 and 20?

A) num = 1

while num < 11 :

   value  = num * 2 – 1

   print(value, ” “)

   num = num + 1

77) Which of the following conditions can be added to the code below so it will loop until the value of sum is greater than 100?

sum = input(“enter an integer”)

while #  Put condition here :

   sum = sum + input(“Enter an integer”)

A) sum <= 100

78) What does the following code compute?

sum = 0

count = 0

value = input(“enter an  integer”)

while value > 0 :

   sum = sum + value

   count = count + 1

   value = input(“enter next integer”)

result = sum * 1.0 / count

print(result)

A) The average of all the positive integers in the input

79) What is the output of the code below?

for val in range(0, 4) :

   print(“+”, end = “”)

   for num in range(0,  val) :

      print(“0”,  end = “”)

A) ++0+00+000

80) What is the output of the code below?

num = 1

for val in range(0, 4) :

   sum = val

   for x in  range(0, val, num) :

      sum = sum +  x

   print(sum , end  = ” “)

A) 0 1 3 6

81) How many times does the following loop execute?

i = 0

found = False

while i < 100 and found != True :

   i = i + 1

   print(i, end = ” “)

   j = i * i

   if i * i * i % j == j :

      found = True

A) 100 times

82) The following program is supposed to sum all of the numbers entered by the user. What line of code must be inserted in the blank so that the program will achieve this goal?

total = 0.0

inputStr = input(“Enter a value: “)

while inputStr != “” :

   value = float(inputStr)

   ____________________

   inputStr = input(“Enter a value: “)

A) total = total + value

83) The following program is supposed to count how many even numbers are entered by the user. What line of code must be inserted in the blank so that the program will achieve this goal?

evens = 0

inputStr = input(“Enter a value: “)

____________________

   value = int(inputStr)

   if value % 2 == 0:

      evens = evens + 1

   inputStr = input(“Enter a value: “)

A) while inputStr != “” :

84) The following program is supposed to continue reading values from the user until a value between 25 and 75 is entered. What line of code must be inserted in the blank so that the program will achieve this goal?

value = int(input(“Enter a value: “))

____________________

   value = int(input(“Enter a value: “))

A) while value < 25 or value > 75 :

85) The following program is supposed to print a message any time the user enters two consecutive values that are the same. What line of code must be inserted in the blank so that the program will achieve this goal?

value = int(input(“Enter a value: “)

inputStr = input(“Enter a value: “)

while inputStr != “” :

   previous = value

   value = int(inputStr)

   ____________________

      print(“Found consecutive values that are the same”) 

   inputStr = input(“Enter a value: “)

A) if previous == value :

86) How many times does the while loop execute?

s = “abcdEfghI”

   found = False

   count = 0

   while found == False :

      if s[count].isupper() :

         print(letter)

         found = True

      count = count + 1

A) 5 times

87) Consider the following code snippet. What should be placed in the blank to cause a message to be displayed when the user enters the same letter twice in a row?

letter = input(“Enter the next letter in the  alphabet: “)

while letter != “”:

   previous = letter

   letter = input(“Enter the next letter”)

   if ________________________ :

      print(“Duplicate input”)

A) letter == previous

88) What is the output of this code snippet if the user enters the numbers 1 2 3 4 -1?

total = 0

validNumber = True

while validNumber :

   value = int(input(“Please enter a positive value < 100: “))

   if value > 0 and value < 100 :

      total = total + value

   else :

      validNumber = False

print(total)

A) 10

89) What will be printed by the statements below?

a = 10

while a  > 5 :

   a = a – 2

   print(a , end = ” “)

A) 8 6 4

90) What is printed by the following code segment?

position = 0

str = input(“Enter a string: “)

while position < len(str) and str[position] != ‘e’ :

   position = position + 1

print(position)

A) The position of the first ‘e’ in the string or the length of the string if there is no ‘e’

91) Which code snippet produces the sum of the first n positive even numbers? Note that 0 is neither positive nor negative.

A) sum = 0

for i in range(1, n + 1) :

   sum = sum + i * 2

92) What is the output of this loop?

i = 0

found = False

while i < 20 and found != True :

   sum = i * 2 + i * 3

   print(sum, end=” “)

   if sum > 50 :

      found = True

   i = i + 1

A) 0 5 10 15 20 25 30 35 40 45 50 55

93) Which of the following for loops will run the loop body 5 times?

A) for i in range(0, 5) :

94) Which of the following for loops will run the loop body 5 times?

A) for i in range(5, 0, -1) :

95) What is the value of j at the end of the following code segment?

j = 0

for i in range(0, 4) :

   j = j + i

A) 6

96) What is the value of j at the end of the following code segment?

j = 0

for i in range(1, 10) :

   if j < 10 :

      j = j + i

A) 10

97) Consider the following for loop:

for i in range(0, 10) :

   print(i)

Which of the following while loops will generate the same output?

A) i = 0

while i < 10 :

   print(i)

   i = i + 1

98) Consider the following while loop:

j = 10

while j >= 5 :

   print(“X”)

   j = j – 1

Which of the following for loops will generate the same output?

A) for j in range(10, -1, -2) :

   print(“X”)

99) What is the output of this loop?

counter = 1

for i in range(1, 100) :

   counter = counter + 1

print(counter)

A) 100

100) What does the following code snippet print?

fruitName = “banana”

for letter in fruitName :

   print(letter, end = ” “)

A) b a n a n a

101) What is the output of the following code snippet?

for i in range(4) :

   for j in  range(3) :

      print(“*”, end=””)

   print()

A) Prints 4 rows of 3 asterisks each

102) How many times does the loop execute in the following code fragment?

for i in  range(0, 50, 4) :

   print(i)

A) 13

103) How many times does the following code snippet display “Loop Execution”?

for i in range(0, 10) :

   print(“Loop Execution”)

The code snippet does not run because of a compile error.

A) Ten times.

104) Which of the following is considered an equivalent while loop for this for loop?

s = 0

for i in range(1, 10) :

   s = s + i

A) s =  0

i = 1

while i < 10 :

   s = s + i

   i = i + 1

105) Which statement about this code snippet is accurate?

years = 50

balance = 10000

targetBalance = 20000

rate = 3

for i in range(1 , years + 1) :

   if balance  >= targetBalance :

      i = years +  1

   else :

      interest =  balance * rate / 100

   balance =  balance + interest

A) The loop will run 50 times.

106) What values does counter variable i assume when this loop executes?

for i in range(20, 2, -6) :

   print(i, end = “, “)

A) 20, 14, 8

107) What is the output of the following code snippet?

f1 = 0

f2 = 1

print(f1, ” “)

print(f2, ” “)

for i in range(1, 11) :

   fRes = f1 + f2

   print(fRes, end = ” “)

   f1 = f2

   f2 = fRes

print()

A) 0 1 1 2 3 5 8 13 21 34 55 89

108) How many iterations does the following loop carry out?

for i in range (-10, 11, 2) :

A) 11 times

109) How many times does the following loop execute?

for d in range(1, 10) :

   d = d / 3

   print(d , ” “)

A) 9

110) Consider the following code segment:

s = “Hello World!”

  __________

    print(ch)

What should be placed in the blank so that the letters of s are printed out with one letter appearing on each line?

A) for ch in s :

111) Which of the following for loops is illegal?

A) for i in range( , ) :

112) When does the execution switch from the inner to the outer loop?

j = 1

for i in  range(0, 10) :  

   while(j < 5) :

      print(“Hello”)

      if j == 2 :

         j = 6

      j = j + 1

   print(“switch  from inner to outer”, i, ”  “, j)

A) When the value of j becomes 6

113) A loop inside another loop is called:

A) A nested loop

114) What is the first and last value of i to be displayed by the following code snippet?

n = 20

for i in range(0, n) :

   for  j in range(0, i) :

      print(i)

A) 1 and 19

115) How many times will the output line be printed in the following code snippet?

for num2 in range(1, 4) :

   for num1 in range(0, 3) :

      print(num2, ” “, num1)

A) 6 times

116) What is the last output line of the code snippet given below?

for i in range(3) :

   for j in  range(5) :

      if i % 2 ==  j % 2 :

         print(“*”, end=””)

      else :

         print(” “, end=””)

   print()

A) * * *

117) What is the last output line of the code snippet given below?

i = 0

j = 0

while i  < 10 :

   num = 1

   j = i

   while j > 1 :

      print(j, end = ” “)

      num = num * 2

      j = j –  1

   print(“***”)

   i = i + 1

A) 9 8 7 6 5 4 3 2 ***

118) Which for loop prints data across each row in the following code snippet?

for i in range(1, 4) :

   for j in range(1, 4) :

      print(“X”, end=””)

   print(“”)

A) The inner for loop

119) What will be the output of the following code snippet?

for i in range(0,7) :

   for j in range(7, i, -1) :  

      print(“*”, end=””)

   print(“”)

A) A right triangle with seven rows and seven columns of asterisks. The number of columns decrements by one on completion of one iteration of the inner loop.

120) In the following code snippet, how many times will “Hello” be printed?

for i in range(0, 10) :

   for j in range(1, 5) :

      print(“Hello”)

A) 40

121) Which of the following code segments is an example of a nested loop?

A) while i < 0 :

   while x == 10 :

122) Consider the following code segment:

for i in range(4) :

   ____________________

      print(“*”, end=””)

   print()

It is supposed to generate the following output:

***

***

***

***

Which line of code should be placed in the blank to achieve this goal?

A) for j in range(3) :

123) How many copies of the letter A will the following code segment display?

for i in range(100) :

   for j in range(5) :

      print(“A”)

A) 500

Question 124

1 / 1 pts

What does the following code snippet display?

for n in  range(1, 11) :

   for x in range(1, 11) :

      print(n*x, end = ” “)

   print()

Nothing because it has compilation error.

It displays a multiplication table for numbers 1-11 times 1-11

It displays a table of all numbers squared from 1-10

It displays a multiplication table for numbers 1-10 times 1-10

Correct answer

Question 125

1 / 1 pts

What type of chart shows the distribution of data across a fixed number of categories?

A Plot

A Grade Chart

A Height Chart

A Histogram

Correct answer

Question 126

1 / 1 pts

Which print statement displays the value of s without starting a new line?

print(end=”s”)

print(s)

print(“s”)

print(s, end=””)

Correct answer

Question 127

1 / 1 pts

What is the output of this code snippet?

str = “ABCabc”

i = 0

while i < len(str) :

   ch = str[i]

   if ch.islower() :

      print(i ,  ” “)

   else :

      i = i + 1

0 1 2

3 4 5

3

3 3 3 3 3 … (infinite loop)

Correct answer

Question 128

1 / 1 pts

Consider the following code segment. It is supposed to count the number of digits (0 – 9) in a string, text.

count = 0

for char in text :

   ____________________

      count = count + 1

What line of code should be placed in the blank to achieve this goal?

if char >= “0” and char <= “9” :

if text[count] >= “0” and text[count] <= “9” :

if text[char] >= “0” and text[char] <= “9” :

if text >= “0” and char <= “9” :

Correct answer

Question 129

1 / 1 pts

Is the code snippet written below legal?

s = “1234”

for i in range (0, 4) :

   print(s[i], s[i + 1])

No; for i = 0, s[i] will result in an string index out of range error

Yes.

No; there should not be a colon at the end of line 2.

No; for i = 3, s[i + 1] will result in an string index out of range error.

Correct answer

Question 130

1 / 1 pts

Consider the following code segment:

found = False

position = 0

text = “Hello World!”

while not found and position < len(text) :

   if text[position] == “o” :

      found = True

   else :

      position = position + 1

What is the value of position at the end of this code segment?

5

4

7

8

Correct answer

Question 131

1 / 1 pts

Consider the following code segment. It is designed to identify the first location within a string, text where two adjacent characters are the same.

i = 1

found = False

while not found and i < len(text) :

   ____________________ :

      found = True

   else :

      i = i + 1

What line of code should be placed in the blank to achieve this goal?

if text[i] == text[0] :

if text[i] == text[i + 1] :

if text[i] == text[i – 1] :

if text[i] == text[i] :

Correct answer

Question 132

1 / 1 pts

What will be the range of the random numbers generated by the following code snippet?

from random import randint

randomNum = randint(1,50)

Between 1 and 49

Between 1 and 50

Between 0 and 49

Between 0 and 50

Correct answer

Question 133

1 / 1 pts

Which of the following is the correct code snippet for throwing a pair of dice to get a sum of the numbers on two dice between 2 and 12 with the same probability as when throwing actual dice?

randint(2, 12)

randint(1, 6)

randint(1, 12) – 2

randint(1, 6) + randint(1, 6)

Correct answer

Question 134

1 / 1 pts

Suppose that a program asks a user to enter multiple integers, either positive or negative, to do some calculation. The data entry will stop when the user enters a certain value to indicate the end of the data. What value should the code use as the sentinel?

An alphabetic character

-1

999

0

Correct answer

Question 135

1 / 1 pts

Which of the following activities can be simulated using a computer?I. Waiting time in a line at a restaurantII. Tossing a coinIII. Shuffling cards for a card game

I and II only

I only

II only

I, II, and III

Correct answer

Question 136

1 / 1 pts

What range of numbers are generated by the random() function?

greater than or equal to zero and less than one

greater than zero and less than or equal to one

greater than zero and less than one

greater than or equal to zero and less than or equal to one

Correct answer

Question 137

1 / 1 pts

Assume the following variable has been declared and given a value as shown:

from random import randint

number  = randint(0, 27) * 2 + 3

What are the smallest and largest values that may be assigned to number?

3, 55

3, 57

0, 26

0, 27

Correct answer

Question 138

1 / 1 pts

Which line of code will generate a random integer from 1 up to and including 10, and store it in x? Assume that the randint function has been imported from the random module.

x = randint(0, 10)

x = randint(1, 11)

x = randint(0, 11)

x = randint(1, 10)

Correct answer

Question 139

1 / 1 pts

Which line of code will generate a random floating-point number between 0 and 6, and store it in x? Assume that the random function has been imported from the random module.

x = random(6)

x = random()

x = random() * 6

x = random(0, 6)

Correct answer

Question 140

1 / 1 pts

What does the following code do?

from random import randint

sum = 0

COUNT = 1000

for i in range(1,COUNT + 1) :

   sum = sum + randint(0, 100)

print(sum / COUNT)

It performs a Monte Carlo fluid dynamics simulation.

It calculates the average of 1000 random numbers between 1 and 101.

It calculates the average of 1000 random numbers between 0 and 100.

It simulates the outcome of throwing a coin.

Correct answer

Question 141

1 / 1 pts

Which of the following statements correctly prints the result of simulating the toss of a pair of coins to get 0 (heads) or 1 (tails) for each coin?

print(randint(0, 2), randint(0, 2))

print(randint(1, 1))

print(randint(0, 1))

print(randint(0, 1), randint(0, 1))

Correct answer

Question 142

1 / 1 pts

Which of the following code snippets will generate a random number between 0 and 79?

val = int(random() * 80)

val = int(random() * 80 – 1)

val = int(random() % 79)

val = int(random() % 80)

Correct answer

Question 143

1 / 1 pts

Which of the following expressions will generate a random integer in the range -20 to 20, inclusive, where each value has an equal chance of being generated?

randint (-20, 20)

randint (-20) + 40

randint(20) – 41

randint(41) – 20

Correct answer

Question 144

1 / 1 pts

Assume the following variable has been declared and given a value as shown:

from random import random

number = random() * 2 + 3

What are the smallest and largest values that may be assigned to number?

0.0, 3.0 (including 3.0)

-3.0, 3.0 (including 3.0)

0.0, 6.0 (excluding 6.0)

3.0, 5.0 (excluding 5.0)

Correct answer

Question 145

1 / 1 pts

Using computer algorithms to manipulate digital images is known as:

Data compression

Digital photography

Image processing

Computer vision

Correct answer

Question 146

1 / 1 pts

A digital image is a collection of __________ arranged in a grid of rows and columns.

canvases

elementary elements

dots

pixels

Correct answer

Question 147

1 / 1 pts

In an RGB color model, what color is represented by 255, 255, 255?

black

yellow

white

green

Correct answer

Question 148

1 / 1 pts

What RGB values represent green?

0, 255, 0

0, 0, 255

128, 128, 128

255, 0, 0

Correct answer

Question 149

1 / 1 pts

Which statement loads an image from a file and stores it in a variable? Assume that the ezgraphics module has already been imported using the statement:

from ezgraphics import GraphicsImage, GraphicsWindow

Image(“mountain.gif”)

image = (“mountain.gif”)

image = GraphicsImage(“mountain.gif”)

graphicsImage = Load(“mountain.gif”)

Correct answer

Question 150

1 / 1 pts

What programming language structure is used iterate over all of the individual pixels in an image?

A nested if statement

A for loop (not nested)

A nested for loop

A while loop (not nested)

Correct answer

Question 151

1 / 1 pts

Which of the following image processing operations changes the grid structure of the image without modifying the pixel values?

Darkening an image

Converting an image to grayscale

Replacing an image with its negative

Rotating an image

Correct answer

Question 152

1 / 1 pts

Which of the following is not a benefit of solving a simpler problem first?

Solving the simpler problem first will motivate you to solve the harder problem.

It can be difficult to figure out how to get started when solving a large task.

Usually, you learn something useful from solving the simpler task.

When the simpler problem is solved first it reduces the amount of time the computer needs to compute the answer for the larger problem.

Leave a Reply

Your email address will not be published. Required fields are marked *