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. What is the output of the code shown?

re.compile('hello', re.X)

a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
b) re.compile(‘hello’, re.VERBOSE)
c) Error
d) Junk value

View Answer

Answer: b [Reason:] The compile function compiles a pattern of regular expression into an object of regular expression. Re.X is a flag which is also used as re.VERBOSE. Hence the output of this code is: re.compile(‘hello’, re.VERBOSE).

2. What is the output of the code shown below?

re.split('[a-c]', '0a3B6', re.I)

a) Error
b) [‘a’, ‘B’]
c) [‘0’, ‘3B6’]
d) [‘a’]

View Answer

Answer: c [Reason:] The function re.split() splits the string on the basis of the pattern given in the parenthesis. Since we have used the flag e.I (that is, re.IGNORECASE), the output is: [‘0’, ‘3B6’].

3. What is the output of the code shown below?

re.sub('morning', 'evening', 'good morning')

a) ‘good evening’
b) ‘good’
c) ‘morning’
d) ‘evening’

View Answer

Answer: a [Reason:] The code shown above first searches for the pattern ‘morning’ in the string ‘good morning’ and then replaces this pattern with ‘evening’. Hence the output of this code is: ‘good evening’.

4. The function re.error raises an exception if a particular string contains no match for the given pattern. State whether true or false.
a) True
b) False

View Answer

Answer: b [Reason:] The function re.error raises an exception when a string passed to one of its functions here is not a valid regular expression. It does not raise an exception if a particular string does not contain a match for the given pattern.

5. The output of the code shown below:

re.escape('new**world')

a) ‘new world’
b) ‘new**world’
c) ‘**’
d) ‘new’, ‘*’, ‘*’, ‘world’

View Answer

Answer: b [Reason:] The function re.escape escapes all the characters in the pattern other than ASCII letters and numbers. Hence the output of the code shown above is: ‘new**world’.

6. What is the output of the code shown below?

re.fullmatch('hello', 'hello world')

a) No output
b) []
c) <_sre.SRE_Match object; span=(0, 5), match='hello'>
d) Error

View Answer

Answer: a [Reason:] The function re.fullmatch applies the pattern to the entire string and returns an object if match is found and none if match in not found. In the code shown above, match is not found. Hence there is no output.

7. Choose the option wherein the two choices do not refer to the same option.
a) re.I
re.IGNORCASE
b) re.M
re.MULTILINE
c) re.X
re.VERBOSE
d) re.L
re.LOWERCASE

View Answer

Answer: d [Reason:] The function re.L is also written as re.LOCALE. There is no function such as re.LOWERCASE in the re module of Python.

8. The difference between the functions re.sub and re.subn is that re.sub returns a _______________ whereas re.subn returns a __________________
a) string, list
b) list, tuple
c) string, tuple
d) tuple, list

View Answer

Answer: c [Reason:] The difference the functions re.sub and re.subn is that re.sub returns a string whereas re.subn returns a tuple.

9. The output of the code shown below is:

re.split('mum', 'mumbai*', 1)

a) Error
b) [”, ‘bai*’]
c) [”, ‘bai’]
d) [‘bai*’]

View Answer

Answer: b [Reason:] The code shown above splits the string based on the pattern given as an argument. Hence the output of the code is: [”, ‘bai*’].

10. What is the output of the code shown below?

re.findall('good', 'good is good')
re.findall('good', 'bad is good')

a) [‘good’, ‘good’]
[‘good’]
b) (‘good’, ‘good’)

(good)

c) (‘good’)

(‘good’)

d) [‘good’]
[‘good’]

View Answer

Answer: a [Reason:] The function findall returns a list of all the non overlapping matches in a string. Hence the output of the first function is: [‘good’, ‘good’] and that of the second function is: [‘good’].

Python MCQ Set 2

1. The output of the snippet of code shown below is:

re.split(r'(nd)=', 'n1=3.1, n2=5, n3=4.565')

a) Error
b) [”, ‘n1’, ‘3.1, ‘, ‘n2’, ‘5, ‘, ‘n3’, ‘4.565’]
c) [‘n1’, ‘3.1, ‘, ‘n2’, ‘5, ‘, ‘n3’, ‘4.565’]
d) [‘3.1, ‘, ‘5, ‘, ‘4.565’]

View Answer

Answer: b [Reason:] In the snippet of code shown above, we extract the numbers as a list of floating point values, including the initial empty string. The example shown above demonstrate how groups in the regular expression influence the result of re.split. Hence the output of the code shown above is: [”, ‘n1’, ‘3.1, ‘, ‘n2’, ‘5, ‘, ‘n3’, ‘4.565’]

2. The function of re.search is:
a) Matches a pattern at the start of the string
b) Matches a pattern at the end of the string
c) Matches a pattern from any part of a string
d) Such a function does not exist

View Answer

Answer: c [Reason:] The re module of Python consists of a function re.search. It’s function is to match a pattern from anywhere in a string.

3. Which of the following functions creates a Python object?
a) re.compile(str)
b) re.assemble(str)
c) re.regex(str)
d) re.create(str)

View Answer

Answer: a [Reason:] The function re.compile(srt) compiles a pattern of regular expression into an object of regular expression. Hence re.compile(str) is the only function from the above options which creates an object.

4. Which of the following pattern matching modifiers permits whitespace and comments inside the regular expression:
a) re.L
b) re.S
c) re.U
d) re.X

View Answer

Answer: d [Reason:] The modifier re.X allows whitespace and comments inside the regular expressions.

5. What is the output of the code shown below?

s = 'welcome home'
m = re.match(r'(.*)(.*?)', s)
print(m.group())

a) (‘welcome’, ‘home’)
b) [‘welcome’, ‘home’]
c) welcome home
d) [‘welcome’ // ‘home’ ]

View Answer

Answer: c [Reason:] The code shown above shows the function re.match combined with the use of special characters. Hence the output of this code is: welcome home.

6. The function of re.match is:
a) Error
b) Matches a pattern anywhere in the string
c) Matches a pattern at the end of the string
d) Matches a pattern at the start of the string

View Answer

Answer: d [Reason:] The function of re.match matches a pattern at the start of the string.

7. The special character B matches the empty string, but only when it is:
a) at the beginning or end of a word
b) not at the beginning or end of a word
c) at the beginning of the word
d) at the end of the word

View Answer

Answer: b [Reason:] The special character B matches the empty string, but only when it is not at the beginning or end of a word.

8. The output of the code shown below is:

import re
s = "A new day"
m = re.match(r'(.*)(.*?)', s)
print(m.group(2))
 
print(m.group(0))

a) No output
A new day
b) No output
No output
c) [‘A’, ‘new’, ‘day’]
(‘A’, ‘new’, ‘day’)
d) Error
[‘A’, ‘new’, ‘day’]

View Answer

Answer: a [Reason:] The code shown above demonstrates the use of the function re.match, with different arguments given to the group method. Hence the first function does not return any output whereas the second function returns the output: A new day

9. Which of the following special characters matches a pattern only at the end of the string?
a) B
b) X
c) Z
d) A

View Answer

Answer: c [Reason:] B matches a pattern which is not at the beginning or end of a string. X refers to re.VERBOSE. A matches a pattern only at the start of a string. Z matches a pattern only at the end of a string.

10. The output of the two codes shown below is the same. Sate whether true or false.

p = re.compile('hello')
r = p.match('hello everyone')
print(r.group(0))
 
r = re.match('hello', 'hello everyone')
print(r.group(0))

a) True
b) False

View Answer

Answer: a [Reason:] The two codes shown above are equivalent. Both of these codes result in the same output, that is: hello. Hence this statement is true.

11. What is the output of the code shown?

re.match('sp(.*)am', 'spam')

a) <_sre.SRE_Match object; span=(1, 4), match='spam'>
b) <_sre.SRE_Match object; span=(0, 4), match='spam'>
c) No output
d) Error

View Answer

Answer: b [Reason:] The code shown above demonstrates the function re.match, combined with a special character. The output of the code shown is: <_sre.SRE_Match object; span=(0, 4), match='spam'>

12. Which of the following special characters represents a comment (that is, the contents of the parenthesis are simply ignores)?
a) (?:…)
b) (?=…)
c) (?!…)
d) (?#…)

View Answer

Answer: d [Reason:] The special character (?#…) represent a comment, that is, the contents of the parenthesis are simply ignored.

13. Which of the codes shown below results in a match?
a) re.match(‘George(?=Washington)’, ‘George Washington’)
b) re.match(‘George(?=Washington)’, ‘George’)
c) re.match(‘George(?=Washington)’, ‘GeorgeWashington’)
d) re.match(‘George(?=Washington)’, ‘Georgewashington’)

View Answer

Answer: c [Reason:] The code shown above demonstrates the use of the function re.match, along with the special character ?=. This results in a match only when ‘George’ is immediately followed by ‘Washington’. Also, we have not used the module to ignore case. Hence the match is case-sensitive. Therefore the only option which results in a match is: re.match(‘George(?=Washington)’, ‘GeorgeWashington’)

Python MCQ Set 3

1. What is the output of the code shown below?

re.split(r'(a)(t)', 'Maths is a difficult subject')

a) [‘M a t h s i s a d i f f i c u l t s u b j e c t’]
b) [‘Maths’, ‘is’, ‘a’, ‘difficult’, ‘subject’]
c) ‘Maths is a difficult subject’
d) [‘M’, ‘a’, ‘t’, ‘hs is a difficult subject’]

View Answer

Answer: d [Reason:] The code shown above demonstrates the use of the function re.match. The first argument of this function specifies the pattern. Since the pattern contains groups, those groups are incorporated in the resultant list as well. Hence the output of the code shown above is [‘M’, ‘a’, ‘t’, ‘hs is a difficult subject’].

2. The output of the two codes shown below is the same. State whether true or false.

CODE 1
>>> re.split(r'(a)(t)', 'The night sky')
CODE 2
>>> re.split(r's+', 'The night sky')

a) True
b) False

View Answer

Answer: b [Reason:] The output of the first code is: [‘The night sky’] whereas the output of the second code is:[‘The’, ‘night’, ‘sky’]. Clearly, the outputs of the two codes are different. Hence the statement given above is a false one.

3. What is the output of the code shown below?

import re
s = 'abc123 xyz666 lmn-11 def77'
re.sub(r'b([a-z]+)(d+)', r'21:', s)

a) ‘123abc: 666xyz: lmn-11 77def:’
b) ‘77def: lmn-11: 666xyz: 123abc’
c) ‘abc123:’, ‘xyz666:’, ‘lmn-11:’, ‘def77:’
d) ‘abc123: xyz666: lmn-11: def77’

View Answer

Answer: a [Reason:] The function re.sub returns a string produced by replacing every non overlapping occurrence of the first argument with the second argument in the third argument. Hence the output is: ‘123abc: 666xyz: lmn-11 77def:’

4. What is the output of the code shown?

re.subn('A', 'X', 'AAAAAA', count=4)

a) ‘XXXXAA, 4’
b) (‘AAAAAA’, 4)
c) (‘XXXXAA’, 4)
d) ‘AAAAAA, 4’

View Answer

Answer: c [Reason:] The line of code shown above demonstrates the function re.subn. This function is very similar to the function re.sub except that in the former, a tuple is returned instead of a string. The output of the code shown above is: (‘XXXXAA’, 4).

5. What is the output of the code shown below?

n = re.sub(r'w+', 'Hello', 'Cats and dogs')

a) Hello
Hello
Hello
b) ‘Hello Hello Hello’
c) [‘Hello’, ‘Hello’, ‘Hello’]
d) (‘Hello’, ‘Hello’, ‘Hello’)

View Answer

Answer: b [Reason:] The code shown above demonstrates the function re.sub. Since the string given as an argument consists of three words. The output of the code is: ‘Hello Hello Hello’. Had the string consisted of 4 words, the output would be: ‘Hello Hello Hello Hello’

6. What is the output of the following code?

w = re.compile('[A-Za-z]+')
w.findall('It will rain today')

a) ‘It will rain today’
b) (‘It will rain today’)
c) [‘It will rain today’]
d) [‘It’, ‘will’, ‘rain’, ‘today’]

View Answer

Answer: d [Reason:] The code shown above demonstrates the function re.findall. Since all the words in the string match the criteria, the output of the code is: [‘It’, ‘will’, ‘rain’, ‘today’].

7. In the functions re.search.start(group) and re.search.end(group), if the argument groups not specified, it defaults to __________
a) Zero
b) None
c) One
d) Error

View Answer

Answer: a [Reason:] In the functions re.search.start(group) and re.search.end(group), if the argument groups not specified, it defaults to Zero.

8. What is the output of the code shown below?

re.split(r's+', 'Chrome is better than explorer', maxspilt=3)

a) [‘Chrome’, ‘is’, ‘better’, ‘than’, ‘explorer’]
b) [‘Chrome’, ‘is’, ‘better’, ‘than explorer’]
c) (‘Chrome’, ‘is’, ‘better’, ‘than explorer’)
d) ‘Chrome is better’ ‘than explorer’

View Answer

Answer: b [Reason:] The code shown above demonstrates the use of the function re.split, including the use of maxsplit. Since maxsplit is equal to 3, the output of the code shown above is:[‘Chrome’, ‘is’, ‘better’, ‘than explorer’]

9. What is the output of the code shown below?

a=re.compile('[0-9]+')
a.findall('7 apples and 3 mangoes')

a) [‘apples’ ‘and’ ‘mangoes’]
b) (7, 4)
c) [‘7’, ‘4’]
d) Error

View Answer

Answer: c [Reason:] The code shown above demonstrates the use of the functions re.compile and re.findall. Since we have specified in the code that only digits from 0-9 be found, hence the output of this code is: [‘7’, ‘4’].

Python MCQ Set 4

1. Which of the following functions returns a dictionary mapping group names to group numbers?
a) re.compile.group
b) re.compile.groupindex
c) re.compile.index
d) re.compile.indexgroup

View Answer

Answer: b [Reason:] The function re.compile.groupindex returns a dictionary mapping group names to group numbers.

2. Which of the following statements regarding the output of the function re.match is incorrect?
a) ‘pq*’ will match ‘pq’
b) ‘pq?’ matches ‘p’
c) ‘p{4}, q’ does not match ‘pppq’
d) ‘pq+’ matches ‘p’

View Answer

Answer: d [Reason:] All of the above statements are correct except that ‘pq+’ match ‘p’. ‘pq+’ will match ‘p’ followed by any non-zero number of q’s, but it will not match ‘p’.

3. The snippet of code shown below results in an error. State whether true or false.

c=re.compile(r'(d+)([A-Z]+)([a-z]+)')
c.groupindex

a) True
b) False

View Answer

Answer: b [Reason:] In the code shown above, none of the group names match the group numbers. In such a case, no error is thrown. The output of the code is an empty dictionary, that is, {}.

4. Which of the following functions does not accept any argument?
a) re.purge
b) re.compile
c) re.findall
d) re.match

View Answer

Answer: a [Reason:] The function re.purge is used to clear the cache and it does not accept any arguments.

5. What is the output of the code shown below?

a = re.compile('0-9')
a.findall('3 trees')

a) []
b) [‘3’]
c) Error
d) [‘trees’]

View Answer

Answer: c [Reason:] The output of the code shown above is an empty list. This is due to the way the arguments have been passed to the function re.compile. Carefully read the code shown below in order to understand the correct syntax: >>> a = re.compile(‘[0-9]’) >>> a.findall(‘3 trees’) [‘3’].

6. Which of the following lines of code will not show a match?
a) >>> re.match(‘ab*’, ‘a’)
b) >>> re.match(‘ab*’, ‘ab’)
c) >>> re.match(‘ab*’, ‘abb’)
d) >>> re.match(‘ab*’, ‘ba’)

View Answer

Answer: d [Reason:] In the code shown above, ab* will match to ‘a’ or ‘ab’ or ‘a’ followed by any number of b’s. Hence the only line of code from the above options which does not result in a match is: >>> re.match(‘ab*’, ‘ba’).

7. What is the output of the code shown below?

m = re.search('a', 'The blue umbrella')
m.re.pattern

a) {}
b) ‘The blue umbrella’
c) ‘a’
d) No output

View Answer

Answer: c [Reason:] The PatternObject is used to produce the match. The real regular expression pattern string must be retrieved from the PatternObject’s pattern method. Hence the output of this code is: ‘a’.

8. What is the output of the function shown below?

re.sub('Y', 'X', 'AAAAAA', count=2)

a) ‘YXAAAA’
b) (‘YXAAAA’)
c) (‘AAAAAA’)
d) ‘AAAAAA’

View Answer

Answer: d [Reason:] The code shown above demonstrates the function re.sub, which returns a string. The pattern specified is substituted in the string and returned. Hence the output of the code shown above is: ‘AAAAAA’.

Python MCQ Set 5

1. Which of these about a set is not true?
a) Mutable data type
b) Allows duplicate values
c) Data type with unordered values
d) Immutable data type

View Answer

Answer: d [Reason:] A set is a mutable data type with non-duplicate, unordered values, providing the usual mathematical set operations.

2. Which of the following is not the correct syntax for creating a set?
a) set([[1,2],[3,4]])
b) set([1,2,2,3,4])
c) set((1,2,3,4))
d) {1,2,3,4}

View Answer

Answer: a [Reason:] The argument given for the set must be an iterrable.

3. What is the output of the following code?

nums = set([1,1,2,3,3,3,4,4])
print(len(nums))

a) 7
b) Error, invalid syntax for formation of set
c) 4
d) 8

View Answer

Answer: c [Reason:] A set doesn’t have duplicate items.

4. What is the output of the following piece of code?

a = [5,5,6,7,7,7]
b = set(a)
def test(lst):
    if lst in b:
        return 1
    else:
        return 0
for i in  filter(test, a):
    print(i,end=" ")

a) 5 5 6
b) 5 6 7
c) 5 5 6 7 7 7
d) 5 6 7 7 7

View Answer

Answer: c [Reason:] The filter function will return all the values from list a which are true when passed to function test. Since all the members of the set are non-duplicate members of the list, all of the values will return true. Hence all the values in the list are printed.

5. Which of the following statements is used to create an empty set?
a) { }
b) set()
c) [ ].
d) ( )

View Answer

Answer: b [Reason:] { } creates a dictionary not a set. Only set() creates an empty set.

6. What is the output of the following piece of code when executed in the python shell?

>>> a={5,4}
>>> b={1,2,4,5}
>>> a<b

a) {1,2}
b) True
c) False
d) Invalid operation

View Answer

Answer: b [Reason:] a<b returns True if a is a proper subset of b.

7. If a={5,6,7,8}, which of the following statements is false?
a) print(len(a))
b) print(min(a))
c) a.remove(5)
d) a[2]=45

View Answer

Answer: d [Reason:] The members of a set can be accessed by their index values since the elements of the set are unordered.

8. If a={5,6,7}, what happens when a.add(5) is executed?
a) a={5,5,6,7}
b) a={5,6,7}
c) Error as there is no add function for set data type
d) Error as 5 already exists in the set

View Answer

Answer: b [Reason:] There exists add method for set data type. However 5 isn’t added again as set consists of only non-duplicate elements and 5 already exists in the set. Execute in python shell to verify.

9. What is the output of the following code?

>>> a={4,5,6}
>>> b={2,8,6}
>>> a+b

a) {4,5,6,2,8}
b) {4,5,6,2,8,6}
c) Error as unsupported operand type for sets
d) Error as the duplicate item 6 is present in both sets

View Answer

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

10. What is the output of the following code?

>>> a={4,5,6}
>>> b={2,8,6}
>>> a-b

a) {4,5}
b) {6}
c) Error as unsupported operand type for set data type
d) Error as the duplicate item 6 is present in both sets

View Answer

Answer: a [Reason:] – operator gives the set of elements in set a but not in set b.

11. What is the output of the following piece of code?

>>> a={5,6,7,8}
>>> b={7,8,10,11}
>>> a^b

a) {5,6,7,8,10,11}
b) {7,8}
c) Error as unsupported operand type of set data type
d) {5,6,10,11}

View Answer

Answer: d [Reason:] ^ operator returns a set of elements in set A or set B, but not in both (symmetric difference).

12. What is the output of the following code?

>>> s={5,6}
>>> s*3

a) Error as unsupported operand type for set data type
b) {5,6,5,6,5,6}
c) {5,6}
d) Error as multiplication creates duplicate elements which isn’t allowed

View Answer

Answer: a [Reason:] The multiplication operator isn’t valid for the set data type.

13. What is the output of the following piece of code?

>>> a={5,6,7,8}
>>> b={7,5,6,8}
>>> a==b

a) True
b) False

View Answer

Answer: a [Reason:] It is possible to compare two sets and the order of elements in both the sets doesn’t matter if the values of the elements are the same.

14. What is the output of the following piece of code?

>>> a={3,4,5}
>>> b={5,6,7}
>>> a|b

a) Invalid operation
b) {3, 4, 5, 6, 7}
c) {5}
d) {3,4,6,7}

View Answer

Answer: d [Reason:] The operation in the above piece of code is union operation. This operation produces a set of elements in both set a and set b.

15. Is the following piece of code valid?

a={3,4,{7,5}}
print(a[2][0])

a) Yes, 7 is printed
b) Error, elements of a set can’t be printed
c) Error, subsets aren’t allowed
d) Yes, {7,5} is printed

View Answer

Answer: c [Reason:] In python, elements of a set must not be mutable and sets are mutable. Thus, subsets can’t exist.

Synopsis and Project Report

You can buy synopsis and project from distpub.com. Just visit https://distpub.com/product-category/projects/ and buy your university/institute project from distpub.com