Friday, 12 January 2024

How much do you know about list in Python?



1. Which value is used to represent the first index of

list?

(a) 1 (b) 0

(c) −1 (d) a

Ans. (b) To access the list’s elements, index number is used. The

index number should be an integer. Index of 0 refers to first

element, 1 refers to second element and so on.


2. Choose the output of following Python code.

1= list ()

print ( 1)

(a) [] (b) ()

(c) [,] (d) Empty

Ans. (a) Empty list can be created in Python using []. To create

empty list, list () is also used.


3. Suppose list

1= [10, 20, 30, 40, 50, 60, 70]

print( 1[−3])

(a) 30 (b) 50

(c) 40 (d) Error

Ans. (b) The index of −1 refers to the last element, −2 refers to the

second last element and so on. Hence, −3 refers to third last

element, i.e. 50.


4. Choose the output from following code.

list1 = [‘A’, ‘R’, ‘I’,‘H’,‘A’,‘N’,‘T’]

print (list1 [7])

(a) T (b) N

(c) A (d) Error

Ans. (d) In the given code, we are trying to access 8th element

from the list which does not exist as we are having total 7

elements for which the last index is 6. So, Python will give

an IndexError.


5. Which function is used to insert an element at

specified position in the list?

(a) extend () (b) append ()

(c) insert () (d) add ()

Ans. (c) insert () function is used to insert an element at specified

position in the list. This method takes two arguments : one

for index number and second for element value.

Syntax list_name.insert(index, element)


6. Choose the correct option for the following.

l1 = [2, 5, 7]

l = l1+ 5

print (l)

(a) [7, 10, 12]

(b) [2, 5, 7, 5]

(c) [5, 2, 5, 7]

(d) TypeError

Ans. (d) + operator cannot add list with other type as number or

string because this operator is used only with list types.

So, it will give TypeError as it can only concatenate list (not

“int”) to list.


7. What is the output of following code?

l1 = [3, 2, 6]

l = l1 * 2

print (l)

(a) [3, 2, 6, 3, 2, 6]

(b) [6, 4, 12]

(c) [3, 4, 12]

(d) TypeError

Ans. (a) * operator can repeat the elements of the list.

Syntax list = list1 * digit


8. Which of the following is true regarding lists in

Python?

(a) Lists are immutable.

(b) Size of the lists must be specified before its initialisation.

(c) Elements of lists are stored in contiguous memory

location.

(d) size(list1) command is used to find the size of lists.

Ans. (c) Elements of lists are stored in contiguous memory

location, so it is true regarding lists in Python.


9. Suppose list1 is [56, 89, 75, 65, 99], what is the

output of list1 [− 2]?

(a) Error (b) 75

(c) 99 (d) 65

Ans. (d) −1 corresponds to the last index in the list, −2 represents

the second last element and so on.

So, the output for list1 [− 2] is 65 because 65 is second last

element of list1.


10. Identify the output of following code.

List1=[1, 2, 3, 7, 9]

L=List1.pop(9)

print(L)

(a) Syntax error (b) 9

(c) [1, 2, 3, 7] (d) None of these

Ans. (a) In pop(9), parentheses put index number instead of

element. In the given list, maximum index number is 4, then

9 is out of index range.


11. Suppose list1 is [2445,133,12454,123], what is the

output of max(list1)?

(a) 2445 (b) 133

(c) 12454 (d)123

Ans. (c) max() returns the maximum element in the list. From

given options, 12454 is the element with maximum value.


12. To add a new element to a list, which command will

we use?

(a) list1.add(8)

(b) list1.append(8)

(c) list1.addLast(8)

(d) list1.addEnd(8)

Ans. (b) We use the function append() to add an element to the list.


13. What will be the output of the following Python

code?

list1=[9, 5, 3, 5, 4]

list1[1:2]=[7,8]

print(list1)

(a) [9,5, 3, 7, 8] (b) [9, 7, 8, 3, 5, 4]

(c) [9,[ 7, 8], 3, 5,4] (d) Error

Ans. (b) In the piece of code, slice assignment has been

implemented. The sliced list is replaced by the assigned

elements in the list.


14. Consider the declaration a=[2, 3, ‘Hello’, 23.0].

Which of the following represents the data type of

‘a’?

(a) String (b) Tuple

(c) Dictionary (d) List

Ans. (d) List contains a sequence of heterogeneous elements

which store integer, string as well as object. It can created to

put elements separated by comma (,) in square brackets [].


15. Identify the output of the following Python

statement.

x=[[1, 2, 3, 4], [5, 6, 7, 8]]

y=x [0] [2]

print(y)

(a) 3 (b) 4

(c) 6 (d) 7

Ans. (a) x is a list, which has two sub-lists in it. Elements of first

list will be represented by [0] [i] and elements of second list

will be represented by [1] [i].


16. Which method will empty the entire list?

(a) pop() (b) clear()

(c) sort() (d) remove()

Ans. (b) clear() method is used to remove all the items of a list.

This method will empty the entire list.

Syntax

list_name.clear()


17. Which of the following allows us to sort the list

elements in descending order?

(a) reverse = True

(b) reverse = False

(c) sort (descending)

(d) sort. descending

Ans. (a) sort() is used to sort the given list in ascending order. The

sort() has an argument called reverse = True. This allows us

to sort the list elements in descending order.


18. Identify the correct output.

>>>l1 = [34, 65, 23, 98]

>>>l1. insert(2, 55)

>>>l1

(a) [34, 65, 23, 55] (b) [34, 55, 65, 23, 98]

(c) [34, 65, 55, 98] (d) [34, 65, 55, 23, 98]

Ans. (d) insert() is used to insert an element at specified position

in the list. This method takes two arguments : one for index

number and second for element value.

Syntax

list_name.insert(index, element)


19. Find the output from the following code.

list1=[2, 5, 4, 7, 7, 7, 8, 90]

del list1[2 : 4]

print(list1)

(a) [2, 5, 7, 7, 8, 90] (b) [5, 7, 7, 7, 8, 90]

(c) [2, 5, 4, 8, 90] (d) Error

Ans. (a) del keyword is used to delete the elements from the list.


20. Slice operation is performed on lists with the use of

(a) semicolon (b) comma

(c) colon (d) hash

Ans. (c) In Python list, there are multiple ways to print the whole

list with all the elements, but to print a specific range of

elements from the list, we use slice operation. Slice

operation is performed on lists with the use of colon (:).

0 Comments:

Post a Comment

Popular Posts

Categories

AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (893) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

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