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 the does random.seed(3) return?
a) True
b) None
c) 3
d) 1

View Answer

Answer: b [Reason:] The function random.seed() always returns a None.

2. Which of the following cannot be returned by random.randrange(4)?
a) 0
b) 3
c) 2.3
d) none of the mentioned

View Answer

Answer: c [Reason:] Only integers can be returned.

3. Which of the following is equivalent to random.randrange(3)?
a) range(3)
b) random.choice(range(0, 3))
c) random.shuffle(range(3))
d) random.select(range(3))

View Answer

Answer: b [Reason:] It returns one number from the given range.

4. The function random.randint(4) can return only one of the following values. Which?
a) 4
b) 3.4
c) error
d) 5

View Answer

Answer: c [Reason:] Error, the function takes two arguments.

5. Which of the following is equivalent to random.randint(3, 6)?
a) random.choice([3, 6])
b) random.randrange(3, 6)
c) 3 + random.randrange(3)
d) 3 + random.randrange(4)

View Answer

Answer: d [Reason:] random.randint(3, 6) can return any one of 3, 4, 5 and 6.

6. Which of the following will not be returned by random.choice(“1 ,”)?
a) 1
b) (space)
c) ,
d) none of the mentioned

View Answer

Answer: d [Reason:] Any of the characters present in the string may be returned.

7. Which of the following will never be displayed on executing print(random.choice({0: 1, 2: 3}))?
a) 0
b) 1
c) KeyError: 1
d) none of the mentioned

View Answer

Answer: a [Reason:] It will not print 0 but dict[0] i.e. 1 may be printed.

8. What does random.shuffle(x) do when x = [1, 2, 3]?
a) error
b) do nothing, it is a placeholder for a function that is yet to be implemented
c) shuffle the elements of the list in-place
d) none of the mentioned

View Answer

Answer: c [Reason:] The elements of the list passed to it are shuffled in-place.

9. Which type of elements are accepted by random.shuffle()?
a) strings
b) lists
c) tuples
d) integers

View Answer

Answer: b [Reason:] Strings and tuples are immutable and an integer has no len().

10. What is the range of values that random.random() can return?
a) [0.0, 1.0].
b) (0.0, 1.0].
c) (0.0, 1.0)
d) [0.0, 1.0)

View Answer

Answer: d [Reason:] Any number that is greater than or equal to 0.0 and lesser than 1.0 can be returned.

Python MCQ Set 2

1. Which are the two built-in functions to read a line of text from standard input, which by default comes from the keyboard?
a) Raw_input & Input
b) Input & Scan
c) Scan & Scanner
d) Scanner

View Answer

Answer: a [Reason:] Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard. These functions are: raw_input and input

2. What is the output of this program?

  1. str = raw_input("Enter your input: ");
  2. print "Received input is : ", str

a) Enter your input: Hello Python
Received input is : Hello Python
b) Enter your input: Hello Python
Received input is : Hello
c) Enter your input: Hello Python
Received input is : Python
d) None of the mentioned

View Answer

Answer: a [Reason:] The raw_input([prompt]) function reads one line from standard input and returns it as a string. This would prompt you to enter any string and it would display same string on the screen. When I typed “Hello Python!”

3. What is the output of this program?

  1. str = input("Enter your input: ");
  2. print "Received input is : ", str

a) Enter your input: [x*5 for x in range(2,10,2)].
Received input is : [x*5 for x in range(2,10,2)].
b) Enter your input: [x*5 for x in range(2,10,2)].
Received input is : [10, 30, 20, 40].
c) Enter your input: [x*5 for x in range(2,10,2)].
Received input is : [10, 10, 30, 40].
d) None of the mentioned

View Answer

Answer: a [Reason:] None.

4. Which one of the following is not attributes of file
a) closed
b) softspace
c) rename
d) mode

View Answer

Answer: c [Reason:] rename is not the attribute of file rest all are files attributes. Attribute Description file.closed Returns true if file is closed, false otherwise. file.mode Returns access mode with which file was opened. file.name Returns name of the file. file.softspace Returns false if space explicitly required with print, true otherwise.

5. What is the use of tell() method in python?
a) tells you the current position within the file
b) tells you the end position within the file
c) tells you the file is opened or not
d) none of the mentioned

View Answer

Answer: a [Reason:] The tell() method tells you the current position within the file; in other words, the next read or write will occur at that many bytes from the beginning of the file.

6. What is the current syntax of rename() a file?
a) rename(current_file_name, new_file_name)
b) rename(new_file_name, current_file_name,)
c) rename(()(current_file_name, new_file_name))
d) none of the mentioned

View Answer

Answer: a [Reason:] This is the correct syntax which has shown below. rename(current_file_name, new_file_name)

7. What is the current syntax of remove() a file?
a) remove(file_name)
b) remove(new_file_name, current_file_name,)
c) remove(() , file_name))
d) none of the mentioned

View Answer

Answer: a [Reason:] remove(file_name)

8. What is the output of this program?

  1. fo = open("foo.txt", "rw+")
  2. print "Name of the file: ", fo.name
  3.  
  4. # Assuming file has following 5 lines
  5. # This is 1st line
  6. # This is 2nd line
  7. # This is 3rd line
  8. # This is 4th line
  9. # This is 5th line
  10.  
  11. for index in range(5):
  12.    line = fo.next()
  13.    print "Line No %d - %s" % (index, line)
  14.  
  15. # Close opend file
  16. fo.close()

a) Compilation Error
b) Syntax Error
c) Displays Output
d) None of the mentioned

View Answer

Answer: c [Reason:] It displays the output as shown below. The method next() is used when a file is used as an iterator, typically in a loop, the next() method is called repeatedly. This method returns the next input line, or raises StopIteration when EOF is hit. Output: Name of the file: foo.txt Line No 0 – This is 1st line

Line No 1 – This is 2nd line

Line No 2 – This is 3rd line

Line No 3 – This is 4th line

Line No 4 – This is 5th line

9. What is the use of seek() method in files?
a) sets the file’s current position at the offset
b) sets the file’s previous position at the offset
c) sets the file’s current position within the file
d) none of the mentioned

View Answer

Answer: a [Reason:] Sets the file’s current position at the offset. The method seek() sets the file’s current position at the offset. Following is the syntax for seek() method: fileObject.seek(offset[, whence])

Parameters offset — This is the position of the read/write pointer within the file.

whence — This is optional and defaults to 0 which means absolute file positioning, other values are 1 which means seek relative to the current position and 2 means seek relative to the file’s end.

10. What is the use of truncate() method in file?
a) truncates the file’s size
b) deletes the content of the file
c) deletes the file’s size
d) none of the mentioned

View Answer

Answer: a [Reason:] The method truncate() truncates the file’s size. Following is the syntax for truncate() method: fileObject.truncate( [ size ])

Parameters size — If this optional argument is present, the file is truncated to (at most) that size.

Python MCQ Set 3

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

  1. >>>names = ['Amir', 'Bear', 'Charlton', 'Daman']
  2. >>>print(names[-1][-1])

a) A
b) Daman
c) Error
d) n

View Answer

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

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

  1. names1 = ['Amir', 'Bear', 'Charlton', 'Daman']
  2. names2 = names1
  3. names3 = names1[:]
  4.  
  5. names2[0] = 'Alice'
  6. names3[1] = 'Bob'
  7.  
  8. sum = 0
  9. for ls in (names1, names2, names3):
  10.     if ls[0] == 'Alice':
  11.         sum += 1
  12.     if ls[1] == 'Bob':
  13.         sum += 10
  14.  
  15. print sum

a) 11
b) 12
c) 21
d) 22

View Answer

Answer: b [Reason:] When assigning names1 to names2, we create a second reference to the same list. Changes to names2 affect names1. When assigning the slice of all elements in names1 to names3, we are creating a full copy of names1 which can be modified independently.

3. Suppose list1 is [1, 3, 2], What is list1 * 2 ?
a) [2, 6, 4].
b) [1, 3, 2, 1, 3].
c) [1, 3, 2, 1, 3, 2] .
D) [1, 3, 2, 3, 2, 1].

View Answer

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

4. Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is :
a) [0, 1, 2, 3].
b) [0, 1, 2, 3, 4].
c) [0.0, 0.5, 1.0, 1.5].
d) [0.0, 0.5, 1.0, 1.5, 2.0].

View Answer

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

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

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

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

View Answer

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

6. To add a new element to a list we use which command ?
a) list1.add(5)
b) list1.append(5)
c) list1.addLast(5)
d) list1.addEnd(5)

View Answer

Answer: b [Reason:] We use the function append to add an element to the list.

7. To insert 5 to the third position in list1, we use which command ?
a) list1.insert(3, 5)
b) list1.insert(2, 5)
c) list1.add(3, 5)
d) list1.append(3, 5)

View Answer

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

8. To remove string “hello” from list1, we use which command ?
a) list1.remove(“hello”)
b) list1.remove(hello)
c) list1.removeAll(“hello”)
d) list1.removeOne(“hello”)

View Answer

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

9. Suppose list1 is [3, 4, 5, 20, 5], what is list1.index(5) ?
a) 0
b) 1
c) 4
d) 2

View Answer

Answer: d [Reason:] Execute help(list.index) to get details.

10. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5) ?
a) 0
b) 4
c) 1
d) 2

View Answer

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

Python MCQ Set 4

1. What is “Hello”.replace(“l”, “e”)
a) Heeeo
b) Heelo
c) Heleo
d) None

View Answer

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

2. To retrieve the character at index 3 from string s=”Hello” what command do we execute (multiple answers allowed) ?
a) s[].
b) s.getitem(3)
c) s.__getitem__(3)
d) s.getItem(3)

View Answer

Answer: c [Reason:] __getitem(..) can be used to get character at index specified as parameter.

3. To return the length of string s what command do we execute ?
a) s.__len__()
b) len(s)
c) size(s)
d) s.size()

View Answer

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

4. If a class defines the __str__(self) method, for an object obj for the class, you can use which command to invoke the __str__ method.
a) obj.__str__()
b) str(obj)
c) print obj
d) All of the mentioned

View Answer

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

5. To check whether string s1 contains another string s2, use
a) s1.__contains__(s2)
b) s2 in s1
c) s1.contains(s2)
d) si.in(s2)

View Answer

Answer: a [Reason:] s2 in s1 works in the same way as calling the special function __contains__ .

6. Suppose i is 5 and j is 4, i + j is same as
a) i.__add(j)
b) i.__add__(j)
c) i.__Add(j)
d) i.__ADD(j)

View Answer

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

7. What is the output of the following code ?

  1. class Count:
  2.     def __init__(self, count = 0):
  3.        self.__count = count
  4.  
  5. c1 = Count(2)
  6. c2 = Count(2)
  7. print(id(c1) == id(c2), end = " ")
  8.  
  9. s1 = "Good"
  10. s2 = "Good"
  11. print(id(s1) == id(s2))

a) True False
b) True True
c) False True
d) False False

View Answer

Answer: c [Reason:] Execute in the shell objects cannot have same id, however in the case of strings its different.

8. What is the output of the following code ?

  1. class Name:
  2.     def __init__(self, firstName, mi, lastName):
  3.         self.firstName = firstName
  4.         self.mi = mi
  5.         self.lastName = lastName
  6.  
  7. firstName = "John"
  8. name = Name(firstName, 'F', "Smith")
  9. firstName = "Peter"
  10. name.lastName = "Pan"
  11. print(name.firstName, name.lastName)

a) Peter Pan
b) John Pan
c) Peter Smith
d) John Smith

View Answer

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

9. What function do you use to read a string?
a) input(“Enter a string”)
b) eval(input(“Enter a string”))
c) enter(“Enter a string”)
d) eval(enter(“Enter a string”))

View Answer

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

10. Suppose x is 345.3546, what is format(x, “10.3f”) (_ indicates space)
a) __345.355
b) ___345.355
c) ____345.355
d) _____345.354

View Answer

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

Python MCQ Set 5

1. What is the result of math.trunc(3.1)?
a) 3.0
b) 3
c) 0.1
d) 1

View Answer

Answer: b [Reason:] The integral part of the floating point number is returned.

2. What is the output of print(math.trunc(‘3.1’))?
a) 3
b) 3.0
c) error
d) none of the mentioned

View Answer

Answer: c [Reason:] TypeError, a string does not have __trunc__ method.

3. Which of the following is the same as math.exp(p)?
a) e ** p
b) math.e ** p
c) p ** e
d) p ** math.e

View Answer

Answer: b [Reason:] math.e is the constant defined in the math module.

4. What is returned by math.expm1(p)?
a) (math.e ** p) – 1
b) math.e ** (p – 1)
c) error
d) none of the mentioned

View Answer

Answer: a [Reason:] One is subtracted from the result of math.exp(p) and returned.

5. What is the default base used when math.log(x) is found?
a) e
b) 10
c) 2
d) none of the mentioned

View Answer

Answer: a [Reason:] The natural log of x is returned by default.

6. Which of the following aren’t defined in the math module?
a) log2()
b) log10()
c) logx()
d) none of the mentioned

View Answer

Answer: c [Reason:] log2() and log10() are defined in the math module.

7. What is returned by int(math.pow(3, 2))?
a) 6
b) 9
c) error, third argument required
d) error, too many arguments

View Answer

Answer: b [Reason:] math.pow(a, b) returns a ** b.

8. What is output of print(math.pow(3, 2))?
a) 9
b) 9.0
c) None
d) none of the mentioned

View Answer

Answer: b [Reason:] math.pow() returns a floating point number.

9. What is the value of x if x = math.sqrt(4)?
a) 2
b) 2.0
c) (2, -2)
d) (2.0, -2.0)

View Answer

Answer: b [Reason:] The function returns one floating point number.

10. What does math.sqrt(X, Y) do?
a) calculate the Xth root of Y
b) calculate the Yth root of X
c) error
d) return a tuple with the square root of X and Y

View Answer

Answer: c [Reason:] The function takes only one argument.

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