Python Mid Term Exam Questions 2024
Python Mid Term Exam Questions 2024
Object-oriented programming (OOP) in a course titled 'Object Oriented Programming using Python' focuses on organizing code into objects that combine data and behavior, boosting modularity, code reuse, and scalability. OOP concepts like classes, inheritance, and encapsulation help in building complex applications. For example, a simple Circle class may include attributes like radius and methods for computing area and perimeter, allowing representation of circle objects with encapsulated properties and behaviors. Such structure aids clarity, maintainability, and can form foundations for building upon with further related features .
A Python program identifies consonants in a string by iterating over each character and checking if it is a consonant, typically defined as alphabetic characters that are not vowels (a, e, i, o, u). The 'count_consonants' function iterates through the string using a for loop, employing an if condition to verify if each character is an English consonant, incrementing a count variable for each consonant. This logic ensures accurate counting of consonants, regardless of the string content .
Tuples in Python provide memory and performance efficiency because they are immutable, meaning their size and data do not change after creation, allowing Python to optimize memory allocation. This immutability also means operations like insertion or deletion, which require reallocating memory, are avoided, leading to faster access time. A suitable scenario for using a tuple over a list is when representing fixed collections of items, such as the days of the week or a coordinate pair, where the elements should remain constant throughout the program .
Decorators in Python enhance code reusability by allowing the separation of cross-cutting concerns from core functionality. This separation means functionalities like logging, authentication, or caching can be applied across different functions without modifying their codebase. A logging decorator can wrap multiple functions to log their execution without altering their logic. For example, a logging function can be defined as a decorator that logs before and after function execution, and then applied to any function needing such logging, thus promoting DRY (Don't Repeat Yourself) principles .
Lambda functions in Python are anonymous functions defined using the lambda keyword, which can take multiple arguments but can contain only a single expression. Unlike regular functions defined with the def keyword, lambda functions have no name and are typically used for short-term operations. For example, a lambda function to filter out even numbers from a list would be: filtered_even = list(filter(lambda x: x % 2 != 0, [1, 2, 3, 4, 5, 6])). This filters the numbers 1, 3, and 5, which are odd, out of the list .
Lists in Python are ordered and mutable collections, useful for dynamic data storage where modification of contents is needed. Tuples are similar but immutable, making them suited for fixed data. Sets are unordered collections of unique elements, useful for operations like membership tests and eliminations of duplicates. Dictionaries store data as key-value pairs, ideal for quickly accessing data via unique keys. Each structure offers unique strengths: lists for flexibility, tuples for consistency and performance, sets for uniqueness, and dictionaries for key-based data retrieval .
Decorators in Python are a design pattern that allows additional functionality to be wrapped around existing functions. They are applied by prefixing a function definition with a decorator name using the @ syntax, e.g., @log_decorator before a function to ensure logging. A logging decorator can be created to log the input and output or time of function execution, and then applied to multiple functions to standardize logging across the application without altering individual function bodies, thus enforcing consistent logging practices .
The *args and **kwargs in Python enhance function flexibility by allowing variable numbers of positional and keyword arguments, respectively. *args can capture additional positional arguments, while **kwargs captures named arguments as a dictionary. For example, def function(*args): would allow a call like function(1, 2, 3), with args being a tuple (1, 2, 3). Similarly, def function(**kwargs): could handle function(a=1, b=2), with kwargs being {'a': 1, 'b': 2}, allowing dynamic and flexible argument passing in functions .
The recursive approach in Python is advantageous for problems naturally expressed in terms of smaller instances of the same problem, like the Fibonacci sequence, where recursion elegantly expresses the relation F(n) = F(n-1) + F(n-2). For example, a recursive function might return the sum of calls to calculate the previous two terms. However, recursion can be inefficient due to excessive function calls and potential stack overflow. Iterators, on the other hand, provide efficiency by maintaining the state across loop iterations without additional function calls, making them preferred for large data processing. Generating Fibonacci via an iterator avoids overhead by yielding each term sequentially .
Excluding calculators in examinations like the FAITH Odd Semester Mid Term Examination emphasizes cognitive skills such as mental arithmetic, problem-solving, and logical reasoning. This restriction encourages students to develop a deeper understanding of mathematical concepts and enhances their ability to perform calculations manually. Analyzing and verifying results without reliance on digital tools may also improve student's error-checking and estimation abilities, fostering a comprehensive grasp of underlying principles rather than relying on computational shortcuts .