ISE 135 Exam 1
Consider the following code segment: 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
Consider the following code segment: numPizzas = 1 numPeople = 4 if numPeople == 5 : numPizzas = 2 After this code segment executes, what value is in the variable numPizzas?
1
How many times does the following code fragment display "Hi"? i = 10 while i >= 0 : print("Hi") i = i - 1
11 times
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?
2
What is wrong with the following code snippet? num = 78A
78A is not a valid value in a Python program
How many times does the code snippet given below display "Loop Execution"? i = 1 while i != 10 : print("Loop Execution") i = i + 1
9 times
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"
if a == b : print("W") else : print("X") if b == c : print("Y") else : print("Z") If a is 0, b is 1 and c is 1 then the output generated by this code segment is:
X followed by Y on the next line
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 snippet: age = int(input("Enter your age: ")) if age < 13 : print("Child", end="") if age >= 13 and age <= 19 : print("Teen", end="") if age > 19 and age < 30 : print("Young adult", end="") if age >= 30 and age <= 50 : print("Adult", end="") if age > 50 : print("Young at heart", end="") Assuming that the user enters 55 as the age, what is the output?
Young at heart
What happens in this code snippet if sideLength = -10? def cubeSurfaceArea(sideLength) : if sideLength >= 0 : return 6 * (sideLength * sideLength) # There are six sides to a cube; surface area of each side is
a special value of None will be returned from the function
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
Which condition will cause the statement block of the if statement to execute only when count is 0?
if count == 0 :
Which of the following options checks that city is neither Atlanta or Philadelphia?
if not city == "Atlanta" or not city == "Philadelphia"
Which of the following expressions represents a legal way of checking whether a value assigned to the num variable is either less than 100 or more than 200?
if num < 100 or num > 200 :
What does the following code snippet print? fruitName = "banana" for letter in fruitName : print(letter, end = " ")
b a n a n a
Rewrite the following algebraic expression to an equivalent Python expression: 32 <= temp <= 100
if temp >= 32 and temp <= 100
Which statement correctly tests if the user entered the letter Y?
if userInput == "Y" :
Given two variables x and y, how do you test whether exactly one of them is zero?
if x == 0 and y != 0 or y == 0 and x != 0 :
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
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?
return area
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
s = 0 i = 0 while i < 10 : s = s + i i = i + 1
The operator >= stands for
greater than or equal to
Given the following code snippet: grade = int(input("Enter student grade: ")) if grade >= 90 : letterGrade = "A" elif grade >= 80 and grade < 90 : letterGrade = "B" elif grade >= 70 and grade < 80 : letterGrade = "C" elif grade >= 60 and grade < 70 : letterGrade = "D" else : letterGrade = "E" print(letterGrade) what is value of grade when the user enters 75?
"C"
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 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 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 snippet given below? i = 0 while i != 9 : print(i, end = " ") i = i + 2
0 2 4 6 8 10 12 14 ... (infinite loop)
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
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 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
1 time
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? a = 10 while a > 5 : print(a , end = " ") a = a - 2
10 8 6
What is the output of this loop? counter = 1 for i in range(1, 100) : counter = counter + 1 print(counter)
100
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
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
Assuming a user enters 30, 55, and 10 as the input, 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 not (num1 > num2 and num1 > num3) : print(num1) elif not(num2 > num1 and num2 > num3) : print(num2) elif not (num3 > num1 and num3 > num2) : 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
Given the following code snippet: MIN_SPEED = 45 MAX_SPEED = 65 speed = 55 if not (speed < MAX_SPEED) : speed = speed - 10 if not (speed > MIN_SPEED) : speed = speed + 10 print(speed) what output is produced?
55
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 printed by the following code snippet? num = int("45") * float("1.5") print(num)
67.5
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?
result
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 number 3
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
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 is printed by the following code snippet? name = "Robert" formalName = name.upper() print(formalName)
ROBERT
The following program is supposed to display a message indicating if the integer entered by the user is even or odd. What is wrong with the program? num = int(input("Enter an integer: ")) print("The integer is", evenOdd(num)) def evenOdd(n) : if n % 2 == 0 : return "even" return "odd"
The function definition must appear before the function is called.
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
The loop will run 50 times
A sequence of steps that is unambiguous, executable, and terminating is called
an algorithm
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 of the following statements computes the minimum of the variables a, b, c and d, and stores it in x?
b. x = min(min(a, b), min(c, d))
What is it called when you join two strings together in Python?
concatenation
To store a value for later use in Python, the programmer needs to create a:
variable
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 // 100 cents = pennies % 100
Given that the following code snippet: isFelon = False answer = input("have you ever committed a felony? ") if answer == "Yes" or answer == "yes" : isFelon = True age = int(input("what is your age? ")) which statement assigns the variable mayVote a value of True if a person may vote if they are 18 or older and not a felon?
mayVote = age >= 18 and not isFelon
Which of the following conditions is true only when the integer variable middle is between 0 and 10 inclusive?
middle >= 0 and middle <= 10
Which statement correctly creates a new variable by combining the two string variables: firstName and lastName?
name = firstName + lastName
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 = 20 num2 = 10 num1 = num1 + num2 / 2 num2 = num1
num1 = 25.0, num2 = 25.0
Which of the following variables should be coded as a constant in Python?
pi: 3.14159
Which of the following code segments will display Hello World! when it is run?
print("Hello", "World!")
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])
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, 1), randint(0, 1))
What must be done first before you can use a function from the standard library?
the function must be imported
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
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: ")
while inputStr != "" :
Which of the following statements causes Python to report an error?
x = 17 + "18.4"