Lecture 12 - List Comprehension, Functions as Objects, Testing, Debugging Flashcards
Explain what is Testing
The process of running a program with specific inputs to determine if it behaves as expected according to its specification.
Explain what is Debugging
The process of identifying and fixing errors (bugs) in a program that is not functioning as intended.
Explain what is a Test Suite
A collection of test cases designed to evaluate the correctness of a program or a part of it.
Explain what is Unit Testing
Testing individual components or units of a program, typically functions, in isolation.
Explain the key difference between using a traditional for loop to create a new list and using a list comprehension for the same purpose. What is a primary advantage of list comprehensions?
A traditional for loop to create a new list typically involves initialising an empty list, iterating through an existing iterable, performing an operation on each element, and appending the result to the new list. A list comprehension achieves the same in a more concise, single-line expression. A primary advantage of list comprehensions is often improved code readability for these common list creation patterns.
Describe the role of the conditional if clause within a list comprehension. Provide a brief example of a scenario where you might use it.
The conditional if clause in a list comprehension acts as a filter, determining which elements from the iterable will have the expression applied to them and be included in the new list. For example, you might use it to create a list containing only the even numbers squared from a larger list of integers.
In Python, functions are considered first-class objects. What does this mean in practical terms for how you can treat and use functions in your code?
Functions being first-class objects means they can be treated like any other data type in Python: they can be assigned to variables, passed as arguments to other functions, and returned as values from functions. This flexibility enables powerful programming paradigms like higher-order functions and closures.
Explain the concept of a function returning another function. Why might this be a useful programming technique, even though the immediate benefit isn’t always obvious?
A function returning another function means that the outer function’s execution results in the creation and delivery of an inner function object. This can be useful for creating functions with specific behaviour based on initial parameters, achieving partial execution, or encapsulating functionality within a certain scope.
What is a default parameter in a Python function definition? How does it affect how the function can be called?
A default parameter in a Python function definition is a parameter that has a predefined value assigned to it. If a value for that parameter is not provided when the function is called, the default value is automatically used. This allows functions to be called with fewer arguments in common cases.
When defining a Python function, what is the rule regarding the order of default parameters in the parameter list? Why is this rule in place?
In a Python function definition, default parameters must always be placed at the end of the parameter list, after any parameters without default values. This is because when calling the function with positional arguments, Python assigns values to parameters in order, and it needs to know where the optional parameters begin.
Explain the main principle behind black box testing. What information is used to create test cases in this approach?
Black box testing is a testing approach where the internal structure or implementation of the code being tested is unknown. Test cases are designed solely based on the specification or requirements of the software, focusing on the inputs and expected outputs without regard to how the code works internally.
What is glass box testing, and how does it differ from black box testing in its approach to generating test cases? What is a potential drawback of relying solely on glass box testing?
Glass box testing involves examining the internal structure and code of the program to design test cases. Unlike black box testing, it aims to ensure that all possible paths and conditions within the code are tested. A potential drawback is that it might not uncover issues related to the specification or missing functionalities if the tester’s understanding is tied to the implementation.
Describe the “bisection method” as a debugging technique using print statements. How does it help narrow down the location of a bug?
The “bisection method” in debugging involves strategically placing print statements at roughly the midpoint of a section of code you suspect contains a bug. By examining the values of variables at that point, you can determine whether the issue lies before or after that midpoint, effectively halving the search space with each step.
What is regression testing, and why is it an important practice during the software development and debugging process?
Regression testing is the practice of re-running previously successful test cases after code changes (such as bug fixes or new features) have been made. Its importance lies in ensuring that the changes have not introduced new bugs or caused previously working functionality to fail.
Explain what is Integration Testing
Testing the interaction and communication between different modules or units of a program when they are combined.
Explain what is Black Box Testing
A testing approach where the tester is unaware of the internal implementation of the software and designs test cases based only on the specification and expected behaviour.
Explain what is Glass Box Testing
A testing approach where the tester has knowledge of the internal structure and code of the software and designs test cases to exercise specific paths and conditions within the code.
Explain what is Path-Complete Testing
A glass box testing ideal where every possible execution path through the code is tested at least once. This is often impractical for complex programs.
Explain what is a Bug
An error, flaw, or defect in a program that causes it to produce an incorrect or unexpected result, or to behave in unintended ways.
Explain what is
Explain what is a Covert Bug
A bug that does not have an obvious manifestation; the program may run to completion without any apparent issues but produce an incorrect result.
Explain what is an Overt Bug
A bug that has an obvious and immediate manifestation, such as a program crash or incorrect output.
Explain what is a Persistent Bug
A bug that occurs every time the program is run with the same inputs under the same conditions.
Explain what is an Intermittent Bug
A bug that occurs only sporadically, even when the program is run with the same inputs and seemingly under the same conditions.
Explain what is Defensive Programming
Writing code with the aim of preventing errors and making it easier to test and debug, often involving clear specifications, modularity, and input validation.