Python Exam Questions for B.Tech CSE
Python Exam Questions for B.Tech CSE
Python does not support method overloading like some other languages because it allows functions to be defined with the same name but with dynamic argument handling. Instead, it uses default arguments or *args to handle different numbers or types of inputs. This flexibility reduces redundancy but requires careful implementation to avoid runtime errors due to incorrect argument handling, influencing how developers design functions for robustness and versatility .
Mutable data types in Python, such as lists and dictionaries, allow modification of their content without changing their identity. For example, a list [1, 2, 3] can have its elements changed, appended, or removed. Immutable data types, on the other hand, like tuples and strings, cannot be altered once created. Any change would involve creating a new object. For instance, any modification to a tuple would require creating a new tuple object .
To create a Circle class in Python, define a constructor (__init__) that initializes the radius attribute. Then, implement methods to calculate the area (using πr²) and the circumference (using 2πr). Encapsulation is important here for maintaining the integrity of the circle's data, allowing manipulation through defined interfaces, and protecting internal state from unintended external modifications, enhancing code reliability and reusability .
NumPy arrays are more efficient than Python lists because they store elements in contiguous blocks of memory, allowing faster computations through vectorized operations with optimized C/Fortran libraries, reducing loop overhead. This efficiency is crucial for large-scale data analysis tasks, enabling faster processing and the application of mathematical operations on entire arrays without explicit loops, greatly enhancing performance in scientific computing and data manipulation tasks .
The PIL library offers functions like 'resize()' to alter image dimensions, 'rotate()' to change orientation, and 'convert()' to modify image modes (e.g., color to grayscale). These functions enable flexible and straightforward image manipulations, essential for preparing images for further analysis or visualization, enhancing their usability in automated image-processing pipelines .
OpenCV is a more comprehensive and efficient library for image processing, supporting real-time operations and a wide array of features including computer vision algorithms. PIL (or its successor Pillow) is simpler, providing basic image processing capabilities like opening, manipulating, and saving image files. OpenCV is suited for demanding applications like motion detection, whereas PIL is adequate for basic tasks like image format conversions .
The 'remove()' function in Python's lists deletes the first occurrence of a specified value, e.g., in [1, 2, 3], lst.remove(2) removes 2. 'Pop()' removes an item at a specified index and returns it, e.g., in [1, 2, 3], lst.pop(1) removes and returns 2. 'Remove()' is used when the value is known, while 'pop()' is useful when direct index access is needed .
'While' loops in Python are condition-based, repeatedly executing a block of code as long as a condition remains true, suitable for scenarios where the number of iterations isn't known upfront. 'For' loops iterate over items of a sequence (like lists or strings), ideal when the number of iterations is predetermined. This makes 'for' loops generally cleaner and more predictable for iterating over sequences, while 'while' loops offer more flexibility for conditions that might vary .
Pandas DataFrames offer tabular data representation with labeled axes, providing powerful data manipulation and analysis capabilities. For example, creating a DataFrame for students with columns Name, Age, Marks allows operations like filtering students with Marks > 70 and computing average marks seamlessly using built-in functions. This structure facilitates complex data transformations and insights extraction through intuitive syntax .
Broadcasting in NumPy allows arithmetic operations between arrays of different shapes by 'stretching' the smaller array across the larger one without copying data. This enables concise and efficient computation, allowing operations like adding a scalar to a matrix without explicit loops. It simplifies code and improves computational performance, crucial for large-scale scientific computations .