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

Python Programming MCQs and Answers

The document provides a comprehensive overview of Python programming, including multiple-choice questions, short questions, and long questions covering topics such as data types, operators, control structures, functions, and object-oriented programming. It emphasizes the importance of Python's syntax, variable naming rules, and the use of libraries and modules. Additionally, it explains concepts like exception handling and unit testing, making it a valuable resource for learners and practitioners of Python.

Uploaded by

Irfan Ul Haq
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)
39 views14 pages

Python Programming MCQs and Answers

The document provides a comprehensive overview of Python programming, including multiple-choice questions, short questions, and long questions covering topics such as data types, operators, control structures, functions, and object-oriented programming. It emphasizes the importance of Python's syntax, variable naming rules, and the use of libraries and modules. Additionally, it explains concepts like exception handling and unit testing, making it a valuable resource for learners and practitioners of Python.

Uploaded by

Irfan Ul Haq
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

Unit 2 Python Programming

60 MCQs with Answers


1. Ali wants to print a welcome message on the screen. Which function should he use?

a) show()
b) display()
c) print()
d) echo()
Answer: c

2. When installing Python, checking “Add Python to PATH” helps Zoya to:

a) Run Python from any directory


b) Increase Python speed
c) Protect her code
d) Update Python automatically
Answer: a

3. In Python, code written inside # is:

a) A warning
b) A comment
c) An error
d) A shortcut
Answer: b

4. Hassan wants to store his age as a number. Which data type will Python choose?

a) str
b) int
c) list
d) bool
Answer: b

5. If Sara enters 12.5 as height, Python reads it as:

a) bool
b) float
c) string
d) integer
Answer: b

6. Which symbol is used for exponent?

a) ^
b) **
c) //
d) %%
Answer: b

7. What will 7 % 3 return?

a) 1
b) 2
c) 3
d) 0
Answer: b

8. Adeel compares two values to check if they are equal. Which operator will he use?

a) =
b) ==
c) :=
d) =>
Answer: b

9. x = x + 5 can be written shortly as:

a) x=>5
b) x:=5
c) x+=5
d) x-+5
Answer: c

10. Which operator returns True only if both conditions are True?

a) or
b) not
c) xor
d) and
Answer: d

11. If temperature is 35, which block runs?

if temperature > 30:

print("Hot!")

a) Nothing
b) print runs
c) Error
d) Loop starts
Answer: b

12. A shortcut if-else can be written in:

a) 3 lines
b) 2 lines
c) 1 line
d) No shortcut
Answer: c

13. Aisha wants her program to repeat an action until a condition becomes false. Which loop?

a) for
b) while
c) repeat
d) do-while
Answer: b

14. A for-loop repeats for each item in a:

a) file
b) browser
c) sequence
d) comment
Answer: c

15. Which of the following is a built-in data structure?

a) record
b) list
c) folder
d) index
Answer: b

16. A list is written using:

a) {}
b) ()
c) []
d) <>
Answer: c

17. Sara wants to access the 1st element of a list. She should use index:

a) 1
b) 0
c) -1
d) First
Answer: b

18. Which method adds a new item to the end of a list?

a) push()
b) append()
c) add()
d) insert()
Answer: b

19. Which method removes the first matching element?

a) pop()
b) delete()
c) remove()
d) cancel()
Answer: c

20. Tuples are different from lists because they are:

a) slower
b) mutable
c) unchangeable
d) unsafe
Answer: c

21. A tuple uses:

a) []
b) ()
c) {}
d) <>
Answer: b

22. Waleed wants to create reusable code blocks. He should use:

a) classes
b) modules
c) loops
d) functions
Answer: d

23. A function is defined using:

a) def
b) func
c) define
d) create
Answer: a

24. A function with parameters means it:

a) cannot return value


b) accepts input values
c) must print output
d) needs a loop
Answer: b
25. A default parameter is used when:

a) user gives wrong value


b) no value is passed
c) value is negative
d) value is large
Answer: b

26. A library in Python is like:

a) A folder containing tools


b) A game
c) A virus
d) A shortcut
Answer: a

27. The random library helps in generating:

a) time
b) lists
c) random numbers
d) strings
Answer: c

28. Packages help large projects by:

a) slowing execution
b) organizing modules
c) hiding errors
d) removing functions
Answer: b

29. Index -1 means:

a) First element
b) Last element
c) Middle element
d) Error
Answer: b

30. Slicing allows:

a) running loops
b) skipping errors
c) cutting portions of sequence
d) downloading libraries
Answer: c

31. OOP stands for:


a) Object Oriented Programming
b) Open Online Program
c) Order of Operations
d) Object Operated Process
Answer: a

32. A class in Python acts as:

a) table
b) template for objects
c) loop
d) condition
Answer: b

33. An object is:

a) a variable
b) a function
c) an instance of a class
d) a loop
Answer: c

34. The method that initializes an object is:

a) start()
b) build()
c) init()
d) create()
Answer: c

35. self represents:

a) Python version
b) the current object
c) memory address
d) file name
Answer: b

36. Exception handling helps to:

a) delete errors
b) avoid errors
c) catch errors safely
d) stop code always
Answer: c

37. The keyword for testing risky code is:

a) attempt
b) try
c) catch
d) check
Answer: b

38. The keyword for handling errors is:

a) handle
b) else
c) except
d) final
Answer: c

39. File handling allows Python to:

a) talk to the internet


b) store data permanently
c) print faster
d) increase speed
Answer: b

40. To read a file, we use:

a) open( , "r" )
b) open( , "w" )
c) open( , "a" )
d) open( , "x" )
Answer: a

41. "w" mode does what?

a) reads file
b) appends data
c) writes and overwrites
d) deletes file
Answer: c

42. "a" mode is used to:

a) remove text
b) add text without deleting old text
c) rename file
d) copy file
Answer: b

43. Unit testing ensures:

a) whole system works


b) individual parts work
c) network works
d) only loops work
Answer: b

44. Debugging helps to:

a) make code slow


b) hide errors
c) find and fix errors
d) delete functions
Answer: c

45. Python comments are used to:

a) run faster
b) explain code
c) slow program
d) block loops
Answer: b

46. Integer division uses:

a) //
b) /
c) %
d) **
Answer: a

47. '5' is a:

a) integer
b) float
c) string
d) boolean
Answer: c

48. Which one is a boolean value?

a) "True"
b) True
c) "False"
d) "0"
Answer: b

49. Which operator checks “not equal”?

a) ==
b) !==
c) !=
d) <>
Answer: c
50. Loops stop when:

a) the code breaks


b) the IDE closes
c) RAM fills
d) an integer is found
Answer: a

51. The import keyword is used to:

a) create loops
b) load modules
c) delete libraries
d) hide error
Answer: b

52. Which data structure stores “key-value pairs”?

a) list
b) tuple
c) dictionary
d) string
Answer: c

53. BMI calculation uses:

a) slicing
b) arithmetic operators
c) file handling
d) classes
Answer: b

54. In a class, functions inside are called:

a) loops
b) objects
c) methods
d) executions
Answer: c

55. A package is a collection of:

a) classes
b) modules
c) loops
d) conditions
Answer: b

56. A file must be closed to:


a) free memory
b) increase size
c) convert to tuple
d) avoid printing
Answer: a

57. Debuggers help developers by:

a) skipping errors
b) tracing errors step-by-step
c) hiding code
d) stopping IDE
Answer: b

58. Python is called high-level because it:

a) uses English-like syntax


b) is slow
c) uses binary
d) needs hardware
Answer: a

59. While loop needs a:

a) function
b) condition
c) class
d) file
Answer: b

60. Lists allow storing:

a) only numbers
b) only strings
c) multiple types
d) only booleans
Answer: c

25 Short Questions
1. What is Python and why is it popular?

Python is a high-level, easy-to-learn language with simple syntax. It is popular because it supports
multiple programming paradigms, has huge libraries, and is ideal for beginners as well as professionals.

2. What is the purpose of variables in Python?

Variables store data values that can change during program execution. They act as containers for
numbers, strings, or other data types.
3. Explain variable naming rules.

Variables must start with a letter or underscore, cannot use reserved keywords, and are case sensitive.
They may include numbers after the first character.

4. What is the use of the input() function?

The input() function takes data from the user as a string. It helps programs become interactive by
receiving real-time input.

5. What is type conversion?

Type conversion means converting data from one type to another, such as using int() or float() to
convert input strings into numbers.

6. Define arithmetic operators.

Arithmetic operators include +, -, *, /, %, **, and //. They are used to perform mathematical calculations
like addition or division.

7. What are comparison operators?

Comparison operators compare values and return True or False. Examples include >, <, ==, !=, >=, and
<=.

8. What are logical operators?

Logical operators combine conditions and return boolean values. These include and, or, and not.

9. Define control structures.

Control structures manage the flow of a program. They include selection (if-else), decision making, and
loops (while, for).

10. Explain the if-else statement.

It checks a condition and executes one block if true, otherwise the else block runs. It helps programs
make decisions.

11. What is a loop?

A loop repeatedly executes a block of code while a condition is true or for each item in a sequence.

12. What is a while loop?

A while loop runs as long as its condition remains true. It is useful for unknown number of repetitions.

13. What is a for loop?

A for loop iterates through sequences like lists and strings, running once for each element.

14. What is a list?

A list stores multiple items and is mutable, meaning values can be changed. It uses square brackets.
15. What is slicing?

Slicing extracts a portion of a list or string using index ranges. It supports both positive and negative
indices.

16. What is a tuple?

A tuple is similar to a list but immutable. Once created, its values cannot be changed.

17. Define a function.

A function is a reusable block of code defined using def and executed when called. It helps reduce
repetition.

18. What is a parameter?

Parameters are placeholders that receive values when a function is called. They help customize function
behavior.

19. What is a module?

A module is a Python file containing functions or variables. It promotes code organization by grouping
related code.

20. What is a package?

A package is a folder containing multiple modules. It helps structure large programs neatly.

21. What is OOP?

OOP is Object-Oriented Programming that organizes code into classes and objects for modularity and
reusability.

22. What is a class?

A class is a blueprint for creating objects. It defines attributes and methods.

23. What is an object?

An object is an instance created from a class that contains actual data and behavior.

24. What is exception handling?

Exception handling safely manages errors using try and except blocks so programs do not crash.

25. What is unit testing?

Unit testing checks individual parts of a program to ensure each works correctly before combining them.

15 Long Questions
1. Explain the Python development environment setup.

To start programming in Python, one must install Python from the official website and ensure “Add to
PATH” is selected. A code editor or IDE like VS Code or PyCharm helps write, run, and debug code easily.
The development environment allows editing, saving, compiling, and executing Python programs. It also
includes tools for testing and managing libraries.

2. Describe Python variables and their naming rules.

Variables store values that can change during execution. Python assigns data types automatically based
on value. Naming must follow rules: begin with letter or underscore, avoid keywords, and be case-
sensitive. Clear and meaningful variable names improve program readability. Python supports int, float,
string, and boolean data types for variables.

3. Explain different types of operators in Python.

Python supports arithmetic operators (+, -, *, /, %, **, //) for calculations. Comparison operators
compare values and return boolean results. Assignment operators like =, += help assign or update
values. Logical operators (and, or, not) combine conditions. Using these operators together allows
Python to evaluate expressions and make decisions effectively.

4. Describe decision-making statements with examples.

Decision statements like if, if-else, and elif allow Python to choose between multiple code paths. If
executes only when condition is true. If-else executes one of two possible blocks. Elif handles multiple
conditions sequentially. These help programs behave differently based on user input or computation
results.

5. Explain while and for loops with examples.

A while loop repeats as long as its condition is true, useful when repetitions are not predetermined. A
for loop iterates over sequences such as lists or ranges, running once per element. Loops reduce
repetition and automate tasks like counting, searching, or processing data. Both loops improve
efficiency in Python programs.

6. Discuss lists, their operations, and methods.

Lists are mutable collections created using square brackets. Python allows accessing elements through
indexing and slicing. Common methods include append(), insert(), remove(), sort(), and reverse(). Lists
can store mixed data types and grow dynamically. Their flexibility makes them one of Python’s most
useful structures.

7. Explain tuples and how they differ from lists.

Tuples use parentheses and are immutable, meaning once created, values cannot change. They support
indexing and slicing like lists but ensure data safety by preventing modification. Tuples are often used
for fixed collections such as coordinates or predefined settings. Their immutability gives them
performance advantages.

8. Explain slicing with indexing (positive and negative).

Python supports slicing sequences by specifying start and end indices. Positive indices begin from 0,
while negative indices start from -1. Slicing extracts sub-sequences without modifying the original. It
helps retrieve selected items efficiently. Both positive and negative indexing make retrieval flexible.
9. Describe functions, parameters, and return values.

Functions are blocks of reusable code defined using def. Parameters receive input values, making
functions flexible. Python allows default parameters when no value is passed. Functions may return
values using return statement, enabling data output from function calls. They improve readability and
reduce repetition.

10. Explain modules, importing, and libraries.

Modules are files containing functions or variables. They can be imported into programs using import.
Libraries like random and datetime provide ready-made tools that reduce coding effort. Importing helps
reuse tested code and organize large projects. Python’s module system makes programming modular
and efficient.

11. What are packages and why are they useful?

Packages contain multiple modules grouped in directories. They help organize complex programs by
dividing them into manageable sections. Using packages improves maintainability, readability, and
collaboration among developers. Python allows importing modules from packages with dot notation.
Packages support scalable software development.

12. Describe Object-Oriented Programming with an example.

OOP organizes code into classes and objects to model real-world entities. A class defines attributes and
methods, while an object represents an instance with actual data. The init method initializes attributes.
OOP promotes reusability, structure, and modularity. Example: A ToyCar class with size, color, and
wheels.

13. Explain the role of self and init() in classes.

self refers to the current object and allows access to attributes inside methods. init() automatically runs
when an object is created and initializes object variables. Together, they define object behavior and
ensure each instance holds unique data. They are essential parts of Python classes.

14. Describe exception handling and why it is important.

Exception handling uses try and except to manage runtime errors. Instead of crashing, Python safely
handles errors and continues execution. It is crucial for user-friendly and stable programs. It catches
issues like ZeroDivisionError, improving reliability.

15. Explain file handling operations (read, write, append).

File handling allows reading and writing data permanently. Using open(), "r" reads content, "w" writes
and overwrites, and "a" appends without deleting previous data. Proper closing of files ensures memory
safety. File handling is essential for saving data such as logs, user profiles, and reports.

You might also like