Exercise: Variables
Questions for: Variables
What is the value of
x after executing the following code:
x = [1, 2, 3]
y = x
x[0] = 4
print(y)
A:
[4, 2, 3]B:
[1, 2, 3]C:
[4, 2, 2]D:
An error is raised
Answer: A
In Python, when a list is assigned to a variable, the variable holds a reference to the list rather than a copy of the list. In this case, the variable
y holds a reference to the same list as x. When x[0] is changed to 4, the list is modified and both x and y reflect this change.
Which of the following is true about Python variable names?
A:
Variable names are case-sensitive
B:
Variable names cannot be longer than 32 characters
C:
Variable names can contain special characters like @ or $
D:
None of the above
Answer: A
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 $.Discuss About this Question.
What is the output of the following code:
x = 10; y = 3
z = x % y
print(z)
A:
1
B:
3
C:
9
D:
Results an error due to the semicolon (
; ) .
Answer: A
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.
Discuss About this Question.
Which of the following is a valid way to concatenate two strings?
A:
str1 . str2B:
str1 + str2C:
str1 * str2D:
str1 | str2
Answer: B
In Python, the
+ operator is used to concatenate two strings. For example, "Hello, " + "ExamAdept" would result in the string "Hello, ExamAdept".Discuss About this Question.
What is the value of
x after executing the following code?
x = 5
x = "ExamAdept"
A:
5B:
"ExamAdept"C:
"5"D:
An error is raised
Answer: B
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 "ExamAdept".Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.