KTU Python Programming Theory Questions
KTU Python Programming Theory Questions
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 .