2. Elementary Programming - 34 Questions

2. Elementary Programming

34 Questions
Created by Y. Daniel Liang
Free
Python is a computer programming language that lets you work more quickly than other programming languages. Experienced programmers in any other language can pick up Python very quickly, and beginners find the clean syntax and indentation structure easy to learn. This quiz tests elementary programming language in python.
1. What function do you use to read a string?

input("Enter a string")
eval(input("Enter a string"))
enter("Enter a string")
eval(enter("Enter a string"))

2. What is the result of eval("1 + 3 * 2")?

"1 + 3 * 2"
7
8
"1 + 6"

3. If you enter 1 2 3 in three separate lines, when you run this program, what will be displayed?

print("Enter three numbers: ")
number1 = eval(input())
number2 = eval(input())
number3 = eval(input())

# Compute average
average = (number1 + number2 + number3) / 3

# Display result
print(average)

1.0
2.0
3.0
4.0

4. _______ is the code in natural language mixed with some program code.

Python program
A Python statement
Pseudocode
A flowchart diagram

5. If you enter 1 2 3 in one line, when you run this program, what will happen?

print("Enter three numbers: ")
number1 = eval(input())
number2 = eval(input())
number3 = eval(input())

# Compute average
average = (number1 + number2 + number3) / 3

# Display result
print(average)

The program runs correctly and displays 1.0
The program runs correctly and displays 2.0
The program runs correctly and displays 3.0
The program will have a runtime error on the input.

6. You can place the line continuation symbol __ at the end of a line to tell the interpreter that the statement is continued on the next line.

/
\
#
*

7. Which of the following is a valid identifier?

\$343
mile
9X
8+9

8. If you enter 1, 2, 3, in one line, when you run this program, what will be displayed?

number1, number2, number3 = eval(input("Enter three numbers: "))

# Compute average
average = (number1 + number2 + number3) / 3

# Display result
print(average)

1.0
2.0
3.0
4.0

9. What will be displayed by the following code?

x = 1
x = 2 * x + 1
print(x)

1
2
3
4

10. What will be displayed by the following code?

x = 1
x = x + 2.5
print(x)

2
3
3.5
The statements are illegal

11. What will be displayed by the following code?

x, y = 1, 2
x, y = y, x
print(x, y)

1 1
2 2
1 2
2 1

12. The following code reads two number. Which of the following is the correct input for the code?

x, y = eval(input("Enter two numbers: "))

1 2
"1 2"
1, 2
1, 2,

13. What is the result of 45 / 4?

10
11
11.25
12

14. In the expression 45 / 4, the values on the left and right of the / symbol are called ____.

operators
operands
parameters
arguments

15. What is the result of 45 // 4?

10
11
11.25
12

16. Which of the following expressions will yield 0.5?

1 / 2
1 // 2
1.0 // 2
1.0 / 3

17. Which of the following expression results in a value 1?

2 % 1
15 % 4
25 % 5
37 % 6

18. 25 % 1 is _____

1
2
3
0

19. 24 % 5 is _____

4
3
2
1

20. 2 ** 3 evaluates to __________.

9
8
9.0
8.0

21. 2 ** 3.0 evaluates to __________.

9
8
9.0
8.0

22. 2 * 3 ** 2 evaluates to __________.

36
12
18
81

23. What is y displayed in the following code?

x = 1
y = x = x + 1
print("y is", y)

y is 0.
y is 1 because x is assigned to y first.
y is 2 because x + 1 is assigned to x and then x is assigned to y.
The program has a compile error since x is redeclared in the statement int y = x = x + 1.

24. If a number is too large to be stored in memory, it _____________.

causes overflow
causes underflow
causes no error
cannot happen in Python

25. What is the result of evaluating 2 + 2 ** 3 / 2?

4
6
4.0
6.0

26. What is the value of i printed?

j = i = 1
i += j + j * 5
print("What is i?", i)

1
5
6
7

27. What is x after the following statements?

x = 1
x *= x + 1

x is 1
x is 2
x is 3
x is 4

28. What is x after the following statements?

x = 2
y = 1
x *= y + 1

x is 1
x is 2
x is 3
x is 4

29. Which of the following statements are the same?

(A) x -= x + 4
(B) x = x + 4 - x
(C) x = x - (x + 4)

(A) and (B) are the same
(A) and (C) are the same
(B) and (C) are the same
(A), (B), and (C) are the same

30. Suppose x is 1. What is x after x += 2?

1
2
3
4

31. Suppose x is 1. What is x after x -= 1?

0
1
2
-1

32. What is x after the following statements?

x = 1
y = 2
x *= y + 1

x is 1
x is 2
x is 3
x is 4

33. Which of the following functions return 4.

int(3.4)
int(3.9)
round(3.4)
round(3.9)