Python Programming - Variables

Exercise : Variables - General Questions
  • Variables - General Questions
26.
Which of the following is a valid way to convert a string '10' to an integer?
int('10')
'10'.toint()
integer('10', base=10)
All of the above
Answer: Option
Explanation:

int('10') is the correct way to convert a string to an integer in Python.

'10'.toint() is not a valid method for converting a string to an integer; instead, it will raise an AttributeError.

integer('10', base=10) is also not a valid way to convert a string to an integer, it will raise a NameError: name 'integer' is not defined.


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

In this code, a list my_list is defined with four elements.

The second line uses slice assignment to replace the second and third elements of the list with the new list [5, 6].

When the modified list is printed to the console, the output is [1, 5, 6, 4].


28.
Which of the following is NOT a valid way to declare a tuple?
my_tuple = 1, 2, 3
my_tuple = (1, 2, 3)
my_tuple = tuple(1, 2, 3)
All of the above are valid
Answer: Option
Explanation:

my_tuple = tuple(1, 2, 3) is not a valid way to declare a tuple in Python.

Instead, you can use my_tuple = 1, 2, 3 or my_tuple = (1, 2, 3) to create a tuple directly, or use the tuple() function to convert another iterable (like a list) into a tuple.

For example: my_tuple = tuple([1, 2, 3])


29.
What is the output of the following code:
x = 5
y = x
x = 10
print(y)
5
10
An error is raised
None of the above
Answer: Option
Explanation:

In this code, a variable x is assigned the value 5, and a variable y is assigned the same value by copying the value of x.

The value of x is then changed to 10, but the value of y remains unchanged at 5.

When the value of y is printed to the console, the output is 5.


30.
Which of the following is true about the pass statement?
It is a keyword used to create empty code blocks.
It is used to terminate a loop or a function.
It is used to raise an exception.
It is not a valid keyword in Python.
Answer: Option
Explanation:

The pass statement in Python is used as a placeholder for code that has not been implemented yet.

It is a keyword that creates an empty code block and does nothing. It is commonly used when defining functions, loops, and conditional statements that will be filled in later.