Select Page
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

## Python MCQ Set 1

1. Which of the following commands will create a list?
a) list1 = list()
b) list1 = [].
c) list1 = list([1, 2, 3])
d) all of the mentioned

Answer: d [Reason:] Execute in the shell to verify

2. What is the output when we execute list(“hello”)?
a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’].
b) [‘hello’].
c) [‘llo’].
d) [‘olleh’].

Answer: a [Reason:] Execute in the shell to verify.

3. Suppose listExample is [‘h’,’e’,’l’,’l’,’o’], what is len(listExample)?
a) 5
b) 4
c) None
d) Error

Answer: a [Reason:] Execute in the shell and verify.

4. Suppose list1 is [2445,133,12454,123], what is max(list1) ?
a) 2445
b) 133
c) 12454
d) 123

Answer: c [Reason:] Max returns the maximum element in the list.

5. Suppose list1 is [3, 5, 25, 1, 3], what is min(list1) ?
a) 3
b) 5
c) 25
d) 1

Answer: d [Reason:] Min returns the minimum element in the list.

6. Suppose list1 is [1, 5, 9], what is sum(list1) ?
a) 1
b) 9
c) 15
d) Error

Answer: c [Reason:] Sum returns the sum of all elements in the list.

7. To shuffle the list(say list1) what function do we use ?
a) list1.shuffle()
b) shuffle(list1)
c) random.shuffle(list1)
d) random.shuffleList(list1)

Answer: c [Reason:] Execute in the shell to verify .

8. Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following is correct syntax for slicing operation ?
a) print(list1[0])
b) print(list1[:2])
c) print(list1[:-2])
d) all of the mentioned

Answer: d [Reason:] Slicing is allowed in lists just as in the case of strings.

9. Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1] ?
a) Error
b) None
c) 25
d) 2

Answer: c [Reason:] -1 corresponds to the last index in the list.

10. Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1] ?
a) [2, 33, 222, 14].
b) Error
c) 25
d) [25, 14, 222, 33, 2].

Answer: a [Reason:] Execute in the shell to verify.

## Python MCQ Set 2

1. What is the output when following statement is executed ?

1. `>>>"a"+"bc"`

a) a
b) bc
c) bca
d) abc

Answer: d [Reason:] + operator is concatenation operator.

2. What is the output when following statement is executed ?

1. `>>>"abcd"[2:]`

a) a
b) ab
c) cd
d) dc

Answer: c [Reason:] Slice operation is performed on string.

3. The output of executing string.ascii_letters can also be achieved by:
a) string.ascii_lowercase_string.digits
b) string.ascii_lowercase+string.ascii_upercase
c) string.letters
d) string.lowercase_string.upercase

Answer: b [Reason:] Execute in shell and check.

4. What is the output when following code is executed ?

1. `>>> str1 = 'hello'`
2. `>>> str2 = ','`
3. `>>> str3 = 'world'`
4. `>>> str1[-1:]`

a) olleh
b) hello
c) h
d) o

Answer: d [Reason:] -1 corresponds to the last index.

5. What arithmetic operators cannot be used with strings ?
a) +
b) *
c) –
d) All of the mentioned

Answer: c [Reason:] + is used to concatenate and * is used to multiply strings.

6. What is the output when following code is executed ?

1. `>>>print r"nhello"`

The output is
a) a new line and hello
b) nhello
c) the letter r and then hello
d) error

Answer: b [Reason:] When prefixed with the letter ‘r’ or ‘R’ a string literal becomes a raw string and the escape sequences such as n are not converted.

7. What is the output when following statement is executed ?

1. `>>>print('new' 'line')`

a) Error
b) Output equivalent to print ‘newnline’
c) newline
d) new line

Answer: c [Reason:] String literals seperated by white space are allowed. They are concatenated.

8. What is the output when following statement is executed ?
>>> print(‘x97x98’)
a) Error
b) 97
98
c) x97
d) x97x98

Answer: c [Reason:] x is an escape sequence that means the following 2 digits are a hexadicmal number encoding a character.

9. What is the output when following code is executed ?

1. `>>>str1="helloworld"`
2. `>>>str1[::-1]`

a) dlrowolleh
b) hello
c) world
d) helloworld

Answer: a [Reason:] Execute in shell to verify.

10. print(0xA + 0xB + 0xC) :
a) 0xA0xB0xC
b) Error
c) 0x22
d) 33

Answer: d [Reason:] 0xA and 0xB and 0xC are hexadecimal integer literals representing the decimal values 10,11 and 12 respectively. There sum is 33.

## Python MCQ Set 3

1. Which of the following is a Python tuple?
a) [1, 2, 3].
b) (1, 2, 3)
c) {1, 2, 3}
d) {}

Answer: b [Reason:] Tuples are represented with round brackets.

2. Suppose t = (1, 2, 4, 3), which of the following is incorrect?
a) print(t[3])
b) t[3] = 45
c) print(max(t))
d) print(len(t))

Answer: b [Reason:] Values cannot be modified in the case of tuple, that is, tuple is immutable.

3. What will be the output?

1. `>>>t=(1,2,4,3)`
2. `>>>t[1:3]`

a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)

Answer: c [Reason:] Slicing in tuples takes place just as it does in strings.

4. What will be the output?

1. `>>>t=(1,2,4,3)`
2. `>>>t[1:-1]`

a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)

Answer: c [Reason:] Slicing in tuples takes place just as it does in strings.

5. What will be the output?

1. `>>>t = (1, 2, 4, 3, 8, 9)`
2. `>>>[t[i] for i in range(0, len(t), 2)]`

a) [2, 3, 9].
b) [1, 2, 4, 3, 8, 9].
c) [1, 4, 8].
d) (1, 4, 8)

Answer: c [Reason:] Execute in the shell to verify.

6. What will be the output?

1. `d = {"john":40, "peter":45}`
2. `d["john"]`

a) 40
b) 45
c) “john”
d) “peter”

Answer: a [Reason:] Execute in the shell to verify.

7. What will be the output?

1. `>>>t = (1, 2)`
2. `>>>2 * t`

a) (1, 2, 1, 2)
b) [1, 2, 1, 2].
c) (1, 1, 2, 2)
d) [1, 1, 2, 2].

Answer: a [Reason:] * operator concatenates tuple.

8. What will be the output?

1. `>>>t1 = (1, 2, 4, 3)`
2. `>>>t2 = (1, 2, 3, 4)`
3. `>>>t1 < t2`

a) True
b) False
c) Error
d) None

Answer: b [Reason:] Elements are compared one by one in this case.

9. What will be the output?

1. `>>>my_tuple = (1, 2, 3, 4)`
2. `>>>my_tuple.append( (5, 6, 7) )`
3. `>>>print len(my_tuple)`

a) 1
b) 2
c) 5
d) Error

Answer: d [Reason:] Tuples are immutable and don’t have an append method. An exception is thrown in this case.

10. What will be the output?

1. `numberGames = {}`
2. `numberGames[(1,2,4)] = 8`
3. `numberGames[(4,2,1)] = 10`
4. `numberGames[(1,2)] = 12`
5. `sum = 0`
6. `for k in numberGames:`
7. `    sum += numberGames[k]`
8. `print len(numberGames) + sum`

a) 30
b) 24
c) 33
d) 12

Answer: c [Reason:] Tuples can be used for keys into dictionary. The tuples can have mixed length and the order of the items in the tuple is considered when comparing the equality of the keys.

## Python MCQ Set 4

1. Which is the correct operator for power(x^y)?
a) X^y
b) X**y
c) X^^y
d) None of the mentioned

Answer: b [Reason:] In python, power operator is x**y i.e. 2**3=8.

2. Which one of these is floor division?
a) /
b) //
c) %
d) None of the mentioned

Answer: b [Reason:] When both of the operands are integer then python chops out the fraction part and gives you the round off value, to get the accurate answer use floor division. This is floor division. For ex, 5/2 = 2.5 but both of the operands are integer so answer of this expression in python is 2.To get the 2.5 answer, use floor division.

3. What is the order of precedence in python?
i) Parentheses
ii) Exponential
iii) Multiplication
iv) Division
vi) Subtraction
a) i,ii,iii,iv,v,vi
b) ii,i,iii,iv,v,vi
c) ii,i,iv,iii,v,vi
d) i,ii,iii,iv,vi,v

Answer: a [Reason:] For order of precedence, just remember this PEMDAS (similar to BODMAS)

4. What is answer of this expression, 22 % 3 is?
a) 7
b) 1
c) 0
d) 5

Answer: b [Reason:] Modulus operator gives remainder. So, 22%3 gives the remainder, that is, 1.

5. Mathematical operations can be performed on a string. State whether true or false.
a) True
b) False

Answer: b [Reason:] You can’t perform mathematical operation on string even if the string is in the form: ‘1234…’.

6. Operators with the same precedence are evaluated in which manner?
a) Left to Right
b) Right to Left
c) Cant say
d) None of the mentioned

7. What is the output of this expression, 3*1**3?
a) 27
b) 9
c) 3
d) 1

Answer: c [Reason:] First this expression will solve 1**3 because exponential have higher precedence than multiplication, so 1**3 = 1 and 3*1 = 3. Final answer is 3.

8. Which one of the following have the same precedence?
b) Multiplication and Division
c) Both a and b
d) None of the mentioned

9. The expression Int(x) implies that the variable x is converted to integer. State whether true or false.
a) True
b) False

10. Which one of the following have the highest precedence in the expression?
a) Exponential
c) Multiplication
d) Parentheses

Answer: d [Reason:] Just remember: PEDMAS, that is, Parenthesis, Exponentiation, Division, Multiplication, Addition, Subtraction. Note that the precedence order of Division and Multiplication is the same. Likewise, the order of Addition and Subtraction is also the same.

## Python MCQ Set 5

1. What is the output of the following?

`print("xyyzxyzxzxyy".count('yy'))`

a) 2
b) 0
c) error
d) none of the mentioned

Answer: a [Reason:] Counts the number of times the substring ‘yy’ is present in the given string.

2. What is the output of the following?

`print("xyyzxyzxzxyy".count('yy', 1))`

a) 2
b) 0
c) 1
d) none of the mentioned

Answer: a [Reason:] Counts the number of times the substring ‘yy’ is present in the given string, starting from position 1.

3. What is the output of the following?

`print("xyyzxyzxzxyy".count('yy', 2))`

a) 2
b) 0
c) 1
d) none of the mentioned

Answer: c [Reason:] Counts the number of times the sub-string ‘yy’ is present in the given string, starting from position 2.

4. What is the output of the following?

`print("xyyzxyzxzxyy".count('xyy', 0, 100))`

a) 2
b) 0
c) 1
d) error

Answer: a [Reason:] An error will not occur if the end value is greater than the length of the string itself.

5. What is the output of the following?

`print("xyyzxyzxzxyy".count('xyy', 2, 11))`

a) 2
b) 0
c) 1
d) error

Answer: b [Reason:] Counts the number of times the sub-string ‘xyy’ is present in the given string, starting from position 2 and ending at position 11.

6. What is the output of the following?

`print("xyyzxyzxzxyy".count('xyy', -10, -1))`

a) 2
b) 0
c) 1
d) error

Answer: b [Reason:] Counts the number of times the substring ‘xyy’ is present in the given string, starting from position 2 and ending at position 11.

7. What is the output of the following?

`print('abc'.encode())`

a) abc
b) ‘abc’
c) b’abc’
d) h’abc’

Answer: c [Reason:] A bytes object is returned by encode.

8. What is the default value of encoding in encode()?
a) ascii
b) qwerty
c) utf-8
d) utf-16

Answer: c [Reason:] The default value of encoding is utf-8.

9. What is the output of the following?

`print("xyyzxyzxzxyy".endswith("xyy"))`

a) 1
b) True
c) 3
d) 2

Answer: b [Reason:] The function returns True if the given string ends with the specified sub-string.

10. What is the output of the following?

`print("xyyzxyzxzxyy".endswith("xyy", 0, 2))`

a) 0
b) 1
c) True
d) False