Thursday, 18 January 2024

Top 20 Python List Questions




Question 1:

What is a list 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 list in Python?

a) list()

b) empty_list = []

c) empty_list = list()

d) Both b and c


Question 3:

How do you access the first element of a list?

a) list[0]

b) list.first()

c) list.first

d) list.get(0)


Question 4:

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

a) list.insert(0, element)

b) list.add(element)

c) list.append(element)

d) list.extend(element)


Question 5:

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

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

b) It returns the last element of the list

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

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


Question 6:

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

a) element in list

b) list.contains(element)

c) list.exists(element)

d) element.exists(list)


Question 7:

What does the list.remove(element) function do?

a) Removes the first occurrence of the specified element from the list

b) Removes all occurrences of the specified element from the list

c) Removes the last element from the list

d) Removes the element at the specified index


Question 8:

How do you reverse the order of elements in a list?

a) list.reverse()

b) list.sort(reverse=True)

c) list.reorder()

d) list.flip()


Question 9:

What is the difference between the append() and extend() methods in Python lists?

a) There is no difference, and the terms are interchangeable

b) append() adds a single element, while extend() adds multiple elements

c) extend() adds a single element, while append() adds multiple elements

d) Both methods are used for removing elements from a list


Question 10:

What is the output of the following code?

my_list = [1, 2, 3]

new_list = my_list * 2

print(new_list)

a) [1, 2, 3, 1, 2, 3]

b) [2, 4, 6]

c) [1, 4, 9]

d) [1, 2, 3, 6, 9]


Question 11:

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

a) list.index(element)

b) list.find(element)

c) list.search(element)

d) list.loc(element)


Question 12:

How do you copy the elements of one list to another list in Python?

a) new_list = old_list.copy()

b) new_list = old_list.clone()

c) new_list = copy(old_list)

d) new_list = old_list[:]


Question 13:

What is the purpose of the pop() method in Python lists?

a) Adds an element to the end of the list

b) Removes the last element from the list and returns it

c) Removes the first occurrence of the specified element

d) Sorts the elements of the list


Question 14:

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

a) Lists are mutable, while tuples are immutable

b) Lists are immutable, while tuples are mutable

c) Both lists and tuples are mutable

d) Both lists and tuples are immutable


Question 15:

How do you insert an element at a specific index in a list?

a) list.add(index, element)

b) list.insert(index, element)

c) list.insert(element, index)

d) list.put(index, element)


Question 16:

Which method is used to clear all elements from a list?

a) list.clear()

b) list.remove_all()

c) list.delete()

d) list.empty()


Question 17:

What does the sorted() function do when applied to a list?


a) Reverses the order of elements in the list

b) Sorts the elements of the list in ascending order

c) Removes duplicate elements from the list

d) Shuffles the elements of the list randomly


Question 18:

What is the purpose of the count() method in Python lists?

a) Counts the total number of elements in the list

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

c) Counts the sum of all elements in the list

d) Counts the average value of elements in the list


Question 19:

How do you create a list of numbers from 1 to 5 in Python?

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

b) list = range(1, 6)

c) list = list(1, 6)

d) list = [range(1, 6)]


Question 20:

What is the output of the following code?

my_list = [3, 1, 4, 1, 5, 9, 2]

my_list.sort()

print(my_list)

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 : 

Question 1: b) A collection of ordered elements

Question 2: d) Both b and c

Question 3: a) list[0]

Question 4: c) list.append(element)

Question 5: a) It returns the total number of elements in the list

Question 6: a) element in list

Question 7: a) Removes the first occurrence of the specified element from the list

Question 8: a) list.reverse()

Question 9: b) append() adds a single element, while extend() adds multiple elements

Question 10: a) [1, 2, 3, 1, 2, 3]

Question 11: a) list.index(element)

Question 12: d) new_list = old_list[:]

Question 13: b) Removes the last element from the list and returns it

Question 14: a) Lists are mutable, while tuples are immutable

Question 15: b) list.insert(index, element)

Question 16: a) list.clear()

Question 17: b) Sorts the elements of the list in ascending order

Question 18: b) Counts the occurrences of a specific element in the list

Question 19: b) list = range(1, 6)

Question 20: a) [1, 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