Survey

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts

Turing's proof wikipedia, lookup

Elementary mathematics wikipedia, lookup

Transcript
```Practice Exam 1
Multiple Choice and fill in the blank
1. Show the printout of the following code: (write the printout next to each
print statement if the print statement is executed in the program). If
nothing prints then write “nothing prints”.
print(2 ** 3)
8
print(34 % 7)
6
print(3 + 4 * 2 > 2 * 9)
False
number = 4
if number % 3 == 0:
print(3 * number)
nothing prints
print(4 * number)
16
x = 943;
print(x // 100)
9
print(x % 100)
43
print(x, "is", "even" if x % 2 == 0 else "odd")
y = -1
y += 1
print(y)
943 is odd
0
2. If you enter 1 2 3 in three separate lines, when you run this
program, what will be the output?
print("Enter three numbers: ")
number1 = eval(input())
number2 = eval(input())
number3 = eval(input())
# Compute average
average = (number1 + number2 + number3) / 3
# Display result
print(average)
2.0
1
3. What is the printout of the following code?
x, y = 1, 2
x, y = y, x
print(x, y)
2 1
4. What is print(round(3.52))? 4
5. What function do you use to read a string?
A.
B.
C.
D.
enter("Enter a string")
eval(input("Enter a string"))
eval(enter("Enter a string"))
input("Enter a string")
6. Suppose i is an int type variable. Which of the following
statements display the character whose Unicode is stored in
variable i?
A.
B.
C.
D.
print(int(i))
print(i)
print(chr(i))
print(str(i))
7. What is the output of the following code?
ch = 'F'
if ch >= 'A' and ch <= 'Z':
print(ch)
F
8. The "less than or equal to" comparison operator is __<=_____.
9. Analyze the following code.
even = False
if even:
print("It is even!")
A. The code displays It is even!
B. The code displays nothing.
C. The code is wrong. You should replace if even: with if
even = True:
D. The code is wrong. You should replace if even: with if
even == True:
2
10. Suppose x = 1, y = -1, and z = 1. What is the printout of
the following statement?
if x > 0:
if y > 0:
print("x > 0 and y > 0")
elif z > 0:
print("x < 0 and z > 0")
A.
B.
C.
D.
no printout.
x > 0 and y > 0
x < 0 and z < 0
x < 0 and z > 0
11. Assume x is 0. What is the output of the following
statement?
if x > 0:
print("x is greater than 0")
elif x < 0:
print("x is less than 0")
else:
print("x equals 0")
A.
B.
C.
D.
x is greater than 0
x is less than 0
None
x equals 0
12. What is x after evaluating
x = 2 if 2 > 3 else 3
x = 3
13. Analyze the following code:
even = 231 % 2 == 0
if even = True:
print("It is even!")
else:
print("It is odd!")
A.
B.
C.
D.
The
The
The
The
program
program
program
program
has a runtime error
displays "It is odd!"
displays "It is even!"
has a syntax error on even = True
3
14.
BC
15.
B
16.
B
17.
C
18.
C
19.
B E
20.
C
4
21.
B
22.
C
23.
A
24.
C
25.
C
26.
B
5
27.
A C
28.
B
Programs
29. Write a program that prompts the user to enter the three
points (x1, y1), (x2, y2), and (x3, y3) of a triangle and
displays its area. The formula for computing the area of a
triangle is
s = (side1 + side2 + side3)/2
area 
s(s  side1)(s  side2)(s  side3)
Here is a sample run:
Note: The length of a side is
( x1 x2)2  ( y1 y 2)2
x1, y1, x2, y2, x3, y3 = \
eval(input("Enter three points for a triangle: "))
# Compute the length of
side1 = ((x1 - x2) ** 2
side2 = ((x1 - x3) ** 2
side3 = ((x3 - x2) ** 2
the three sides
+ (y1 - y2) ** 2) ** 0.5
+ (y1 - y3) ** 2) ** 0.5
+ (y3 - y2) ** 2) ** 0.5
s = (side1 + side2 + side3) / 2;
area = (s * (s - side1) * (s - side2) * (s - side3)) ** 0.5
print("The area of the triangle is", area)
6
30. Write a program that prompts the user to enter three integers
and displays them in increasing order.
# Enter three numbers
number1, number2, number3 = eval(input("Enter three integers: "))
if number1 > number2:
number1, number2 = number2, number1
if number2 > number3:
number2, number3 = number3, number2
if number1 > number2:
number1, number2 = number2, number1
print("The sorted numbers are", number1, number2, number3)
31.
The following program should prompt the user to enter two
integers and then print a blank line followed by the
product of those two integers. For example, here is the
output of the program if the user enters 2 and 3:
Enter 2 integers: 2, 3
2 * 3 = 6
#Enter two integers
number1, number2 = eval(input("Enter two integers: "))
#print blank line
print()
#print results
print(number1, '*', number2, '=', number1 * number2)
32.
Write a multi-way if-elif-else statement that will print
out whether an integer, n, is





4th quartile (between 75 and 100, inclusive)
3rd quartile (between 50 and 74, inclusive)
2nd quartile (between 25 and 49, inclusive)
1st quartile (between 0 and 24, inclusive)
Out of range (not between 0 and 100, inclusive)
For example, if n is 85, your multiway if-elif-else
statement should print out
7
4th quartile
If n is 50, the statement should print out
3rd quartile
If n is 101, the statement should print out
Out of range
Use the simplest possible multi-way-elif-else statement
with the simplest possible Boolean expressions.
You may assume that n is declared and assigned a value
somewhere in the program before the multi-way-elif-else
statement (i.e., you do not need to declare and assign it a
value).
if n >= 75 and n <= 100:
print("4th quartile")
elif n >= 50 and n <= 74:
print("3rd quartile")
elif n >= 25 and n <= 49:
print("2nd quartile")
elif n >= 0 and n <= 24:
print("1st quartile")
else:
print("Out of Range")
8
```
Related documents