ISE 135 Test 1
What symbol is used to begin a comment in a Python program?
#
Which of the following statements correctly calculates the average of three numbers: num1, num2, and num3?
( num1 + num2 + num3 ) / 3
What is the value of result after the following code snippet? num1 = 10 num2 = 20 num3 = 2 result = num1 / num2 / num3 print(result)
.25
What extension is used for Python files?
.py
What is the index value of the letter 'h' in the string below? message = "hello"
0
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 = " ")
0 1 3 6
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
0 5 10 15 20 25 30 35 40 45 50 55
c = 2 b = 1 if b == 0 : c = c + 1 else : c = c - 1 print(c) What value is printed by this code segment?
1
numPizzas = 1 numPeople = 4 if numPeople == 5 : numPizzas = 2 After this code segment executes, what value is in the variable numPizzas?
1
Consider the following pseudocode. What does it produce? Set a = 0 Set b = 0 Set c = 1 Set d = 1 Report the value of d Repeat until a equals 10 Set d = b + c Set b = c Set c = d Add 1 to a Report the value of d
1 1 2 3 5 8 13 21 34 55 89
How many times will the following loop run? i = 0 while i < 10 : print(i) i = i + 1
10
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)
10
What will be printed by the statements below? val = 1 sum = 0 while val < 5 : sum = sum + val val = val + 1 print(sum)
10
What will be printed by the statements below? for ctr in range(10, 5, -1) : print(ctr, end = " ")
10 9 8 7 6
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
100 times
How many times does the loop execute in the following code fragment? for i in range(0, 50, 4) : print(i)
13
Given the code snippet below, what is returned by the function call: mystery(5,3)? def mystery(num1, num2) : result = num1 * num2 return result
15
Consider the following program: def main() : a = 2 doubleIt(a) print(a) def doubleIt(x) : x = x * 2 main() What output is generated when this program is run? a. 4 b. Python reports an error because doubleIt does not contain a return statement c. 8 d. 2
2
Given the code snippet below, what is returned by the function call: mystery(mystery(5, 3), mystery(5, 3))? def mystery(num1, num2) : result = num1 * num2 return result
225
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, 57
Assuming a user enters 30, 20, and 10 as the input values, what is the output of the following code snippet? num1 = int(input("Enter a number: ")) num2 = int(input("Enter a number: ")) num3 = int(input("Enter a number: ")) if num1 > num2 : if num1 > num3 : print(num1) else : print(num3) else : if num2 > num3 : print(num2) else : print(num3)
30
if count > 0 : x = x + 1 print(x) If count is initialized to -1 and x is initialized to 4 then the value displayed by this code segment is:
4
Consider the following program: def main() : a = 10 print(doTwice(a)) def doTwice(x) : x = x * 2 x = x * 2 return x main() What output is generated when this program is run?
40
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
5 times
What is printed by the following code snippet? num = int("45") * float("1.5") print(num)
67.5
What is wrong with the following code snippet? num = 78A
78A is not a valid value in a Python program
What is the definition of a nested statement?
A decision statement that is contained inside the statement block of another decision statement
Consider the following pseudocode. What does it produce? Create a list of consecutive integers from two to n (2, 3, 4, ..., n). Initially, let p equal 2. Repeat the following steps until p is greater than n: Remove all of the multiples of p less than or equal to n from the list. If the list contains a number greater than p Find the first number remaining in the list greater than p. Replace p with this number. Otherwise set p equal to n + 1
All prime numbers up to n
What is the wrong with the following code snippet? grade = int(input("Enter student grade: ")) if grade >= 90 : letterGrade = "A" if grade >= 80 : letterGrade = "B" if grade >= 70 : letterGrade = "C" if grade >= 60 : letterGrade = "D" else : letterGrade = "E" print(letterGrade)
Anyone with a grade higher than 60 will receive a "D"
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 50
What is the output of the following code snippet when the user enters 75 as the grade? grade = int(input("Enter student grade: ")) if grade >= 90 : letterGrade = "A" if grade >= 80 : letterGrade = "B" if grade >= 70 : letterGrade = "C" if grade >= 60 : letterGrade = "D" else : letterGrade = "E" print(letterGrade)
D
Which of the following activities can be simulated using a computer? I. Waiting time in a line at a restaurant II. Tossing a coin III. Shuffling cards for a card game
I, II and III
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 calculates the average of 1000 random numbers between 0 and 100.
Which type of error is usually the most difficult to locate in your program?
Logic Error
The following function is supposed to return -1 when x is negative, +1 when x is positive, or 0 if x is zero. What, if anything, is wrong with the function? def plusMinusZero(x) : if x == 0 : return 0 elif x <= 0 : return -1 else x >= 0 : return 1 a. A return statement must be added at the end of the function b. The <= and >= must be replaced with < and > c. Both occurrences of elif must be replaced with if d. Nothing is wrong with the function
Nothing is wrong with the function
What is wrong with the following code snippet: num1 = 10 num2 = 20 num3 = 30 total = Num1 + Num2 + Num3
Python is case sensitive so Num1, Num2, and Num3 are undefined
What error will Python display when it attempts to execute the following if/else statement? if a = b : print("Equal") else : print("Not Equal") if a > b : print("a is larger") else : print("b is larger")
Python will display an error indicating that = is invalid sytax
What error will Python display when it attempts to execute the following if/else statement? if a == b : print("Equal") else : print("Not Equal") if a > b : print("a is larger") else : print("b is larger")
Python will display an error indicating that there is a problem with the indentation
What is printed by the following code snippet? name = "Robert" formalName = name.upper() print(formalName)
ROBERT
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)
The average of all the positive integers in the input
Which of the following options refers to the technique of simulating program execution on a sheet of paper?
Tracing
What is wrong with the following code? def grade(score) : if score >= 90 : return "A" elif score >= 80 : return "B" elif score >= 70 : return "C" elif score >= 60 : return "D" a. Another return statement needs to be added to the function b. The type of the parameter variable is invalid c. The name of the parameter variable is illegal d. One of the existing return statements is not correct
a. Another return statement needs to be added to the function
The following function is supposed to compute the area of a triangle and return the area as the function's result. def triangleArea(base, height) : area = base * height / 2 _________________________ What line of code must be placed in the blank to achieve this goal? a. return area b. print(area) c. print area d. return triangleArea
a. return area
A sequence of steps that is unambiguous, executable, and terminating is called:
algorithm
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
How is a value stored in a variable?
an assignment statement
When a function is called, the values placed in parentheses are referred to as:
arguments
Which statement correctly creates a new variable by combining the two string variables: firstName and lastName? Select one: a. name = firstName & lastName b. name = firstName + lastName c. name = "firstName" + "lastName" d. name = first name + last name
b
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
bcde
Which of the following code segments will display Hello World! when it is run? Select one: a. print("Hello", ",", "World", "!") b. print("Hello", "World", "!") c. print("Hello", "World!") d. print(Hello "," World"!")
c
Which of the following code snippets displays the output exactly 10 times? Select one: a.i = 1 while i < 10 : print("This is example 4.") i = i + 1 b.i = 0 while i <= 10 : print("This is example 1.") i = i + 1 c.i = 0 while i < 10 : print("This is example 2.") i = i + 1 d. i = 0 while i < 10 : print("This is example 3."
c
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? Select one: a. randint(20) - 41 b. randint (-20) + 40 c. randint (-20, 20) d. randint(41) - 20
c
Consider the following functions: def printIt(x) : print(x) def incrementIt(x) : return x + 1 def decrementIt(x) : return x - 1 def doubleIt(x) : return x * 2 Which of the following function calls is not a reasonable thing to do? a. print(incrementIt(5)) b. print(doubleIt(5)) c. print(printIt(5)) d. print(decrementIt(5))
c. print(printIt(5))
What is it called when you join two strings together in Python?
concatenation
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 Select one: a.s = 0 i = 0 while i <= 10 : s = s + i i = i + 1 b.s = 0 i = 1 while i <= 10 : s = s + i i = i + 1 c.s = 0 i = 0 while i < 10 : s = s + i i = i + 1 d.s = 0 i = 1 while i < 10 : s = s + i i = i + 1
d
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? Select one: a.randint(1, 12) - 2 b.randint(1, 6) c.randint(2, 12) d.randint(1, 6) + randint(1, 6)
d
Which of the following loops executes exactly 10 times? Select one: a.i = 0 for i in range (1, 10) : print(i) b.found = False while i < 10 and found != True : i = i + 1 if i % 10 == 0 : found = True c.for i in range(1, 11) : i = 1 d.i = 0 while i <= 10 : i = i + 1
d
Which of the following loops will print the odd numbers between 0 and 20? Select one: a.num = 1 while num < 20 : num = num + 2 print(num, " ") b.num = 1 while num < 10 : print(num, " ") num = num + 2 c.num = 1 while num < 20 : value = num * 2 - 1 print(value, " ") num = num + 1 d.num = 1 while num < 11 : value = num * 2 - 1 print(value, " ") num = num + 1
d
Assume that you have an integer variable, pennies, that currently contains an integer number of pennies. Which statement determines the number of dollars and cents for that number of pennies?
dollars = pennies // 100cents = pennies % 100
What must be done first before you can use a function from the standard library?
function must be imported
The operator >= stands for
greater than or equal to
What range of numbers are generated by the random() function?
greater than or equal to zero and less than one
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)
i = 671, j = 0
Which condition will cause the statement block of the if statement to execute only when count is 0?
if count == 0 :
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: ")
if previous == value :
Which statement correctly tests if the user entered the letter Y?
if userInput == "Y" :
What functions can be used to convert a string into a number?
int and float
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 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 == previous
Which line in the following program is a comment line? 1: print("Your lucky number is...") 2: lucky = 7 3: # Display the lucky number 4: print(lucky)
line 3
Consider the following function: def factorial(n) : result = 1 for i in range(1, n + 1) : result = result * i return result What is the parameter variable for this function?
n
What is printed by the following code snippet: street = " Main Street" address = 123 + street print(address)
nothing is printed, this code snippet causes an error
What output is generated by the following code snippet? firstName = "Pamela" middleName = "Rose" lastName = "Smith" print(firstName[0], middleName[0], lastName[5])
nothing, this causes an index of bounds error
What will be the values of the variables num1 and num2 after the given set of assignments? num1 = 20num2 = 10num1 = num1 + num2 / 2num2 = num1
num1 = 25.0, num2 = 25.0
Which of the following variables should be coded as a constant in Python?
pi: 3.14159
Given the code snippet below, what code is needed to print the person's initials? firstName = "Pamela" middleName = "Rose" lastName = "Smith"
print(firstName[0], middleName[0], lastName[0])
What is wrong with the following code snippet? ((num1 + num2) * num3 / 2 * (1 - num4)
there is an extra parenthesis
What is the purpose of this code snippet? def mystery(n) : if n % 2 == 0 : return True else : return False
to determine if n is even or odd
To store a value for later use in Python, the programmer needs to create a:
variable
Which of the following statements causes Python to report an error?
x = 17 + "18.4"