0% found this document useful (0 votes)
5 views11 pages

Python Programming Quiz Exercises

Uploaded by

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

Python Programming Quiz Exercises

Uploaded by

Sdwa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ADDIS ABABA UNIVERSITY [Ei-ABC]

PRE-EGINEERING
INTRODUCTION TO PROGRAMMING WITH PYTHON
HOME MADE MULTIPLE QUESTION EXERCISES

1. What is Python?
a) A high-level programming c) A hardware device
language d) A web browser
b) A database management
system
2. Which of the following is used to declare a variable in Python?
a) let c) const
b) var d) None of the above
3. What is the output of the following code?
print(2 + 3 * 4 - 6)
a) 15 c) 14
b) 17 d) 24
4. Which of the following is NOT a valid data type in Python?
a) int d) string
b) float e) char
c) boolean
5. What is the output of the following code?
print("Hello" + "World")
a) Hello c) HelloWorld
b) World d) None of the above
6. Which statement is used to exit a loop in Python?
a) break c) continue
b) exit d) return
7. What is the output of the following code?
x = [1, 2, 3, 4, 5]
print(x[2])
a) 1 c) 3
b) 2 d) 4
8. Which of the following is used to read user input in Python?
a) input() c) get()
b) read() d) scanf()

Prepared by: Kibrom G., Bekuretsion B. and Kassahun T.


ADDIS ABABA UNIVERSITY [Ei-ABC]
PRE-EGINEERING
INTRODUCTION TO PROGRAMMING WITH PYTHON
HOME MADE MULTIPLE QUESTION EXERCISES

9. What is the correct way to write a comment in Python?


a) // This is a comment c) /* This is a comment */
b) # This is a comment d) <!-- This is a comment -->
10. What is the output of the following code?
x=5
if x > 10:
print("Greater than 10")
else:
print("Less than or equal to 10")
a) Greater than 10 c) 5
b) Less than or equal to 10 d) None of the above
11. Which of the following is used to open a file in Python?
a) open() c) file()
b) read() d) start()
12. What does the len() function do in Python?
a) Returns the length of a c) Rounds a floating-point
string or list number
b) Converts a string to d) None of the above
lowercase
13. What is the output of the following code?
x = [1, 2, 3]
y=x
[Link](4)
print(x)
a) [1, 2, 3] c) [4, 3, 2, 1]
b) [1, 2, 3, 4] d) None of the above
14. Which of the following is the correct way to define a function in
Python?
a) function my_function(): c) func my_function():
b) def my_function(): d) define my_function():

Prepared by: Kibrom G., Bekuretsion B. and Kassahun T.


ADDIS ABABA UNIVERSITY [Ei-ABC]
PRE-EGINEERING
INTRODUCTION TO PROGRAMMING WITH PYTHON
HOME MADE MULTIPLE QUESTION EXERCISES

15. Whatis the output of the following code?


x = 10
y=3
print(x / y)
a) 3.33 c) 3
b) 3.0 d) 3.3333333333333335

////////////////////////////////////////////////////////////////////////////
Multiple questions with answer
////////////////////////////////////////////////////////////////////////////

Python Basics:

1. Which of the following is a correct way to assign a value to a


variable in Python?
a) x = 5
b) 5 = x
c) x == 5
d) x := 5

Answer: a) x = 5

2. What is the output of the following code?


print("Hello, World!")
a) Hello, World!
b) Hello World!
c) HelloWorld
d) None of the above

Answer: a) Hello, World!

Prepared by: Kibrom G., Bekuretsion B. and Kassahun T.


ADDIS ABABA UNIVERSITY [Ei-ABC]
PRE-EGINEERING
INTRODUCTION TO PROGRAMMING WITH PYTHON
HOME MADE MULTIPLE QUESTION EXERCISES

3. How do you add comments in Python?


a) // This is a comment
b) /* This is a comment */
c) # This is a comment
d) <!-- This is a comment -->

Answer: c) # This is a comment

4. Which symbol is used for exponentiation in Python?


a) ^
b) **
c) *
d) //

Answer: b) **

5. What is the result of the following expression?


5+2*3
a) 11
b) 21
c) 17
d) 13

Answer: d) 13

Python Variables:

6. Which of the following is a valid variable name in Python?


a) my-variable
b) 123variable

Prepared by: Kibrom G., Bekuretsion B. and Kassahun T.


ADDIS ABABA UNIVERSITY [Ei-ABC]
PRE-EGINEERING
INTRODUCTION TO PROGRAMMING WITH PYTHON
HOME MADE MULTIPLE QUESTION EXERCISES

c) _variable
d) variable#

Answer: c) _variable

7. What is the value of x after the following code is executed?


x=5
x=x+2
a) 2
b) 5
c) 7
d) 10

Answer: c) 7

8. What is the scope of a variable defined inside a function in


Python?
a) Local to the function
b) Global to the entire program
c) Global to the file where the function is defined
d) Global to the module where the function is defined

Answer: a) Local to the function

9. What is the output of the following code?


x = 10
y=5
z=x+y
print(z)
a) 105
b) 15

Prepared by: Kibrom G., Bekuretsion B. and Kassahun T.


ADDIS ABABA UNIVERSITY [Ei-ABC]
PRE-EGINEERING
INTRODUCTION TO PROGRAMMING WITH PYTHON
HOME MADE MULTIPLE QUESTION EXERCISES

c) x + y
d) None of the above

Answer: b) 15

10. Which of the following is not a valid data type for a variable in
Python?
a) int
b) float
c) string
d) void

Answer: d) void

Python Data Types:

11. Which data type is used to represent whole numbers in Python?


a) int
b) float
c) string
d) boolean

Answer: a) int

12. What is the value of x after the following code is executed?


x = 5.0
a) 5
b) 5.0
c) "5"
d) None of the above

Prepared by: Kibrom G., Bekuretsion B. and Kassahun T.


ADDIS ABABA UNIVERSITY [Ei-ABC]
PRE-EGINEERING
INTRODUCTION TO PROGRAMMING WITH PYTHON
HOME MADE MULTIPLE QUESTION EXERCISES

Answer: b) 5.0

13. What is the output of the following code?


x = "Hello"
y = 'World'
print(x + y)
a) Hello
b) World
c) HelloWorld
d) None of the above

Answer: c) HelloWorld

14. Which data type is used to represent a sequence of characters in


Python?
a) int
b) float
c) string
d) boolean

Answer: c) string

15. What is the output of the following code?


x = True
y = False
print(x and y)
a) True
b) False
c) None
d) Error

Prepared by: Kibrom G., Bekuretsion B. and Kassahun T.


ADDIS ABABA UNIVERSITY [Ei-ABC]
PRE-EGINEERING
INTRODUCTION TO PROGRAMMING WITH PYTHON
HOME MADE MULTIPLE QUESTION EXERCISES

Answer: b) False

Python Conditional Statements and Loops:

16. Which keyword is used to start an "else if" block in Python?


a) elseif
b) elif
c) else if
d) elseif

Answer: b) elif

17. What is the output of the following code?


x = 10
if x > 5:
print("x is greater than 5")
elif x > 2:
print("x is greater than 2")
else:
print("x is less than or equal to 2")
a) x is greater than 5
b) x is greater than 2
c) x is less than or equal to 2
d) Multiple lines will be printed

Answer: a) x is greater than 5

18. Which of the following logical operators checks iftwo conditions


are both true in Python?
a) ||
b) &&

Prepared by: Kibrom G., Bekuretsion B. and Kassahun T.


ADDIS ABABA UNIVERSITY [Ei-ABC]
PRE-EGINEERING
INTRODUCTION TO PROGRAMMING WITH PYTHON
HOME MADE MULTIPLE QUESTION EXERCISES

c) !
d) and

Answer: d) and

19. What is the output of the following code?


numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
a) 1 2 3 4 5
b) [1, 2, 3, 4, 5]
c) 5 4 3 2 1
d) None of the above

Answer: a) 1 2 3 4 5

20. What is the output of the following code?


x=0
while x < 5:
print(x)
x += 1
a) 1 2 3 4 5
b) 0 1 2 3 4
c) 0 1 2 3 4 5
d) None of the above

Answer: b) 0 1 2 3 4

Python Functions:

21. What is the keyword used to define a function in Python?

Prepared by: Kibrom G., Bekuretsion B. and Kassahun T.


ADDIS ABABA UNIVERSITY [Ei-ABC]
PRE-EGINEERING
INTRODUCTION TO PROGRAMMING WITH PYTHON
HOME MADE MULTIPLE QUESTION EXERCISES

a) def
b) function
c) define
d) func

Answer: a) def

22. What is the output of the following code?


def greet(name):
print("Hello, " + name + "!")

greet("Alice")
a) Hello, Alice!
b) Hello, name!
c) Alice
d) None of the above

Answer: a) Hello, Alice!

23. Which of the following is not a valid way to call a function in


Python?
a) greet()
b) greet("Bob")
c) call greet()
d) greet(name="Alice")

Answer: c) call greet()

24. What is the purpose of a return statement in a function?


a) To terminate the execution of the function
b) To print a value to the console

Prepared by: Kibrom G., Bekuretsion B. and Kassahun T.


ADDIS ABABA UNIVERSITY [Ei-ABC]
PRE-EGINEERING
INTRODUCTION TO PROGRAMMING WITH PYTHON
HOME MADE MULTIPLE QUESTION EXERCISES

c) To retrieve user input


d) To send a value back to the caller

Answer: d) To send a value back to the caller

25. What is the output of the following code?


def add_numbers(x, y):
return x + y

result = add_numbers(3, 4)
print(result)
a) 3
b) 4
c) 7
d) None of the above

Answer: c) 7

Prepared by: Kibrom G., Bekuretsion B. and Kassahun T.

Common questions

Powered by AI

Python is preferred for beginners because it has a simple and intuitive syntax, dynamic typing, and supports a range of basic data types such as 'int', 'float', 'string', etc., which makes it easy to handle variables without explicitly declaring data types .

Python distinguishes between float division and integer division. '10 / 3' performs float division, yielding the precise result '3.3333333333333335' by converting operands into floats if necessary. In contrast, '10 // 3' performs integer division, which truncates the result to '3', reflecting Python's type versatility and precision handling in numerical computations .

When 'x = y' where 'y' is a list, 'x' becomes a reference to the same object as 'y'. Therefore, any modification to 'y' will also reflect in 'x'. When an operation like 'y.append(4)' is performed after the assignment, the change is seen in both variables, making the output of 'print(x)' as '[1, 2, 3, 4]' . This happens because of Python’s mutable data type behavior with lists, where two variables can reference the same memory location.

Python uses the '#' symbol for single-line comments to maintain simplicity and readability in scripts. This choice avoids multi-character combinations that could complicate parsing, allowing the language to remain minimalistic. This simplicity enhances code readability and maintainability, making it easy for developers to include explanations without disrupting the flow of the code .

The 'elif' keyword in Python serves as a concise way to chain together multiple conditions in an if-else block. This enhances logical flow control by allowing developers to check various scenarios in a clear, straightforward manner without deeply nested code structures. It simplifies code readability by presenting conditions as part of a logical sequence, making debugging and maintenance easier .

Local variables in Python are restricted in scope to the function level, meaning they cannot be accessed outside their respective functions. This scoping ensures that functions are isolated, reducing errors caused by accidental variable modifications elsewhere in the code. It enhances program integrity by providing safe encapsulation of data and behavior, essential for writing modular and maintainable code .

Python follows the standard mathematical precedence rules where multiplication and division have a higher precedence over addition and subtraction. Therefore, in the expression 'print(2 + 3 * 4 - 6)', the multiplication is performed first, followed by addition, and then subtraction, resulting in an output of 8 .

The 'input()' function in Python reads a line from input, converts it into a string, and returns it, allowing developers to interact with users by capturing textual data during program execution. This functionality is crucial for developing interactive applications where user input drives the flow or processing logic, providing flexibility in program interaction .

Logical operators in Python such as 'and', 'or', and 'not' allow for compound condition evaluations, enabling succinct and straightforward multi-condition checks. For instance, 'print(True and False)' evaluates both conditions and returns 'False', as the 'and' operator requires both operands to be True to yield True. This facilitates complex decision-making in code, enhancing control flow .

The expression 'print('Hello' + 'World')' in Python combines ('concatenates') the two string literals into a single string 'HelloWorld'. This operation illustrates Python's handling of strings as immutable sequences, where the '+' operator is used for concatenation rather than arithmetic addition .

You might also like