Python Programming - Data Types

Exercise : Data Types - General Questions
  • Data Types - General Questions
36.
What will be the output of the following code snippet?
x = 10
y = "5"
result = x + int(y)
print(result)
15
105
"10" + "5"
Error
Answer: Option
Explanation:
The int(y) converts the string '5' to an integer, and then the addition results in 10 + 5 = 15.

37.
Which of the following statements is true about tuples?
Tuples are mutable
Tuples can not have duplicate elements
Elements in a tuple can be accessed using indices
Tuples are created using square brackets
Answer: Option
Explanation:
Tuples in Python are ordered and immutable. Elements can be accessed using indices.

38.
What will be the output of the following code snippet?
my_string = "Python"
print(my_string[1:5:2])
yhn
yh
yhon
yto
Answer: Option
Explanation:

The slicing syntax [start:stop:step] is used, selecting characters from index 1 to 5 with a step of 2.

my_string[1:5:2] slices the string my_string starting from index 1 (inclusive) up to index 5 (exclusive), with a step size of 2.

So, it selects characters at indices 1 and 3 (1+step-size) (skipping index 5), which are 'y' and 'h' respectively.


39.
What is the output of the following code snippet?
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[-2:])
(4, 5)
(2, 3, 4, 5)
(3, 4, 5)
(2, 3)
Answer: Option
Explanation:

my_tuple[-2:] slices the tuple my_tuple starting from the second-to-last element (index -2) to the end of the tuple.

So, it selects elements at indices -2 and -1, which are 4 and 5 respectively, resulting in the tuple (4, 5).


40.
What does the min() function in Python do?
Returns the minimum value in a list or tuple
Rounds a floating-point number down to the nearest integer
Converts a string to lowercase
Checks if a variable is of a certain type
Answer: Option
Explanation:

The min() function is used to find the smallest value in a list or tuple.

For example, if you have the following list:

list = [1, 2, 3, 4, 5]

You can use the min() function to find the smallest element in the list, which would be 1.

The min() function can be useful for a variety of tasks, such as finding the smallest value in a dataset, or finding the smallest number of items in a stock.