Python Programming - Variables

Exercise : Variables - General Questions
  • Variables - General Questions
6.
Which of the following is not a valid data type?
int
float
decimal
str
Answer: Option
Explanation:
In Python, the decimal data type is not built-in but can be imported from the decimal module. The other data types listed (int, float, and str) are built-in data types in Python.
from decimal import Decimal

# Using the Decimal data type to perform precise arithmetic
x = Decimal('10.5')
y = Decimal('3')
result = x / y

print(result)  # Output: 3.500000000000000000000000000

7.
What is the value of x after executing the following code?
x = 5
x = "IndiaBIX"
5
"IndiaBIX"
"5"
An error is raised
Answer: Option
Explanation:
In Python, variables can be assigned to different data types. When a variable is assigned to a new value, the old value is overwritten with the new value. In this case, the variable x is first assigned to the integer 5, but is then reassigned to the string "IndiaBIX".

8.
Which of the following is a valid way to concatenate two strings?
str1 . str2
str1 + str2
str1 * str2
str1 | str2
Answer: Option
Explanation:
In Python, the + operator is used to concatenate two strings. For example, "Hello, " + "IndiaBIX" would result in the string "Hello, IndiaBIX".

9.
What is the output of the following code:
x = 10; y = 3
z = x % y
print(z)
1
3
9
Results an error due to the semicolon ( ; ) .
Answer: Option
Explanation:

In Python, the % operator is used to calculate the remainder of a division operation. In this case, 10 divided by 3 has a remainder of 1, which is stored in the variable z and printed to the console.

Python does not usually use explicit line-ending characters; the parser can almost always figure out where a statement ends from the positioning of newlines. However, Python's syntax allows you to use a semicolon to end a statement instead, which allows you to place multiple statements on a single line.


10.
Which of the following is true about Python variable names?
Variable names are case-sensitive
Variable names cannot be longer than 32 characters
Variable names can contain special characters like @ or $
None of the above
Answer: Option
Explanation:
In Python, variable names are case-sensitive, which means that my_variable and My_Variable are two different variable names. Variable names can be any length and can contain letters, numbers, and underscores, but cannot begin with a number and cannot contain special characters like @ or $.