Friday, 19 January 2024

Top 20 Python Tuple Questions

 



What is a tuple in Python?

a) A collection of unordered elements

b) A collection of ordered elements

c) A single element

d) A data type


Question 2:

How do you create an empty tuple in Python?

a) tuple()

b) empty_tuple = ()

c) empty_tuple = tuple()

d) Both b and c


Question 3:

How do you access the first element of a tuple?

a) tuple[0]

b) tuple.first()

c) tuple.first

d) tuple.get(0)


Question 4:

Which of the following statements is used to add an element to a tuple?

a) tuple.insert(0, element)

b) Tuples are immutable, so elements cannot be added once a tuple is created

c) tuple.add(element)

d) tuple.extend(element)


Question 5:

What is the key difference between a tuple and a list in Python?

a) Tuples are mutable, while lists are immutable

b) Tuples are ordered, while lists are unordered

c) Tuples are immutable, while lists are mutable

d) Tuples can contain only numeric elements


Question 6:

How do you check if an element is present in a tuple?

a) element in tuple

b) tuple.contains(element)

c) tuple.exists(element)

d) element.exists(tuple)


Question 7:

What does the tuple.count(element) method do?

a) Counts the total number of elements in the tuple

b) Counts the occurrences of a specific element in the tuple

c) Counts the sum of all elements in the tuple

d) Counts the average value of elements in the tuple


Question 8:

How do you concatenate two tuples in Python?

a) tuple1 + tuple2

b) tuple1.concat(tuple2)

c) concat(tuple1, tuple2)

d) combine(tuple1, tuple2)


Question 9:

How do you create a tuple with a single element?

a) single_tuple = (1)

b) single_tuple = 1,

c) single_tuple = (1,)

d) Both a and b


Question 10:

Which method is used to find the index of the first occurrence of a specified element in a tuple?

a) tuple.index(element)

b) tuple.find(element)

c) tuple.search(element)

d) tuple.loc(element)


Question 11:

What happens when you try to modify an element in a tuple?

a) It is not possible to modify elements in a tuple as they are immutable

b) The element is updated successfully

c) Python raises an exception

d) The element is deleted from the tuple


Question 12:

How do you create a tuple with elements from 1 to 5 in Python?

a) tuple = (1, 2, 3, 4, 5)

b) tuple = range(1, 6)

c) tuple = tuple(1, 6)

d) tuple = (range(1, 6))


Question 13:

What is the purpose of the len() function when used with a tuple?

a) It returns the total number of elements in the tuple

b) It returns the last element of the tuple

c) It returns the length of each element in the tuple

d) It returns the sum of all elements in the tuple


Question 14:

How do you check if two tuples are equal?

a) tuple1.is_equal(tuple2)

b) tuple1 == tuple2

c) tuple1.equals(tuple2)

d) tuple1.equals(tuple2, strict=True)


Question 15:

What is the purpose of the max() function when used with a tuple?

a) It returns the maximum element in the tuple

b) It returns the index of the maximum element in the tuple

c) It returns the sum of all elements in the tuple

d) It returns the average value of elements in the tuple


Question 16:

Which method is used to remove the last element from a tuple?

a) tuple.remove_last()

b) tuple.pop()

c) tuple.delete_last()

d) Tuples are immutable, so elements cannot be removed


Question 17:

How do you convert a list to a tuple in Python?

a) tuple(list)

b) tuple = list

c) tuple.from_list(list)

d) tuple.convert(list)


Question 18:

What is the purpose of the sorted() function when applied to a tuple?

a) Reverses the order of elements in the tuple

b) Sorts the elements of the tuple in ascending order

c) Removes duplicate elements from the tuple

d) Shuffles the elements of the tuple randomly


Question 19:

What does the tuple.index(element) method return if the element is not found in the tuple?

a) None

b) -1

c) 0

d) Raises a ValueError


Question 20:

What is the output of the following code?

my_tuple = (3, 1, 4, 1, 5, 9, 2)

my_tuple.sort()

print(my_tuple)

a) (1, 1, 2, 3, 4, 5, 9)

b) (9, 5, 4, 3, 2, 1, 1)

c) (1, 1, 2, 3, 4, 5, 9, 2)

d) (1, 2, 3, 4, 5, 9)


Answer: 

  1. b) A collection of ordered elements
  2. d) Both b and c
  3. a) tuple[0]
  4. b) Tuples are immutable, so elements cannot be added once a tuple is created
  5. c) Tuples are immutable, while lists are mutable
  6. a) element in tuple
  7. b) Counts the occurrences of a specific element in the tuple
  8. a) tuple1 + tuple2
  9. c) single_tuple = (1,)
  10. a) tuple.index(element)
  11. a) It is not possible to modify elements in a tuple as they are immutable
  12. a) tuple = (1, 2, 3, 4, 5)
  13. a) It returns the total number of elements in the tuple
  14. b) tuple1 == tuple2
  15. a) It returns the maximum element in the tuple
  16. d) Tuples are immutable, so elements cannot be removed
  17. a) tuple(list)
  18. b) Sorts the elements of the tuple in ascending order
  19. d) Raises a ValueError
  20. d) (1, 2, 3, 4, 5, 9)

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (51) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (932) Python Coding Challenge (358) Python Quiz (22) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses