Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

Which of the following statement(s) would give an error during execution of the following code?

tup = (20,30,40,50,80,79)

print(tup) #Statement 1

print(tup[3]+50) #Statement 2

print(max(tup)) #Statement 3

tup[4]=80 #Statement 4

Options:

a. Statement 1

b. Statement 2

c. Statement 3

d. Statement 4

Answer

Answer by student:

d. Statement 4

Detailed answer by teachoo:

To answer this question, we need to know the syntax and usage of tuples and indexing in Python.

  • A tuple is a data structure that stores a sequence of immutable values, which means they cannot be changed once created. A tuple is created by enclosing the values in parentheses () and separating them by commas. For example, tup = (20,30,40,50,80,79) is a tuple that stores six integer values.
  • Indexing is a technique that allows us to access a specific value in a tuple or a list by specifying its position. The index of the first value is 0, the index of the second value is 1, and so on. The index of the last value is -1, the index of the second last value is -2, and so on. We can use square brackets [] to access the value at a given index. For example, tup[0] will return 20 and tup[-1] will return 79 from the tuple tup.

In this question, we have a tuple tup = (20,30,40,50,80,79) and we want to find which of the following statements would give an error during execution of the code.

Let’s go through each of the statements and see why they would give an error or not:

  • Statement 1: print(tup): This statement will not give an error because it will print the entire tuple tup as it is. The output will be (20,30,40,50,80,79).
  • Statement 2: print(tup[3]+50): This statement will not give an error because it will print the sum of the value at index 3 in the tuple tup and 50. The value at index 3 is 50, so the output will be 100.
  • Statement 3: print(max(tup)): This statement will not give an error because it will print the maximum value in the tuple tup. The max() function returns the largest value in a sequence. The maximum value in tup is 80, so the output will be 80.
  • Statement 4: tup[4]=80: This statement will give an error because it will try to change the value at index 4 in the tuple tup to 80. However, tuples are immutable, which means they cannot be modified after creation. Therefore, this statement will raise a TypeError: ‘tuple’ object does not support item assignment.

So, the correct answer is d. Statement 4.

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.