Q. 1

What is the output of the following code ? 

p = [[x, x / 2, x * 2] for x in range(-2, 5, 2) if x > 0]
print(p)

 

Options:

Explanation

Correct answer is : D

range(-2, 5, 2) will result in range object of [-2, 0, 2, 4].

[[x, x / 2, x * 2] for x in range(-2, 5, 2) if x > 0] will be similar to the folowing code and will result in [[2, 1.0, 4], [4, 2.0, 8]]

list1 = list()
for x in [-2, 0, 2, 4]:
    if x > 0:
        list1.append([x, x / 2, x * 2])

print(list1)

 

Discuss It

Q. 2

What is the output of the following code ?

students1 = ['John', 'Doe', 'Bob', 'Foo']
students2 = students1
students3 = students1[:]
students2[0] = 'Alec'
students3[1] = 'Steve'
total = 0

for ls in (students1, students2, students3):
    if ls[0] == 'Alec':
        total += 1

    if ls[1] == 'Steve':
        total += 5

print(total)

 

Options:

Explanation

Correct answer is : B

In the expression students2 = students1 , we create a new name students2 and refer to the origional students1. This is known as shared reference. So any in place change like students2[0] = 'Alec' , will also change the value of students1[0].

In the expression like students3 = students1[:] , we are creating a copy of students1 , so any inplace change will not affect the students1 

Therefore in the loop while iterating through all the 3 list, we will encouter 'Alec' 2 times for students1 and students2 and 'Steve' only once.

The result will be 1+1+5 = 7 

Discuss It

Q. 3

What is the output of the following code ?

>>> s = [23, 56, 18 ,32]
>>> s.reverse()
>>> s

Options:

Explanation

Correct answer is : C

s.reverse()     reverses the items of s in place

Refer to https://www.pythoneasy.com/python-programming-tutorial/python-list-methods

Discuss It

Q. 4

What is the output of the following code ?

>>> s1 = ['python2' , 'python3', 'java8', 'perl5', 'java9']
>>> s2 = [i.upper() for i in s1 if i[0] == 'p' ]
>>> s2

 

Options:

Explanation

Correct answer is : B

i.upper() in list comprehension will convert each lowercase letter to upper case. But for the condition if i[0] == 'p' , it will only consider the element which starts with "p".

So the result will be ['PYTHON2', 'PYTHON3', 'PERL5']

Discuss It

Q. 5

What is the output of the following code ?

>>> s1 = [34, 56, 71]
>>> s2 = s1
>>> s2[2] = 21
>>> s1

 

Options:

Explanation

Correct answer is : B

In s2 = s1 , both s1 and s2 shared the reference and refers to the same location in the memory.

When we change s2 in place elements s2[2] = 21, it also changes s1.

 

Discuss It

Q. 6

What is the output of the following code ? 

>>> s1 = [57, 18 , 3, 23]
>>> s2 = s1[:]
>>> s2[2] = 67
>>> s1

 

Options:

Explanation

Correct answer is : A

In the expression s2 = s1[:] , it assigns a shallow copy of s1 to s2.

So any in place change will not affect s1 and the s1 value will remain same

Discuss It

Q. 7

What is the output of the following code ?

>>> s1 = [92 , 57, [82, 75]]
>>> s2 = s1[:]
>>> s2[2][1] = 23
>>> s1

 

Options:

Explanation

Correct answer is : A

In the expression s2 = s1[:] , a shallow copy of s1 is assigned to s2.

In case of shallow copy the reference to the inner elements like [82, 75] will be same.

So any change to [82, 75] in the new list s2 will change the inner list [82, 75] and s2[2][1] = 23 will make it [82, 23]

 

Discuss It

Q. 8

What is the output of the following code ? 

>>> s = ['apple', 'orange', 'banana']
>>> s.sort(key=len)
>>> s

 

Options:

Explanation

Correct answer is : A

s.sort(key=len) - Here "s" will sort as per the key "len".. as "apple" has length 5 . It will ramain in the first postion and others remain unchanged .

So the result will be unchanged

Discuss It

Q. 9

What is the output of the following code ?

>>> s = [45, 67, 2]
>>> p = s.copy()
>>> p == s, p is s

 

Options:

Explanation

Correct answer is : D

When we do p = s.copy() , it gives a shallow copy of s. So the reference will be changed so p is s will gives False and p == s  gives true as it matches the values inside the list

Discuss It

Q. 10

What is the output of the following code ?

s = list((23,) * 4)
print((2) * 4)
print(s)

 

Options:

Explanation

Correct answer is : C

(23,) * 4 gives a tuple (23, 23, 23, 23) and when converted to list using list() ,  it gives [23, 23, 23, 23]

(2) * 4 is nothing but 2 * 4 i.e. 8

so the result will be 

8
[23, 23, 23, 23]

Discuss It
Submit Your Answers

Interview Questions

on
Data Types

Tutorial

on
Data Types

Programs

on
Data Types
Click any Link
to navigate to certain page easily
Write a line to us
Your Email
Title
Description