Test 1 programming principles multiple choice

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Which of the following in this expression is evaluated first? age >= 65 or age <= 18 or status == "retired" a- age >= 65 b- age <= 18 c- status == "retired" d- age >= 65 or age <= 18

a- age >= 65

What will be displayed after the following code is executed? counter = 1 while counter <= 20: print(counter, end=" ") counter *= 3 print("\nThe loop has ended.") a. 1 3 9 The loop has ended. b. 1 3 9 The loop has ended. c. 1 3 9 12 15 18 The loop has ended d. 1 3 9 The loop has ended.

a. 1 3 9 The loop has ended.

Which of the following can use the range() function to loop through a block of statements? a. for statement b. an if statement c. while statement d. a break statement

a. for statement

The and operator has a. higher precedence than the or operator, but lower precedence than the not operator b. higher precedence than the or and not operators c. lower precedence than the or operator, but higher precedence than the not operator d. lower precedence than the or and not operators

a. higher precedence than the or operator, but lower precedence than the not operator

Which of the following statements imports a module into the default namespace? a. import temperature b. import temperature as temp c. from temperature import * d. import temperature as t

a. import temperature

Before you can use a standard module like the random module, you need to a. import the module b. import the module into a custom namespace c. import the module into the global namespace d. import the module into its default namespace

a. import the module

A local variable is defined a. inside a function b. inside the main() function c. inside an if statement d. outside of all functions

a. inside a function

A file that contains reusable code is called a a. module b. hierarchy chart c. function d. namespace

a. module

Which of the following creates a Boolean variable? a- flag = True or False b- flag = True c- if flag == True: d- flag == "False"

b- flag = True

For the following code, what will the result be if the user enters 5 at the prompt? sum = 0 end_value = int(input("Enter a number: ")) for i in range(1, end_value): sum = sum + i print(sum, end=", ") a. 10, b. 1, 3, 6, 10, c. 1, 3, 6, 10, 16, d. 1, 2, 3, 4, 5,

b. 1, 3, 6, 10,

What is the value of my_num after the following statements execute? my_num = 5 my_num += 20 my_num -= 12 my_num *= 0.5 a. 0.5 b. 6.5 c. 6 d. 12.5

b. 6.5

What is the first error in the display_info() function? 1. # This application displays a student's score after a 5-point curve 2. 3. def display_info(fname, lname, score): 4. print("Hello, " , fname, " " , Lname) 5. print("Your score on this exam is ", score) 6. score = score + 5 7. 8. def main(): 9. first = input("first name: ") 10. last = input("last name: ") 11. grade = input("exam score: ") 12. display_info(last, first, score) 13. 14. # if started as the main module, call the main function 15. if __name__ == "__main__": 16. main() a. The arguments on line 3 should be first and last, not fname and lname b. The variable Lname on line 4 does not exist c. The print statement on line 4 should use the + operator instead of commas d. You cannot add a number to itself, as on line 6

b. The variable Lname on line 4 does not exist

A return statement a. must be coded within every function b. can be used to return a local variable to the calling function c. can be used to allow the function to modify the value of a global variable d. can only be used once in each function

b. can be used to return a local variable to the calling function

What line number of the following code contains an error and what type of error is it? 1. def sales_tax(amt) 2. sale = amt + (amt * .06) 3. return sale 4. 5. def main(): 6. print("Welcome to the 6% tax calculator!\n") 7. total = int(input("Please enter the total amount: ")) 8. print("The total amount after tax is: ", sales_tax(total)) a. line 1, runtime error b. line 1, syntax error c. line 3, runtime error d. line 8, logic error

b. line 1, syntax error

What is the scope of the variable named s? def get_username(first, last): s = first + "." + last return s.lower() def main(): first_name = input("Enter your first name: ") last_name = input("Enter your last name: ") username = get_username(first_name, last_name) print("Your username is: " + username) if __name__ == "__main__": main() a. global b. local c. global in main() but local in get_username() d. local in main() but global in get_username()

b. local

To compare two strings with mixed uppercase and lowercase letters, a programmer can first use the a. upper() method to convert the first character in each string to uppercase. b. lower() method to convert all characters in each string to lowercase. c. lower() method to convert all characters after the first character in each string to lowercase. d. upper() method to convert all characters after the first character in each string to uppercase.

b. lower() method to convert all characters in each string to lowercase.

Given that pi = 3.1415926535, which of the following print() functions displays: pi = 3.14 a. print("pi= ", round(pi, 2)) b. print("pi = " + round(pi, 2)) c. print("pi = ", float(pi, 2)) d. print("pi = ", round(pi))

b. print("pi = " + round(pi, 2))

The goal of __________ is to find all the errors in a program. a. editing b. testing c. debugging d. interpreting

b. testing

Which of the following will get a floating-point number from the user? a- my_number = input(float("Enter a number:")) b- my_number = float(input "Enter a number:") c- my_number = float(input("Enter a number:")) d- my_number = input("Enter a number:") float_num = my_number

c- my_number = float(input("Enter a number:"))

Python will sort the strings that follow in this sequence: Peach peach 1peach 10Peaches a. Peach, peach, 1peach, 10Peaches b. 1peach, 10Peaches, Peach, peach c. 10Peaches, 1peach, Peach, peach d. 10peaches, 1peach, peach, Peach

c. 10Peaches, 1peach, Peach, peach

Which type of expression has a value of either true or false? a. Relational b. Binary c. Boolean d. if-else

c. Boolean

Which of the following is not true about top-down coding and testing? a. You start with just a few functions and the code in the main() function that calls those functions. b. You add the code for a few functions at a time, including the code in the main() function that calls those functions. c. You should always start by coding and testing the most difficult functions. d. Top-down testing makes debugging easier because you know that any errors are caused by the code you've just added.

c. You should always start by coding and testing the most difficult functions.

The best way to call the main() function of a program is to code a. main() b. an if statement that calls the main() function only if the function exists c. an if statement that calls the main() function only if the current module is the main module d. a while statement that calls the main() function in each loop

c. an if statement that calls the main() function only if the current module is the main module

To jump to the end of the current loop, you can use the a. end statement b. continue statement c. break statement d. switch statement

c. break statement

The goal of __________ is to fix all the errors in a program. a. editing b. testing c. debugging d. interpreting

c. debugging

When you trace the execution of a program, you insert print() functions at key points in the program. It makes sense for these functions to do all but one of the following. Which one is it? a. display the name of the function that the print() function is in b. display the values of the local variables in the function c. display the values of the global constants used by the function d. display the values of the global variables used by the function

c. display the values of the global constants used by the function

What will be the result of the following code if the user enters 81 at the prompt? score_curve = 7 score = input("Enter your score on the exam: ") score_curve += score print(score_curve) a. 88 will be displayed on the console. b. 81 will be displayed on the console c. error: you cannot use the += operator to add a string variable to an int value d. error: you cannot print a numeric variable

c. error: you cannot use the += operator to add a string variable to an int value

To test the functions of a module from the IDLE shell, you a. run the module with varying input values b. run the module and then call any function from the IDLE shell c. import the module and then call any function from the IDLE shell d. call any function in the module by using the default namespace

c. import the module and then call any function from the IDLE shell

Python relies on correct __________ to determine the meaning of a statement. a. continuation b. punctuation c. indentation d. comments

c. indentation

Which of the following is not a common type of syntax error? a. forgetting a colon b. forgetting to close a parenthesis c. invalid variable names d. improper indentation

c. invalid variable names

What line number of the following code contains an error and what type of error is it? 1. count = 1 2. while count <= 4: 3. print(count, end=" ") 4. i *= 1 5. print("\nThe loop has ended.") a. line 2, syntax error b. line 3, syntax error c. line 4, runtime error d. line 5, logic error

c. line 4, runtime error

When you plan the test runs for a program, you should do all but one of the following. Which one is it? a. list the valid entries for each test run b. list the invalid entries and unexpected user actions for each test run c. list the expected exceptions for each test run d. list the expected results for each test run

c. list the expected exceptions for each test run

What will be displayed after the following code executes? guess = 19 if guess < 19: print("Too low") elif guess > 19: print("Too high") a. Too low b. Too high c. nothing will display

c. nothing will display

Which type of error throws an exception that stops execution of the program? a. exceptional b. syntax c. runtime d. logic

c. runtime

The following is an example of __________. print("Hello out there!") # get input name = input("Who are you?") print("Goodbye, " , name) a. Java code b. bytecode c. source code d. shebang line

c. source code

When an exception occurs while a program is running, a. the program crashes b. an error message is displayed on the console c. the program crashes and an error message is displayed d. an error message is displayed but the program continues

c. the program crashes and an error message is displayed

What is the argument of the print() function in the following Python statement? print("My student ID is " + str(123456) ) a. 123456 b. str(123456) c. "My student ID is " d. "My student ID is " + str(123456)

d. "My student ID is " + str(123456)

Python comments a. are ignored by the compiler b. can be used to document what a program or portion of code does c. can be used so certain lines of code are not executed during testing d. all of the above

d. all of the above

Python is considered a good first language to learn because: a. it has a simple syntax b. it has most of the features of traditional programming languages c. it is open source d. all of the above

d. all of the above

A runtime error is also known as: a. syntax error b. logical error c. violation d. an exception

d. an exception

Which of the following would produce a user-friendly correct result? 1. discount_rate = .1 2. item = 89.95 3. discount = item * discount_rate 4. print("The discount on this item is $", discount)) Output: The discount on this item is $ 8.995000000000001 a. change line 1 to: discount_rate = 10% b. change line 2 to: item = int(89.95) c. change line 3 to: discount = int(item * discount_rate) d. change line 4 to: print("The discount on this item is $", round(discount, 2))

d. change line 4 to: print("The discount on this item is $", round(discount, 2))

Which of the following data types would you use to store the number 25.62? a. Str b. Int c. num d. float

d. float

A global variable a. is defined inside the main() function b. cannot be modified inside a function c. cannot be accessed from within a function d. is defined outside of all functions

d. is defined outside of all functions

Which type of error prevents a program from compiling and running? e. Exceptional f. Syntax g. Runtime h. logic

f. Syntax


संबंधित स्टडी सेट्स

Physics A2 Temp UP TO DATE (EXCLUDING MAGNETIC FIELDS) For printing

View Set

Unit 3, Section 2: Political Culture

View Set

2.3 Macroeconomic Objectives: Inflation

View Set

II. Azure Resource & Resource Groups

View Set