Python Final

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

*What is printed by the following code snippet? name = "today is thursday" name = name.replace("t", "T") name.replace("i", "I") print(name) 1. Today is Thursday 2. Today Is Thursday 3. Today Is thursday 4. today is thursday

1

1. Consider the following code segment: x= 5 y =7 z = x - y* 2 After this code segment executes, the value of z is: 1. -9 2. -4 3. 5 4. 7

1

Consider the following program: def main() : a =3 doubleIt(a) print(a) def doubleIt(x) : return x * 2 main() What output is generated when this program is run? 1. 3 2. 6 3. 8 4. 9

1

It is the "yield" that makes Python interpret the function as a generator. 1. True 2. False

1

Using De Morgan's law, what is the equivalent to this statement? if not (state == "PA" or state == "OH") 1. |if state != "PA" and state != "OH"| 2. |if state != "PA" or state != "OH"| 3. |if state == "PA" and state == "OH"| 4. |if state == "PA" or state == "OH"|

1

What is displayed by the following code segment? print("\"\\Hello World!\\\"") 1. "\Hello World!\" 2. \"\\Hello World!\\\" 3. "\"\\Hello World!\\\"" 4. The program reports an error

1

What is printed by the following code snippet? print("The answers are:", 4 + 3 * 2, 7 * 5 - 24) 1. The answers are: 10 11 2. The answers are: 14 11 3. The answers are: 24 10 4. Nothing, this code snipped causes a compile time error

1

What is printed by the following code snippet? name = "today is thursday" name.replace("t", "T") name.replace("i", "I") print(name) 1. today is thursday 2. Today is Thursday 3. Today Is Thursday 4. Today Is thursday

1

What is printed by the following code snippet? print(Hello) 1. Nothing, an error is produced indicating that Hello is not defined 2. Hello 3. 'Hello' 4. "Hello"

1

What is supplied to a function when it is called? 1. arguments 2. numbers 3. return values 4. variables

1

What is the difference between an editor and an interpreter? 1. An editor allows program files to be entered and modified; an interpreter reads and executes program files 2. An editor allows program files to be entered and modified; an interpreter produces an indexed database of terms and keywords 3. An editor allows program files to be entered and modified; an interpreter produces an organized list of files 4. An editor converts program files into an executable program; an interpreter allows program files to be entered and modified

1

What is the value of words after the following code segment? words = "Hello" + "World" * 3 1. "HelloWorldWorldWorld" 2. "Hello World World World" 3. "HelloWorldHelloWorldHelloWorld" 4. "Hello World Hello World Hello World"

1

What is wrong with this assignment statement? num + 10 = num2 1. The left hand side of an assignment statement cannot include an operator. 2. Nothing, this statement compiles and executes. 3. The value of 10 must be defined before this statement can be executed. 4. The num variable must be defined before this statement can be executed

1

What range of numbers are generated by the random() function? 1. greater than or equal to zero and less than one 2. greater than zero and less than one 3. greater than zero and less than or equal to one 4. greater than or equal to zero and less than or equal to one

1

Which function call will cause Python to report an error? 1. abs(1, 2) 2. max(1, 2) 3. min(1, 2) 4. round(1, 2)

1

Which of the following checks to see if there is a comma anywhere in the string variable |name|? 1. |if "," in name :| 2. |if name.contains(",") :| 3. |if "," not in name :| 4. |if name.startswith(",") :|

1

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? 1. |randint (-20, 20) | 2. |randint(20) - 41 | 3. |randint (-20) + 40 | 4. |randint(41) - 20 |

1

Which of the following is *not* an example of a relational operator? 1. |=| 2. |<| 3. |<=| 4. |!=|

1

Which of the following names is not a legal variable name? 1. bottle-volume 2. cans_per_pack 3. four 4. x2

1

Which of the following statements correctly defines a function? 1. |def functionName(parameterName1, parameterName2) :| 2. |def functionName(parameterName1, parameterName2)| 3. |functionName(parameterName1, parameterName2) :| 4. |functionName(parameterName1, parameterName2)|

1

Which output format string correctly allows for 5 positions before and two digits after the decimal point? 1. "%8.2f" 2. "%5.2f" 3. "%7.2f" 4. "%5d.2f"

1

What will be printed by the statements below? for ctr in range(10, 5, -1) : print(ctr, end = " ") 1. 10 9 8 7 6 5 2. 10 9 8 7 6 3. 5 6 7 8 9 10 4. 6 7 8 9 10

1: start, stop, step

*Consider the following code segment: x = 100 print("%d%%" % x) The output generated by this code segment is: 1. %100%% 2. 100% 3. %100 4. 100%%

2

A class that represents the most general entity in an inheritance hierarchy is called a/an ______. 1. Default class 2. Superclass 3. Subclass 4. Inheritance class

2

Although the following code statement is valid, print(10/0), what will happen when this code is executed? 1. The program prints 0 2. The error message ZeroDivisionError: int division or modulo by zero is displayed 3. The program runs, but nothing is printed 4. The error message SyntaxError: EOL while scanning string literal

2

Consider the follow code segment. It is supposed to convert numeric marks to letter grades. However, it may contain a bug. Examine the program, and identify what bug, if any, is present. grade = "F" if mark >= 80 : grade = "A" if mark >= 70 : grade = "B" if mark >= 60 : grade = "C" if mark >= 50 : grade = "D" 1. The greater than or equal signs need to be replaced with equal signs 2. All instances of if, except the first, need to be replaced with elif 3. All instances of if, except the first, need to be replaced with else 4. There is nothing wrong with the code segment (it works as intended)

2

Consider the following code segment: a = input("Enter the value of a: ") b = input("Enter the value of b: ") print(a + b) When this code segment is run the user enters 1 at the first prompt and 3 at the second prompt. The output displayed is: 1. 4 2. 13 3. 1+3 4. 1 + 3

2

Consider the following code segment: x = 25 print("%%%d" % x) The output generated by this code segment is: 1. 25% 2. %25 3. %%25 4. %%%25

2

Consider the following code snippet: attendance = True failed = False Which of the following |if| statements include a condition that evaluates to |True|? 1. |if attendance == "true" :| 2. |if attendance :| 3. |if failed :| 4. |if attendance == failed :|

2

Consider the following program: def main() : a =3 print(doubleIt(a)) def doubleIt(x) : return x * 2 main() What output is generated when this program is run? 1. 3 2. 6 3. 8 4. 9

2

The following logical expression will 'short-circuit'... quantity > 0 or price/quantity < 10 1. When |quantity| is equal to 0 2. When |quantity| is equal to 5 3. When |price/quantity| is less than 10 4. When |price/quantity| is greater than 10

2

What is printed by the following code snippet? print("The answer is", 25 + 84) 1. The answer is 2584 2. The answer is 109 3. The answer is 25 + 84 4. Nothing, this code snipped causes a compile time error

2

What is printed by the following code snippet? print(25 + 84) 1. 2584 2. 109 3. 25 + 84 4. Nothing, this code snipped causes a compile time error

2

What is the correct sequence of steps invoked by the Python Interpreter: 1. source code -> virtual machine -> byte code -> compiler 2. source code -> compiler -> byte code -> virtual machine 3. compiler -> source code -> virtual machine -> byte code 4. byte code -> virtual machine -> source code -> compiler

2

What is the index value of the letter 'h' in the string below ? message = "hello" 1. 1 2. 0 3. 3 4. 4

2

What is the value of result after the following code snippet? num1 = 10 num2 = 20 num3 = 2 result = num1 // num2 // num3 print(result) 1. 1 2. 0 3. The code has an error 4. 0.25

2

What is the value of result after the following code snippet? num1 = 50 num2 = 20 num3 = 2 result = num1 // num2 // num3 print(result) 1. 0 2. 1 3. 1.25 4. The code has an error

2

What is wrong with the following code snippet: num1 = 10 num2 = 20 num3 = 30 total = Num1 + Num2 + Num3 1. Nothing, the variable total will be the sum of the three numbers 2. Python is case sensitive so Num1, Num2, and Num3 are undefined 3. total must be initialized to zero first 4. The numbers should be 10.0, 20.0 and 30.0

2

What string method can be used to determine if the string contained in the variable |text| only consists of letters? 1. |text.isalnum()| 2. |text.isalpha()| 3. |text.isdigit()| 4. |text.islower()|

2

What value is printed by the following code snippet? name = "John R. Johnson" firstName = "John" location = name.find(firstName) print(location) 1. |-1| 2. |0| 3. |8| 4. |1|

2

What values does counter variable i assume when this loop executes? for i in range(30, -10, -10) : print(i, end = " ") 1. 30 20 10 0 -10 2. 30 20 10 0 3. 20 10 0 4. 30 20 10

2

What will be printed by the following code snippet? name = "Ravi Avalon" counter = name.count("av") print(counter) 1. |0| 2. |1| 3. |2| 4. |-1|

2

Which of the following code segments will display Hello World! when it is run? 1. print(Hello "," World"!") 2. print("Hello", "World!") 3. print("Hello", "World", "!") 4. print("Hello", ",", "World", "!")

2

Which of the following if statements is problematic because of the limited precision of floating-point numbers? 1. |if 4 // 3 == 1 :| 2. |if math.sqrt(2) * math.sqrt(2) == 2.0 :| 3. |if "10" == 5 :| 4. |if 4 <= 4 :|

2

Which of the following is considered a string in Python? 1. Today is Wednesday 2. "Today is Wednesday" 3. # Today is Wednesday # 4. Today_is_Wednesday

2

Which of the following statements causes Python to report an error? 1. x = 17 + 18.4 2. x = 17 + "18.4" 3. x = 17 + int(18.4) 4. x = 17 + float("18.4")

2

Which type of error is usually the most difficult to locate in your program? 1. Indentation Error 2. Logic Error 3. Syntax Error 4. Zero Division Error

2

*What is the value of result after the following code snippet? num1 = 300 num2 = 20 num3 = 7 result = num1 // num2 // num3 print(result) 1. 0 2. 1 3. 2 4. 2.75

3

Assuming a user enters 47, 27, and 7 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) 1. 0 2. 7 3. 27 4. 47 5. Error

3

Consider a function named |calc|. It accepts two string arguments and returns their sum as an integer. Which of the following statements is a correct invocation of the calc function? 1. total = calc() 2. total = calc(9) 3. total = calc("4", "5") 4. total = calc(4, 5)

3

Consider the following code segment: a = input("Enter the value of a: ") b = input("Enter the value of b: ") print(a + b) When this code segment is run the user enters 1 at the first prompt and 5 at the second prompt. The output displayed is: 1. 1 2. 6 3. 15 4. 1 + 5

3

Consider the following code segment: def f1(): print("a", end="") return "b" def f2(): print("c", end="") d = f1() print(d, end="") print("e", end="") def f3(): print("f", end="") f2() print("g", end="") f3() What output is generated when it runs? 1. |fg| 2. |fceg| 3. |fcabeg| 4. |fcadeg|

3

Consider the following code segment: title = "Python for Everyone" newTitle = title.replace("e", "*") After this code runs, the value stored in newTitle is: 1. "Python for *veryone" 2. "Python for Ev*ryone" 3. "Python for Ev*ryon*" 4. "Python for *v*ryon*"

3

Consider the following code segment: a = input("Enter the value of a: ") b = input("Enter the value of b: ") print(a + b) When this code segment is run the user enters 2 at the first prompt and 3 at the second prompt. The output displayed is: 1. 5 2. 2 + 3 3. 23 4. nothing is printed, this code snippet causes an error

3

Rewrite the following algebraic expression to an equivalent Python expression: 32 <= temp <= 100 1. |if temp <= 32 and temp <= 100| 2. |if temp <= 32 or temp <= 100| 3. |if temp >= 32 and temp <= 100| 4. |if temp >= 32 or temp <= 100|

3

What is another name for a compile-time error? 1. Logic error 2. Semantic error 3. Syntax error 4. Lexicographic error

3

What is another name for a compile-time error? 1. Logic error 2. Semantic error 3. Syntax error 4. Lexicographic error

3

What is printed by the following code snippet? print("25 + 84") 1. 2584 2. 109 3. 25 + 84 4. Nothing, this code snipped causes a compile time error

3

What is printed by the following code snippet? print("Good", "Morning", "Class", "!") 1. GoodMorningClass! 2. Good Morning Class! 3. Good Morning Class ! 4. nothing, this code produces a syntax error

3

What is printed from the following code snippet: message = "ho.." print(message * 3) 1. ho..ho..ho 2. ho.. 3. ho..ho..ho.. 4. nothing is printed, this code snippet causes an error

3

What is the output of the following code snippet if |count| contains 56? if count % 2 == 0 : print("Count is an even number") else : print("Count is an odd number") 1. |Count is an even number| 2. |Count is an odd number| 3. Nothing, there is a syntax error 4. Nothing, the program runs but does not print any output 4

3

What is wrong with the following code snippet? mystery(10, 2) def mystery(num1, num2) : result = num1 ** num2 return result 1. nothing, it will return |20| 2. nothing, it will return |100| 3. the function must be defined before the statement that calls it 4. a variable must be used to store the result of the function call

3

What is wrong with the following code snippet? 2ndNum = 78 1. The 2ndNum variable is never assigned a value 2. The 2ndNum variable is assigned a non-numeric value 3. The 2ndNum variable is not a valid variable name 4. The 2ndNum variable is never initialized

3

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" 1. The name of the parameter variable is illegal 2. The type of the parameter variable is invalid 3. Another |return| statement needs to be added to the function 4. One of the existing |return| statements is not correct

3

Which line in the following program is a comment line? print("Your lucky number is...") (1) lucky = 7 (2) # Display the lucky number. (3) print(lucky) (4) 1. Line number 1 2. Line number 2 3. Line number 3 4. Line number 4

3

Which line of code will generate a random integer from 0 up to and including 10, and store it in |x|? Assume that the |randint| function has been imported from the |random| module. 1. |x = randint(0, 11)| 2. |x = randint(1, 11)| 3. |x = randint(0, 10)| 4. |x = randint(1, 10)|

3

Which of the following operators is used to invert a conditional statement? 1. or 2. and 3. not 4. equal

3

Which of the following statements is true about the if statement? 1. The |if| statement can have only one condition that evaluates to an integer value. 2. The |if| block is optional. 3. The |else| block is optional. 4. The |if| and |else| blocks can be aligned to any indentation level.

3

Which of the following symbols can be used to begin a string literal in Python? 1. * 2. # 3. " 4. >

3

Which output format string correctly allows for 6 positions before and 3 digits after the decimal point? 1. "%6.3f" 2. "%9.3f" 3. "%10.3f" 4. "%10d.3f"

3

*Using De Morgan's law, what is the equivalent to this statement? if not (state != "PA" and state == "OH") 1. |if state != "PA" and state != "OH"| 2. |if state == "PA" and state == "OH"| 3. |if state == "PA" or state == "OH"| 4. |if state == "PA" or state != "OH"|

4

Assuming a user enters 10, 20, and 30 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) 1. 0 2. 10 3. 20 4. 30

4

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) 1. 55 2. 10 3. 0 4. 30

4

Assuming that the user enters a value of 45, what is the output of the following code snippet? number = int(input("Please enter a number: ")) if number < 100 : number = number + 5 if number < 500 : number = number - 2 if number > 15 : number = number + 1 else : number = number - 1 print(number) 1. |105| 2. |45| 3. |43| 4. |49|

4

Class variables are also known as: 1. global variables 2. interface variables 3. local variables 4. static variables

4

Consider the following code segment: if a > b : print("X") if a == b : print("Y") What is displayed if |a| is 0 and |b| is 0? 1. |X| 2. |Y| 3. |X| followed by |Y| on the next line 4. Nothing

4

Given the code snippet below, what is returned by the function call: |mystery(mystery(2, 3), mystery(1, 2))|? 1. 2312 2. (2, 3) * (1, 2) 3. 6, 2 4. 12

4

Given the following code snippet, which statement correctly allows the function to update the global variable |total|? 1. total = 0 2. def main(): 3. avg = 0 4. for i in range(6) : 5. iSquared = i * i 6. total = total + iSquared 7. avg = total / i 8. print(total) 9. print(avg) main() : 1. add the keyword |global| to line 1 2. line 1 already allows the |total| variable to be updated 3. move line 1 inside the function definition 4. add the statement |global total| after line 2

4

Suppose that |b| is |False| and |x| is 0. Which of the following expressions evaluates to |True|? 1. |b or x == 1| 2. |b and x == 0| 3. |not b and x == 1| 4. |not b or x == 1|

4

What is The output? name = "John R. Johnson" firstName = "Son" location = name.find(firstName) print(location-1) 1. Error 2. 12 3. -1 4. -2 5. 0 6. 1 7. True 8. False

4

What is printed by the following code snippet: street = " Main Street" address = 123 + street print(address) 1. 123Main Street 2. 123 Main Street 3. 123 "Main Street" 4. nothing is printed, this code snippet causes an error

4

What is printed by the following code snippet: street = " Main Street" address = 94089 + street print(address) 1. 94089Main Street 2. 94089 Main Street 3. 94089 "Main Street" 4. nothing is printed, this code snippet causes an error

4

What is printed by the following code snippet? name = "today is thursday" name.replace("t", "T") name.replace("i", "I") print(name) 1. Today is Thursday 2. Today Is Thursday 3. Today Is thursday 4. today is thursday

4

What is returned by the following function if x = 5.64? round(x) 1. Nothing, there is an error in the statement 2. 5 3. 5.6 4. 6

4

What is returned by the function: round(3.14159, 2)? 1. 3 2. 3.14159 3. 3.2 4. 3.14

4

What is returned by the function: round(x) if x = 5.50? 1. Nothing, there is an error in the statement 2. 5 3. 5.4 4. 6

4

What is the output from the following Python program? def main() : a = 10 r = cubeVolume() print(r) def cubeVolume() : return r ** 3 main() 1. |10| 2. |30| 3. |1000| 4. Nothing, there is an error.

4

What is the output of the following code snippet when the user enters 100 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) 1. A 2. B 3. C 4. D

4

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) 1. A 2. B 3. C 4. D

4

What is the value of result after the following code snippet? num1 = 10 num2 = 20 num3 = 2 result = num1 / num2 / num3 print(result) 1. 1 2. 0 3. The code has an error 4. 0.25

4

What is the value of the price variable after the following code snippet is executed? price = 42 if price < 40 : price = price + 10 if price > 30 : price = price * 2 if price < 100 : price = price - 20 1. |42| 2. |52| 3. |84| 4. |64|

4

What is wring with the following code: print("Hello") print("World!") 1. The print function cannot be called twice 2. The print function is missing an argument 3. Nothing, the program prints Hello World on the same line 4. The second line should not be indented

4

What range of numbers are generated by the random() function? 1. greater than or equal to zero and less than or equal to one 2. greater than zero and less than one 3. greater than zero and less than or equal to one 4. greater than or equal to zero and less than one

4

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

Which of the following conditions is true, given that |num1| contains 3 and |num2| contains 4? 1. |num1 + 1 < num2| 2. |num1 + 1 > num2| 3. |num1 + num2 != 7| 4. |num1 - num2 <= 0|

4

Which of the given print statements generates the following output? ABCDE"\"\ 1. print("ABCDE"\"\") 2. print("ABCDE\"\") 3. print("ABCDE\"\"\"\") 4. print("ABCDE\"\\\"\\")

4

Which statement correctly creates a new variable by combining the two string variables: firstName and lastName? 1. name = "firstName" + "lastName" 2. name = first name + last name 3. name = firstName & lastName 4. name = firstName + lastName

4

Which statement correctly creates a new variable by combining the two string variables: firstName and lastName? 1. name = "firstName" + "lastName" 2. name = firstname + lastname 3. name = "firstName + lastName" 4. name = firstName + lastName

4

Which statement draws a square on the canvas? 1. canvas.drawRect(0, 50, 0, 50) 2. canvas.drawRect(50, 50, 0, 0) 3. canvas.drawRect(0, 0, 50, 100) 4. canvas.drawRect(0, 0, 50, 50)

4

Which statement finds the last letter of the string variable name? 1. last_letter = name[len(name)] 2. last_letter = len(name) - 1 3. last_letter = len(name) 4. last_letter = name[len(name) - 1]

4


Kaugnay na mga set ng pag-aaral

Chapter 7 Distribution of Sample Means

View Set

Assessment Related to Hygiene and Personal Care

View Set

Upper & Lower Limb - BRS and Lippincotts

View Set