Monday 27 November 2023

Python Basics MCQs Set 2

MCQs based on Python Basics (Set 2)


10 important MCQs related to Python basic syntax, along with their answers:


1. Which of the following is the correct syntax for assigning a value to a variable in Python?

(a) my_variable := 10

(b) my_variable = 10

(c) my_variable = "10"

(d) my_variable '10'


2. What is the output of the following Python code?

print("Hello, World!)


(a) Hello, World!

(b) "Hello, World!

(c) Syntax Error

(d) Import Error


3. What is the data type of the variable my_number in the following Python code?

my_number = 10


(a) int

(b) float

(c) string

(d) boolean


4. Which of the following is the correct syntax for an if statement in Python?

(a) if condition:

      body

(b) if condition;

      body

(c) if condition: {

      body }

(d) if condition; {

      body }


5. What is the difference between the + and = operators in Python?

(a) The + operator is used for addition of numbers, while the = operator is used for assignment.

(b) The + operator is used for concatenation of numbers, while the = operator is used for assignment.

(c) The + operator is used for assignment while the = operator is used for addition.

(d) None of these


6. What is the purpose of the print() function in Python?

(a) To print output to the console

(b) To read input from the user

(c) To convert data types

(d) To check if a string is empty


7. What is the purpose of the indentation in Python code?

(a) To make the code more readable

(b) To define code blocks

(c) To prevent errors

(d) All of the above


8. What is the purpose of the for loop in Python?

(a) To prevent errors from occurring

(b) To check if a condition is true

(c) To handle errors in code

(d) To iterate over a sequence of items


9. Which of the following is not a valid Python identifier?

(a) my_variable

(b) 1st_name

(c) while1

(d) _my_variable


10. What is the purpose of the input() function in Python?

(a) To convert data types

(b) To print output to the console

(c) To read input from the user

(d) To check if a string is empty



Answers

1. (b) my_variable = 10

2. (c) Syntax Error

3. (a) int

4. (a) if condition:

         body

5. (a) The + operator is used for addition of numbers, while the = operator is used for assignment. 

6. (a) To print output to the console

7. (b) To define code blocks

8. (d) To iterate over a sequence of items

9. (b) 1st_name

10. (c) To read input from the user


Back to Python MCQs >


No comments:

Post a Comment