Chapter 4
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 2. i = 0 while i <= 10 : print(i) i = i + 1 3. i = 1 while i < 10 : print(i) i = i + 1 4. i = 1 while i <= 10 : print(i) i = i + 1
1. i = 0 while i < 10 : print(i) i = i + 1
Print Odd Numbers Which of the following loops will print the odd numbers between 0 and 20? 1. num = 1 while num < 20 : print(num, " ") num = num + 2 2. num = 1 while num < 20 : print(num, " ") num = num + 1 3. num = 0 while num < 20 : print(num, " ") num = num + 2 4. num = 1 while num < 20 : num = num + 2 print(num, " ")
1. num = 1 while num < 20 : print(num, " ") num = num + 2
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: ")) 1. 0 2. 2 3. 3 4. 15
1. 0
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) 1. 100 2. 49 3. 60 4. 10
1. 100
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? 1. 4 2. 5 3. 7 4. 8
1. 4
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() 1. It displays a multiplication table for numbers 1-10 times 1-10 2. Nothing because it has compilation error. 3. It displays a table of all numbers squared from 1-10 4. It displays a multiplication table for numbers 1-11 times 1-11
1. It displays a multiplication table for numbers 1-10 times 1-10
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) 1. When the value of j becomes 6 2. When the program executes completely 3. When the condition for the outer loop is met 4. When the value of i is incremented
1. When the value of j becomes 6
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) 1. i = 1951, j = 0 2. i = 1951, j = 45 3. i = 65, j = 1 4. i = 65, j = 45
1. i = 1951, j = 0
Which while loop executes 10 times? Which of the following code snippets displays the output exactly 10 times? 1. I = 0 while i <= 10 : print("This is example 1.") i = i + 1 2. i = 0 while i < 10 : print("This is example 2.") i = i + 1 3. i = 0 while i < 10 : print("This is example 3.") 4. i = 1 while i < 10 : print("This is example 4.") i = i + 1
2. i = 0 while i < 10 : print("This is example 2.") i = i + 1
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 1. s = 0 i = 0 while i <= 10 : s = s + i i = i + 1 2. s = 0 i = 1 while i < 10 : s = s + i i = i + 1 3. s = 0 i = 1 while i <= 10 : s = s + i i = i + 1 4. s = 0 i = 0 while i < 10 : s = s + i i = i + 1
2. s = 0 i = 1 while i < 10 : s = s + i i = i + 1
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 1. No output will be displayed because of a compilation error. 2. "Hello" will be displayed only once. 3. "Hello" will be displayed an infinite number of times. 4. No output will be displayed even after successful compilation of the code snippet.
2. "Hello" will be displayed only once.
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 1. -7.5 2. -2.5 3. 2.5 4. 7.5
2. -2.5
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() 1. 0 1 5 7 9 11 13 15 17 19 55 2. 0 1 1 2 3 5 8 13 21 34 55 89 3. 0 1 4 6 8 10 12 14 16 18 34 4. 0 1 6 7 9 12 14 17 19 21 55
2. 0 1 1 2 3 5 8 13 21 34 55 89
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)) 1. 0 2. 1 3. 10 4. 19
2. 1
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 1. 1 3 5 7 9 11 13 15 17 19 2. 1 3 5 7 9 11 13 19 3. 1 3 5 7 9 11 13 15 17 4. 1 3 5 7 9 11 13 17 19
2. 1 3 5 7 9 11 13 19
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 1. 1 3 5 2. 1 3 9 3. 1 3 5 7 9 4. 1 3 5 9
2. 1 3 9
Given the following code, what is printed? for count in range(31,7,-2): print (count, end = ",") 1. 31,27,23,19,15,11 2. 31,27,23,19,15,11, 3. 31,27,23,19,15,11,7, 4. 31,27,23,19,15,11,7
2. 31,27,23,19,15,11,
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) 1. It simulates the outcome of throwing a coin. 2. It calculates the average of 1000 random numbers between 0 and 100. 3. It performs a Monte Carlo fluid dynamics simulation. 4. It calculates the average of 1000 random numbers between 1 and 101.
2. It calculates the average of 1000 random numbers between 0 and 100.
How do you fix an off-by-one error? 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 1. Replace i = 0 with i = 1 2. Replace while i <= 10 with while i < 10 3. Replace i = i + 1 with i = i + 2 4. Replace while i <= 10 with while i + 1< 10
2. Replace while i <= 10 with while i < 10
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!") 1. The average salary: 0.0 2. The average salary: 30.0 3. The average salary: 24.83333 4. There will be no output as the code snippet will not compile.
2. The average salary: 30.0
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 1. The value stored there has changed to something new 2. The variable is the wrong data type for the code being executed 3. The expression being evaluated uses that variable 4. The variable must be inside a loop
2. The variable is the wrong data type for the code being executed
What does the for loop print? What does the following code snippet print? fruitName = "banana" for letter in fruitName : print(letter, end = " ") 1. banana 2. b a n a n a 3. Nothing, there is a syntax error 4. Nothing, this is an infinite loop
2. b a n a n a
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? 1. for i in range(0, 4) : 2. for i in range(0, 5) : 3. for i in range(0, 6) : 4. for i in range(1, 5) :
2. for i in range(0, 5) :
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? 1. if text[i] == text[0] : 2. if text[i] == text[i - 1] : 3. if text[i] == text[i] : 4. if text[i] == text[i + 1] :
2. if text[i] == text[i - 1] :
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 1. years = years + 1 2. years = years - 1 3. balance = balance + 1 4. balance = balance - 1
2. years = years - 1
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") 1. sum != 0 2. sum <= 100 3. sum > 100 4. sum == 100
2.sum <= 100
How many times does the loop execute? ow many iterations does the following loop carry out? for i in range (-10, 11, 2) : 1. infinite 2. None because of compilation error 3. 11 times 4. 20 times
3. 11 times
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) 1. 11 2. 12 3. 13 4. 14
3. 13
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 = ", ") 1. 20, 14, 8, 2 2. 20, 14, 8, 2, -4 3. 20, 14, 8 4. 14, 8, 2
3. 20, 14, 8
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 1. 3 4 5 2. 3 3. 3 3 3 3 3 ... (infinite loop) 4. 0 1 2
3. 3 3 3 3 3 ... (infinite loop)
Finding the first match s = "abcdEfghI" found = False count = 0 while found == False : if s[count].isupper() : print(letter) found = True count = count + 1 1. 9 times 2. 8 times 3. 5 times 4. 1 time
3. 5 times
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 1. 2 10 1 2. 4 8 2 3. 6 6 3 4. 0 12 0
3. 6 6 3
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 1. Infinite times 2. 8 times 3. 9 times 4. 10 times
3. 9 times
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) 1. 3 times 2. 6 times 3. 9 times 4. 12 times
3. 9 times
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]) 1. Yes. 2. No; there should not be a colon at the end of line 2. 3. No; for i = 3, s[i + 1] will result in an string index out of range error. 4. No; for i = 0, s[i] will result in an string index out of range error
3. No; for i = 3, s[i + 1] will result in an string index out of range error.
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) 1. The average of all the integers in the input 2. The sum of all the positive integers in the input divided by the number of integers in the input 3. The average of all the positive integers in the input 4. The second smallest value in the input
3. The average of all the positive integers in the input
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? 1. if text[char] >= "0" and text[char] <= "9" : 2. if text[count] >= "0" and text[count] <= "9" : 3. if char >= "0" and char <= "9" : 4. if text >= "0" and char <= "9" :
3. if char >= "0" and char <= "9" :
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") 1. letter == letter 2. alphabet[0] == letter 3. letter == previous 4. alphabet[0] == previous
3. letter == previous
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? 1. randint(1, 6) 2. randint(2, 12) 3. randint(1, 6) + randint(1, 6) 4. randint(1, 12) - 2
3. randint(1, 6) + randint(1, 6)
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() 1. No output 2. * * * * * * ... infinite loop 3. * * * * 4. * * *
4. * * *
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 1. No output 2. 0 2 4 6 8 3. 10 12 14 16 18 ... (infinite loop) 4. 0 2 4 6 8 10 12 14 ... (infinite loop)
4. 0 2 4 6 8 10 12 14 ... (infinite loop)
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) 1. 0 and 20 2. 1 and 20 3. 0 and 19 4. 1 and 19
4. 1 and 19
How many iterations of while loop? How many times will the following loop run? i = 0 while i < 10 : print(i) i = i + 1 1. 0 2. 8 3. 9 4. 10
4. 10
Which statement about sentinels is correct? Which of the following statements is correct about a sentinel? 1. A sentinel is a value that creates a bridge between a data set and unrelated input. 2. A sentinel is a value that is part of the data to be processed by the program. 3. A sentinel is a value that terminates a program. 4. A sentinel is a value that indicates the end of an input sequence.
4. A sentinel is a value that indicates the end of an input sequence.
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) 1. Between 1 and 49 2. Between 0 and 50 3. Between 0 and 49 4. Between 1 and 50
4. Between 1 and 50
Which statement about Boolean conditions is true? When hand-tracing a portion of code, which statement about Boolean conditions is true? 1. They typically are too complex to be evaluated. 2. They do not need to be monitored because their result usually is not stored in a variable. 3. It is rare to encounter a Boolean condition. 4. They are crucial to evaluate since they determine if-statement conditions and looping.
4. They are crucial to evaluate since they determine if-statement conditions and looping.
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) 1. j = 27 2. j = 12 3. j = 13 4. j = 14
4. j = 14
Which code generates random numbers 0-79? Which of the following code snippets will generate a random number between 0 and 79? 1. val = int(random() % 80) 2.val = int(random() * 80 - 1) 3. val = int(random() % 79) 4. val = int(random() * 80)
4. val = int(random() * 80)