CIS 3260 Midterm
Python was created by _____.
Guido van Rossum
_____ is the physical aspect of the computer that can be seen.
Hardware
Suppose income is 5000, what will be displayed by the following code? if income > 3000: print("Income is greater than 3000") elif income > 4000: print("Income is greater than 4000")
Income is greater than 3000
In some languages every statement ends with a semi-colon, ;. What happens if you put a semi-colon at the end of a Python statement?
It doesn't affect the statement. Semi-colon is optional at the end of a statement in Python.
Suppose you write a program for computing the perimeter of a circle and you mistakenly write your program so that is computes the area of a circle. What kind of error is this?
Logic error
Computer can execute the code in _____.
Machine language
_____ is a program that runs on a computer to manage and control a computer's activities.
Operating system
What if you put a period at the end of a statement?
Period is not allowed at the end of a statement.
_____ is interpreted.
Python
Which of the following statements is true?
Python 3 is a newer version, but it is NOT backward compatible with Python 2
_____ are instructions to the computer. (Multiple correct answers)
Software, or Programs
Python is case-sensitive.
True
What is the output of the code in a) and b) if number is 40 and 55? (a) if number % 2 == 0: print(number, "is even.") print(number, "is odd.") (b) if number % 2 == 0: print(number, "is even.") else: print(number, is odd.")
When number is 40 a) 40 is even 40 is odd b) 40 is even When number is 55 a) 55 is odd b) 55 is even
_____ is an operating system.
Windows XP
In math notation you can multiply x and y like this: x y. What happens if you try that in Python?
You have to use * between x and y.
What are the results of the following expressions? a. 42 / 5 b. 42 // 5 c. 42 % 5 d. 40 %5 e. 5 ** 2
a. 8.4 b. 8 c. 2 d. 0 e. 25
Write a program that prompts the user to enter two characters and displays the status and major represented in the characters. The first character is number 1, 2, 3, and 4, indicating whether a student is a freshman, sophomore, junior, or senior; the second character indicates the major. Suppose the following characters are used to denote the majors: A: Accounting C: Computer Information Systems
firstCharacter = float(input("Enter First Character:")) secondCharacter = str(input("Enter Second Character:")) if firstCharacter == 1: grade = "Freshman" elif firstCharacter == 2: grade = "Sophomore" elif firstCharacter == 3: grade = "Junior" elif firstCharacter == 4: grade = "Senior" if secondCharacter == "A": major = "Accounting" elif secondCharacter == "C": major = "Computer Information Systems" print(grade, "in", major)
Suppose isPrice is a boolean variable, which of the following is the CORRECT and SHORTEST statement for testing if isPrime is true.
if isPrime:
Analyze the following code? interestRate = 0.05 interest = interestrate * 45
interestrate is not defined
The following code displays _____. temperature = 50 if temperature >= 100: print("too hot") elif temperature <= 40: print("too cold") else: print("just right")
just right
Suppose m and r are integers. Write a Python expression for mr^2. What is 2 * 2 ** 3?
mr^2 = m * r * r, or m * r **2 2 * 2 ** 3 = 16
Write a program that asks the user to enter the purchase amount and then displays the sales tax. Assume the sales tax is 6% of the purchase. Sample run: Enter purchase amount: 197.55 Sales tax is 11.853 Steps you could follow: Step 1: get input from use for the purchase amount, store it into variable "price" Step 2: calculate the sales tax, store it into variable "tax" Step 3: display the sales tax
price = float(input("Enter purchase amount:")) tax = price * 0.06 print("Sales tax is", tax) Round two digits after decimal point: price = float(input("Enter purchase amount:")) tax = price * 0.06 print("Sales tax is", round(tax,2))
Which of the following code is correct?
print("Java and Python")
The volume of a sphere with radius r is 4/3 π r^3. What is the volume of a sphere with radius 5?
print((4/3)*(3.14159)*(5**3))
Write a program that reads in the radius and length of a cylinder and computers the area and volume using the following formulas: area = radius * radius * π volume = area * length Sample run: Enter the radius of a cylinder: 5.5 Enter the length of a cylinder: 12.9 The area is 95.0330975 The volume of the cylinder is 1225.92695775
radius = float(input("Enter the radius of a cylinder:")) length = float(input("Enter the length of a cylinder:")) area = radius * radius * 3.14159 volume = area * length print("The area is", area) print("The volume of a cylinder is", volume)
Write an if statement that increases pay by 3% if score is greater than 90. Assume we have assigned 100 to score, and 1000 to pay. score = 100 pay = 1000
score = 100 pay = 1000 if score > 90: pay = pay + pay * 0.03 print(pay)
To check if x has an absolute value smaller than or equal to 4, which of the following conditional expression can be used?
x <= 4 and x >= -4
After the following statements, what are the values of x and y? x = 15 y = x x = 22
x = 22 y = 15
What is x after the following statements? x = 1 y = 2 x *= y + 1
x is 3 1 *= 2 + 1 1 *= 3 1*3 = 3
Assume x is an integer. Write a simple Boolean expression as a condition to check if x is not divisible by 2.
x%2 != 0
The NOT equal comparison operator is
!=
What are the results for following expressions in Python? Assume x = 2, y = 3, z = 1 (1) x < y and y < z (2) not(z < y) (3) x < y < z (4) x % 2 == 0 (5) x % 2 == 0 or z % 5 == 0 (6) x / 5 (7) x // 5 (8) x % 5 (9) x ** 2
(1) False (2) False (3) False (4) True (5) True (6) 0.4 (7) 0 (8) 0 (9) 4
Clunker Motors Inc. is recalling all vehicles from model years 1995-1998 and 2004-2006. Given a variable modelYear, write a simple Boolean expression as a condition to check if the value of modelYear does fall within either of the two ranges or not.
-
What is the output of the code below if number is 4? if number % 2 == 0: print(number, "is even.") print(number, "is odd.")
4 is even
We've seen that n = 42 is legal. What about 42 = n?
42 = n is not legal. Syntax error
_____ translates high-level language program into machine language program.
A compiler / An interpreter
Why do computers use zeros and ones?
Because digital devices have two stable states and it is natural to use one state for 0 and the other for 1
Take two numbers and check whether the sum is greater than 5, less than 5,equal to 5; and whether sum is odd. Complete the program below by filling in blanks. print("Enter first number") first = int( input()) print("Enter second number") second = int( input()) #Blank 1 print("Sum is greater than 5:",sum>5) print("Sum is equal to 5:",#Blank 2) print("Sum is lesser than 5:",#Blank 3) print("Sum is an odd:",#Blank 4)
Blank 1: sum = first + second Blank 2: sum == 5 Blank 3: sum < 5 Blank 4: sum%2 == 1
How about x = y = 1?
Correct