Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Lists in Python (1)

The document contains a comprehensive set of questions and answers related to lists in Python, covering syntax, methods, and operations. It includes multiple-choice questions, short answer questions, and programming tasks that involve manipulating lists. The content is designed to test knowledge of Python lists and their functionalities.

Uploaded by

Vishal .U.S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lists in Python (1)

The document contains a comprehensive set of questions and answers related to lists in Python, covering syntax, methods, and operations. It includes multiple-choice questions, short answer questions, and programming tasks that involve manipulating lists. The content is designed to test knowledge of Python lists and their functionalities.

Uploaded by

Vishal .U.S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Lists in Python

1. Which of the following is the correct syntax to create a list in Python?


a) list = (1, 2, 3)
b) list = [1, 2, 3]
c) list = {1, 2, 3}
d) list = 1, 2, 3

2. Which of the following methods is used to add an element to the end of a list in Python?
a) add()
b) insert()
c) append()
d) extend()

3. What will be the output of the following code?


list = [1, 2, 3, 4]
print(list[2])
a) 1
b) 2
c) 3
d) 4

4. How can you remove the last item from a list in Python?
a) remove()
b) del
c) pop()
d) discard()

5. Which of the following is the correct syntax to access the second last element of a list list
= [10, 20, 30, 40, 50]?
a) list[-1]
b) list[2]
c) list[-2]
d) list[1]

6. What will the following code print?


list = [1, 2, 3]
list.append([4, 5])
print(list)
a) [1, 2, 3, 4, 5]
b) [1, 2, 3, [4, 5]]
c) [[4, 5], 1, 2, 3]
d) [[1, 2, 3], 4, 5]

7. Which method is used to insert an element at a specific position in a list?


a) add()
b) insert()
c) put()
d) insert_at()
8. What is the output of the following code?
list = [1, 2, 3, 4]
list.remove(3)
print(list)
a) [1, 2, 3, 4]
b) [1, 2, 4]
c) [1, 2, 3]
d) [2, 3, 4]

9. What does the extend() method do?


a) Adds an element to the list
b) Adds multiple elements to the list
c) Removes an element from the list
d) Removes all elements from the list

10. What will be the output of the following code?


list = [1, 2, 3, 4]
list.pop(2)
print(list)
a) [1, 2, 4]
b) [1, 2, 3]
c) [2, 3, 4]
d) [1, 3, 4]

11. Which method is used to sort a list in ascending order?


a) sort()
b) sorted()
c) order()
d) ascend()

12. What is the default index of the first element in a Python list?
a) 1
b) 0
c) -1
d) None

13. What will be the result of the following operation?


list = [10, 20, 30, 40]
list[1:3] = [50, 60]
print(list)
a) [10, 50, 60, 40]
b) [50, 60]
c) [10, 50, 60, 30, 40]
d) [10, 20, 30, 40]

14. What is the result of the following operation?


list = [1, 2, 3] + [4, 5]
a) [1, 2, 3, 4, 5]
b) [1, 2, 3]
c) Error
d) [1, 2, 3] + [4, 5]
15. Which of the following methods is used to reverse a list in Python?
a) reverse()
b) revert()
c) invert()
d) flip()

16. What will be the output of this code?


list = [1, 2, 3]
del list[1]
print(list)
a) [1, 3, 4]
b) [2, 3, 4]
c) [1, 2, 3]
d) Error

17. What is the purpose of list.index(value)?


a) To remove the first occurrence of value
b) To find the index of value
c) To add value at the end of the list
d) To check if value exists in the list

18. Which of the following operations can be performed on a list?


a) You can assign a new value to an existing index
b) You can delete an element
c) You can append a new item
d) All of the above

19. What is the output of this code?


list = ['apple', 'banana', 'cherry']
list[1] = 'grape'
print(list)
a) ['apple', 'banana', 'cherry']
b) ['apple', 'grape', 'cherry']
c) Error
d) ['banana', 'apple', 'cherry']

20. What will be the result of this operation?


list = [1, 2, 3]
list[1] = [4, 5]
print(list)
a) [1, [4, 5], 3]
b) [4, 5, 1, 2, 3]
c) Error
d) [1, 4, 5, 2, 3]

21. Which of the following is a mutable type in Python?


a) int
b) string
c) tuple
d) list
22. What will be the output of this code?
list = ['apple', 'banana', 'cherry']
list[1] = 'grape'
print(list)
a) ['apple', 'banana', 'cherry']
b) ['apple', 'grape', 'cherry']
c) Error
d) ['banana', 'apple', 'cherry']

23. What is the correct syntax to initialize an empty list in Python?


a) []
b) list()
c) list = []
d) All of the above

24. What does list.clear() do?


a) Clears the list and removes all elements
b) Clears the list and resets the index
c) Removes the last element of the list
d) Clears all non-numeric values from the list

25. Which function returns the number of elements in a list?


a) size()
b) length()
c) count()
d) len()

26. What will be the result of list = [2, 4, 6] * 2?


a) [2, 4, 6, 2, 4, 6]
b) [2, 4, 6]
c) [4, 6, 2, 4, 6]
d) [6, 6, 6]

27. How can you check if an item exists in a list?


a) item in list
b) item.exists(list)
c) list.contains(item)
d) item.index(list)

28. Which of the following methods is used to remove an element from a specific position in a
list?
a) remove()
b) pop()
c) delete()
d) discard()

29. Which of the following operations will add an element to the beginning of a list?
a) list.insert(0, element)
b) list.append(element)
c) list.extend([element])
d) list[0] = element
30. What will the following code return?
list = [1, 2, 3, 4, 5]
list[::2]
a) [1, 3, 5]
b) [2, 4]
c) [1, 2, 3, 4, 5]
d) [1, 4]

31. What is the output of the following code?


list = [1, 2, 3, 4]
list.pop()
print(list)
a) [1, 2, 3]
b) [2, 3, 4]
c) [1, 2, 3, 4]
d) Error

32. What will be the result of the following code?


list = [1, 2, 3]
list += [4, 5]
print(list)
a) [1, 2, 3, 4, 5]
b) [1, 2, 3]
c) [4, 5]
d) [1, 2, 3, 5, 4]

33. How do you find the index of an element in a list?


a) list.index(element)
b) list.find(element)
c) index(element)
d) list.position(element)

34. Which of the following functions can be used to create a list from a string?
a) list()
b) split()
c) to_list()
d) string_to_list()

35. What will the following code return?


list = [10, 20, 30]
list.insert(2, 40)
list.pop(3)
print(list[1]**2)
a) 100
b) 400
c) 900
d) 1600
Short Answers Question:
1. Write about List in Python.
2. What is the difference between append() and extend()?
3. What is the difference between pop() and remove()?
4. Given a list l = [45, 30, 15, 60, 25, 100, 200, 150, 80, 5, 90, 180]. Write the output for the
following:
(i) l[: 3]
(ii) l[5: 10]
(iii) l[-6: 11]
(iv) l[2: -3]
(v) l[6:]
(vi) l[::-1]
(vii) l[2:10:2]
(viii) l[8:1:-1]
(ix) l[-3:2:-2]
(x) l[::3]
5. Given a list l = [45, 30, 15, 60, 25]. Write the python statement to perform the following:
(i) to add the element 45 to the end of the list.
(ii) to add the elements 90 and 122 to the end of the list.
(iii) to add the element 10 at the index position 3.
(iv) to add the element 200 to the beginning of the list.
(v) to remove the last element from the list.
(vi) to remove the element 60.
(vi) to remove the element at index position 4.
(vii) to reverse the elements of the list.
(viii) to sort the elements of the list in ascending order.
(ix) to sort the elements of the list in descending order.
(x) to find number of times the element 15 has appeared in the list.
(xi) to find the index position of the element 30 from the list.
(xii) to create a copy of the list to a variable named “list_copy”.
(xiii) to modify the element 30 to 100.
(xiv) to modify the first element to 120.

6. Given the list l = [45, 30, 15, 60, 25]. Write the python statement to perform the following:
(i) to print the 3rd element from the list.
(ii) to print the 2nd, 3rd and 4th element from the list.
(iii) to print the list in reverse.
(iv) to print the last element from the list.
(v) to print the sum of the elements of the list.
(vi) to print the lowest element.
(vii) to print the greatest element.
(viii) to print the total number of elements present in the list.
7. Write python program to get n number of integer number as input and add them into a list.
8. Write python program to print the elements of the list separated by ‘&’.
9. Given a list l consisting of integer numbers, write python program for each of the following
(i) to find the maximum and minimum number without using built in min() and max()
functions.
(ii) to find the sum and average without using the built in function sum() and len().
(iii) to get an integer element as input and then search and display its index if element found
else print “Element not found” in the list.
(iv) to add the even numbers to a list named even and odd numbers to a list named odd.
(v) to add 10 to even numbers and 20 to odd numbers.
(vi) to add 20 to elements at even index position and 40 to elements at odd index position.
(vii) to swap the elements available odd index position with its previous element.
(viii) to shift the largest element to end of the list.
(ix) To display the numbers divisible by both 7 and 11. Display “None of number is divisible
by both 7 and 11” if no number is divisible by 7 and 11.
(x) to display all the 4 digit number from the list.
(xi) to display the prime numbers from it.
(xii) to display the perfect numbers in it.
10. Given a list consisting of string(words) as element. Write a python program to print the largest
word from the list.
11. Given a list containing nested lists with each nested list containing elements with structure,
studentID, name, class and mark...
students = [ [201, "Arun Kumar", "11", 87],
[202, "Meenakshi R", "12", 82],
[203, "Rajeshwari Nair", "12", 100],
[204, "Suresh Babu", "11", 76],
[205, "Anitha Subramaniam", "11", 99] ]

Write python program for each of the following


(i) To print the records of students from class 12.
(ii) To print the names of students from class 11.
(iii) To print the id, names of the class 11 students who have secured more than 90
marks.
(iv) To print the name of the student from class 11 who has scored highest mark.
(v) To print the class 12 average mark.
(vi) To print the count of students who have scored more than 90 in class 12.
(vii) To add the id and names of students to new list named “centum” of those
students who have scored 100.

12. Given a list named ‘names’.. Write a python program to remove the last element and print it.
Also, if the list is empty then a message “List empty” is to be printed.
13. Given a list named ‘names’.. Write a python program to remove and print all the elements and
finally print “List empty” when all the elements have been removed(Even if list is empty in
beginning).
14. Given a list named ‘names’.. Write a python program to print the top element(last element)
from the list. If the list is empty then it should print “List empty”
15. Given a list named ‘names’.. Write a python program to print all the elements in reversed order.
If the list is empty then it should print “List empty”.
16. Given a list named ‘names’.. Write python program to print those words which doesn’t consist
of vowels in it.

You might also like