0% found this document useful (0 votes)
423 views14 pages

Python test1-QB-CA1

This document provides a set of 10 multiple choice questions about Python precedence and associativity. The questions cover order of operations, operator precedence like exponentiation vs multiplication, and evaluation of expressions. Correct answers are provided along with explanations for each question. The questions assess understanding of Python operator precedence and associativity rules.

Uploaded by

ItkalkarShailesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
423 views14 pages

Python test1-QB-CA1

This document provides a set of 10 multiple choice questions about Python precedence and associativity. The questions cover order of operations, operator precedence like exponentiation vs multiplication, and evaluation of expressions. Correct answers are provided along with explanations for each question. The questions assess understanding of Python operator precedence and associativity rules.

Uploaded by

ItkalkarShailesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Precedence and

Associativity – 1”.

1. The value of the expressions 4/(3*(2-1)) and 4/3*(2-1) is the same.


a) True
b) False
View Answer

Answer: a
Explanation: Although the presence of parenthesis does affect the order of precedence, in the case
shown above, it is not making a difference. The result of both of these expressions is 1.333333333.
Hence the statement is true.

2. What will be the value of the following Python expression?

4+3%5

a) 4
b) 7
c) 2
d) 0
View Answer

Answer: b
Explanation: The order of precedence is: %, +. Hence the expression above, on simplification results in 4
+ 3 = 7. Hence the result is 7.

3. Evaluate the expression given below if A = 16 and B = 15.

A % B // A

a) 0.0
b) 0
c) 1.0
d) 1
View Answer

Answer: b
Explanation: The above expression is evaluated as: 16%15//16, which is equal to 1//16, which results in
0.

advertisement

4. Which of the following operators has its associativity from right to left?
a) +
b) //
c) %
d) **
View Answer

Answer: d
Explanation: All of the operators shown above have associativity from left to right, except
exponentiation operator (**) which has its associativity from right to left.

5. What will be the value of x in the following Python expression?

x = int(43.55+2/2)

a) 43
b) 44
c) 22
d) 23
View Answer

Answer: b
Explanation: The expression shown above is an example of explicit conversion. It is evaluated as
int(43.55+1) = int(44.55) = 44. Hence the result of this expression is 44.

6. What is the value of the following expression?

2+4.00, 2**4.0

a) (6.0, 16.0)
b) (6.00, 16.00)
c) (6, 16)
d) (6.00, 16.0)
View Answer

Answer: a
Explanation: The result of the expression shown above is (6.0, 16.0). This is because the result is
automatically rounded off to one decimal place.

7. Which of the following is the truncation division operator?


a) /
b) %
c) //
d) |
View Answer
Answer: c
Explanation: // is the operator for truncation division. It is called so because it returns only the integer
part of the quotient, truncating the decimal part. For example: 20//3 = 6.

8. What are the values of the following Python expressions?

2**(3**2)

(2**3)**2

2**3**2

a) 64, 512, 64
b) 64, 64, 64
c) 512, 512, 512
d) 512, 64, 512
View Answer

Answer: d
Explanation: Expression 1 is evaluated as: 2**9, which is equal to 512. Expression 2 is evaluated as 8**2,
which is equal to 64. The last expression is evaluated as 2**(3**2). This is because the associativity of **
operator is from right to left. Hence the result of the third expression is 512.

9. What is the value of the following expression?

8/4/2, 8/(4/2)

a) (1.0, 4.0)
b) (1.0, 1.0)
c) (4.0. 1.0)
d) (4.0, 4.0)
View Answer

Answer: a
Explanation: The above expressions are evaluated as: 2/2, 8/2, which is equal to (1.0, 4.0).

10. What is the value of the following expression?

float(22//3+3/3)

a) 8
b) 8.0
c) 8.3
d) 8.33
View Answer
Answer: b
Explanation: The expression shown above is evaluated as: float( 7+1) = float(8) = 8.0. Hence the result of
this expression is 8.0.

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social
networks below and stay updated with latest contests, videos, internships and jobs!

Python MCQ (Multi Choice Questions)

1) What is the maximum possible length of an identifier?

a. 16

b. 32

c. 64

d. None of these above

Hide Answer Workspace

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?

a. Zim Den

b. Guido van Rossum

c. Niene Stom

d. Wick van Rossum

Hide Answer Workspace

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?

a. 1995

b. 1972

c. 1981

d. 1989

Hide Answer Workspace

Answer: (d) 1989

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

4) In which language is Python written?

a. English

b. PHP

c. C

d. All of the above

Hide Answer Workspace

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?

a. .py

b. .python

c. .p

d. None of these
Hide Answer Workspace

Answer: (a) .py

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

6) In which year was the Python 3.0 version developed?

a. 2008

b. 2000

c. 2010

d. 2005

Hide Answer Workspace

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?

a. Key

b. Brackets

c. Indentation

d. None of these

Hide Answer Workspace

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?

a. /

b. //
c. #

d. !

Hide Answer Workspace

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?

a. Classes are real-world entities while objects are not real

b. Objects are real-world entities while classes are not real

c. Both objects and classes are real-world entities

d. All of the above

Hide Answer Workspace

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?

1. class Name:  

2.     def __init__(javatpoint):  

3.         javajavatpoint = java  

4. name1=Name("ABC")  

5. name2=name1  

a. It will throw the error as multiple references to the same object is not possible

b. id(name1) and id(name2) will have same value

c. Both name1 and name2 will have reference to two different objects of class Name

d. All of the above


Hide Answer Workspace

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?

a. Object

b. Function

c. Attribute

d. Argument

Hide Answer Workspace

Answer: (b) Function

Explanation: Function is also known as the method.

12) Which of the following declarations is incorrect?

a. _x = 2

b. __x = 3

c. __xyz__ = 5

d. None of these

Hide Answer Workspace

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?

a. To identify the variable

b. It confuses the interpreter


c. It indicates a private variable of a class

d. None of these

Hide Answer Workspace

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?

a. val

b. raise

c. try

d. with

Hide Answer Workspace

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?

a. All variable names must begin with an underscore.

b. Unlimited length

c. The variable name length is a maximum of 2.

d. All of the above

Hide Answer Workspace

Answer: (b) Unlimited length

Explanation: None

16) Which of the following declarations is incorrect in python language?


a. xyzp = 5,000,000

b. x y z p = 5000 6000 7000 8000

c. x,y,z,p = 5000, 6000, 7000, 8000

d. x_y_z_p = 5,000,000

Hide Answer Workspace

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?

a. _val

b. val

c. try

d. _try_

Hide Answer Workspace

Answer: (c) try

Explanation: "try" is a keyword.

18) Which of the following operators is the correct option for power(ab)?

a. a ^ b

b. a**b

c. a ^ ^ b

d. a ^ * b

Hide Answer Workspace

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?

a. Parentheses, Exponential, Multiplication, Division, Addition, Subtraction

b. Multiplication, Division, Addition, Subtraction, Parentheses, Exponential

c. Division, Multiplication, Addition, Subtraction, Parentheses, Exponential

d. Exponential, Parentheses, Multiplication, Division, Addition, Subtraction

Hide Answer Workspace

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

Explanation: PEMDAS (similar to BODMAS).

20) Which one of the following has the same precedence level?

a. Division, Power, Multiplication, Addition and Subtraction

b. Division and Multiplication

c. Subtraction and Division

d. Power and Division

Hide Answer Workspace

Answer: (b) Division and Multiplication

Explanation: None

21) Which one of the following has the highest precedence in the expression?

a. Division

b. Subtraction

c. Power

d. Parentheses

Hide Answer Workspace
Answer: (d) Parentheses

Explanation: PEMDAS (similar to BODMAS).

22) Which of the following functions is a built-in function in python language?

a. val()

b. print()

c. print()

d. None of these

Hide Answer Workspace

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:

1. round(4.576)  

What will be the output of this function?

a. 4

b. 5

c. 576

d. 5

Hide Answer Workspace

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?


1. pow(x,y,z)  

a. (x**y) / z

b. (x / y) * z

c. (x**y) % z

d. (x / y) / z

Hide Answer Workspace

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

Explanation: None

25) Study the following function:

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

What will be the output of this function?

a. False

b. True

c. 0

d. Invalid code

Hide Answer Workspace

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:

1. x = 1  

2. while True:  

3.     if x % 5 = = 0:  

4.         break  
5.     print(x)   

6.     x + = 1  

What will be the output of this code?

a. error

b. 2 1

c. 0 3 1

d. None of these

Hide Answer Workspace

Answer: (a) error

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

You might also like