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 following?

print('{0:.2}'.format(1/3))

a) 0.333333
b) 0.33
c) 0.333333:.2
d) Error

View Answer

Answer: b [Reason:] .2 specifies the precision.

2. What is the output of the following?

print('{0:.2%}'.format(1/3))

a) 0.33
b) 0.33%
c) 33.33%
d) 33%

View Answer

Answer: c [Reason:] The symbol % is used to represent the result of an expression as a percentage.

3. What is the output of the following?

print('ab12'.isalnum())

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

View Answer

Answer: a [Reason:] The string has only letters and digits.

4. What is the output of the following?

print('ab,12'.isalnum())

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

View Answer

Answer: b [Reason:] The character , is not a letter or a digit.

5. What is the output of the following?

print('ab'.isalpha())

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

View Answer

Answer: a [Reason:] The string has only letters.

6. What is the output of the following?

print('a B'.isalpha())

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

View Answer

Answer: b [Reason:] Space is not a letter.

7. What is the output of the following?

print('0xa'.isdigit())

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

View Answer

Answer: b [Reason:] Hexadecimal digits aren’t considered as digits (a-f).

8. What is the output of the following?

print(''.isdigit())

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

View Answer

Answer: b [Reason:] If there are no characters then False is returned.

9. What is the output of the following?

print('my_string'.isidentifier())

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

View Answer

Answer: a [Reason:] It is a valid identifier.

10. What is the output of the following?

print('__foo__'.isidentifier())

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

View Answer

Answer: a [Reason:] It is a valid identifier.

Python MCQ Set 2

1. Which of these in not a core datatype?
a) Lists
b) Dictionary
c) Tuples
d) Class

View Answer

Answer: d [Reason:] Class is a user defined datatype.

2. Given a function that does not return any value, What value is thrown by default when executed in shell.
a) int
b) bool
c) void
d) None

View Answer

Answer: d [Reason:] Python shell throws a NoneType object back.

3. Following set of commands are executed in shell, what will be the output?

  1. >>>str="hello"
  2. >>>str[:2]
  3. >>>

a) he
b) lo
c) olleh
d) hello

View Answer

Answer: a [Reason:] We are printing only the 1st two bytes of string and hence the answer is “he”.

4. Which of the following will run without errors ?
a) round(45.8)
b) round(6352.898,2,5)
c) round()
d) round(7463.123,2,1)

View Answer

Answer: a [Reason:] Execute help(round) in the shell to get details of the parameters that are passed into the round function.

5. What is the return type of function id ?
a) int
b) float
c) bool
d) dict

View Answer

Answer: a [Reason:] Execute help(id) to find out details in python shell.id returns a integer value that is unique.

6. In python we do not specify types,it is directly interpreted by the compiler, so consider the following operation to be performed.

  1. >>>x = 13 ? 2

objective is to make sure x has a integer value, select all that apply (python 3.xx)
a) x = 13 // 2
b) x = int(13 / 2)
c) x = 13 % 2
d) All of the mentioned

View Answer

Answer: d [Reason:] // is integer operation in python 3.0 and int(..) is a type cast operator.

7. What error occurs when you execute?
apple = mango
a) SyntaxError
b) NameError
c) ValueError
d) TypeError

View Answer

Answer: b [Reason:] Mango is not defined hence name error.

8. Carefully observe the code and give the answer.

  1. def example(a):
  2.     a = a + '2'
  3.      a = a*2
  4.     return a
  5. >>>example("hello")

a) indentation Error
b) cannot perform mathematical operation on strings
c) hello2
d) hello2hello2

View Answer

Answer: a [Reason:] Python codes have to be indented properly.

9. What dataype is the object below ?
L = [1, 23, ‘hello’, 1].
a) list
b) dictionary
c) array
d) tuple

View Answer

Answer: a [Reason:] List datatype can store any values within it.

10. In order to store values in terms of key and value we use what core datatype.
a) list
b) tuple
c) class
d) dictionary

View Answer

Answer: d [Reason:] Dictionary stores values in terms of keys and values.

11. Which of the following results in a SyntaxError ?
a) ‘”Once upon a time…”, she said.’
b) “He said, ‘Yes!’”
c) ‘3’
d) ”’That’s okay”’

View Answer

Answer: c [Reason:] Carefully look at the colons.

12. The following is displayed by a print function call:

  1. tom
  2. dick
  3. harry

Select all of the function calls that result in this output
a) print(”’tom
ndick
nharry”’)
b) print(”’tomdickharry”’)
c) print(‘tomndicknharry’)
d) print(‘tom
dick
harry’)

View Answer

Answer: c [Reason:] The n adds a new line.

13. What is the average value of the code that is executed below ?

  1. >>>grade1 = 80
  2. >>>grade2 = 90
  3. >>>average = (grade1 + grade2) / 2

a) 85
b) 85.1
c) 95
d) 95.1

View Answer

Answer: b [Reason:] Cause a decimal value to appear as output.

14. Select all options that print
hello-how-are-you
a) print(‘hello’, ‘how’, ‘are’, ‘you’)
b) print(‘hello’, ‘how’, ‘are’, ‘you’ + ‘-‘ * 4)
c) print(‘hello-‘ + ‘how-are-you’)
d) print(‘hello’ + ‘-‘ + ‘how’ + ‘-‘ + ‘are’ + ‘you’)

View Answer

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

15. What is the return value of trunc() ?
a) int
b) bool
c) float
d) None

View Answer

Answer: a [Reason:] Executle help(math.trunc) to get details.

Python MCQ Set 3

1. Which of the following statements create a dictionary?
a) d = {}
b) d = {“john”:40, “peter”:45}
c) d = {40:”john”, 45:”peter”}
d) All of the mentioned

View Answer

Answer: d [Reason:] Dictionaries are created by specifying keys and values.

2. Read the code shown below carefully and pick out the keys?

  1. d = {"john":40, "peter":45}

a) “john”, 40, 45, and “peter”
b) “john” and “peter”
c) 40 and 45
d) d = (40:”john”, 45:”peter”)

View Answer

Answer: b [Reason:] Dictionaries appear in the form of keys and values.

3. What will be the output?

  1. d = {"john":40, "peter":45}
  2. "john" in d

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

View Answer

Answer: a [Reason:] In can be used to check if the key is int dictionary.

4. What will be the output?

  1. d1 = {"john":40, "peter":45}
  2. d2 = {"john":466, "peter":45}
  3. d1 == d2

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

View Answer

Answer: b [Reason:] If d2 was initialized as d2 = d1 the answer would be true.

5. What will be the output?

  1. d1 = {"john":40, "peter":45}
  2. d2 = {"john":466, "peter":45}
  3. d1 > d2

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

View Answer

Answer: c [Reason:] Arithmetic > operator cannot be used with dictionaries.

6. What is the output?

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

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

View Answer

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

7. Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use
a) d.delete(“john”:40)
b) d.delete(“john”)
c) del d[“john”].
d) del d(“john”:40)

View Answer

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

8. Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which command do we use?
a) d.size()
b) len(d)
c) size(d)
d) d.len()

View Answer

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

9. What will be the output?

  1. d = {"john":40, "peter":45}
  2. print(list(d.keys()))

a) [“john”, “peter”].
b) [“john”:40, “peter”:45].
c) (“john”, “peter”)
d) (“john”:40, “peter”:45)

View Answer

Answer: a [Reason:] The output of the code shown above is a list containing only keys of the dictionary d, in the form of a list.

10. Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value using the expression d[“susan”]?
a) Since “susan” is not a value in the set, Python raises a KeyError exception
b) It is executed fine and no exception is raised, and it returns None
c) Since “susan” is not a key in the set, Python raises a KeyError exception
d) Since “susan” is not a key in the set, Python raises a syntax error

View Answer

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

Python MCQ Set 4

1. To open a file c:scores.txt for reading, we use
a) infile = open(“c:scores.txt”, “r”)
b) infile = open(“c:scores.txt”, “r”)
c) infile = open(file = “c:scores.txt”, “r”)
d) infile = open(file = “c:scores.txt”, “r”)

View Answer

Answer: b [Reason:] Execute help(open) to get more details.

2. To open a file c:scores.txt for writing, we use
a) outfile = open(“c:scores.txt”, “w”)
b) outfile = open(“c:scores.txt”, “w”)
c) outfile = open(file = “c:scores.txt”, “w”)
d) outfile = open(file = “c:scores.txt”, “w”)

View Answer

Answer: b [Reason:] w is used to indicate that file is to be written to.

3. To open a file c:scores.txt for appending data, we use
a) outfile = open(“c:scores.txt”, “a”)
b) outfile = open(“c:scores.txt”, “rw”)
c) outfile = open(file = “c:scores.txt”, “w”)
d) outfile = open(file = “c:scores.txt”, “w”)

View Answer

Answer: a [Reason:] a is used to indicate that data is to be apended.

4. Which of the following statements are true?
a) When you open a file for reading, if the file does not exist, an error occurs
b) When you open a file for writing, if the file does not exist, a new file is created
c) When you open a file for writing, if the file exists, the existing file is overwritten with the new file
d) All of the mentioned

View Answer

Answer: d [Reason:] The program will throw an error.

5. To read two characters from a file object infile, we use
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()

View Answer

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

6. To read the entire remaining contents of the file as a string from a file object infile, we use
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()

View Answer

Answer: b [Reason:] read function is used to read all the lines in a file.

7. What is the output?

  1. f = None
  2. for i in range (5):
  3.     with open("data.txt", "w") as f:
  4.         if i > 2:
  5.             break
  6. print(f.closed)

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

View Answer

Answer: a [Reason:] The WITH statement when used with open file guarantees that the file object is closed when the with block exits.

8. To read the next line of the file from a file object infile, we use
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()

View Answer

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

9. To read the remaining lines of the file from a file object infile, we use
a) infile.read(2)
b) infile.read()
C) infile.readline()
d) infile.readlines()

View Answer

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

10. The readlines() method returns
a) str
b) a list of lines
c) a list of single characters
d) a list of integers

View Answer

Answer: b [Reason:] Every line is stored in a list and returned.

Python MCQ Set 5

1. Which of the following is the use of function in python?
a) Functions are reusable pieces of programs
b) Functions don’t provide better modularity for your application
c) you can’t also create your own functions
d) All of the mentioned

View Answer

Answer: a [Reason:] Functions are reusable pieces of programs. They allow you to give a name to a block of statements, allowing you to run that block using the specified name anywhere in your program and any number of times.

2. Which keyword is use for function?
a) Fun
b) Define
c) Def
d) Function

View Answer

Answer: c [Reason:] None.

3. What is the output of the below program?

  1. def sayHello():
  2.     print('Hello World!') 
  3. sayHello() 
  4. sayHello()

a) Hello World!
Hello World!
b) ‘Hello World!’
‘Hello World!’
c) Hello
Hello
d) None of the mentioned

View Answer

Answer: a [Reason:] Functions are defined using the def keyword. After this keyword comes an identifier name for the function, followed by a pair of parentheses which may enclose some names of variables, and by the final colon that ends the line. Next follows the block of statements that are part of this function.

  1. def sayHello():
  2.     print('Hello World!') # block belonging to the function
  3. # End of function #
  4.  
  5. sayHello() # call the function
  6. sayHello() # call the function again

4. What is the output of the below program?

  1. def printMax(a, b):
  2.     if a > b:
  3.         print(a, 'is maximum')
  4.     elif a == b:
  5.         print(a, 'is equal to', b)
  6.     else:
  7.         print(b, 'is maximum')
  8. printMax(3, 4)

a) 3
b) 4
c) 4 is maximum
d) None of the mentioned

View Answer

Answer: c [Reason:] Here, we define a function called printMax that uses two parameters called a and b. We find out the greater number using a simple if..else statement and then print the bigger number.

5. What is the output of the below program ?

  1. x = 50
  2. def func(x):
  3.     print('x is', x)
  4.     x = 2
  5.     print('Changed local x to', x)
  6. func(x)
  7. print('x is now', x)

a) x is now 50
b) x is now 2
c) x is now 100
d) None of the mentioned

View Answer

Answer: a [Reason:] The first time that we print the value of the name x with the first line in the function’s body, Python uses the value of the parameter declared in the main block, above the function definition. Next, we assign the value 2 to x. The name x is local to our function. So, when we change the value of x in the function, the x defined in the main block remains unaffected. With the last print function call, we display the value of x as defined in the main block, thereby confirming that it is actually unaffected by the local assignment within the previously called function.

6. What is the output of the below program?

  1. x = 50
  2. def func():
  3.     global x
  4.     print('x is', x)
  5.     x = 2
  6.     print('Changed global x to', x)
  7. func()
  8. print('Value of x is', x)

a) x is 50
Changed global x to 2
Value of x is 50
b) x is 50
Changed global x to 2
Value of x is 2
c) x is 50
Changed global x to 50
Value of x is 50
d) None of the mentioned

View Answer

Answer: b [Reason:] The global statement is used to declare that x is a global variable – hence, when we assign a value to x inside the function, that change is reflected when we use the value of x in the main block.

7. What is the output of below program?

  1. def say(message, times = 1):
  2.     print(message * times)
  3. say('Hello')
  4. say('World', 5)

a) Hello
WorldWorldWorldWorldWorld
b) Hello
World 5
c) Hello
World,World,World,World,World
d) Hello
HelloHelloHelloHelloHello

View Answer

Answer: a [Reason:] For some functions, you may want to make some parameters optional and use default values in case the user does not want to provide values for them. This is done with the help of default argument values. You can specify default argument values for parameters by appending to the parameter name in the function definition the assignment operator (=) followed by the default value. The function named say is used to print a string as many times as specified. If we don’t supply a value, then by default, the string is printed just once. We achieve this by specifying a default argument value of 1 to the parameter times. In the first usage of say, we supply only the string and it prints the string once. In the second usage of say, we supply both the string and an argument 5 stating that we want to say the string message 5 times.

8. What is the output of the below program?

  1. def func(a, b=5, c=10):
  2.     print('a is', a, 'and b is', b, 'and c is', c)
  3.  
  4. func(3, 7)
  5. func(25, c = 24)
  6. func(c = 50, a = 100)

a) a is 7 and b is 3 and c is 10
a is 25 and b is 5 and c is 24
a is 5 and b is 100 and c is 50
b) a is 3 and b is 7 and c is 10
a is 5 and b is 25 and c is 24
a is 50 and b is 100 and c is 5
c) a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
d) None of the mentioned

View Answer

Answer: c [Reason:] If you have some functions with many parameters and you want to specify only some of them, then you can give values for such parameters by naming them – this is called keyword arguments – we use the name (keyword) instead of the position (which we have been using all along) to specify the arguments to the function. The function named func has one parameter without a default argument value, followed by two parameters with default argument values.

In the first usage, func(3, 7), the parameter a gets the value 3, the parameter b gets the value 7 and c gets the default value of 10.

In the second usage func(25, c=24), the variable a gets the value of 25 due to the position of the argument. Then, the parameter c gets the value of 24 due to naming i.e. keyword arguments. The variable b gets the default value of 5.

In the third usage func(c=50, a=100), we use keyword arguments for all specified values. Notice that we are specifying the value for parameter c before that for a even though a is defined before c in the function definition.

9. What is the output of below program?

  1. def maximum(x, y):
  2.     if x > y:
  3.         return x
  4.     elif x == y:
  5.         return 'The numbers are equal'
  6.     else:
  7.         return y
  8.  
  9. print(maximum(2, 3))

a) 2
b) 3
c) The numbers are equal
d) None of the mentioned

View Answer

Answer: b [Reason:] The maximum function returns the maximum of the parameters, in this case the numbers supplied to the function. It uses a simple if..else statement to find the greater value and then returns that value.

10. Which of the following is a features of DocString?
a) Provide a convenient way of associating documentation with Python modules, functions, classes, and methods
b) All functions should have a docstring
c) Docstrings can be accessed by the __doc__ attribute on objects
d) All of the mentioned

View Answer

Answer: d [Reason:] Python has a nifty feature called documentation strings, usually referred to by its shorter name docstrings. DocStrings are an important tool that you should make use of since it helps to document the program better and makes it easier to understand.

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