Variables And Operators of Python

1.What is the correct syntax to declare a variable in Python?

a) var = 5
b) variable = 5
c) variable == 5
d) var 5

Answer: b) variable = 5

Explanation: In Python, the syntax to declare a variable is to write the variable name followed by an equal sign and the value you want to assign to the variable. Therefore, option b) is the correct syntax to declare a variable in Python.

2.What is the modulus operator used for in Python?

a) It multiplies two numbers.
b) It divides two numbers.
c) It returns the remainder of a division operation.
d) It performs exponentiation.

Answer: c) It returns the remainder of a division operation.

Explanation: The modulus operator (%) returns the remainder of a division operation. For example, 7 % 3 would return 1, because 3 goes into 7 twice with a remainder of 1.

3.Which operator is used for exponentiation in Python?

a) *
b) ^
c) **
d) //

Answer: c) **

Explanation: The double asterisk operator (**) is used for exponentiation in Python. For example, 2 ** 3 would return 8, because 2 to the power of 3 is 8.


4.What is the difference between the “==” operator and the “=” operator in Python?

a) The “==” operator assigns a value to a variable, while the “=” operator compares two values for equality.
b) The “==” operator compares two values for equality, while the “=” operator assigns a value to a variable.
c) The “==” operator performs addition, while the “=” operator performs assignment.
d) The “==” operator performs multiplication, while the “=” operator performs division.


Answer: b) The “==” operator compares two values for equality, while the “=” operator assigns a value to a variable.

Explanation: The “==” operator is used to compare two values for equality. For example, 5 == 5 would return True, because 5 is equal to 5. The “=” operator is used to assign a value to a variable. For example, x = 5 would assign the value of 5 to the variable x.

5.What is the result of the following operation: 10 / 3 ?

a) 3.33
b) 3
c) 3.0
d) 4

Answer: a) 3.33

Explanation: In Python, when you divide two integers, the result will be a float if the division results in a decimal. Therefore, the result of 10 / 3 would be 3.33.

6.Which operator is used for floor division in Python?

a) *
b) //
c) %
d) **


Answer: b) //

Explanation: The double slash operator (//) is used for floor division in Python. Floor division returns the largest integer less than or equal to the result of the division operation. For example, 7 // 3 would return 2, because the largest integer less than or equal to 2.33 is 2.

7.What is the order of precedence for arithmetic operators in Python?

a) +, -, *, /, %, **
b) **, *, /, %, +, –
c) *, /, %, +, -, **
d) **, %, *, /, +, –

Answer: b) **, *, /, %, +, –

Explanation: In Python, the order of precedence for arithmetic operators is as follows: exponentiation (**), multiplication (*), division (/), modulus (%), addition (+), and subtraction (-).

8.What is the result of the following operation: 5 * 2 ** 3?

a) 16
b) 40
c) 80
d) 256

Answer: c) 80

Explanation: In Python, exponentiation has a higher order of precedence than multiplication. Therefore, the expression 2 ** 3 will be evaluated first, resulting in 8. Then, the expression 5 * 8 will be evaluated, resulting in 40. Therefore, the final result is 40.

9.What is the result of the following operation: 15 % 4?

a) 3
b) 3.75
c) 4
d) 3.5

Answer: a) 3

Explanation: The modulus operator (%) returns the remainder of a division operation. In this case, 15 divided by 4 is 3 with a remainder of 3. Therefore, the result of 15 % 4 is 3.

10.Which of the following is a valid variable name in Python?

a) 1st_place
b) first_place
c) first-place
d) FirstPlace

Answer: b) first_place

Explanation: In Python, variable names must begin with a letter or underscore. They can then be followed by any combination of letters, numbers, and underscores. Therefore, option b) first_place is a valid variable name in Python. Option a) 1st_place is invalid because variable names cannot begin with a number. Option c) first-place is invalid because variable names cannot contain hyphens. Option d) FirstPlace is valid, but not the recommended convention as the convention for variable names in Python is lowercase_with_underscores.

11.What is the result of the following operation: 4 + 5.0?

a) 9
b) 9.0
c) 45
d) TypeError

Answer: b) 9.0

Explanation: In Python, when you add an integer to a float, the result will be a float. Therefore, the result of 4 + 5.0 would be 9.0.

12.What is the result of the following operation: 2 ** 3 ** 2?

a) 64
b) 512
c) 40353607
d) 134217728

Answer: d) 134217728

Explanation: In Python, exponentiation has right-to-left associativity. Therefore, the expression 3 ** 2 will be evaluated first, resulting in 9. Then, the expression 2 ** 9 will be evaluated, resulting in 512. Therefore, the final result is 512.

13.Which operator is used to check if two values are not equal in Python?

a) !=
b) ==
c) =
d) <>

Answer: a) !=

Explanation: The “!=” operator is used to check if two values are not equal in Python. For example, 5 != 3 would return True, because 5 is not equal to 3.

14.Which operator is used to concatenate two strings in Python?

a) +
b) –
c) *
d) /

Answer: a) +

Explanation: The plus operator (+) is used to concatenate two strings in Python. For example, “hello” + “world” would result in the string “helloworld”.

15.What is the result of the following operation: 10 // 3.0?

a) 3.33
b) 3
c) 3.0
d) 4

Answer: b) 3

Explanation: In Python, when you perform floor division on an integer and a float, the result will be a float. Therefore, the expression 10 // 3.0 would first perform floor division, resulting in 3, and then return the result as a float because one of the operands is a float.

16.Which operator is used to compare two values and return True if the first value is greater than the second value in Python?

a) >
b) <
c) ==
d) >=

Answer: a) >

Explanation: The greater than operator (>) is used to compare two values and return True if the first value is greater than the second value in Python. For example, 5 > 3 would return True, because 5 is greater than 3.

17.What is the result of the following operation: 7 / 2?

a) 3.5
b) 3
c) 4
d) 2

Answer: a) 3.5

Explanation: In Python, when you divide two integers, the result will be a float if the division results in a decimal. Therefore, the result of 7 / 2 would be 3.5.

Variables And Operators of Python

18.Which operator is used to perform logical AND in Python?

a) &&
b) &
c) ||
d) |

Answer: b) &

Explanation: The ampersand (&) operator is used to perform logical AND in Python. For example, (5 > 3) & (7 < 10) would return True, because both expressions evaluate to True.

19.What is the result of the following operation: 3 + 4 * 2?

a) 10
b) 14
c) 11
d) 24

Answer: b) 14

Explanation: In Python, the order of precedence for arithmetic operators is as follows: exponentiation (**), multiplication (*), division (/), modulus (%), addition (+), and subtraction (-). Therefore, the multiplication operation (4 * 2) will be performed first, resulting in 8. Then, the addition operation (3 + 8) will be performed, resulting in 11. Therefore, the final result is 14.

20.Which of the following is a valid way to initialize a variable in Python?

a) variable x = 5
b) x = 5
c) x == 5
d) set x to 5


Answer: b) x = 5

Explanation: In Python, you can initialize a variable by simply assigning a value to it using the equals sign (=). Therefore, option b) x = 5 is a valid way to initialize a variable in Python. Option a) is invalid because variable names cannot have spaces, and the correct syntax is “variable_name = value”. Option c) is a comparison operation and will not initialize a variable. Option d) is not valid Python syntax.

21.Which operator is used to perform logical OR in Python?

a) &&
b) |
c) ||
d) &

Answer: c) ||

Explanation: The double vertical bar (||) operator is not used to perform logical OR in Python. The correct operator to use for logical OR is the double pipe (||) operator. For example, (5 > 3) || (7 < 10) would return True, because at least one of the expressions evaluates to True.

22.What is the result of the following operation: 5 % 2?

a) 2.5
b) 2
c) 1.5
d) 1

Answer: d) 1

Explanation: In Python, the modulus operator (%) returns the remainder of integer division. Therefore, the result of 5 % 2 would be 1, because 5 divided by 2 is 2 with a remainder of 1.a

23.Which operator is used to compare two values and return True if the first value is less than or equal to the second value in Python?

a) <=
b) >=
c) !=
d) ==a

Answer: a) <=

Explanation: The less than or equal to operator (<=) is used to compare two values and return True if the first value is less than or equal to the second value in Python. For example, 3 <= 5 would return True, because 3 is less than or equal to 5.

24.What is the result of the following operation: “hello” * 3?

a) “hellohellohello”
b) “hello3”
c) 9
d) TypeError

Answer: a) “hellohellohello”

Explanation: In Python, when you multiply a string by an integer, the result will be a new string that consists of the original string repeated the specified number of times. Therefore, the result of “hello” * 3 would be the string “hellohellohello”.

25.Which of the following is a valid way to declare a floating-point variable in Python?

a) float x = 5.0
b) x = float(5.0)
c) x = 5
d) x = “5.0”

Answer: b) x = float(5.0)

Explanation: In Python, you can convert an integer or a string to a float by using the float() function. Therefore,
option b) x = float(5.0) is a valid way to declare a floating-point variable in Python.
Option a) is invalid because variable names cannot have spaces, and the correct syntax is “variable_name = value”.
Option c) initializes a variable as an integer, not a float.
Option d) initializes a variable as a string, not a float.

26.Which operator is used to perform exponentiation in Python?

a) ^
b) **
c) *
d) //

Answer: b) **

Explanation: The double asterisk (**) operator is used to perform exponentiation in Python. For example, 2 ** 3 would return 8, because 2 to the power of 3 is 8.

27.What is the result of the following operation: “hello” + “world”?

a) “helloworld”
b) “hello world”
c) “hello+world”
d) TypeError

Answer: a) “helloworld”

Explanation: In Python, when you use the plus (+) operator to concatenate two strings, the result will be a new string that consists of the two original strings combined. Therefore, the result of “hello” + “world” would be the string “helloworld”.

28.Which of the following is a valid way to declare a boolean variable in Python?

a) x = true
b) x = False
c) x = TRUE
d) x = 0

Answer: b) x = False

Explanation: In Python, the boolean values are True and False, with the first letter capitalized. Therefore, option b) x = False is a valid way to declare a boolean variable in Python. Option a) is invalid because the value should be capitalized as False. Option c) is invalid because TRUE is not a valid boolean value in Python. Option d) initializes a variable as an integer, not a boolean.

29.What is the result of the following operation: “hello”[1]?

a) “h”
b) “e”
c) “l”
d) “o”

Answer: b) “e”

Explanation: In Python, you can access individual characters in a string by using indexing. Indexing starts at 0, so “hello”[1] refers to the second character in the string, which is “e”.

30.Which operator is used to perform floor division in Python?

a) //
b) /
c) %
d) **

Answer: a) //

Explanation: The double slash (//) operator is used to perform floor division in Python. Floor division returns the quotient of a division operation, rounded down to the nearest integer. For example, 7 // 2 would return 3, because 7 divided by 2 is 3 with a remainder of 1.

31.Which of the following is NOT a valid variable name in Python?

a) my_variable
b) 123variable
c) MY_VARIABLE
d) _my_variable

Answer: b) 123variable

Explanation: In Python, variable names must start with a letter or an underscore, and can only contain letters, numbers, and underscores. Therefore, option b) 123variable is not a valid variable name in Python because it starts with a number.a

32.What is the result of the following operation: 4 + 2 * 3?

a) 18
b) 10
c) 16
d) 24

Answer: b) 10

Explanation: In Python, when you have multiple operators in a single expression, the order of operations (PEMDAS) is followed: Parentheses, Exponents, Multiplication and Division (from left to right), Addition and Subtraction (from left to right). Therefore, in the expression 4 + 2 * 3, the multiplication is performed first, resulting in 6, and then the addition is performed, resulting in 10.

33.Which operator is used to perform logical NOT in Python?

a) !
b) &&
c) ||
d) &

Answer: a) !

Explanation: The exclamation mark (!) operator is used to perform logical NOT in Python. It is placed in front of a boolean expression and returns the opposite boolean value. For example, !(5 > 3) would return False, because 5 is greater than 3, but the exclamation mark reverses the result to False.

34.What is the result of the following operation: len(“hello”)?

a) “hello”
b) 5
c) 6
d) TypeError

Answer: b) 5

Explanation: In Python, the len() function returns the number of items in a sequence. For strings, it returns the number of characters in the string. Therefore, the result of len(“hello”) would be 5, because “hello” has five characters.

Variables And Operators of Python

35.Which of the following is a valid way to declare an integer variable in Python?

a) x = 3.0
b) x = “3”
c) x = int(3)
d) x = 3++

Answer: c) x = int(3)

Explanation: In Python, you can convert a float or a string to an integer by using the int() function. Therefore, option c) x = int(3) is a valid way to declare an integer variable in Python. Option a) initializes a variable as a float, not an integer. Option b) initializes a variable as a string, not an integer. Option d) is not a valid syntax in Python.

36.What is the result of the following operation: 5 % 2?

a) 2.5
b) 2
c) 3
d) 1.5

Answer: d) 1

Explanation: In Python, the percent (%) operator is used to perform modulus, which returns the remainder of a division operation. Therefore, the result of 5 % 2 would be 1, because 5 divided by 2 is 2 with a remainder of 1.

37.Which operator is used to perform identity comparison in Python?

a) ==
b) !=
c) is
d) not

Answer: c) is

Explanation: The “is” operator is used to perform identity comparison in Python. It tests if two variables refer to the same object in memory. For example, if you have two variables x and y that both point to the same list object [1,2,3], then x is y would return True because they refer to the same object.

38.What is the result of the following operation: 4 / 2?

a) 2.0
b) 2
c) 1.5
d) 0.5

Answer: a) 2.0

Explanation: In Python, the division operator (/) performs floating-point division, which means that it returns a float value even if both operands are integers. Therefore, the result of 4 / 2 would be 2.0, which is a float value.

39.Which of the following is a valid way to declare a string variable in Python?

a) x = ‘hello’
b) x = “hello”
c) x = ”’hello”’
d) All of the above

Answer: d) All of the above

Explanation: In Python, you can declare a string variable using either single quotes (”), double quotes (“”) or triple quotes (”’ ”’). All three options are valid and produce the same result.

40.What is the result of the following operation: 2 ** 0.5?

a) 1.41
b) 1
c) 2
d) TypeError

Answer: a) 1.41

Explanation: In Python, the double asterisk (**) operator is used to perform exponentiation. When you raise a number to a fractional power, it performs a square root operation. Therefore, 2 ** 0.5 would return the square root of 2, which is approximately 1.41.