20.1 Programming Paradigms (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
Explain the key characteristics of low-level programming. In your answer, discuss the advantages and disadvantages of this approach compared to higher-level programming paradigms.
Low-level programming refers to programming that operates directly with a computer's hardware. Key characteristics include:
- Direct Memory Access: Programmers have explicit control over memory allocation and deallocation.
- Hardware Abstraction: Minimal or no abstraction layers between the code and the hardware. Instructions are often close to machine code.
- Use of Assembly Language: Often involves writing in assembly language, which is a symbolic representation of machine code.
- Manual Resource Management: Programmers are responsible for managing resources like memory and I/O devices.
Advantages of Low-Level Programming:
- Performance: Direct hardware control allows for highly optimized code, leading to superior performance.
- Resource Control: Fine-grained control over hardware resources is possible.
- Embedded Systems: Essential for programming embedded systems where resources are limited and performance is critical.
Disadvantages of Low-Level Programming:
- Complexity: Requires a deep understanding of computer architecture.
- Time-Consuming: Development is typically slower and more laborious.
- Error-Prone: Manual memory management increases the risk of errors like memory leaks and segmentation faults.
- Portability: Code is often highly specific to a particular hardware architecture, reducing portability.
Comparison to Higher-Level Programming: Higher-level programming paradigms (e.g., Python, Java) provide abstraction, making development faster and easier. However, they often sacrifice some performance and control over hardware resources. Low-level programming is preferred when performance and resource control are paramount, while higher-level programming is favored for rapid development and maintainability.
2.
Describe the key characteristics of the imperative (procedural) programming paradigm. Your answer should include a discussion of how programs are structured and executed.
The imperative (or procedural) programming paradigm is one of the oldest and most widely used approaches to software development. Its core characteristic is that a program is viewed as a sequence of commands that are executed in a specific order. Here's a breakdown of its key features:
- State and Variables: Imperative programming relies heavily on variables to store data and modify the program's state. The program's execution changes the values of these variables.
- Statements: Programs are composed of statements that perform actions, such as assigning values to variables, performing calculations, and controlling the flow of execution.
- Sequential Execution: Instructions are typically executed sequentially, one after the other, although control flow statements (like loops and conditional statements) allow for branching and repetition.
- Algorithms: Imperative programming often involves designing algorithms – step-by-step procedures – to solve problems. These algorithms are then translated into code.
- Emphasis on 'How': The paradigm focuses on *how* a task is accomplished – the precise steps the computer must take.
- Examples: Popular imperative languages include C, Pascal, and Fortran.
In essence, an imperative program is a set of instructions that tell the computer exactly what to do and in what order to do it. The program's state is constantly being updated as these instructions are executed.
3.
Explain the benefits of using object-oriented programming over procedural programming. Consider aspects such as code reusability, maintainability, and scalability. Provide specific examples to illustrate your points.
To model a vehicle management system using OOP, I would create the following classes:
Class Name | Attributes | Relationships |
Vehicle | make, model, year, registration number, mileage | Abstract class - base for other vehicle types. |
Car | fueltype, numberofdoors, enginesize | Inherits from Vehicle. |
Truck | cargocapacity, numberof_axles | Inherits from Vehicle. |
Motorcycle | enginecapacity, numberof_wheels | Inherits from Vehicle. |
Relationships:
- Inheritance: Car, Truck, and Motorcycle would inherit from the
Vehicle
class. This allows them to share common attributes (like make, model, year) and methods (like start, stop). - Composition/Aggregation: A
Vehicle
class might have a composition relationship with a Engine
class. The Vehicle
*contains* an Engine
. This would allow for more complex vehicle models in the future.
This design demonstrates abstraction by hiding the specific details of each vehicle type within its respective class. Polymorphism is demonstrated by the ability to treat any Vehicle
object as a Vehicle
and call a common method like start()
or accelerate()
. Encapsulation is achieved by controlling access to the vehicle's attributes through getter and setter methods.