Python Multiple Choice Questions


Python Multiple Choice Questions

1) What is the maximum possible length of an identifier?
16
32
64
None of these above 



Answer: (d) None of these above

Explanation: The maximum possible length of an identifier is not defined in the python language. It can be of any number.

2) Who developed the Python language?
Zim Den
Guido van Rossum
Niene Stom
Wick van Rossum 



Answer: (b) Guido van Rossum

Explanation: Python language was developed by Guido van Rossum in the Netherlands.

3) In which year was the Python language developed?
1995
1972
1981
1989 



Answer: (d) 1989

Explanation: Python language was developed by Guido van Rossum in 1989.

4) In which language is Python written?
English
PHP
C
All of the above 



Answer: (b) C

Explanation: Python is written in C programming language, and it is also called CPython.

5) Which one of the following is the correct extension of the Python file?
.py
.python
.p
None of these 



Answer: (a) .py

Explanation: ".py" is the correct extension of the Python file.

6) In which year was the Python 3.0 version developed?
2008
2000
2010
2005 



Answer: (a) 2008

Explanation: Python 3.0 version was developed on December 3, 2008.

7) What do we use to define a block of code in Python language?
Key
Brackets
Indentation
None of these 



Answer: (c) Indentation

Explanation: Python uses indentation to define blocks of code. Indentations are simply spaces or tabs used as an indicator that is part of the indent code child. As used in curly braces C, C++, and Java.

8) Which character is used in Python to make a single line comment?
/
//
#



Answer: (c) #

Explanation: "#" character is used in Python to make a single-line comment.

9) Which of the following statements is correct regarding the object-oriented programming concept in Python?
Classes are real-world entities while objects are not real
Objects are real-world entities while classes are not real
Both objects and classes are real-world entities
All of the above 



Answer: (b) Objects are real-world entities while classes are not real

Explanation: None

10) Which of the following statements is correct in this python code?

class Name:
def __init__(javatpoint):
javajavatpoint = java
name1=Name("ABC")
name2=name1 class Name: def __init__(javatpoint): javatpoint = java name1=Name("ABC") name2=name1
It will throw the error as multiple references to the same object is not possible
id(name1) and id(name2) will have same value
Both name1 and name2 will have reference to two different objects of class Name
All of the above 



Answer: (b) id(name1) and id(name2) will have same value

Explanation: "name1" and "name2" refer to the same object, so id(name1) and id(name2) will have the same value.

11) What is the method inside the class in python language?
Object
Function
Attribute
Argument 



Answer: (b) Function

Explanation: Function is also known as the method.

12) Which of the following declarations is incorrect?
_x = 2
__x = 3
__xyz__ = 5
None of these 



Answer: (d) None of these

Explanation: All declarations will execute successfully but at the expense of low readability.

13) Why does the name of local variables start with an underscore discouraged?
To identify the variable
It confuses the interpreter
It indicates a private variable of a class
None of these 



Answer: (c) It indicates a private variable of a class

Explanation: Since there is no concept of private variables in Python language, the major underscore is used to denote variables that cannot be accessed from outside the class.

14) Which of the following is not a keyword in Python language?
val
raise
try
with 



Answer: (a) val

Explanation: "val" is not a keyword in python language.

15) Which of the following statements is correct for variable names in Python language?
All variable names must begin with an underscore.
Unlimited length
The variable name length is a maximum of 2.
All of the above 



Answer: (b) Unlimited length

Explanation: None

16) Which of the following declarations is incorrect in python language?
xyzp = 5,000,000
x y z p = 5000 6000 7000 8000
x,y,z,p = 5000, 6000, 7000, 8000
x_y_z_p = 5,000,000 



Answer: (b) x y z p = 5000 6000 7000 8000

Explanation: Spaces are not allowed in variable names.

17) Which of the following words cannot be a variable in python language?
_val
val
try
_try_ 



Answer: (c) try

Explanation: "try" is a keyword.

18) Which of the following operators is the correct option for power(ab)?
a ^ b
a**b
a ^ ^ b
a ^ * b 



Answer: (b) a**b

Explanation: The power operator in python is a**b, i.e., 2**3=8.

19) Which of the following precedence order is correct in Python?
Parentheses, Exponential, Multiplication, Division, Addition, Subtraction
Multiplication, Division, Addition, Subtraction, Parentheses, Exponential
Division, Multiplication, Addition, Subtraction, Parentheses, Exponential
Exponential, Parentheses, Multiplication, Division, Addition, Subtraction 



Answer: (a) Parentheses, Exponential, Multiplication, Division, Addition, Subtraction

Explanation: PEMDAS (similar to BODMAS).

20) Which one of the following has the same precedence level?
Division, Power, Multiplication, Addition and Subtraction
Division and Multiplication
Subtraction and Division
Power and Division 



Answer: (b) Division and Multiplication

Explanation: None

21) Which one of the following has the highest precedence in the expression?
Division
Subtraction
Power
Parentheses 



Answer: (d) Parentheses

Explanation: PEMDAS (similar to BODMAS).

22) Which of the following functions is a built-in function in python language?
val()
print()
print()
None of these 



Answer: (b) print()

Explanation: The print() function is a built-in function in python language that prints a value directly to the system.

23) Study the following function:

round(4.576) round(4.576)

What will be the output of this function?
4
5
576



Answer: (d) 5

Explanation: The round function is a built-in function in the Python language that round-off the value (like 3.85 is 4), so the output of this function will be 5.

24) Which of the following is correctly evaluated for this function?

pow(x,y,z) pow(x,y,z)
(x**y) / z
(x / y) * z
(x**y) % z
(x / y) / z 



Answer: (c) (x**y) % z

Explanation: None

25) Study the following function:

all([2,4,0,6]) all([2,4,0,6])

What will be the output of this function?
False
True
0
Invalid code 



Answer: (a) False

Explanation: If any element is zero, it returns a false value, and if all elements are non-zero, it returns a true value. Hence, the output of this "all([2,4,0,6])" function will be false.

26) Study the following program:

x = 1
while True:
if x % 5 = = 0:
break
print(x)
x + = 1 x = 1 while True: if x % 5 = = 0: break print(x) x + = 1

What will be the output of this code?
error
2 1
0 3 1
None of these 



Answer: (a) error

Explanation: Syntax error, there should not be a space between + and =.

27) Which one of the following syntaxes is the correct syntax to read from a simple text file stored in ''d:\java.txt''?
Infile = open(''d:\\java.txt'', ''r'')
Infile = open(file=''d:\\\java.txt'', ''r'')
Infile = open(''d:\java.txt'',''r'')
Infile = open.file(''d:\\java.txt'',''r'') 



Answer: (a) Infile = open(''c:\\scores.txt'', ''r'')

Explanation: None

28) Study the following code:

x = ['XX', 'YY']
for i in a:
i.lower()
print(a) x = ['XX', 'YY'] for i in a: i.lower() print(a)

What will be the output of this program?
['XX', 'YY']
['xx', 'yy']
[XX, yy]
None of these 



Answer: (a) ['XX', 'YY']

Explanation: None

29) Study the following function:

import math
abs(math.sqrt(36)) import math abs(math.sqrt(36))

What will be the output of this code?
Error
-6
6
6.0 



Answer: (d) 6.0

Explanation: This function prints the square of the value.

30) Study the following function:

any([5>8, 6>3, 3>1]) any([5>8, 6>3, 3>1])

What will be the output of this code?
False
Ture
Invalid code
None of these 



Answer: (b) True

Explanation: None

31) Study the following statement:

>>>"a"+"bc" >>>"a"+"bc"

What will be the output of this statement?
a+bc
abc
a bc



Answer: (b) abc

Explanation: In Python, the "+" operator acts as a concatenation operator between two strings.

32) Study the following code:

>>>"javatpoint"[5:] >>>"javatpoint"[5:]

What will be the output of this code?
javatpoint
java
point
None of these 



Answer: (c) point

Explanation: Slice operation is performed on the string.

33) The output to execute string.ascii_letters can also be obtained from:?
character
ascii_lowercase_string.digits
lowercase_string.upercase
ascii_lowercase+string.ascii_upercase 



Answer: (d) string.ascii_lowercase+string.ascii_upercase

Explanation: None

34) Study the following statements:

>>> str1 = "javat"
>>> str2 = ":"
>>> str3 = "point"
>>> str1[-1:] >>> str1 = "javat" >>> str2 = ":" >>> str3 = "point" >>> str1[-1:]

What will be the output of this statement?
t
j
point
java 



Answer: (a) t

Explanation: The correct output of this program is "t" because -1 corresponds to the last index.

35) Study the following code:

>>> print (r"\njavat\npoint") >>> print (r"\njavat\npoint")

What will be the output of this statement?
java
point
java point
\njavat\npoint
Print the letter r and then javat and then point 



Answer: (c) \njavat\npoint

Explanation: None

36) Study the following statements:

>>> print(0xA + 0xB + 0xC) >>> print(0xA + 0xB + 0xC)

What will be the output of this statement?
33
63
0xA + 0xB + 0xC
None of these 



Answer: (a) 33

Explanation: A, B and C are hexadecimal integers with values 10, 11 and 12 respectively, so the sum of A, B and C is 33.

37) Study the following program:

class book:
def __init__(a, b):
a.o1 = b

class child(book):
def __init__(a, b):
a.o2 = b

obj = page(32)
print "%d %d" % (obj.o1, obj.o2) class book: def __init__(a, b): a.o1 = b class child(book): def __init__(a, b): a.o2 = b obj = page(32) print "%d %d" % (obj.o1, obj.o2)

Which of the following is the correct output of this program?
32
32 32
32 None
Error is generated 



Answer: (d) Error is generated

Explanation: Error is generated because self.o1 was never created.

38) Study the following program:

class Std_Name:
def __init__(self, Std_firstName, Std_Phn, Std_lastName):
self.Std_firstName = Std_firstName
self. Std_PhnStd_Phn = Std_Phn
self. Std_lastNameStd_lastName = Std_lastName

Std_firstName = "Wick"
name = Std_Name(Std_firstName, 'F', "Bob")
Std_firstName = "Ann"
name.lastName = "Nick"
print(name.Std_firstName, name.Std_lastName) class Std_Name: def __init__(self, Std_firstName, Std_Phn, Std_lastName): self.Std_firstName = Std_firstName self. Std_Phn = Std_Phn self. Std_lastName = Std_lastName Std_firstName = "Wick" name = Std_Name(Std_firstName, 'F', "Bob") Std_firstName = "Ann" name.lastName = "Nick" print(name.Std_firstName, name.Std_lastName)

What will be the output of this statement?
Ann Bob
Ann Nick
Wick Bob
Wick Nick 



Answer: (d) Wick Nick

Explanation: None

39) Study the following statements:

>>> print(ord('h') - ord('z')) >>> print(ord('h') - ord('z'))

What will be the output of this statement?
18
-18
17
-17 



Answer: (b) -18

Explanation: ASCII value of h is less than the z. Hence the output of this code is 104-122, which is equal to -18.

40) Study the following program:

x = ['xy', 'yz']
for i in a:
i.upper()
print(a) x = ['xy', 'yz'] for i in a: i.upper() print(a)

Which of the following is correct output of this program?
['xy', 'yz']
['XY', 'YZ']
[None, None]
None of these 



Answer: (a) ['xy', 'yz']

Explanation: None

41) Study the following program:

i = 1:
while True:
if i%3 == 0:
break
print(i) i = 1: while True: if i%3 == 0: break print(i)

Which of the following is the correct output of this program?
1 2 3
3 2 1
1 2
Invalid syntax 



Answer: (d) Invalid syntax

Explanation: Invalid syntax, because this declaration (i = 1:) is wrong.

42) Study the following program:

a = 1
while True:
if a % 7 = = 0:
break
print(a)
a += 1 a = 1 while True: if a % 7 = = 0: break print(a) a += 1

Which of the following is correct output of this program?
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
Invalid syntax 



Answer: (b) 1 2 3 4 5 6

Explanation: None

43) Study the following program:

i = 0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0) i = 0 while i < 5: print(i) i += 1 if i == 3: break else: print(0)

What will be the output of this statement?
1 2 3
0 1 2 3
0 1 2
3 2 1 



Answer: (c) 0 1 2

Explanation: None

44) Study the following program:

i = 0
while i < 3:
print(i)
i += 1
else:
print(0) i = 0 while i < 3: print(i) i += 1 else: print(0)

What will be the output of this statement?
0 1
0 1 2
0 1 2 0
0 1 2 3 



Answer: (c) 0 1 2 0

Explanation: None

45) Study the following program:

z = "xyz"
j = "j"
while j in z:
print(j, end=" ") z = "xyz" j = "j" while j in z: print(j, end=" ")

What will be the output of this statement?
xyz
No output
x y z
j j j j j j j.. 



Answer: (b) No output

Explanation: "j" is not in "xyz".

46) Study the following program:

x = 'pqrs'
for i in range(len(x)):
x[i].upper()
print (x) x = 'pqrs' for i in range(len(x)): x[i].upper() print (x)

Which of the following is the correct output of this program?
PQRS
pqrs
qrs
None of these 



Answer: (b) pqrs

Explanation: None

47) Study the following program:

d = {0: 'a', 1: 'b', 2: 'c'}
for i in d:
print(i) d = {0: 'a', 1: 'b', 2: 'c'} for i in d: print(i)

What will be the output of this statement?
a b c
0 1 2
0 a 1 b 2 c
None of these above 



Answer: (b) 0 1 2

Explanation: None

48) Study the following program:

d = {0, 1, 2}
for x in d:
print(x) d = {0, 1, 2} for x in d: print(x)

What will be the output of this statement?
{0, 1, 2} {0, 1, 2} {0, 1, 2}
0 1 2
Syntax_Error
None of these above 



Answer: (b) 0 1 2

Explanation: None

49) Which of the following option is not a core data type in the python language?
Dictionary
Lists
Class
All of the above 



Answer: (c) Class

Explanation: Class is not a core data type because it is a user-defined data type.

50) What error will occur when you execute the following code?

MANGO = APPLE MANGO = APPLE
NameError
SyntaxError
TypeError
ValueError 



Answer: (a) NamaError

Explanation: Mango is not defined hence the name error.

51) Study the following program:

def example(a):
aa = a + '1'
aa = a*1
return a
>>>example("javatpoint") def example(a): a = a + '1' a = a*1 return a >>>example("javatpoint")

What will be the output of this statement?
hello2hello2
hello2
Cannot perform mathematical operation on strings
indentationError 



Answer: (d) indentationError

Explanation: None

52) Which of the following data types is shown below?

L = [2, 54, 'javatpoint', 5] L = [2, 54, 'javatpoint', 5]

What will be the output of this statement?
Dictionary
Tuple
List
Stack 



Answer: (c) List

Explanation: Any value can be stored in the list data type.

53) What happens when '2' == 2 is executed?
False
Ture
ValueError occurs
TypeError occurs 



Answer: (a) False

Explanation: It only evaluates to false.

54) Study the following program:

try:
if '2' != 2:
raise "JavaTpoint"
else:
print("JavaTpoint has not exist")
except "JavaTpoint":
print ("JavaTpoint has exist") try: if '2' != 2: raise "JavaTpoint" else: print("JavaTpoint has not exist") except "JavaTpoint": print ("JavaTpoint has exist")

What will be the output of this statement?
invalid code
JavaTpoint has not exist
JavaTpoint has exist
none of these above 



Answer: (a) invalid code

Explanation: A new exception class must inherit from a BaseException, and there is no such inheritance here.

55) Study the following statement

z = {"x":0, "y":1} z = {"x":0, "y":1}

Which of the following is the correct statement?
x dictionary z is created
x and y are the keys of dictionary z
0 and 1 are the values of dictionary z
All of the above 



Answer: (d) All of the above

Explanation: All of the above statements is correct regarding Python code.


Comments

Popular posts from this blog

Mini-Max Algorithm in Artificial Intelligence

Alpha-Beta Pruning

Software Engineering Multiple Choice Questions