# Python/Python Mcq Set 9 Sample Test,Sample questions

## Question: ``` Is the following Python code valid? >>> a,b=1,2,3```

1.Yes, this is an example of tuple unpacking. a=1 and b=2

2.Yes, this is an example of tuple unpacking. a=(1,2) and b=3

3.No, too many values to unpack

4.Yes, this is an example of tuple unpacking. a=1 and b=(2,3)

1.print(t[3])

2. t[3] = 45

3.print(max(t))

4.print(len(t))

## Question: ``` What will be the output of the following Python code? >>> a=("Check")*3 >>> a```

1. (â€˜Checkâ€™,â€™Checkâ€™,â€™Checkâ€™)

2.* Operator not valid for tuples

3. (â€˜CheckCheckCheckâ€™)

4.Syntax error

## Question: ``` What will be the output of the following Python code? >>> a=(2,3,1,5) >>> a.sort() >>> a```

1. (1,2,3,5)

2.(2,3,1,5)

3.None

4.Error, tuple has no attribute sort

1. True

2. False

3. Error

4.None

## Question: ``` What will be the output of the following Python code? A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] B = [[3, 3, 3], [4, 4, 4], [5, 5, 5]] zip(A, B)```

2.Address of the matrices A and B

3. No output

4.[3, 6, 9, 16, 20, 24, 35, 40, 45]

## Question: ``` What will be the output of the following Python code? l=["good", "oh!", "excellent!", "#450"] [n for n in l if n.isalpha() or n.isdigit()]```

1.[â€˜goodâ€™, â€˜ohâ€™, â€˜excellentâ€™, â€˜450â€™ ]

2. [â€˜goodâ€™]

3.[â€˜goodâ€™, â€˜#450â€™]

4. [â€˜oh!â€™, â€˜excellent!â€™, â€˜#450â€™]

1.[0]

2.0

3. [0.00]

4.Error

1.[1, 2, 3]

2.(1, 2, 3)

3.{1, 2, 3}

4.{}

## Question: ` Write a list comprehension to produce the list: [1, 2, 4, 8, 16â€¦â€¦212].`

1. [(2**x) for x in range(0, 13)]

2.[(x**2) for x in range(1, 13)]

3.[(2**x) for x in range(1, 13)]

4. [(x**2) for x in range(0, 13)]

## Question: `If a=(1,2,3,4), a[1:-1] is ____`

1.Error, tuple slicing doesnâ€™t exist

2. [2,3]

3.(2,3,4)

4.(2,3)

## Question: ```Is the following Python code valid? >>> a,b,c=1,2,3 >>> a,b,c```

1. Yes, [1,2,3] is printed

2. No, invalid syntax

3. Yes, (1,2,3) is printed

4.1 is printed

## Question: ```Is the following Python code valid? >>> a=(1,2,3) >>> b=('A','B','C') >>> c=tuple(zip(a,b))```

1.Yes, c will be ((1, â€˜Aâ€™), (2, â€˜Bâ€™), (3, â€˜Câ€™))

2.Yes, c will be ((1,2,3),(â€˜Aâ€™,â€™Bâ€™,â€™Câ€™))

3.No because tuples are immutable

4.No because the syntax for zip function isnâ€™t valid

## Question: ```Is the following Python code valid? >>> a=(1,2,3) >>> b=a.update(4,)```

1.Yes, a=(1,2,3,4) and b=(1,2,3,4)

2.Yes, a=(1,2,3) and b=(1,2,3,4)

3.No because tuples are immutable

4.No because wrong syntax for update() method

## Question: ```Is the following Python code valid? >>> a=(1,2,3,4) >>> del a```

1.No because tuple is immutable

2. Yes, first element in the tuple is deleted

3. Yes, the entire tuple is deleted

4.No, invalid syntax for del method

## Question: ```Is the following Python code valid? >>> a=2,3,4,5 >>> a```

1. Yes, 2 is printed

2. Yes, [2,3,4,5] is printed

3.No, too many values to unpack

4.Yes, (2,3,4,5) is printed

## Question: `What is the data type of (1)?`

1. Tuple

2. Integer

3.List

4.Both tuple and integer

## Question: `What is the list comprehension equivalent for: list(map(lambda x:x**-1, [1, 2, 3]))?`

1. [1|x for x in [1, 2, 3]]

2.[-1**x for x in [1, 2, 3]]

3. [x**-1 for x in [1, 2, 3]]

4. [x^-1 for x in range(4)]

## Question: ```What is the list comprehension equivalent for? {x : x is a whole number less than 20, x is even} (including zero)```

1. [x for x in range(1, 20) if (x%2==0)]

2. [x for x in range(0, 20) if (x//2==0)]

3. [x for x in range(1, 20) if (x//2==0)]

4. [x for x in range(0, 20) if (x%2==0)]

## Question: `What type of data is: a=[(1,1),(2,4),(3,9)]?`

1. Array of tuples

2.List of tuples

3.Tuples of lists

4.Invalid type

## Question: ```What will be the output of the following Python code? >>> a,b=6,7 >>> a,b=b,a >>> a,b```

1. (6,7)

2.Invalid syntax

3. (7,6)

4.Nothing is printed

## Question: ```What will be the output of the following Python code? >>> a=(0,1,2,3,4) >>> b=slice(0,2) >>> a[b]```

1. Invalid syntax for slicing

2. [0,2]

3.(0,1)

4. (0,2)

## Question: ```What will be the output of the following Python code? >>> a=(1,2) >>> b=(3,4) >>> c=a+b >>> c```

1. (4,6)

2. (1,2,3,4)

3.Error as tuples are immutable

4.None of these

## Question: ```What will be the output of the following Python code? >>> a=(1,2,3,4) >>> del(a[2])```

1. Now, a=(1,2,4)

2. Now, a=(1,3,4)

3.Now a=(3,4)

4.Error as tuple is immutable

## Question: ```What will be the output of the following Python code? >>> a=(2,3,4) >>> sum(a,3)```

1.Too many arguments for sum() method

2.The method sum() doesnâ€™t exist for tuples

3.12

4. 9

## Question: ```What will be the output of the following Python code? >>> a=[(2,4),(1,2),(3,9)] >>> a.sort() >>> a```

1.[(1, 2), (2, 4), (3, 9)]

2.[(2,4),(1,2),(3,9)]

3.Error because tuples are immutable

4.Error, tuple has no sort attribute

## Question: ```What will be the output of the following Python code? >>> import collections >>> a=collections.namedtuple('a',['i','j']) >>> obj=a(i=4,j=7) >>> obj```

1. a(i=4, j=7)

2.obj(i=4, j=7)

3.(4,7)

4.An exception is thrown

1. 1

2. 2

3. 5

4.Error

1. (1, 2, 1, 2)

2.[1, 2, 1, 2]

3.(1, 1, 2, 2)

4.[1, 1, 2, 2]

## Question: ```What will be the output of the following Python code? >>>t = (1, 2, 4, 3, 8, 9) >>>[t[i] for i in range(0, len(t), 2)]```

1. [2, 3, 9]

2.[1, 2, 4, 3, 8, 9]

3.[1, 4, 8]

4.(1, 4, 8)

1. (1, 2)

2.(1, 2, 4)

3.(2, 4)

4.(2, 4, 3)

1. (1, 2)

2.(1, 2, 4)

3.(2, 4)

4.(2, 4, 3)

1. [7, 8, 9]

2.[4, 5, 6]

3. [2, 5, 8]

4.[1, 4, 7]

## Question: ```What will be the output of the following Python code? A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] [[col + 10 for col in row] for row in A]```

1. [[11, 12, 13], [14, 15, 16], [17, 18, 19]]

2.Error

3.[11, 12, 13], [14, 15, 16], [17, 18, 19]

4. [11, 12, 13, 14, 15, 16, 17, 18, 19]

## Question: ```What will be the output of the following Python code? A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] B = [[3, 3, 3], [4, 4, 4], [5, 5, 5]] [B[row][col]*A[row][col] for row in range(3) for col in range(3)]```

1.[3, 6, 9, 16, 20, 24, 35, 40, 45]

2. Error

3. [0, 30, 60, 120, 160, 200, 300, 350, 400]

4. 0

## Question: ```What will be the output of the following Python code? A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] B = [[3, 3, 3], [4, 4, 4], [5, 5, 5]] [[col1 * col2 for (col1, col2) in zip(row1, row2)] for (row1, row2) in zip(A, B)]```

1. [0, 30, 60, 120, 160, 200, 300, 350, 400]

2.[[3, 6, 9], [16, 20, 24], [35, 40, 45]]

3.No output

4. error

1. [1, 5, 9]

2.[3, 5, 7]

3. [4, 5, 6]

4.[2, 5, 8]

1.[1, 5, 9]

2.[4, 5, 6]

3.[3, 5, 7]

4. [2, 5, 8]

1.40

2.45

3.â€œjohnâ€

4.â€œpeterâ€

## Question: ```What will be the output of the following Python code? l=[[1, 2, 3], [4, 5, 6]] for i in range(len(l)): for j in range(len(l[i])): l[i][j]+=10 l```

1.No output

2. Error

3.[[1, 2, 3], [4, 5, 6]]

4. [[11, 12, 13], [14, 15, 16]]

1.30

2. 24

3. 33

4.12

## Question: ```What will be the output of the following Python code? r = [11, 12, 13, 14, 15, 16, 17, 18, 19] A = [[0, 10, 20], [30, 40, 50], [60, 70, 80]] for row in A: for col in row: r.append(col+10)```

1. [11, 12, 13, 14, 15, 16, 17, 18, 19, 10, 20, 30, 40, 50, 60, 70, 80, 90]

2. [10, 20, 30, 40, 50, 60, 70, 80, 90]

3.[11, 12, 13, 14, 15, 16, 17, 18, 19]

4. [0, 10, 20, 30, 40, 50, 60, 70, 80]

## Question: ```What will be the output of the following Python code? [ord(ch) for ch in 'abc']```

1.[97, 98, 99]

2. [â€˜97â€™, â€˜98â€™, â€˜99â€™]

3.[65, 66, 67]

4.[65, 66, 67]

## Question: ```What will be the output of the following Python code? >>> a=(1,2,(4,5)) >>> b=(1,2,(3,4)) >>> a<b```

1.False

2.True

3.Error, < operator is not valid for tuples

4.Error, < operator is valid for tuples but not if there are sub-tuples

1. (1, 2)

2. (1, 2, 4)

3. (2, 4)

4. (2, 4, 3)

## Question: ```What will be the output of the following Python list comprehension? [j for i in range(2,8) for j in range(i*2, 50, i)]```

1.A list of prime numbers up to 50

2.A list of numbers divisible by 2, up to 50

3. A list of non prime numbers, up to 50

4.error

1. A[2][3]

2.A[2][1]

3. A[1][2]

4. A[3][2]

## Question: `Which of these about a set is not true?`

1.Mutable data type

2.Allows duplicate values

3.Data type with unordered values

4.Immutable data type

## Question: ```Write a list comprehension equivalent for the Python code shown below. for i in range(1, 101): if int(i*0.5)==i*0.5: print(i)```

1. [i for i in range(1, 100) if int(i*0.5)==(i*0.5)]

2. [i for i in range(1, 101) if int(i*0.5)==(i*0.5)]

3. [i for i in range(1, 101) if int(i*0.5)=(i*0.5)]

4.[i for i in range(1, 100) if int(i*0.5)=(i*0.5)]

## Question: `Write a list comprehension for producing a list of numbers between 1 and 1000 that are divisible by 3.`

1. [x in range(1, 1000) if x%3==0]

2. [x for x in range(1000) if x%3==0]

3. [x%3 for x in range(1, 1000)]

4.[x%3=0 for x in range(1, 1000)]

## More MCQS

##### Olete Team
Online Exam TestTop Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on Online Exam Testwebsite is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!