programming for everybody week 4 quiz answers

Chapter 2

1. In the following code,

print(98.6)

What is "98.6"?

  • An iteration / loop statement
  • A constant
  • A variable
  • A conditional statement

2. What does the following code print out?

print(“123” + “abc”)
  • This is a syntax error because you cannot add strings
  • 123abc
  • 123+abc
  • hello world

3. Which of the following is a bad Python variable name?

  • SPAM23
  • #spam
  • spam_23
  • _spam

4. Which of the following is not a Python reserved word?

  • speed
  • else
  • for
  • if

5. Assume the variable x has been initialized to an integer value (e.g., x = 3). What does the following statement do?

x = x + 2
  • Create a function called “x” and put the value 2 in the function
  • Exit the program
  • Retrieve the current value for x, add two to it and put the sum back into x
  • This would fail as it is a syntax error

6. Which of the following elements of a mathematical expression in Python is evaluated first?

  • Parentheses ( )
  • Subtraction –
  • Addition +
  • Multiplication *

7. What is the value of the following expression

42 % 10

Hint - the "%" is the remainder operator

  • 420
  • 4210
  • 42
  • 2

8. What will be the value of x after the following statement executes:

x = 1 + 2 * 3 – 8 / 4
  • 2
  • 5.0
  • 1.0
  • 3

9. What will be the value of x when the following statement is executed:

x = int(98.6)
  • 6
  • 98
  • 99
  • 100

10.What does the Python input() function do?

  • Pause the program and read data from the user
  • Connect to the network and retrieve a web page.
  • Read the memory of the running program
  • Take a screen shot from an area of the screen

11. Which of the following variables is the "most mnemonic"?

  • x1q3z9ocd
  • hours
  • x
  • variable_173

12. In the following code,

x=2

What is "x"?

  • A function
  • A constant
  • A Central Processing Unit
  • A variable

13. Which of the following is not a Python reserved word?

  • spam
  • break
  • continue
  • for

Assignment 2.2

2.2 Write a program that uses input to prompt a user for their name and then welcomes them. Note that input will pop up a dialog box. Enter Sarah in the pop-up box when you are prompted so your output will match the desired output.

name = input(“Please enter your name: “)
print(“Hello ” + name)

Assignment 2.3

2.3 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Use 35 hours and a rate of 2.75 per hour to test the program (the pay should be 96.25). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking or bad user data.

hours = input(“Enter the number of hours worked: “)
rate = input(“Enter the rate per hour: “)

hours = float(hours)
rate = float(rate)

gross_pay = hours * rate

print(“Pay:”, gross_pay)

Leave a Reply