Test 1
What symbol is used to begin a comment in a Python program? a. ! b. $ c. @ d. #
d. #
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 b. 9 times c. 8 times d. 1 time
a. 5 times
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) a. Anyone with a grade higher than 60 will receive a "D" b. The code block will not compile c. Nothing is wrong, students will get the correct grade d. Everyone will get an "E"
a. Anyone with a grade higher than 60 will receive a "D"
What is the purpose of this code snippet? def mystery(n) : if n % 2 == 0 : return True else : return False a. to determine if n is even or odd b. to find the value of n divided by 2 c. to determine if n is positive or negative d. to find the remainder of n divided by 2
a. to determine if n is even or odd
What is it called when you join two strings together in Python? a. repetition b. concatenation c. conversion d. addition
b. concatenation
Which condition will cause the statement block of the if statement to execute only when count is 0? a. if count =< 0 b. if count == 0 : c. if count = 0 : d. if count < 0 :
b. if count == 0 :
To store a value for later use in Python, the programmer needs to create a: a. character b. boolean c. variable d. number
c. variable
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. b. Yes, it is legal and prints "Do you think in Python?" once. c. Yes, it is legal and prints "Do you think in Python?" twice. d. No, it is not legal and gives a compilation error.
a. Yes, it is legal but does not print anything.
What is wrong with the following code snippet? num = 78A a. The num variable is never initialized b. 78A is not a valid value in a Python program c. The name num is not a valid variable name d. The num variable is never assigned a value
b. 78A is not a valid value in a Python program
Which of the following statements correctly calculates the average of three numbers: num1, num2, and num3? a. num1 + num2 + num3 / 3 b. ( num1 + num2 + num3 / 3 ) c. num1 + num2 + num3 % 3 d. ( num1 + num2 + num3 ) / 3
d. ( num1 + num2 + num3 ) / 3
Given two variables x and y, how do you test whether exactly one of them is zero? a. if x == 0 and y != 0 and y == 0 and x != 0 : b. if x == 0 or y == 0 : c. if x == 0 and y != 0 or y == 0 and x != 0 : d. if x = 0 or y = 0 :
c. if x == 0 and y != 0 or y == 0 and x != 0 :
The operator >= stands for a. greater than or equal to b. greater than c. this is not a valid Python operator d. not equal to
a. greater than or equal to
Which of the following statements causes Python to report an error? a. x = 17 + "18.4" b. x = 17 + 18.4 c. x = 17 + int(18.4) d. x = 17 + float("18.4")
a. x = 17 + "18.4"
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. 10 b. 3 c. 1 d. 2
b. 3
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. The code fragment displays no output because it does not compile. b. 63 c. 62 d. 0
b. 63
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? a. dollars = pennies / 100 cents = pennies % 100 b. dollars = pennies // 100 cents = pennies % 100 c. dollars = pennies // 100 cents = pennies / 100 d. dollars = pennies % 100 cents = pennies / 100
b. dollars = pennies // 100 cents = pennies % 100
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. print area b. return area c. print(area) d. return triangleArea
b. return area
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. * * * * * * ... infinite loop b. * * * * c. * * * d. No output
c. * * *
Rewrite the following algebraic expression to an equivalent Python expression: 32 <= temp <= 100 a. if temp <= 32 and temp <= 100 b. if temp >= 32 or temp <= 100 c. if temp >= 32 and temp <= 100 d. if temp <= 32 or temp <= 100
c. if temp >= 32 and temp <= 100
Which of the following conditions is true only when the integer variable middle is between 0 and 10 inclusive? a. 0 < middle < 10 b. 0 <= middle or middle <= 10 c. middle >= 0 and middle <= 10 d. middle > 0 and middle < 10
c. middle >= 0 and middle <= 10
What is the output of the code snippet given below? i = 0 while i != 9 : print(i, end = " ") i = i + 2 a. No output b. 10 12 14 16 18 ... (infinite loop) c. 0 2 4 6 8 d. 0 2 4 6 8 10 12 14 ... (infinite loop)
d. 0 2 4 6 8 10 12 14 ... (infinite loop)
What is printed by the following code snippet? name = "Robert" formalName = name.upper() print(formalName) a. Robert b. robert c. formalName d. ROBERT
d. ROBERT
What extension is used for Python files? a. .py b. .txt c. .Python d. .dat
a. .py
Given the code snippet below, what is returned by the function call: mystery(5,3)? def mystery(num1, num2) : result = num1 * num2 return result a. 15 b. 8 c. 0 d. 2
a. 15
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? a. 20 b. 10 c. 40 d. 2
c. 40
How many times does the code snippet given below display "Loop Execution"? i = 1 while i != 10 : print("Loop Execution") i = i + 1 a. Infinite times b. 8 times c. 9 times d. 10 times
c. 9 times
Which of the following options checks that city is neither Atlanta or Philadelphia? a. if not (city == "Atlanta" or city == "Philadelphia") b. if not city == "Atlanta" or not city == "Philadelphia" c. if not (city == "Atlanta" and city == "Philadelphia") d. if not city == "Atlanta" or city == "Philadelphia"
a. if not (city == "Atlanta" or city == "Philadelphia")
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") a. No error will be displayed b. Python will display an error indicating that = is invalid syntax c. Python will display an error indicating that an if statement cannot reside inside the body of an else d. Python will display an error indicating that there is a problem with the indentation
b. Python will display an error indicating that = is invalid syntax
Which of the following variables should be coded as a constant in Python? a. string: "hello" b. character: 'a' c. pi: 3.14159 d. number: 1234
c. pi: 3.14159
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 a. 1 1 3 5 7 9 11 13 15 17 19 21 b. 1 2 3 4 5 6 7 8 9 10 11 c. 1 1 3 6 9 12 15 18 21 24 27 30 d. 1 1 2 3 5 8 13 21 34 55 89
d. 1 1 2 3 5 8 13 21 34 55 89
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? a. n b. result c. factorial d. i
a. n
Which type of error is usually the most difficult to locate in your program? a. Logic Error b. Zero Division Error c. Syntax Error d. Indentation Error
a. Logic Error
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? a. Young at heart b. Adult c. Child d. Teen
a. Young at heart
What is the output of this loop? counter = 1 for i in range(1, 100) : counter = counter + 1 print(counter) a. 49 b. 100 c. 10 d. 60
b. 100
How many times will the following loop run? i = 0 while i < 10 : print(i) i = i + 1 a. 9 b. 0 c. 10 d. 8
c. 10
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 a. All factorial numbers up to n b. All even numbers up to n c. All prime numbers up to n d. All odd numbers up to n
c. All prime numbers up to n
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) a. Line number 1 b. Line number 2 c. Line number 3 d. Line number 4
c. Line number 3
What does the following code snippet print? fruitName = "banana" for letter in fruitName : print(letter, end = " ") a. Nothing, this is an infinite loop b. banana c. b a n a n a d. Nothing, there is a syntax error
c. b a n a n a
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 a. 0 b. 225 c. 15 d. 30
b. 225
A sequence of steps that is unambiguous, executable, and terminating is called: a. a programming task b. pseudocode c. a logarithm d. an algorithm
d. an algorithm
What must be done first before you can use a function from the standard library? a. the function must be defined b. the function must be enclosed in parenthesis c. the function must be imported d. the function must be included in a module
c. the function must be imported
Which statement correctly creates a new variable by combining the two string variables: firstName and lastName? a. name = first name + last name b. name = "firstName" + "lastName" c. name = firstName & lastName d. name = firstName + lastName
d. name = firstName + lastName
What output is generated by the following code snippet? firstName = "Pamela" middleName = "Rose" lastName = "Smith" print(firstName[0], middleName[0], lastName[5]) a. PRh b. P R h c. PRS d. nothing, this causes an index of bounds error
d. nothing, this causes an index of bounds error
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 5 9 b. 1 3 5 7 9 c. 1 3 9 d. 1 3 5
c. 1 3 9
Which of the following code segments will display Hello World! when it is run? a. print("Hello", ",", "World", "!") b. print("Hello", "World", "!") c. print("Hello", "World!") d. print(Hello "," World"!")
c. print("Hello", "World!")
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? a. 50 b. 65 c. 45 d. 55
d. 55
When a function is called, the values placed in parentheses are referred to as: a. operators b. statements c. keywords d. arguments
d. arguments
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? a. 2 b. 4 c. 3 d. 1
d. 1
What functions can be used to convert a string into a number? a. stri and len b. int and float c. integer and float d. sqrt, abs and round
b. int and float
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" a. The variable num and the parameter variable n must have the same name. b. The input and print statements must reside in a function named main. c. An else clause must be added to the if statement. d. The function definition must appear before the function is called.
d. 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 a. There is a compilation error. b. The loop will never stop. c. The loop will run at most 50 times, but may stop earlier when balance exceeds or equals targetBalance. d. The loop will run 50 times.
d. The loop will run 50 times.
Consider the following code segment: 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: a. X b. W c. X followed by Z on the next line d. X followed by Y on the next line
d. X followed by Y on the next line
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? a. if num < 100 and num > 200 : b. if num < 100 and num > 200 : c. if num <= 100 or num >= 200 : d. if num < 100 or num > 200 :
d. if num < 100 or num > 200 :
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 a. num1 = 15.0, num2 = 15.0 b. num1 = 20.0, num2 = 10.0 c. num1 = 15.0, num2 = 10.0 d. num1 = 25.0, num2 = 25.0
d. num1 = 25.0, num2 = 25.0
Given the code snippet below, what code is needed to print the person's initials? firstName = "Pamela" middleName = "Rose" lastName = "Smith" a. print(firstName[0], middleName[0], lastName[0]) b. print(firstName, middleName, lastName) c. print(firstName + middleName + lastName) d. print(firstName[1], middleName[1], lastName[1])
a. print(firstName[0], middleName[0], lastName[0])
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 != "" : b. while inputStr == 2 or 4 or 6 or 8 or 10 or ... : c. while inputStr % 2 == 0 : d. while inputStr != 0 :
a. while inputStr != "" :
Which of the following code snippets displays the output exactly 10 times? a. i = 0 while i < 10 : print("This is example 3." b. i = 0 while i < 10 : print("This is example 2.") i = i + 1 c. i = 1 while i < 10 : print("This is example 4.") i = i + 1 d. i = 0 while i <= 10 : print("This is example 1.") i = i + 1
b. i = 0 while i < 10 : print("This is example 2.") i = i + 1
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 = 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 = 1 while i <= 10 : s = s + i i = i + 1 d. 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
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? a. "B" b. "C" c. "D" d. "A"
b. "C"
What will be printed by the statements below? a = 10 while a > 5 : print(a , end = " ") a = a - 2 a. 10 8 6 4 b. 10 8 6 c. 10 9 8 7 6 5 d. 10 8
b. 10 8 6
How many times does the following code fragment display "Hi"? i = 10 while i >= 0 : print("Hi") i = i - 1 a. 10 times b. 11 times c. 9 times d. 12 times
b. 11 times
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. 2 c. 8 d. Python reports an error because doubleIt does not contain a return statement
b. 2
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) a. 0 b. 30 c. 10 d. 55
b. 30
What is printed by the following code snippet? num = int("45") * float("1.5") print(num) a. 46.5 b. 67.5 c. 45 * 1.5 d. nothing, this causes an error
b. 67.5
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 sideLength squared a. the function returns -600 b. a special value of None will be returned from the function c. the function returns 600 d. an error occurs and aborts the program
b. a special value of None will be returned from the function
Which statement correctly tests if the user entered the letter Y? a. if userInput = "Y" : b. if userInput == "Y" : c. if userInput == "y" : d. if userInput = "y" :
b. if userInput == "Y" :
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 = 12 b. j = 14 c. j = 27 d. j = 13
b. j = 14
Which of the following statements computes the minimum of the variables a, b, c and d, and stores it in x? a. min(a, b, c, d) = x b. x = min(min(a, b), min(c, d)) c. x = minimum(a, b, c, d) d. x = min(a, b, c, min(d))
b. x = min(min(a, b), min(c, d))
What is the index value of the letter 'h' in the string below? message = "hello" a. 3 b. 1 c. 0 d. 4
c. 0
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? a. 2 b. 5 c. 1 d. 4
c. 1
How is a value stored in a variable? a. an equality statement b. an expression c. an assignment statement d. a print statement
c. an assignment statement
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? a. mayVote = age > 18 or not isFelon b. mayVote = not ( age >= 18 or isFelon) c. mayVote = age >= 18 and not isFelon d. mayVote = not ( age >= 18 and isFelon)
c. mayVote = age >= 18 and not isFelon
What is printed by the following code snippet: street = " Main Street" address = 123 + street print(address) a. 123 Main Street b. 123 "Main Street" c. nothing is printed, this code snippet causes an error d. 123Main Street
c. nothing is printed, this code snippet causes an error
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? a. print(randint(0, 1)) b. print(randint(0, 2), randint(0, 2)) c. print(randint(0, 1), randint(0, 1)) d. print(randint(1, 1))
c. print(randint(0, 1), randint(0, 1))
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. 15 b. 14 c. 12 d. 10
d. 10
Consider the following code segment: 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: a. 0 b. -1 c. 5 d. 4
d. 4
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. Both occurrences of elif must be replaced with if b. A return statement must be added at the end of the function c. The <= and >= must be replaced with < and > d. Nothing is wrong with the function
d. Nothing is wrong with the function
What is wrong with the following code snippet: num1 = 10 num2 = 20 num3 = 30 total = Num1 + Num2 + Num3 a. Nothing, the variable total will be the sum of the three numbers b. total must be initialized to zero first c. The numbers should be 10.0, 20.0 and 30.0 d. Python is case sensitive so Num1, Num2, and Num3 are undefined
d. Python is case sensitive so Num1, Num2, and Num3 are undefined
What is wrong with the following code snippet? ((num1 + num2) * num3 / 2 * (1 - num4) a. nothing, the code compiles and runs b. parenthesis are not required c. illegal expression d. there is an extra parenthesis
d. there is an extra parenthesis