MCQ

Python MCQ and Answers – Part 1

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

1. Python is a(n) _______ language?

A interpreted

B machine

C compiled

D binary

A
The Python interpreter first reads human code and optimizes it before interpreting it into machine code.

 

 

2. Which of the following functions converts a “string” to “float” in Python?

A str(x)

B float(x)

C long(x [,base])

D int(x [,base])

B
The method float() takes a single parameter, for example: print(float(10)) #10.0

 

 

3. In python 3, what does the // operator do?

A Integer division

B Return the remainder

C Division of float by zero

D Same as a ** b

A
In python 3, 5/2 will return 2.5 and 5 // 2 will return 2.

 

 

4. In Python, which of the following functions checks in a string that all characters are numbers?

A isdigit()

B isalnum()

C capitalize()

D shuffle(lst)

A
The method isdigit() checks if the string is composed of numbers only

str = "123";  # There are only numbers in this string
print str.isdigit()   #True

str = "Welcome to StackHowTo!!!";
print str.isdigit()   #False

 

 

5. What is the data type for a character in python?

A chr

B char

C character

D Python does not have any data type for characters, they are treated as strings.

D
Python has no type for characters. Python only handles strings, a character is simply a String of length 1.

 

 

6. What is the function that compares the elements of the two lists?

A cmp(list1, list2)

B eq(list1, list2)

C len(list1, list2)

D max(list1, list2)

A
The function cmp() used to compare the elements of two lists. This function returns 1, if the first list is “larger” than the second list, -1 if the first list is smaller than the second list, otherwise it returns 0 if the two lists are equal.

# initialization of lists
list1 = [ 1, 2, 4, 3] 
list2 = [ 1, 2, 5, 8] 
list3 = [ 1, 2, 4, 3] 
 
# displays 1 because list2 is larger than list1
print "Comparison of list2 with list1:"
print cmp(list2, list1)    #1
  
# displays 0 because list1 and list3 are same
print "Comparison of list3 with list1:",
print cmp(list3, list1)    #0

 

 

7. Which of the following functions returns the smallest character in the string str?

A lower()

B lstrip()

C upper(str)

D min(str)

D
min() is a built-in function in Python that returns the smallest character in a string.

string = "stackhowto"
print(min(string))   #a

 

 

8. Which of the following function is used to open a file for reading in Python?

A fopen(file_name, mode)

B open(file_name, mode)

C openfile(file_name, mode)

D open_file(file_name, mode)

B
The open() function returns a file object, which has a read() method to read the file contents:

f = open("file.txt", "r")
print(f.read())

 

 

9. In python, what keyword is used to start a function?

A function

B fun

C def

D import

C
Functions in python are defined using “def” keyword, followed by the name of the function. For example: def my_function (): print ("Welcom To StackHowTo!")

 

 

10. Which of the following operators in python evaluates to “true” if it does not find a variable in the specified sequence otherwise “false”?

A **

B is

C not in

D //

C
“X not in mySeq” will return True if X is not an element of mySeq and False otherwise. Example:

>>> 3 not in [2, 3, 4]
False
>>> 3 not in [4, 5, 6]
True

 

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 *