Python Interview Questions
-
Python - Conditions and Branching
1. List the 3 keywords used for conditional execution?
A) = = != <>
B) if else elif
C) and or not
Answer: if else elif
|
2. List the 6 conditional operators?
A) and or not xor neg pos
B) if else elif and or not
C) > < >= <= == !=
Answer: > < >= <= == !=
|
3. What is the name of the operator used to assign 1 of 2 values depending on a condition?
A) logical operator
B) conditional operator
C) ternary operator
Answer: ternary operator
|
4. List the 3 logical operators?
A) and or not
B) = = != <>
C) and xor not
Answer: and xor not
|
5. Write code that adds num_1 to num_2 and saves the result to sum?
A) sum = num_1 + num_2
B) sum = add(num_1, num_2)
C) sum <= num_1 + num_2
Answer: sum = num_1 + num_2
|
6. What operator returns the remainder of a division?
A) %
B) /
C) div
Answer: %
|
7. Use modulus in a condition to test for an even value?
A) if (i % 2) == 0
B) if (i / 2) == 0
C) if (i div 2) == 0
Answer: if (i % 2) == 0
|
8. How do you cast num_1 from a string to an int?
A) num_1 = str(num_1)
B) num_1 = integer(num_1)
C) num_1 = int(num_1)
Answer: num_1 = int(num_1)
|
9. Show the part between curly brackets you would use to format a float to show only 2 decimal values?
A) {.2f}
B) {:.2f}
C) {:.2d}
Answer: {:.2f}
|
10. Write the print function using format to output this for example 1 + 2 = 3?
A) format("1 + 2 = 3")
B) print(format("1 + 2 = 3"))
C) print("{} + {} = {}".format(1,2,3))
Answer: print("{} + {} = {}".format(1,2,3))
|
-