0% found this document useful (0 votes)
133 views4 pages

KTU Python Programming Theory Questions

The document contains a comprehensive set of theory questions related to Python programming, covering topics such as strings, functions, lists, tuples, dictionaries, objects and classes, data modeling, inheritance, abstract classes, and exception handling. Each section includes explanations, programming tasks, and case studies to illustrate key concepts. It serves as a study guide for understanding fundamental Python programming principles and practices.

Uploaded by

yiu873209
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)
133 views4 pages

KTU Python Programming Theory Questions

The document contains a comprehensive set of theory questions related to Python programming, covering topics such as strings, functions, lists, tuples, dictionaries, objects and classes, data modeling, inheritance, abstract classes, and exception handling. Each section includes explanations, programming tasks, and case studies to illustrate key concepts. It serves as a study guide for understanding fundamental Python programming principles and practices.

Uploaded by

yiu873209
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

KTU Python Programming - Theory Questions

Strings and Text Files

1. Explain how strings are stored in memory in Python. What are immutable objects?

2. Write a Python program to count the number of vowels in a text file.

3. Explain the difference between string slicing and substring search. Give examples.

4. What is data encryption? Explain any one encryption technique with a Python example.

5. How can you convert a number to a string in different number systems (binary, octal, hex)? Give examples.

6. List and explain any five string methods in Python with examples.

7. Write a program to find the frequency of each word in a given text file.

8. Case Study: Given a large text file, design a program to extract and display the top 10 most frequent

words. Explain your approach.

Design with Functions

1. Explain the role of functions as abstraction mechanisms. Illustrate with an example.

2. Design a program using top-down design for a simple online billing system. Describe your approach.

3. Write a recursive function to compute the nth Fibonacci number.

4. What is meant by a program's namespace? How is it managed in Python?

5. Explain with examples: local, global, and nonlocal variables.

6. Define a higher-order function. Write a Python program using map() to square a list of numbers.

7. What is lambda in Python? How is it used with filter() and reduce() functions?

Lists

1. Explain any five built-in list functions in Python with examples.

2. Write a Python program to sort a list of student names using bubble sort.

3. What are nested lists? Write a program to find the sum of each row in a 2D list (matrix).

4. Explain slicing in lists with examples.

5. Compare linear search and binary search in terms of performance and implementation.

6. Write a Python program using list comprehension to generate all even numbers between 1 and 100.

7. Case Study: You are given a list of student records (name, age, marks). Design a program to sort them

based on marks and display the top 5 performers.


KTU Python Programming - Theory Questions

Tuples, Sets, Dates and Times

1. Differentiate between lists and tuples. When would you prefer tuples over lists?

2. Write a program to perform union, intersection, and difference operations on sets.

3. Write a Python program to calculate the number of days between two dates using datetime module.

4. Give an example where tuples can be used as dictionary keys but lists cannot. Explain why.

5. Case Study: Design a student attendance management system using dates and sets.

Dictionaries

1. Write a Python program to read a text file and create a dictionary of word frequencies.

2. Explain reverse lookup in dictionaries with an example.

3. Differentiate between dictionary literals and dictionary constructors. Write code to demonstrate adding,

removing, and modifying dictionary entries.

Objects and Classes

1. Define a class in Python to represent a Book with attributes like title, author, and price. Write methods to

display details and apply a discount.

2. Differentiate between a class and an object with suitable examples.

3. What is the purpose of the __init__() method in Python classes? Explain with an example.

4. Write a Python program to model a Student class with instance variables name, roll_no, and marks.

Include appropriate methods.

5. Explain the terms accessor and mutator methods. Give code examples of each.

6. What is meant by encapsulation? How does Python support it using classes?

7. How are instance variables and class variables different in Python? Illustrate with code.

Data Modeling and Class Design

1. Model a class BankAccount with attributes like account number, account holder, and balance. Implement

methods to deposit and withdraw.

2. Case Study: You are designing a class-based system for a Library. Identify the objects, attributes, and

methods involved.
KTU Python Programming - Theory Questions

3. Write a program to model a Car class and a ServiceRecord class where each car has a list of service

records. Demonstrate composition.

4. Design a class for an online shopping item with methods to calculate discount and tax.

5. What are the advantages of designing programs using classes and objects?

Inheritance and Polymorphism

1. What is inheritance? How is it implemented in Python? Give an example.

2. Create a base class Employee and a derived class Manager. Add a method in Manager to calculate

bonus. Demonstrate polymorphism.

3. Write a Python program to show method overriding with inheritance.

4. Explain the super() function in Python with an example.

5. Differentiate between single and multiple inheritance with examples.

6. How is polymorphism achieved in Python? Illustrate using class hierarchy.

7. Case Study: Design a vehicle management system using a base class Vehicle and subclasses Car, Bike,

and Truck. Show method overriding.

Abstract Classes and Interfaces

1. What is an abstract class in Python? How do you implement one using the abc module?

2. Write a Python program using an abstract class Shape with an abstract method area(). Create subclasses

Circle and Rectangle.

3. Explain the difference between abstract classes and interfaces. How can interfaces be simulated in

Python?

4. Can an abstract class have constructors and implemented methods in Python? Justify with example.

5. What happens if a subclass does not override all abstract methods of its parent class?

Exception Handling

1. What is exception handling? Why is it important in object-oriented programming?

2. Write a program that handles a divide-by-zero error using a try-except block.

3. How can you handle multiple exceptions in Python? Give an example.


KTU Python Programming - Theory Questions

4. Write a program to take input from the user and raise a ValueError if the input is not an integer. Handle it

appropriately.

5. What is the difference between except, else, and finally blocks in Python exception handling? Explain with

an example.

6. Case Study: Design a student result program that handles exceptions like invalid marks entry and

division-by-zero when calculating averages.

Common questions

Powered by AI

Encapsulation in Python is supported through private attributes, using naming conventions like a single underscore (_var) for protected and double underscores (__var) for private access. This mechanism restricts direct access to certain components of an object, ensuring controlled access and modification only through methods. This simplifies maintenance, enhances modularity, and prevents inadvertent data corruption, crucial for robust software design .

Tuples are preferred over lists when the data is meant to remain constant, as they are immutable. For instance, using tuples as keys in dictionaries is ideal since they are hashable. Example: a dictionary with tuples as keys, {(1,2): 'point1', (3,4): 'point2'}. In contrast, lists are mutable and allow item modification, suitable for datasets that might change .

Immutable objects in Python are those whose state cannot be modified after they are created. Strings in Python are immutable, meaning once a string is created, the characters within it cannot be changed. Strings are stored in memory as sequences of characters in contiguous locations, and any modification results in the creation of a new string object instead of altering the existing one .

Python's exception handling uses try to wrap code likely to cause an exception, except to catch exceptions, else to execute code if no exceptions occur, and finally to execute cleanup actions regardless of exceptions. Example: try: result=10/num; except ZeroDivisionError: print('Division by zero'); else: print('No exception'); finally: print('Cleanup'). This ensures robust programs that can recover or gracefully exit from errors .

Abstract classes in Python, defined using the abc module, can have constructors and implemented methods to provide shared base functionality to subclasses. Such methods offer default behavior or utility functions that do not need to be overridden. Example: from abc import ABC,abstractmethod; class Shape(ABC): def __init__(self,color): self.color=color; def info(self): return f'Shape color: {self.color}'; subclasses like Circle inherit these capabilities, adding specific details while reusing common structure .

You can convert numbers to strings in different numerical systems using Python's built-in functions: bin(), oct(), and hex(). For example, to convert the decimal number 10: Use bin(10) to get '0b1010' (binary), oct(10) to get '0o12' (octal), and hex(10) to get '0xa' (hexadecimal).

Data encryption is the process of converting plaintext into ciphertext to prevent unauthorized access. One common technique is the Caesar Cipher, which shifts letters by a fixed number. For example, in Python, you can encrypt a message by shifting each character's Unicode code point: msg='hello'; encrypted=''.join(chr((ord(ch)-97+3)%26+97) for ch in msg) results in 'khoor' as encrypted text. This simple method exemplifies the broader concept of encrypting data for security .

Single inheritance means a class derives from one base class, providing a linear hierarchy. E.g., class Derived(Base):... Multiple inheritance allows a class to inherit from multiple classes, combining their features. E.g., class Derived(Base1, Base2):... While powerful, multiple inheritance can introduce complexity like the Diamond Problem, resolved in Python using the Method Resolution Order (MRO) which follows the C3 linearization algorithm .

Division-by-zero errors in Python are handled using try-except blocks to catch the ZeroDivisionError exception. This is significant because unhandled exceptions can cause program crashes, leading to a poor user experience and potential data loss. For instance, try: result=10/0; except ZeroDivisionError: print('Cannot divide by zero') ensures the program can continue or terminate gracefully, maintaining reliability and data integrity .

The super() function allows subclasses to access methods from their superclass without direct reference, enabling method overriding more flexibly. It helps in maintaining code consistency and supports multiple inheritance by respecting the MRO. Example: class Base: def __init__(self): self.name='Base'; class Derived(Base): def __init__(self): super().__init__(); self.name='Derived'. Here, super() ensures Base's __init__() is correctly invoked in Derived .

You might also like