Programming and Problem Solving Through Python Language Theory Question

Tuple in Pyhton - Set 2

  1. Suppose a list with name arr, contains 5 elements. You can get the 2nd element from the list using :
    1. arr[-2]
    2. arr[2]
    3. arr[-1]
    4. arr[1]
    Answer : D
    Explain : To get 2nd element usd arr[1] because index value start form 0 and the 2nd value is at 1.
  2. What is the output of the following code ?
    ms = ('A', 'D', 'H', 'U', 'N', 'I', 'C')
    print(ms[1:4])
    1. ('D', 'H', 'U')
    2. ('D', 'H', 'U', 'N', 'I', 'C')
    3. ('A', 'D', 'H', 'U', 'N', 'I', 'C')
    4. None of these
    Answer : A
    Explain : In this it print the index value start from 1 and stop at 4 index value.
  3. What is the output of the following statement ?
    print ((2, 4) + (1, 5))
    1. (2 , 4), (4 , 5)
    2. (3 , 9)
    3. (2, 4, 1, 5)
    4. Invalid Syntax
    Answer : C
    Explain : It treated both as tuple and +(concatenation) operation perfom on it, which join both.
  4. What is a tuple in Python ?
    1. A sequence of elements that can be modified
    2. A sequence of elements that is ordered and immutable
    3. A list of integers
    4. A dictionary of key-value pairs
    Answer : B
    Explain : Tuple is an ordered sequence of elements of different data types (such as interger,string, float etx) which are immutable (cannot changed after declare).
  5. Which of the following is a valid way to create a tuple in Python ?
    1. my_tuple = [1, 2, 3]
    2. my_tuple = (1, 2, 3)
    3. my_tuple = {1, 2, 3}
    4. my_tuple = '1, 2, 3'
    Answer : B
    Explain : To define tuple use () brackets.
  6. How do you access the elements of a tuple ?
    1. Using square brackets, e.g., my_tuple[0]
    2. Using curly braces, e.g., my_tuple{0}
    3. Using parentheses, e.g., my_tuple(0)
    4. Using angle brackets, e.g., my_tuple<0>
    Answer : A
    Explain : To access element in tuple use [] brackets and give index value.
  7. What will be the output of the following code?
    my_tuple = (1, 2, 3)
    print(my_tuple[1])
    1. 1
    2. 2
    3. 3
    4. Error
    Answer : B
    Explain : Index value start from 0, So at index value 1 the value is 2.
  8. Which of the following methods can be used to find the length of a tuple in Python ?
    1. size()
    2. count()
    3. length()
    4. len()
    Answer : D
    Explain : len() function is used to check the lenght of tuple.
  9. How can you concatenate two tuples, tuple1 and tuple2, in Python?
    1. tuple1 + tuple2
    2. tuple1.append(tuple2)
    3. tuple1.extend(tuple2)
    4. tuple1.concat(tuple2)
    Answer : A
    Explain : To concatenate two tuples, we use + (plus) symbol.
  10. What happens when you try to modify an element of a tuple in Python ?
    1. The element is modified successfully.
    2. A new tuple is created with the modification.
    3. An error occurs because tuples are immutable
    4. The element is removed from the tuple.
    Answer : C
    Explain : Tuple are immutable in nature which cannot be modify.
  11. What’s the main difference between Python lists and tuples ?
    1. Lists can hold any data type and tuples can only contain int and str objects.
    2. Lists are immutable and tuples are mutable.
    3. Lists are faster and tuples are slower.
    4. Lists are mutable and tuples are immutable.
    Answer : D
    Explain : Tuples are immutable, means which cannot change and modify. Tuples are also faster then List.
  12. Which options are correct to create an empty tuple in Python ?
    1. []
    2. {}
    3. ()
    4. ''
    Answer : C
    Explain : To create and empty tuple use parenthesis (). Assign brackets to varaible and it create an empty tuple.
  13. How can you check if an element exists in a tuple in Python ?
    1. By using the contains() method.
    2. By using a for loop.
    3. By using the in keyword.
    4. By using the exists() function.
    Answer : C
    Explain : To check the element exist in tuple use 'Memerber ship' operator which is 'in' or not in'.
  14. Which of the following methods can be used to find the maximum value in a tuple of numbers ?
    1. max()
    2. maximum()
    3. find_max()
    4. largest()
    Answer : A
    Explain : max() function returns the maximum value from the tuple.
  15. Which of the following code snippets correctly creates a tuple with a single element (42) ?
    1. single_tuple = (42)
    2. single_tuple = (42)
    3. single_tuple = [42]
    4. single_tuple = (42,)
    Answer : D
    Explain : We cannot create tuple by using single_tuple = (42) because this is consider as int and if in quote then as string, so we need to add comma after word then it is understand it as tuple.
  16. Next Set

    1