0% found this document useful (0 votes)
179 views10 pages

Python Programming Questions and Solutions

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)
179 views10 pages

Python Programming Questions and Solutions

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

Section A: (10 × 2 = 20 Marks)

Answer any ten of the following questions.


Section A: (10 × 2 = 20 Marks)
Answer any ten of the following questions.
(Each question carries 2 marks)
1. What is the difference between Python 2.x and
Python 3.x?
2. Define the term list in Python. How is it different
from a tuple?
3. What are f-string literals in Python? Give an
example.
4. What is the use of the pass statement in Python?
5. What is the output of the following Python code:
python
Copy code
print(2 ** 3)
6. Define a function in Python. How do you pass
arguments to it?
7. What is the difference between local and global
variables in Python?
8. What does the range() function do in Python?
9. Explain the concept of inheritance in Python.
10. What is the difference between deep copy and
shallow copy?
11. How do you handle exceptions in Python?
Give an example.
12. What is the difference between while loop and
for loop in Python?
Section B: (5 × 6 = 30 Marks)
Answer any five of the following questions.
(Each question carries 6 marks)
1. Write a Python program to check whether a
number is even or odd.
2. What is a dictionary in Python? Write a program
to add, remove, and display items in a dictionary.
3. Write a Python program to find the factorial of a
number using recursion.
4. Write a program to reverse a string in Python
without using the built-in function.
5. Explain file handling in Python. Write a program
to read from and write to a file.
6. What are lambda functions in Python? Write a
Python program to add two numbers using a
lambda function.
7. Write a Python program that accepts a list of
integers and prints the sum and average of the list.

Section C: (3 × 10 = 30 Marks)
Answer any three of the following questions.
(Each question carries 10 marks)
1. Write a Python program to create a class called
Student that includes:
o Attributes: name, roll_number, and

marks.
o Methods to:
 Accept the student's details.

 Display the student's details.

o Create an instance of the class and display the

student’s information.
2. Explain the object-oriented programming (OOP)
concepts in Python. Write a Python program to
demonstrate inheritance by defining a Person
class with attributes like name and age, and an
Employee class that inherits from Person and adds
a salary attribute.
3. Write a Python program that accepts a sentence
from the user and:
o Count the number of vowels in the sentence.

o Reverse the sentence and display it.

4. Write a Python program to find the GCD


(Greatest Common Divisor) of two numbers
using recursion.
5. Write a Python program that:
o Accepts a number from the user and checks if

it is a prime number or not.


o Display an appropriate message based on the

result.

Section D: (1 × 20 = 20 Marks)
Answer the following question.
(Each question carries 20 marks)
1. Case Study:
You are required to create a Student Management
System using Python. The system should:
o Allow the user to add, view, update, and

delete student details such as name, roll


number, and marks.
o Store the student records in a list of objects.

Your tasks are:


o Define a class Student with attributes like
name, roll_number, and marks.
o Implement methods to:
 Add a new student.

 Update a student's details.

 Delete a student's record.

 Display student details.

o Create a menu-driven program to perform


the operations.
o Use appropriate exception handling to catch
errors such as invalid inputs.
(Each question carries 2 marks)
[Link] is the difference between Python 2.x
and Python 3.x?
[Link] is the use of the len() function in
Python? Give an example.
[Link] indentation in Python. Why is it
important?
[Link] are lists and how are they different from
tuples?
[Link] would you define a function in Python?
Provide an example.
[Link] does the continue statement do in a
loop?
[Link] do you import a module in Python? Give
an example.
[Link] is a lambda function? Provide an
example.
[Link] the concept of exception handling in
Python.
10. What is a class in Python? Give an
example.
11. What is the purpose of the self parameter
in a method?
12. What does the break statement do in a
loop?
Section B: (5 × 6 = 30 Marks)
Answer any five of the following questions.
(Each question carries 6 marks)
[Link] a Python program to find the factorial
of a number using a for loop.
[Link] the different types of loops in Python.
Write a program to print numbers 1 to 10
using a while loop.
[Link] are dictionaries in Python? Write a
program to store and display student
information (e.g., name, roll number, marks).
[Link] a Python program to check whether a
given number is prime or not.
[Link] are list comprehensions in Python?
Write a program to find the square of all even
numbers from 1 to 20 using a list
comprehension.
[Link] the concept of object-oriented
programming (OOP) in Python. Define a
class called Car with attributes like make,
model, and year. Add a method to display
car details.
[Link] a Python program to demonstrate
exception handling using try, except, and
finally blocks.
Section C: (3 × 10 = 30 Marks)
Answer any three of the following questions.
(Each question carries 10 marks)
[Link] a Python program to create a menu-
driven program for:
o Finding the sum of two numbers.

o Finding the difference of two numbers.

o Finding the product of two numbers.

o Finding the quotient of two numbers.

Include error handling for division by


zero.
[Link] a Python program to read a file, count
the number of lines, words, and characters in
the file, and display the count. Handle
exceptions if the file does not exist.
[Link] how inheritance works in Python.
Write a Python program to demonstrate
inheritance by defining two classes: Person
and Employee (Employee should inherit from
Person and add an extra attribute salary).
[Link] a Python program that accepts a string
from the user and:
o Counts the number of vowels in the string.

o Reverses the string.

o Converts the string to uppercase.


[Link] are modules in Python? Write a program
to create a module called
math_operations.py with functions for
addition, subtraction, and multiplication.
Import the module in another Python file and
use the functions.

Section D: (1 × 20 = 20 Marks)
Answer the following question.
(Each question carries 20 marks)
[Link] Study:
Create a student management system using
Python. The system should:
o Allow the user to add student details

(name, roll number, marks).


o Display all student details.

o Update student marks for a specific

student.
o Delete a student’s record.

Your tasks are:


o Define a class Student with attributes
like name, roll_number, and marks.
o Implement methods to add, update,
delete, and display student records.
o Use a list of objects to store the student
records.
o Implement a menu-driven program that
allows the user to perform the above
operations.

Common questions

Powered by AI

Python's exception handling mechanism uses try, except, and finally blocks to catch and handle errors without stopping the program. It improves code reliability by isolating error-prone operations and dictating specific responses to different exception types, allowing the rest of the program to execute smoothly. It enhances readability by clarifying error management explicitly within the code. For example: try: value = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero") finally: print("Execution complete") This code captures a division-by-zero error, provides a clear response, and always executes a final statement, reinforcing structured and predictable error handling .

List comprehensions in Python provide a concise way to construct new lists by applying an expression to each element in an iterable and collecting the results. They improve code efficiency by reducing the lines of code and improving readability and Expediting execution by minimizing function calls and loops. An example converting a range of numbers to their squares only if even: squares = [x ** 2 for x in range(1, 21) if x % 2 == 0] This efficiently generates a list of squares for even numbers between 1 and 20 in a single line, demonstrating both succinctness and clarity over traditional loops .

Modules in Python are files containing Python code (functions, classes, variables) intended to be used in other Python programs. They enhance code modularity by encapsulating specific functionalities, which can be independently developed and maintained. This encourages code reusability across different programs. To create a module, write the desired functions in a .py file (for example, math_operations.py), then import it in another script using the import keyword: # math_operations.py def add(a, b): return a + b # Another script import math_operations result = math_operations.add(5, 3) This separates concerns, making code easier to manage and test independently .

Lambda functions in Python are anonymous, one-line functions defined using the lambda keyword. They differ from regular functions primarily in their simplicity and scope of use. Lambda functions can take any number of arguments but only have one expression, making them useful for small operations that are executed once, typically within other functions or as arguments to higher-order functions like map, filter, and reduce. Unlike regular functions, they do not have explicit names, making them less useful for complex operations .

The key differences between Python 2.x and Python 3.x include changes in syntax and behavior of built-in functions. For example, print is a statement in Python 2 and a function in Python 3, requiring parentheses. Unicode handling has improved in Python 3, which treats strings as Unicode by default, enhancing internationalization support. The division operator also behaves differently; '/' performs integer division in Python 2 but true division in Python 3. Such differences necessitate adaptations in code, which can affect readability, compatibility, and processing speed .

F-string literals, introduced in Python 3.6, offer a concise and readable way to embed expressions inside string literals using curly braces {}. They are prefixed with 'f' or 'F'. F-strings are advantageous for their simplicity and efficiency, allowing for inline expression evaluation with less syntax and faster performance than older formatting methods like % and str.format(). They also enhance readability by directly embedding the variables or expressions, which is particularly useful in debugging and dynamic content involving multiple variables. For example: name = "World" print(f"Hello, {name}!") This delivers more readable and maintainable code .

Inheritance in Python allows a class to inherit attributes and methods from another class, called the parent class, facilitating code reuse and logical organization of code. This reduces redundancy as shared functionality can be placed in a parent class and extended or overridden in child classes. For example, a Person class could hold common attributes such as name and age, while a subclass, Employee, could inherit these and add a salary attribute. This hierarchy allows for efficient code reuse and clearer organizational structure in applications .

File handling in Python is managed through built-in functions to open, read, write, and close files. The open() function opens a file and returns a corresponding file object. It requires at least a filename and can take a mode argument ('r', 'w', 'a', etc.). Built-in functions like read(), readline(), write(), and close() operate on this file object. An example code snippet: file = open('example.txt', 'w') file.write('Hello, World!') file.close() To read a file: file = open('example.txt', 'r') print(file.read()) file.close() Using with...as simplifies management by ensuring files are properly closed .

In Python, a class is defined using the class keyword, followed by the class name and a colon. It serves as a blueprint for creating objects. The 'self' parameter within class methods represents the instance of the class. It allows access to attributes and methods of the class in the current context, making it possible to manipulate or reference the instance's state separately for each object. Here’s a simple example: class Example: def __init__(self, value): self.value = value def display(self): print(self.value) The 'self' keyword distinguishes which object to operate on, enabling encapsulation and instance-level concurrency .

In Python, a shallow copy creates a new object but inserts references into it to the objects found in the original. Changes to the original objects are reflected in the shallow copy because both reference the same objects in memory. A deep copy, on the other hand, creates a new object and recursively copies all objects found in the original, producing fully independent objects. This has implications for memory management, as a deep copy consumes more memory, but it isolates changes in the data, avoiding side effects from modifying shared references .

You might also like