Programming and Problem Solving Through Python Language Theory Question

Tuple in Pyhton

  1. In which of following brackets use in tuple ?
    1. []
    2. {}
    3. ()
    4. ''
    Answer : C
    Explain : For, tuple we use () brackets. List us square brackets [].
  2. Which of the following is true about tuples in Python ?
    1. Tuples are mutable.
    2. Tuples are enclosed in square brackets.
    3. Tuples can contain elements of different data types.
    4. Tuples have no specific order.
    Answer : C
    Explain : We can store different types of data type in tuple like int, float, and character value.
  3. What is the correct way to create an empty tuple ?
    1. empty_tuple = []
    2. empty_tuple = ()
    3. empty_tuple = {}
    4. empty_tuple = None
    Answer : B
    Explain : To create an empty tuple we just need to use brackets (). Here empty_tuple is just a variable name. You can change variable name according to your use.
  4. What will be the output of the following code ?
    my_tuple = (1, 2, 3, 4, 5)
    result = my_tuple[2:4]
    print(result)	
    
    1. (1, 2)
    2. (3, 4)
    3. (2, 3)
    4. (3, 4, 5)
    Answer : B
    Explain : In this slicing is done by index value. So, index value 2- 3, 3- 4 and on 4 it stop.
  5. Solve the code :
    tu = (1,2,3,4,5)
    tu[0]= 0
    print(tu)
    
    1. (0,2,3,4,5)
    2. (0,1,2,3,4,5)
    3. (0)
    4. Error
    Answer : D
    Explain : Have you not studied, that tuple is immutable which means it's value can not be modified or change.
  6. What will be the output of following code ?
    tu = (1,2,3,4,5)
    t2=tu[::-1] 
    print(t2)
    
    1. (1, 2, 3, 4)
    2. (5, 4, 3, 2, 1)
    3. (1, 2, 3, 4,5)
    4. Error
    Answer : B
    Explain : For t2 we use tu but in reverse order with the help of slicing.
  7. Solve the code :
    tu = (1,2,3,4,5)
    t2=tu[1:-1] 
    print(t2)
    
    1. (1, 2, 3, 4)
    2. (5, 4, 3, 2, 1)
    3. (2, 3, 4)
    4. Error
    Answer : C
    Explain : Slicing is done from index value 1 which is 2 to -1 stop value which is 4.
  8. What will be the output of following code ?
    tu = ("hello") 
    print(type(tu))
    
    1. <class 'str'>
    2. <class 'tuple'>
    3. <class 'list'>
    4. <class 'chr'>
    Answer : A
    Explain : In this it is string but if we add comma (,) after hello it becomes tuple.
  9. Which of the following methods can be used to find the number of occurrences of a specific element in a tuple ?
    1. count()
    2. find()
    3. occurrences()
    4. search()
    Answer : A
    Explain : count() function is used to find element with name of that element and give 0 if not found.
  10. Solve the given code :
    tu = (5,1,2) 
    print(max(tu))
    
    1. 2
    2. 1
    3. 0
    4. 5
    Answer : D
    Explain : max() function gives the maximum value of string.
  11. Solve the given code :
    tups = (5,1,2) 
    print(sum(tups,2))
    
    1. 6
    2. 8
    3. 10
    4. Error
    Answer : C
    Explain : sum() is used to sum of number. In this sum of number in tuple is 8 and we add 2 more in this tups which become 10.
  12. What will be the output of given code ?
    tups = (5,1,2)
    del(tups)
    print(tups2)
    
    1. ()
    2. blank
    3. Error
    4. None of above
    Answer : C
    Explain : Error : because tups2 is not defined. Make sure you read question first properly 50% problem solve in question, Be smart with hardworking.
  13. What will be the output of following code ?
    tuples = ('Spark','Python','Java','C','Ruby')
    stop = 2
    slice1 = tuples[slice(stop)]
    print(slice1)
    
    1. ('Spark','Python','Java','C','Ruby')
    2. ('Spark','Python','Java')
    3. ('Spark', 'Python')
    4. Error
    Answer : C
    Explain : slice() function is used to slicing in tuples. So slice(stop) means slice(2), it take value which index value is 0, 1 and 2 is stop value.
  14. Output of following code :
    a,b,c=(2,3),(1,4),(2,2)
    print(b)
    print(type(b))
    
    1. (1, 4) and <class 'tuple'>
    2. (1, 4) and <class 'string'>
    3. (1, 4) and <class 'list'>
    4. Error
    Answer : A
    Explain : b=(1,4) and class of this is tuple.
  15. Solve the given code :
    a=1,
    print(type(a))
    
    1. <class 'tuple'>
    2. <class 'string'>
    3. <class 'list'>
    4. <class 'int'>
    Answer : A
    Explain : because of comma (,) it change to string. If comma is not then it is integer.

Next Set

2