1/47
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Are there errors in for loop?
Is the code snippet written below legal?
s = "1234"
for i in range (0, 4) :
print(s[i], s[i + 1])
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.
No; for i = 0, s[i] will result in an string index out of range error
No; for i = 3, s[i + 1] will result in an string index out of range error.
Compare adjacent values
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")
letter == letter
alphabet[0] == letter
letter == previous
alphabet[0] == previous
letter == previous
Complete the for loop that counts the number of digits in a string
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 text[char] >= "0" and text[char] <= "9" :
if text[count] >= "0" and text[count] <= "9" :
if char >= "0" and char <= "9" :
if text >= "0" and char <= "9" :
if char >= "0" and char <= "9" :
Complete the loop that finds the location of two adjacent characters that are the same
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] :
if text[i] == text[i + 1] :
if text[i] == text[i - 1] :
Convert a for loop to a while loop
Consider the following for loop:
for i in range(0, 10) :
print(i)
Which of the following while loops will generate the same output?
1.
i = 0
while i < 10 :
print(i)
i = i + 1
i = 0
while i <= 10 :
print(i)
i = i + 1
i = 1
while i < 10 :
print(i)
i = i + 1
i = 1
while i <= 10 :
print(i)
i = i + 1
1.
i = 0
while i < 10 :
print(i)
i = i + 1
Finding the first match
s = "abcdEfghI"
found = False
count = 0
while found == False :
if s[count].isupper() :
print(letter)
found = True
count = count + 1
9 times
8 times
5 times
1 time
5 times
Given the following code, what is printed?
for count in range(31,7,-2): print (count, end = ",")
31,27,23,19,15,11
31,27,23,19,15,11,
31,27,23,19,15,11,7,
31,27,23,19,15,11,7
31,27,23,19,15,11,
Given the following code:
What is the smallest number that could be printed?
import random result1 = random.randint(0,5) result2 = random.randint(1,9) result = result1 * 10 + result2 print ("result = {}".format(result))
0
1
10
19
1
How do you fix an off-by-one error?
Which statement corrects the off-by-one error in the following code:
i = 0
while i <= 10 :
print(i)
i = i + 1
Replace i = 0 with i = 1
Replace while i <= 10 with while i < 10
Replace i = i + 1 with i = i + 2
Replace while i <= 10 with while i + 1< 10
Replace while i <= 10 with while i < 10
How many iterations of while loop?
How many times does the code snippet given below display "Loop Execution"?
i = 1
while i != 10 :
print("Loop Execution")
i = i + 1
Infinite times
8 times
9 times
10 times
9 times
How many iterations of while loop?
How many times will the following loop run?
i = 0
while i < 10 :
print(i)
i = i + 1
0
8
9
10
10
How many times does for loop execute?
How many times does the loop execute in the following code fragment?
for i in range(0, 50, 4) :
print(i)
How many times does the loop execute?
ow many iterations does the following loop carry out?
for i in range (-10, 11, 2) :
How many times will inner for loop execute?
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)
Insert appropriate code in while loop
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
years = years - 1
balance = balance + 1
balance = balance - 1
Print Odd Numbers
Which of the following loops will print the odd numbers between 0 and 20?
Rewrite a for loop using a while loop
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
Trace a while loop that processes a string
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?
What are final values of three variables in a for loop?
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
What are first and last values displayed by nested for loop?
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)
What are values of variables after while loop executes?
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)
What does code snippet with random numbers do?
hat 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)
What does for loop with character creation display?
What does the following code snippet display?
for n in range(1, 11) :
for x in range(1, 11) :
print(n*x, end = " ")
print()
What does it mean to draw a line through values when hand-tracing?
When hand tracing, drawing a line through the value stored in a variable means that
What does the following loop compute?
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)
What does the for loop print?
What does the following code snippet print?
fruitName = "banana"
for letter in fruitName :
print(letter, end = " ")
What does this while loop print?
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
What is output of nested loops?
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()
What is output of snippet with input that includes a sentinel?
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!")
What is output of while loop with nested if and Boolean conditions?
What happens when the following loop is executed?
val1 = True
val2 = False
while val1 :
if val1 :
print("Hello")
val1 = val2
What is output of while loop with nested if?
What is the output of the following code snippet?
i = 1
while i < 20 :
print(i , " ")
i = i + 2
if i == 15 :
i = 19
What is output of while loop?
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)
What is output of while loop?
What is the output of the code snippet given below?
i = 0
while i != 9 :
print(i, end = " ")
i = i + 2
What is range of random numbers generated by snippet?
What will be the range of the random numbers generated by the following code snippet?
from random import randint
randomNum = randint(1,50)
What is the output of code snippet with while loop
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
What is the output of for loop?
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()
What is the output of for loop?
What is the output of this loop?
counter = 1
for i in range(1, 100) :
counter = counter + 1
print(counter)
What is the sentinel value in the following code segment?
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: "))
What value will cause the while loop to terminate?
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
When does execution switch from inner to outer loop?
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)
Which code generates random numbers 0-79?
Which of the following code snippets will generate a random number between 0 and 79?
Which code simulates throwing two dice and summing the result?
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?
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?
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")
2.sum <= 100
Which of the following for loops will run the loop body 5 times
Which of the following for loops will run the loop body 5 times?
Which statement about Boolean conditions is true?
When hand-tracing a portion of code, which statement about Boolean conditions is true?
Which statement about sentinels is correct?
Which of the following statements is correct about a sentinel?
Which values does the counter variable assume in for loop?
What values does counter variable i assume when this loop executes?
for i in range(20, 2, -6) :
print(i, end = ", ")
Which while loop executes 10 times?
Which of the following code snippets displays the output exactly 10 times?