Python MCQ Tricky Questions

¡Supera tus tareas y exámenes ahora con Quizwiz!

numbers = [1, 2, 3, 4] numbers.append([5,6,7,8]) print(len(numbers))

5 , a list is passed into the list

example = "snow world" example[3] = 's' print example

Error, Strings cannot be modified

x = "abcdef" while i in x: print(i, end=" ")

NameError, i is not defined. i in x (condition)

t[5]

The expression shown above results in a name error. This is because the name 't' is not defined

x = ['ab', 'cd'] for i in x: x.append(i.upper()) print(x)

The loop does not terminate as new elements are being added to the list in each iteration.

dictionary1 = {'Google' : 1, 'Facebook' : 2, 'Microsoft' : 3 } dictionary2 = {'GFG' : 1, 'Microsoft' : 2, 'Youtube' : 3 } dictionary1.update(dictionary2); for key, values in dictionary1.items(): print(key, values)

('Google', 1)('Facebook', 2)('Youtube', 3)('Microsoft', 2)('GFG', 1) Dictionary1.update(dictionary2) is used to update the entries of dictionary1 with entries of dictionary2. If there are same keys in two dictionaries, then the value in second dictionary is used.

d = {0: 'a', 1: 'b', 2: 'c'} for i in d: print(i)

0 1 2 (Loop over the keys only)

a = [0, 1, 2, 3] for a[-1] in a: print(a[-1])

0 1 2 2 (update dictionary)

i=0 def change(i): i=i+1 return i change(1) print(i)

0, Any change made in to an immutable data type in a function isn't reflected outside the function. integers are immutable

total={} def insert(items): if items in total: total[items] += 1 else: total[items] = 1 insert('Apple') insert('Ball') insert('Apple') print (len(total))

2 instead of 3 as apple is repeated.

def change(var, lst): var = 1 lst[0] = 44 k = 3 a = [1, 2, 3] change(k, a) print(k) print(a

3 [44, 2, 3] integer not mutable plus k is a local variable

k = [print(i) for i in my_string if i not in "aeiou"] print(k)

A list of nones, (Already printed i, so list of none is output)

d1 = {"john":40, "peter":45} d2 = {"john":466, "peter":45} d1 > d2

Arithmetic > operator cannot be used with dictionaries.

valid = False while not valid: try: n=int(input("Enter a number")) while n%2==0: print("Bye") valid = True except ValueError: print("Invalid")

Bye (printed infinite number of times) The code shown above results in the word "Bye" being printed infinite number of times. This is because an even number has been given as input. If an odd number had been given as input, then there would have been no output.

x = 'abcd' for i in range(len(x)): x[i].upper() print (x)

Changes do not happen in-place, rather a new instance of the string is returned. no return value

>>>print("D", end = ' ') >>>print("C", end = ' ') >>>print("B", end = ' ') >>>print("A", end = ' ')

D, C, B, A will be displayed on four lines

d = {0: 'a', 1: 'b', 2: 'c'} for x, y in d: print(x, y)

Error, objects of type int aren't iterable.

>>>list1 = [11, 2, 23] >>>list2 = [11, 2, 2] >>>list1 < list2 is

False, list are compared one by one

def increment_items(L, increment): i = 0 while i < len(L): L[i] = L[i] + increment i = i + 1 values = [1, 2, 3] print(increment_items(values, 2)) print(values)

None Values = [3,4,5]

a = [0, 1, 2, 3] i = -2 for i not in a: print(i) i += 1

SyntaxError, not in isn't allowed in for loops

word1="Apple" word2="Apple" list1=[1,2,3] list2=[1,2,3] print(word1 is word2) print(list1 is list2)

True False (list are identical but have diff objects)

list = ['a', 'b', 'c', 'd', 'e'] print list[10: ]

[ ] As one would expect, attempting to access a member of a list using an index that exceeds the number of members (e.g., attempting to access list[10] in the list above) results in an IndexError. However, attempting to access a slice of a list at a starting index that exceeds the number of members in the list will not result in an IndexError and will simply return an empty list.

x = ['ab', 'cd'] for i in x: i.upper() print(x)

['ab', 'cd']

def gfg(x,l=[ ]): for i in range(x): l.append(i*i) print(l) gfg(2) gfg(3,[3,2,1]) gfg(3)

[0, 1] [3, 2, 1, 0, 1, 4] [0, 1, 0, 1, 4] The third function call is the weird one. It uses the original list stored in the original memory block. That is why it starts off with 0 and 1.

a=[1,2,3] b=a.append(4) print(a) print(b)

[1, 2, 3, 4] None, Append function does not return anything

def f(i, values = []): values.append(i) return values f(1) f(2) v = f(3) print(v)

[1, 2, 3]

lst=[3,4,6,1,2] lst[1:2]=[7,8] print(lst)

[3, 7, 8, 6, 1, 2]

L = [2e-04, 'a', False, 87] T = (6.22, 'boy', True, 554) for i in range(len(L)): if L[i]: L[i] = L[i] + T[i] else: T[i] = L[i] + T[i] break

[6.2202, 'aboy', False, 87] As tuples are immutable, the code will end with TypeError, but elements of L will be updated.

print([[i+j for i in "abc"] for j in "def"])

[['ad', 'bd', 'cd'], ['ae', 'be', 'ce'], ['af', 'bf', 'cf']] outer list (without brackets) then inner list.

l=[[1 ,2, 3], [4, 5, 6], [7, 8, 9]] [[row[i] [for row in l] for i in range(3)]

[[1, 4, 7], [2, 5, 8], [3, 6, 9]] In the code shown above, '3' is the index of the list. Had we used a number greater than 3, it would result in an error

a=[[]]*3 a[1].append(7) print(a)

[[7], [7], [7]]

d = {0: 'a', 1: 'b', 2: 'c'} for x in d.keys(): print(d[x])

a b c (Loops over the keys and prints the values)

print([if i%2==0: i; else: i+1; for i in range(4)])

invalid syntax cuz infront should be an expression

>>>list1 = [1, 3] >>>list2 = list1 >>>list1[0] = 4 >>>print(list2)

list 2 = [4, 3]

Which of the following is the correct expansion of list_1 = [expr(i) for i in list_0 if func(i)]?

list_1 = [ ] for i in list_0: if func(i): list_1.append(expr(i)) We have to create an empty list, loop over the contents of the existing list and check if a condition is satisfied before performing some operation and adding it to the new list.

string = "my name is x" for i in ' '.join(string.split()): print (i, end=", ")

m, y, , n, a, m, e, , i, s, , x,

print('new' 'line')

newline (space bar means concatenation)

for i in range(0): print(i)

no output


Conjuntos de estudio relacionados

Percy Jackson: True or False Quiz

View Set

Chemistry Exam #2 (Practice Exams)

View Set

techniques of Therapeutic communication

View Set

Exam 3 matching/practice questions

View Set

The Sword in the Stone, Vocabulary Chapters 1-2

View Set

FST 100A HW5: Lipid Reactions - Exam #2

View Set