Intro to Programming Chapters 1-5 Exam
5
Given: x = 7 , y = 2 , z = 1.5 What is the value of new_num after the following statement executes? new_num = x / y + z
a. 2 b. 5 c. 6 d. 12 Correct Answer is C.
How many times will "Hi again!" be displayed after the following code executes? for i in range(0, 12, 2): print("Hi, again!")
Break Statement
To jump to the end of the current loop, you can use the
break statement
To jump to the end of the current loop, you can use the
The f5 Key
To run a Python program from IDLE, you use:
import the module and then call any function from the IDLE shell
To test the functions of a module from the IDLE shell, you
33
What is the value of my_num after the following statement executes? my_num = (50 + 2 * 10 - 4) / 2
Line 4, Runtime Error
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.")
lions & tigers & bears oh, my!!
What will the following print() function display? print("lions", "tigers", "bears", sep = ' & ', end = ' oh, my!!')
a. view the values of all the variables that you've stepped through b. run the program until the next breakpoint is reached c. view the values of the local variables that are in scope d. step through the program one statement at a time Correct Answer is A.
When the IDLE debugger reaches a breakpoint, you can do all but one of the following. Which one is it?
sort sequence
When two strings are compared, they are evaluated based on the ___________________ of the characters in the string.
Via a Command Prompt
A console application runs
module
A file that contains reusable code is called a
Inside a function
A local variable is defined
Programmer didn't test for a 0 value in the denominator.
A programmer created this code for a client: def divide(numerator, denominator): quotient = numerator / denominator return quotient def main(): numerator = int(input("Enter the numerator: ")) denominator = int(input("Enter the denominator: ")) quotient = divide(numerator, denominator) print("The quotient is ", quotient) if __name__ == "__main__": main() The programmer tested this code with the following three sets of data: 1. numerator = 9, denominator = 3 2. numerator = -50, denominator = 5 3. numerator = 389, denominator = -26 The results were: 1. The quotient is 3.0 2. The quotient is -10.0 3. The quotient is -14.961538461538462 However, once the program was in use, the client reported that sometimes the program crashed. Can you explain why?
Import the Module
Before you can use a standard module like the random module, you need to
a. You bought 1 widget(s) today. b. You bought 0 widget(s) today. c. Nothing, the break statement will cause the program to end d. You bought no widget(s) today. Correct Answer is B.
Code Example 3-1 num_widgets = 0 while True: choice = input("Would you like to buy a widget? (y/n): ") if choice.lower() == "y": num_widgets += 1 else: break print("You bought", num_widgets , "widget(s).") Refer to Code Example 3-1. If the user enters "no" at the prompt, what does the program print to the console?
lopez.maria
Code Example 4-1 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() Refer to Code Example 4-1: If the user enters 'Lopez' for the first prompt in main() and 'Maria' for the second prompt, what will display?
change line 8 to: average_cost = round(item_total / (count - 1))
Code Example 5-1 1. count = 1 2. item_total = 0 3. item = 0 4. while count < 4: 5. item = int(input("Enter item cost: ")) 6. item_total += item 7. count += 1 8. average_cost = round(item_total / count) 9. print("Total cost:", item_total, "\nAverage cost:", average_cost) Refer to Code Example 5-1: If the user enters 5, 10, and 15 at the prompts, the output is: Total cost: 30 Average cost: 8 Which line should be changed to fix this logic error?
The curve is calculated after the score has been displayed.
Code Example 5-2 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() Refer to Code Example 5-2: Assuming the coding errors have been fixed, what is the logic error in this program?
a. the coding of control structures b. the conditions for control statements c. the modules used in a program d. the functions used by a program Correct Answer is A.
Pseudocode can be used to plan
a. are ignored by the compiler b. can be used so certain lines of code are not executed during testing c. can be used to document what a program or portion of code does
Python comments
Main Memory
The data in __________ is lost when an application ends.
shebang line
The following is an example of __________. #!/usr/bin/env python 3
Source Code
The following is an example of __________. print("Hello out there!") # get input name = input("Who are you?") print("Goodbye, " , name)
Debugging
The goal of __________ is to fix all the errors in a program.
code the name of the argument, the assignment operator (=), and the default value
To assign a default value to an argument when you define a function, you
a set of parentheses that contains zero or more arguments
To call a function, you code the function name and
lower() method to convert all characters in each string to lowercase.
To compare two strings with mixed uppercase and lowercase letters, a programmer can first use the
a. display the values of the global constants used by the function b. display the values of the global variables used by the function c. display the values of the local variables in the function d. display the name of the function that the print() function is in Correct Answer is A.
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. flag = True or False b. flag == "False" c. if flag == True: d. flag = True Correct Answer is D.
Which of the following creates a Boolean variable?
a. int b. str c. num d. float Correct Answer is D.
Which of the following data types would you use to store the number 25.62?
a. firstName b. first_name c. pay_rate d. pRate Correct Answer is D.
Which of the following doesn't follow the best naming practices for variables?
a. nest a for loop inside a while loop b. nest an if statement inside a for loop c. nest a while loop inside an elif clause d. nest an else clause within an elif clause Correct Answer is D.
Which of the following is not an acceptable way to code a nested structure?
a. pay_rate b. firstName c. first_name d. prate Correct Answer is B.
Which of the following variable names uses camel case?
syntax error
Which type of error prevents a program from compiling and running?
Syntax Errors
Which type of errors must be fixed before the program can be compiled?
A return Statement
can be used to return a local variable to the calling function