MCQ

Python MCQ and Answers – Part 4

This collection of Python Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer.
 

1. Which of the following keywords is a placeholder for the body of a function?

A break

B continue

C body

D pass

D
You can use “pass” as below:

def myfunc():
    pass
 

2. Suppose we have a = [1,2,3,4,5], which of the following statements is correct?

A print(a[:100]) #Print [1,2,3,4,5] ?

B print(a[:]) #Print [1,2,3,4] ?

C print(a[0:]) #Print [2,3,4,5] ?

D print(a[-1:]) #Print [1,2] ?

A
 

3. What is the output of the following code?
def getLen():
    d = {}
    d["python"] = 22
    d["java"] = 27
    d["php"] = 53
    d["c"] = 87
    return len(d)

print(getLen())

A 3

B 8

C 5

D 4

D
 

4. Who created the Python language?

A Denis Ritchie

B Guido Van Rossum

C James Gosling

D Tom Cruise

B
 

5. Can the “return” statement in Python return multiple values?

A Yes

B No

A
In Python, we can return multiple values from a function. See example below

# This function returns a tuple
def fun(): 
    str = "StackHowTo"
    nbr = 1
    return str, nbr;  

str, nbr = fun()  # Assign the returned tuple
print(str) # StackHowTo
print(nbr) # 1
 

6. What is the purpose of the following code?
if __name__ == "__main__":
     myFunction()

A Create a new module

B Run the python module as the main program

C Create new objects

D Defines a generator

B
When Python runs the “source file” as a main program, it sets the special variable (__name__) to ("__main__").
 

7. In python, we use “try” and “catch” for exception handling?

A Yes

B No

B
We use “try” and “except” for exception handling. Let’s take an example:

try:
    f = open('file.txt', 'r')
    print(f.read())
    f.close()
except IOError:
    print('file not found')
 

6. Which module is used in python to create graphics?

A Graphics

B Turtle

C Canvas

D Tkinter

B
 

7. Which of the following code, link a canvas with a key event ‘p’

A Canvas.entered(Enter, p)

B Canvas.bind(key, p)

C Canvas.bind('<key>', p)

D Canvas.entered('<enter>', p)

C
 

8. Which of the following statements is true?

A Python is a high level programming language.

B Python is an interpreted language.

C Python is an object oriented language.

D All the answers are true

D
 

9. What is the output of the following code: print 9//2

A 4

B 4.5

C 4.0

D Error

A
The // operator in Python returns the integer part of the floating-point number.
 

10. Which function overloads the operator >> ?

A move()

B gt()

C more()

D None of the above

D
The rshift() method overloads the operator >>
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *