Quiz 2
February 8, 2005
CMPT 120, Spring 2005
Instructor: Snezana
Mitrovic-Minic
Total: 16 points + 3 bonus point
1. (1 point) What is a variable?
2. (1 point) What is a boolean expression?
3. (1 point) List Python’s comparison operators.
4. (1 point) List Python’s logical operators
5. (1 point) Are you allowed to use digits in a variable’s name? Explain your answer.
6. (3 points)
a. What does the following print?
>>>
1 * 2 + 3 * 4
b. What does the following print?
>>> 1 * (2 + 3) * 4
c. How do you convert '97' to a number?
d. How do you convert the string '-58.5' to a number?
e. How do you convert the number 7993.64 to a string?
f. What is the difference between 2.6 and "2.6"?
7. (2 points) Write a Python that calculates and prints on the screen the number of seconds in a year.
8. (2 points) Write a Python program that accepts an integer from a user and prints a message saying if x is divisible by 3, or if it is not
9. (2 points) Convert numbers 231 and 24 in binary and add them up.
10. (1 point) Convert numbers 1110111 to decimal number.
11. (1 point) Represent number –3 in the two’s complement notation. Use four bits.
12. Bonus: (3 points) Write a Python program that prints first 30 numbers of Fibonacci sequence in one line. The Fibonacci sequence is as follows:
0, 1, 1, 2, 3, 5, 8, ….
i.e. first two numbers are 0 and 1, each number after that is equal to the sum of the two numbers that preceed it. For example third number is equal to the sum of the first and second number, fourth number is equal to the sum of third and fourth number, and so on.