Python Programming - Variables

Exercise : Variables - General Questions
  • Variables - General Questions
16.
What is the output of the following code:
x = "Hello"
y = "IndiaBIX"
print(y + x)
IndiaBIXHello
IndiaBIX Hello
HelloIndiaBIX
Hello IndiaBIX
Answer: Option
Explanation:

In Python, the + operator can be used to concatenate strings.

In this case, the variables x and y hold the strings "Hello" and "IndiaBIX", respectively.

When they are concatenated with the + operator, the result y+x is "IndiaBIXHello".


17.
What is the value of y after executing the following code:
x = [1, 2, 3]
y = x
x = [4, 5, 6]
print(y)
[4, 5, 6]
[1, 2, 3]
[1, 2, 3, 4, 5, 6]
An error is raised
Answer: Option
Explanation:

In this code, the variable x is initially assigned a list containing the values 1, 2, and 3.

The variable y is then assigned the same list that x holds a reference to.

However, when x is reassigned a new list containing the values 4, 5, and 6, y still holds a reference to the original list [1, 2, 3].

So when y is printed to the console, it prints [1, 2, 3].


18.
Which of the following is a valid way to check if a value is in a list called my_list?
if my_value in my_list
if my_list[ my_value ]
if my_value in my_list == True
All of the above
Answer: Option
Explanation:
In Python, the in and not in operators can be used to check if a value is or is not in a list, respectively.

19.
What is the output of the following code:
my_list = [1, 2, 3]
my_list[1] = 4
print(my_list)
[1, 2, 3]
[1, 4, 3]
[4, 2, 3]
An error is raised
Answer: Option
Explanation:

In this code, the variable my_list is initially assigned a list containing the values 1, 2, and 3.

The second element of the list (which has an index of 1) is then changed to 4 using the assignment operator =.

When my_list is printed to the console, it shows the modified list [1, 4, 3].


20.
What is the output of the following code:
my_string = "hello"
my_string[1] = "a"
print(my_string)
hello
hallo
An error is raised
None of the above
Answer: Option
Explanation:

In Python, strings are immutable, which means their individual characters cannot be modified once they're created.

Attempting to change a character of a string using the index operator [ ] results in a TypeError.