8

Computer Programming and Languages

1. Briefly describe computer program. Also discuss its characteristics.

Ans.: The computer has emerged as the most useful machine in recent times. It can perform a variety of tasks like receiving data, processing them, and producing useful results. However, being a machine, it cannot perform on its own and needs to be instructed to perform even a simple task. It works on a set of instructions called computer program, which clearly specify the ways to carry out a task. An analogy of this may be thought of as the instructions given by the manager or team leader to his/her team. The team members follow those instructions and accordingly perform their duties. Similarly, a computer also takes instructions in the form of computer programs to carry out the requested task.

A program consists of a series of instructions that a computer processes to perform the required operation. In addition, it also includes some fixed data, required to perform the instructions, and the process of defining those instructions and data. Thus, in order to design a program, a programmer must determine the following three basic rudiments:

  1. The instructions to be performed
  2. The order in which those instructions are to be performed
  3. The data required to perform those instructions

To perform a task using a program, a programmer has to consider various inputs of the program along with the process, which is required to convert the input into desired output. Suppose we want to calculate the sum of two numbers, A and B, and store the sum in C. Here A and B are the inputs, addition is the process, and C is the output of the program (Figure 8.1).

images

Figure 8.1 A Program Performing a Task

In addition, a program should be written in such a manner that it is easier to understand the underlying logic. A few important characteristics that a computer program should possess are as follows:

images Portability: It refers to the ability of an application to run on different platforms (operating systems) with or without minimal changes. Due to rapid development in the hardware and the software, nowadays platform change is a common phenomenon. Hence, if a program is developed for a particular platform then the life span of the program is severely affected.
images Readability: The program should be written in such a way that it makes other programmers or users to follow the logic of the program without much effort. If a program is written structurally, it helps the programmers to understand their own program in a better way. Even if some computational efficiency needs to be sacrificed for better readability, it is advisable to use a more user-friendly approach, unless the application's processing is of utmost importance.
images Efficiency: Every program requires certain processing time and memory to process the instructions and data. As you must have realized, processing power and memory are the most precious resources of a computer, a program should be laid out in such a manner that it utilizes the least amount of memory and processing time.
images Structural: To develop a program, the task must be broken down into a number of subtasks. These subtasks are developed independently, and each subtask is able to perform the assigned job without the help of any other subtask. If a program is developed structurally, the program not only becomes more readable, but the testing and documentation process also gets easier.
images Flexibility: A program should be flexible enough to handle most of the changes without having to rewrite the entire program. Most of the programs are developed for a certain period and they require modifications from time to time. For example, in case of payroll management, as the time progresses, some employees may leave the company while some others may join. Hence, the payroll application should be flexible enough to incorporate all the changes without having to reconstruct the entire application.
images Generality: Apart from flexibility, the program should also be general. By generality, we mean that if a program is developed for a particular task then it should also be used for all similar tasks of the same domain. For example, if a program is developed for a particular organization then it should suit all the other similar organizations.
images Documentation: It is one of the most important components of an application development. Even if a program is developed following the best programming practices, it will be rendered useless if the end user is not able to fully utilize the functionality of the application. A well-documented application is also useful for other programmers because even in the absence of the author, they can understand it.

2. Explain program development cycle with the help of a block diagram.

Ans.: Before starting the process of writing a program (coding), the programmer has to determine the problem that needs to be solved. There are different approaches to problem solving. One such common approach is to use the program development cycle where the number of steps may vary according to the person who has formalized the development. Often the process runs in a loop, for example, as the current process is completed, new demands appear and the development process commences again. Figure 8.2 illustrates the development cycle of a program that includes many phases, which are described as follows:

images Problem analysis: The problem is analyzed precisely and completely. Based on understanding, the developer knows about the scope within which the problem needs to be developed.

images

Figure 8.2 Program Development Cycle

images Task analysis: After analyzing the problem, the developer needs to develop various solutions to solve the given problem. From these solutions, the optimum solution is chosen, which can solve the problem comfortably and economically.
images Algorithm development: After selecting the appropriate solution, an algorithm is developed to depict the basic logic of the selected solution. It depicts the solution in logical steps (sequence of instructions). Further, it is represented by flowcharts, decision tables, and pseudocodes. These tools make program logic clear and they eventually help in coding.
images Algorithm testing: Before converting the algorithm into actual code, it should be checked for accuracy. The main purpose of checking the algorithm is to identify major logical errors at an early stage, because logical errors are often difficult to detect and correct at later stages. The testing also ensures that the algorithm is a ‘true’ one and it should work for both normal as well as unusual data.
images Coding: After meeting all the design considerations, the actual coding of the program takes place in the chosen programming language. Depending upon the application domain and available resources, a program can be written by using computer languages of different levels such as machine, assembly, or high-level languages (HLLs).
images Testing and debugging: It is common for the initial program code to contain errors. A program compiler and programmer-designed test data machine tests the code for syntax errors. The results obtained are compared with results calculated manually from these test data. Depending upon the complexity of the program, several rounds of testing may be required.
images Documentation: Once the program is free from all the errors, it is the duty of the program developers to ensure that the program is supported by suitable documentation. These documents should be supplied to the program users. Documenting a program enables the user to operate the program correctly. It also enables other persons to understand the program clearly so that it may, if necessary, be modified or corrected by someone other than the original programmer.
images Implementation: After documentation, the program is installed on the end user's machine and the user is also provided with all the essential documents in order to understand how the program works. The implementation can be viewed as the final testing because only after using the program, the user can point out the drawbacks (if any) and report them to the developers. Based on the feedback from users, the programmers can modify or enhance the program.
images Maintenance and enhancement: After the program is implemented, it should be properly maintained by taking care of the changing requirements of its users and system. The program should be regularly enhanced by adding additional capabilities. This phase is also concerned with detecting and fixing the errors, which were missed in testing phase. Since this step generates the user feedback, the programming cycle continues as the program is modified or reconstructed to meet the changing needs.

3. Define algorithm. List its properties.

Ans.: Algorithm is defined as a finite sequence of explicit instructions that when provided with a set of input values produces an output and then terminates. To be an algorithm, the steps must be unambiguous and after a finite number of steps, the solution of the problem should be achieved. However, it can have steps that repeat (iterate) or require decisions (logic and comparison) until the task is completed.

Different algorithms may accomplish the same task, with a different set of instructions, in more or less the same time, space, and efforts. For example, two different recipes for preparing tea, one ‘add the sugar’ while ‘boiling the water’ and the other ‘after boiling the water’ produce the same result. However, performing an algorithm correctly does not guarantee a solution, if the algorithm is flawed or not appropriate to the context. For example, preparing the tea algorithm will fail if there are no tea leaves present; even if all the motions of preparing the tea are performed as if the tea leaves were there. Once an algorithm has been designed, it can be represented as flowchart, pseudocode, or decision table, which is further expressed in a programming language to develop computer programs. Some desirable properties of an algorithm are as follows:

images There must be no ambiguity in any instruction.
images There should not be any uncertainty about which instruction is to be executed next.
images Its description must be finite.
images It cannot be open-ended.
images It should terminate after a finite number of steps.
images It must be general enough to deal with any contingency.

4. Define flowchart. List some important reasons for using flowcharts.

Ans.: Flowchart is a pictorial representation of an algorithm in which the steps are drawn in the form of different shapes of boxes and the logical flow is indicated by interconnecting arrows. The boxes represent operations and the arrows represent the sequence in which the operations are implemented. The primary purpose of a flowchart is to help the programmer in understanding the logic of the program. Therefore, it is always not necessary to include all the required steps in detail. It outlines the general procedure. Since it provides an alternative, visual way of representing the information flow in a program, program developers often find them very valuable.

It helps to clarify how things are currently working and how they could be improved. It also assists in finding the key elements of a process by drawing clear lines between the end of one process and the start of next one. Developing a flowchart establishes a common understanding among the participants about the process. It helps in revealing redundant or misplaced steps. It also helps in establishing important areas for monitoring or data collection and to identify areas for improvement or increase in efficiency. The reasons for using the flowchart as a problem-solving tool are given as follows:

images Makes logic clear: The main advantage of using a flowchart to plan a task is that it provides a pictorial representation of the task, which makes the logic easier to follow. The symbols are connected in such a way that they show the movement (flow) of information through the system visibly. The steps and how each step is connected to the next can be clearly seen. Even less experienced personnel can trace the actions represented by a flowchart, which is ideal for visualizing fundamental control structures employed in computer programming.
images Communication: Being a graphical representation of a problem-solving logic, a flowchart is a better way of communicating the logic of a system to all concerned. The diagrammatical representation of logic is easier to communicate to all the interested parties as compared to actual program code as the users may not be aware of all the programming techniques and jargons.
images Effective analysis: With the help of a flowchart, the problem can be analyzed in an effective way. This is because the analyzing duties of the programmers can be delegated to other persons, who may or may not know the programming techniques, as they have a broad idea about the logic. Being outsiders, they often tend to test and analyze the logic in an unbiased manner.
images Useful in coding: A flowchart acts as a guide or blueprint during the analysis and program development phase. Once the flowcharts are ready, the programmers can plan the coding process effectively as they know where to begin and where to end, making sure that no steps are omitted. As a result, error-free programs are developed in HLLs and that too at a faster rate.
images Proper testing and debugging: By nature, a flowchart helps in detecting the errors in a program, as the developers know exactly what the logic should do. Developers can test various data for a process so that the program can handle every contingency.
images Appropriate documentation: A flowchart serves as a good program documentation tool. Since normally the programs are developed for novice users, they can take the help of the program documentation to know what the program actually does and how to use the program.

5. List some standard symbols used in flowchart with their description.

Ans.: Flowcharts can be compared with the blueprint of a building. Just as an architect draws a blueprint before starting the construction of a building, a programmer draws a flowchart before writing a computer program. As in the case of the drawing of a blueprint, the flowchart is drawn according to defined rules and using standard flowchart symbols prescribed by American National Standard Institute (ANSI). Some standard symbols that are frequently required for flowcharts are shown in Table 8.1.

Table 8.1 Flowchart Symbols

 

Symbol Symbol Name Description
images Flow lines These lines are used to connect symbols. These lines indicate the sequence of steps and the direction of flow of control.
images Terminal This symbol is used to represent the beginning (start), the termination (end), or halt (pause) in the program logic.
images Input/output This symbol represents information entering or leaving the system such as customer order (input) and servicing (output).
images Processing This symbol is used for representing arithmetic and data movement instructions. It can represent a single step (e.g., add two cups of flour), or an entire sub-process (e.g., make bread) within a larger process.
images Decision This symbol denotes a decision (or branch) to be made. The program should continue along one of the two routes (IF/ELSE). This symbol has one entry and two exit paths. The path chosen depends on whether the answer to a question is yes or no.
images Connector This symbol is used to join different flow lines.
images Off-page connector This symbol is used to indicate that the flowchart continues on the next page.
images Document This symbol is used to represent a paper document produced during the flowchart process.
images Annotation This symbol is used to provide additional information about another flowchart symbol. The content may be in the form of descriptive comments, remarks, or explanatory notes.
images Manual input This symbol represents input to be given by a developer/programmer.
images Manual operation This symbol shows that the process has to be done by a developer/programmer.
images Online storage This symbol represents the online data storage such as hard disks, magnetic drums, or other storage devices.
images Offline storage This symbol represents the offline data storage such as sales on OCR and data on punched cards.
images Communication link This symbol is used to represent data received or to be transmitted from an external system.
images Magnetic disk This symbol is used to represent read from or write to a magnetic disk.

 

6. Write any three disadvantages of flowchart.

Ans.: A flowchart can be used for designing the basic concept of the program in pictorial form but cannot be used for programming purposes. Some of the limitations of the flowchart are given as follows:

images Complex: The major disadvantage in using the flowchart is that when a program is very large, it may continue for many pages, making them hard to follow. It tends to get large very quickly and it is difficult to follow the represented process. It is also very laborious to draw a flowchart for a large program. You can very well imagine the nightmare when a flowchart is to be developed for a program consisting of thousands of statements.
images Costly: Drawing a flowchart is viable only if the problem-solving logic is straightforward and not very lengthy. However, if a flowchart is to be drawn for a huge application, the time and cost factor of program development may get out of proportion, making it a costly affair.
images Difficult to modify: Due to its symbolic nature, any changes or modification to a flowchart usually requires redrawing the entire logic again, and redrawing a complex flowchart is not a simple task. It is not easy to draw thousands of flow lines and symbols along with proper spacing, especially for a large complex program.

7. What is a decision table? Why it is preferred over flowcharts?

Ans.: A decision table is a way of representing an algorithm in a tabular form. It contains all the possible conditions for a specific problem and the corresponding results using condition rules that connect conditions with results. It consists of rows and columns, divided into following four separate quadrants (Figure 8.3):

  1. Conditions: It contains a list of all the possible conditions pertaining to a problem.
  2. Condition alternatives: It contains the condition rules (set of possible values for each condition) of alternatives.
  3. Actions: It contains actions, which can be a procedure or operation to be performed.
  4. Action entries: It contains the action rules, which specify the actions to be performed on the basis of the set of condition alternatives corresponding to that action entry.

images

Figure 8.3 Four Quadrants of a Decision Table

The decision table is preferred over the flowchart because of the following benefits:

images It is easier to draw and modify a decision table as compared to flowchart.
images For the algorithms having complex problem-solving logic, a flowchart may take a number of pages; however, using decision tables, such algorithms can be represented in a compact form.
images Each column in a decision table represents a unique combination of conditions and their corresponding actions. This helps to better understand the decision logic of the program.

8. Write a short note on pseudocode?

Ans.: Pseudocode is made up of two words: pseudo and code. Pseudo means imitation and code refers to instructions, written in a programming language. As the name suggests, pseudocode is not a real programming code, but it models and may even look like a programming code. It is a generic way of describing an algorithm without using any specific programming-language-related notations. In simple terms, it is an outline of a program, written in a form that can be easily converted into actual programming statements. It uses plain English statements rather than symbols to represent the processes in a computer program. It is also known as PDL (Program Design Language), as it emphasizes more on the design aspect of a computer program or structured English, because usually pseudocode instructions are written in normal English, but in a structured way.

It allows the programmer to focus on the steps required to solve a program rather than on how to use the computer language. Some of the most significant benefits of pseudocode are as follows:

images Since it is language independent, it can be used by most programmers. It allows the developer to express the problem logic in plain natural language.
images It is easier to develop a program from a pseudocode rather than from a flowchart or decision table. Programmers do not have to think about syntaxes; they simply have to concentrate on the underlying logic. The focus is on the steps to solve a problem rather than on how to use the computer language.
images The words and phrases used in pseudocode are in line with basic computer operations. This simplifies the translation from the pseudocode to a specific programming language.
images Unlike flowcharts, it is compact and does not tend to run over many pages. Its simple structure and readability makes it easier to modify.

Although it is a very simple mechanism to simplify problem-solving logic, it has the following limitations:

images It does not provide visual representation of the program's logic.
images There are no accepted standards for writing pseudocode. Programmers use their own style of writing pseudocode.
images It is quite difficult for the beginners to write a pseudocode as compared to draw a flowchart.

9. Define programming paradigm. Explain in detail on categories of programming.

Ans.: Programming paradigm refers to the approach used to develop the program for solving a problem. It provides (and determines) the view that the programmer has of the execution of the program. Broadly, programming can be classified into the following three categories:

Unstructured Programming

It refers to writing small and simple programs consisting of only one main program. All the actions such as providing input, processing, and displaying output are done within one program only. This style of programming is generally restricted for developing a small application but if the application becomes large then it poses real difficulties in terms of clarity of the code, modifiability, and ease of use. Although this type of programming style is not recommended, still most programmers start to learn programming using this technique.

Structured Programming

In this type of programming, a program is broken down into small independent tasks that are small enough to be understood easily, without having to understand the whole program at once. Each task has its own functionality and performs a specific part of the actual processing. These tasks are developed independently, and each task can carry out a specified function on its own. When these tasks are completed, they are combined together to solve the problem. In this way, designers map out the large-scale structure of a program in terms of smaller operations, implement and test the smaller operations, and then tie them together into a whole program. Structured programming can be performed in the following two ways:

  1. Procedural programming: This programming has a single program that is divided into small segments called procedures (also known as functions, routines, or subroutines). From the main or controlling procedure, a procedure call is used to invoke the required procedure. After the sequence is processed, the flow of control continues from where the call was made. The main program coordinates calls to procedures and hands over appropriate data as parameters. The data are processed by the procedures and once the program has finished, the resultant data are displayed. The programs coded with procedural paradigms usually fit into a single code file, and thus they are meant only for relatively smaller programs.
  2. Modular programming: In this, large programs are broken down into a number of smaller program units known as modules. Each module is designed to perform a specific function. It is to be noted that each module in itself is a small program that can be compiled and tested by an individual program and all modules are combined to build the whole program.

Object-oriented Programming (OOP)

It is a style of computer programming which promotes building of independent pieces of code that interact with each other. It is an evolutionary form of modular programming, with more formal rules. It allows pieces of programming code to be reused and interchanged between programs. In OOP, programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of a hierarchy of classes united by way of inheritance relationships. In such programs, classes are generally viewed as static, whereas objects typically have a much more dynamic nature, which is encouraged by the existence of polymorphism.

10. Describe in detail all the features of OOP.

Ans.: OOP paradigm emphasizes on many aspects such as objects, abstraction, inheritance, classes, encapsulation, and polymorphism.

Object

It is like a black box that contains code (sequence of computer instructions) and data (information that the instructions operate on). Traditionally, code and data have been kept apart. For example, in a procedural language like C, units of code are called procedures, while units of data are called structures. Procedures and structures are not formally connected in C. A procedure can operate on more than one type of structure, and more than one procedure can operate on the same structure. As a procedural program grows in size, the network of interaction between procedures and data becomes increasingly complex and hard to manage. However, in OOP, code and data are merged into a single item named as object. Objects can be combined into structured networks to form a complete program, similar to how the pieces in a puzzle fit together to create a picture. The data in an object specify its attributes, while the code specifies the actions. For example, a car (object) has attributes like four wheels and five gears, and actions like accelerating and changing gears.

Class

It is an abstraction that captures the common structure and common behaviour of a set of objects. It defines an object or group of objects and how the objects should behave. In other words, it is a template or blueprint that defines the characteristics of an object and describes how the object should look and behave. All objects in a given class are identical in form and behaviour but contain different data in their variables. It means that a class does not represent an object; it represents all the information a typical object should have as well as all the methods it should have.

In the real world, there are many objects of the same kind. For example, the object car falls under the class vehicle. It has some attributes (gears and wheels) and actions (change gears and apply brakes) that are common with other vehicle objects like bus or truck. However, each vehicle's attribute is independent and can be different from that of the other one. For example, a car has four wheels while a bus can have six wheels (Figure 8.4).

images

Figure 8.4 Class and Objects

Abstraction

It is the ability of a program to ignore some aspects of the information it is manipulating and focus only on the essential elements. According to this principle, a general description of an entire class of objects is defined instead of defining each object individually. For example, we do not think of a car as a set of hundreds of individual parts. Rather, we view a car as an object with its own unique behaviour. This abstraction allows us to drive a car without being bothered by the complexity of the parts involved in the making of the car. Hence, we can ignore the details of how the engine and braking system works and focus only on using the object, in this case, driving.

Encapsulation

A primary rule of OOP is that as the user of an object, one should never need to peek inside the box. The main reason behind it is that objects communicate with each other using messages. Messages define the interface to the object, that is, the only thing that an object knows about another object is that it exists. Each object's attribute and method is encapsulated from other objects. This allows the developer to separate an object's implementation from its behaviour. This separation creates a black box effect where the user is isolated from implementation changes. As long as the interface remains the same, any change to the internal implementation is transparent. For example, if the wheel message is sent to the car object, it does not matter to the user how the developer implemented the code to handle this message. All the sending object needs is the correct protocol for interacting with the car object. The developer can change the implementation at any time, but the same message would still work because the interface is the same (Figure 8.5).

images

Figure 8.5 Encapsulation and Objects

Polymorphism

It is the idea of allowing the same code to be used with different types, resulting in more general and abstract implementations. It allows two or more objects to respond to the same message. An analogy of polymorphism in daily life is how students respond to a school bell. When the bell (message) rings, however, it has its own meaning to different students (objects). Some students go home, some go to the library, and some go to other classes. Every student responds to the bell, but their response is different. The sending object does not have to know how the receiving object implements the message, only the receiving objects worries about that.

Inheritance

It is the way to extend or change an already existing parent class (also known as superclass), and make a child class (also known as subclass or derived class) that stays linked with its parent class. It allows a class to have the same behaviour as another class and extend or tailor that behaviour to provide special action for specific needs. The subclass inherits all the existing messages, and therefore all the behaviour of the original class. This is the key for code-reusability. Programmers do not have to start from scratch when writing a new program. They can simply reuse an existing repertoire of classes that have behaviours similar to what they need in the new program. For example, after creating the class shapes, one might make a subclass called triangle, which defines some triangle-specific messages like hypotenuse (Figure 8.6).

images

Figure 8.6 Inheritance

11. Write in detail on generations of programming languages.

Ans.: Since early 1950s, programming languages have evolved tremendously. This evolution has resulted in the development of hundreds of different languages. With each passing year, the languages become user-friendly and more powerful. We can illustrate the development of all the languages in five generations.

First Generation: Machine Language

The first language was binary, also known as machine language, which was used in the earliest computers and machines. We know that computers are digital devices that have only two states: ON and OFF (1 and 0). Therefore, every instruction and data should be written using 0’s and 1’s. Machine language is also known as the computer's native language as this system of codes is directly understood by the computer. Instruction in machine language consists of two parts (Figure 8.7). The first part is an operation, which tells the computer what functions are to be performed. The second part of the instruction is the operand, which tells the computer where to find or store the data on which the desired operation is to be performed. Each computer has its own set of instructions based on its architecture. Hence, machine language may differ from computer to computer.

images

Figure 8.7 Machine Language Instruction Format

Second Generation: Assembly Language

This language was developed in the early 1950s and its main developer was IBM. However, Jack Powell, Bob Nevelen, Clement, and Michael Bradly also helped in the development of the assembly language. It was a stepping stone for all subsequent language development. Assembly language allows the programmer to interact directly with the hardware. It assigns a mnemonic code to each machine language instruction to make it easier to remember or write. It allows better human-readable method of writing programs as compared to writing in binary bit patterns. However, unlike other programming languages, it is not a single language, but a group of languages. Each processor family (and sometimes individual processors within a processor family) has its own assembly language. It provides a mnemonic instruction, usually three letters long, corresponding to each machine instruction. The letters are usually abbreviated indicating what the instruction does. For example, ADD is used to perform an addition operation, SUB for subtraction, etc. It makes it easier for humans to remember how to write instructions to the computer.

The basic unit of an assembly language program is a line of code. It allows the use of symbols and set of rules that can be used and combined to form a line of code. Each line of an assembly language program consists of four columns called fields. The general format of an assembly instruction is

[Label] <Opcode> <Operands> [; Comment]

Brackets […] indicate that the enclosed specification may or may not appear in a statement. If a label is specified, it is associated as a symbolic name with the machine words generated for the assembly statement. If multiple operands are used, each of them is separated by comma. The text after semicolon (;) is just comments. Comments are not a part of actual program, but are used just for reference purposes, that is, to specify what the statement will actually do. Although comments are optional, they are included to facilitate proper documentation. For example,

images

Third Generation: High-level Language

During 1960s, computers started to gain popularity and it became necessary to develop languages that were more like natural languages such as English so that a common user could use the computer efficiently. Since assembly language required deep knowledge of computer architecture, it demanded programming as well as hardware skills to use computers. Due to computer's widespread usage, early 1960s saw the emergence of the third generation programming languages (3GL). Languages such as COBOL, FORTRAN, BASIC, and C are examples of 3GLs and are considered HLLs. HLLs are similar to English language. Programs written using these languages can be machine independent. A single high-level statement can substitute several instructions in machine or assembly language. Unlike assembly and machine programs, high-level programs may be used with different types of computers with little or no modification, thus reducing the re-programming time.

Fourth Generation: 4GL

These languages have simple, English-like syntax rules, commonly used to access databases. The 3GLs are considered as procedural languages because the programmer must list each step and must use logical control structures to indicate the order in which instructions are to be executed. 4GLs, on the other hand, are non-procedural languages. The non-procedural method is simply to state the needed output instead of specifying each step one after another to perform a task. In other words, the computer is instructed what it must do rather than how a computer must perform a task. The non-procedural method is easier to write, but has less control over how each task is actually performed. When using non-procedural languages, the methods used and the order in which each task is carried out is left to the language itself; the user does not have any control over it. In addition, 4GLs sacrifice computer efficiency in order to make programs easier to write. Hence, they require more computer power and processing time. However, with the increase in power and speed of hardware and with diminishing costs, the uses of 4GLs have spread. They have a minimum number of syntax rules. Hence, common people can also use such languages to write application programs. This saves time and allows professional programmers for more complex tasks.

Fifth Generation: Very High-Level Languages

These languages are just the conceptual view of what might be the future of programming languages. They will be able to process natural languages. The computers would be able to accept, interpret, and execute instructions in the native or natural language of the end users. The users will be free from learning any programming language to communicate with the computers. The programmers may simply type the instruction or simply tell the computer by way of microphones what it needs to do. Since these languages are still in their infancy, only a few are currently commercially available. They are closely linked to artificial intelligence and expert systems.

12. Machine language is rarely used nowadays. Justify.

Ans.: Machine language is rarely used nowadays due to its overwhelming limitations. They are as follows:

images Machine dependent: Every computer type differs from the other, based on its architecture. Hence, an application developed for a particular type of computer may not run on the other type of computer. This may prove costly as well as difficult for the organizations.
images Complex language: Machine language is very difficult to read and write. Since all the data and instructions must be converted to binary code, it is almost impossible to remember the instructions. A programmer must specify each operation and the specific location for each piece of data and instruction to be stored. It means that a programmer practically needs to be a hardware expert to have proper control over the machine language.
images Error prone: Since the programmer has to remember all the opcodes and the memory locations, machine language is bound to be error prone. It requires a superhuman effort to keep track of the logic of the problem and, therefore, results in frequent programming errors.
images Tedious: Machine language poses real problems while modifying and correcting a program. Sometimes, the programming becomes too complex to modify and the programmer has to rewrite the entire logic again. Therefore, it is very tedious and time-consuming, and since time is a precious commodity, programming using the machine language tends to be costly.

13. Discuss the advantages and disadvantages of the following:

(a) High level language

(b) Assembly level language

Ans.:

(a) High-level language: It is useful in developing complex software, as it supports complex data structures. It increases the programmer's productivity (the number of lines of code generated per hour). Unlike assembly language, the programmer does not need to learn the instruction set of each computer being worked with. The various advantages of using HLL are as follows:

images Readability: Since it is closer to natural languages, it is easier to learn and understand. In addition, a programmer does not need to be aware of computer architecture; even a common man can use it without much difficulty. This is the main reason of HLL's popularity.
images Machine independent: It is machine independent in the sense that a program created using HLL can be used on different platforms with very little or no change at all.
images Easy debugging: It includes the support for ideas of abstraction so that programmers can concentrate on finding the solution to the problem rapidly, rather than on low-level details of data representation, which results in fewer errors. Moreover, the compilers and interpreters are designed in such a way that they detect and point out the errors instantaneously.
images Easier to maintain: As compared to low-level languages, the programs written in HLL are easy to modify and maintain because HLL programs are easier to understand.
images Low development cost: It permits faster development of programs. Although a high-level program may not be as efficient as an equivalent low-level program, but the savings in programmer's time generally outweighs the inefficiencies of the application. This is because the cost of writing a program is nearly constant for each line of code, regardless of the language. Thus, a HLL, where each line of code translates to 10 machine instructions, costs only a fraction as compared to programs developed in low-level languages.
images Easy documentation: Since the statements written in HLL are similar to natural languages, it is easier to understand as compared to low-level languages.

     But on the other hand, HLL got few disadvantages. They are as follows:

images Poor control on hardware: It is developed to ease the pressure on programmers so that it does not have to know the intricacies of hardware. As a result, sometimes the applications written in HLL cannot completely harness the total power available at hardware level.
images Less efficient: The HLL applications are less efficient as far as computation time is concerned. This is because, unlike low-level languages, HLL must be created and sent through another processing program known as a compiler. This process of translation increases the execution time of an application. Programs written in HLL take more time to execute, and require more memory space. Hence, critical applications are generally written in low-level languages.

(b) Assembly language: It allows the programmer to interact directly with the hardware. It allows better human-readable method of writing programs as compared to writing in binary bit patterns. The advantages of using assembly language to develop a program are as follows:

images Easy to understand and use: It uses mnemonics instead of using numerical opcodes and memory locations used in machine language. Hence, the programs written in assembly language are much easier to understand and use as compared to its machine language counterpart. Being a more user-friendly language as compared to machine language, assembly programs are easier to modify.
images Less error prone: Since mnemonic codes and symbolic addresses are used, the programmer does not have to keep track of the storage locations of the information and instructions. Hence, there are fewer errors while writing an assembly language program. Even in case of errors, assembly programs provide better facility to locate and correct them as compared to machine language programs. Moreover, assemblers also provide various mechanisms to locate the errors. For example, in case of adding two variables such as ADD A, B, if the variables (A and B) are not defined in the program, the assembler will give an error indicating the same so that the programmer can easily correct the mistake.
images Efficiency: It can run much faster and use less memory and other resources than a similar program written in a HLL. A speed increment of 2–20 times faster is common, and, occasionally, an increase of hundreds of times faster is also possible.
images More control on hardware: It also gives direct access to key machine features essential for implementing certain kinds of low-level routines such as an operating system kernel or microkernel, device drivers, and machine control.

     But on the other hand, assembly language got few disadvantages. They are as follows:

images Machine dependent: Different computer architectures have their own machine and assembly languages, which means that programs written in these languages are not portable to other incompatible systems. This makes it a low-level language. If an assembly program is to be shifted to a different type of computer, it has to be modified to suit the new environment.
images Harder to learn: The source code for an assembly language is cryptic and in a very low machine-specific form. Being a machine-dependent language, every type of computer architecture requires a different assembly language, making it hard for a programmer to remember and understand every dialect of assembly. More skilled and highly trained programmers, who know all about the logical structure of the computer, can only create applications using assembly language.
images Slow development time: Even with highly skilled programmers, assembly generated applications are slower to develop as compared to HLL-based applications. In case of assembly language, the development time can be 10–100 times as compared to HLL-generated application.
images Less efficient: A program written in assembly language is less efficient than machine language because every assembly instruction has to be converted into machine language. Therefore, the execution of assembly language program takes more time than machine language program. Moreover, before executing an assembly program, the assembler has to be loaded in the computer's memory for translation and occupies a sizeable memory.
images No standardization: It cannot be standardized because each type of computer has different instruction set, and therefore a different assembly language.
images No support for modern software engineering technology: It provides any inherent support for software engineering technology. It works with just machine-level specifics, not with abstractions. It does not provide inherent support for safety-critical systems. It provides very little opportunity for reuse and there is no OOP support. There is also no specific support for distributed systems. The tools available for working with assembly languages are typically very low-level tools.

14. Explain in detail the working of three types of language translators.

Ans.: Since computers understand only machine language, it is necessary to convert the HLL programs into machine language codes. This is achieved by using language translators or language processors, generally known as compilers, interpreters, or assemblers that accept statements in one language and produce equivalent statements in another language.

Compiler

It is a kind of translator that translates a program into another program, known as target language. Usually, it is used for language translator of HLL into machine language. It replaces single high-level statement with a series of machine language instruction, and usually resides on a disk or other storage media. When a program is to be compiled, its compiler is loaded into the main memory. It stores the entire high-level program, scans it, and translates the whole program into an equivalent machine language program. During the translation process, the compiler reads the source program and checks the syntax (grammatical) errors. If there is any error, it generates an error message, which is usually displayed on the screen. In case of errors, it will not create the object code until all the errors are rectified.

Once the program has been compiled, the resulting machine code is saved in an executable file, which can be run on its own at any time. To be precise, once the executable is generated, there is no need for the actual source code file. Anyway, it is worthwhile to keep the source file(s) because if the source code is modified, it is necessary to recompile the program again to regenerate the executable file containing amendments. Figure 8.8 illustrates the working of a compiler.

images

Figure 8.8 Working of a Compiler

Interpreter

It is also a language translator and translates HLL into machine language. However, unlike compilers, it translates a statement in a program and executes the statement immediately before translating the next source language statement (Figure 8.9). When an error is encountered in the program, the execution of the program is halted and an error message is displayed. Similar to compilers, every interpreted language such as BASIC and LISP has its own interpreters.

images

Figure 8.9 Working of an Interpreter

Assembler

As we know, assembly language is nothing more than a symbolic representation of machine code, which allows symbolic designation of memory locations. However, no matter how close assembly language is to machine code, the computer still cannot understand it. Therefore, the assembly language program must be translated into machine code by a separate program called an assembler. The assembler program recognizes the character strings that make up the symbolic names of the various machine operations, and substitutes the required machine code for each instruction. At the same time, it also calculates the required address in memory for each symbolic name of a memory location, and substitutes those addresses for the names resulting in a machine language program that can run on its own at any time. In short, it converts the assembly codes into binary codes and then it assembles the machine understandable code into the main memory of the computer, making it ready for execution (Figure 8.10).

images

Figure 8.10 Working of an Assembler

The original assembly language program is also known as the source code, while the final machine language program is designated as the object code. If an assembly language program needs to be changed or corrected, it is necessary to make the changes to the source code and then re-assemble it to create a new object program.

15. Write short notes on the following:

(a) Linker

(b) Loader

Ans.: (a) Linker: An application usually consists of hundreds or thousands of lines of codes. The codes are divided into logical groups and stored in different modules so that the debugging and maintenance of the codes becomes easier. Hence, for an application, it is always advisable to adhere to structural (modular) programming practices. When a program is broken into several modules, each module can be modified and compiled independently. In such a case, these modules have to be linked together to create a complete application. This job is done by a tool known as linker. A linker is a program that links several object modules and libraries to form a single, coherent program (executable). Object modules are the machine code output from an assembler or compiler and contain executable machine code and data, together with information that allows the linker to combine the modules together to form a program.

Generally, all HLL use some inbuilt functions like calculating square roots, finding logarithm values, etc. These functions are usually provided by the language itself, the programmer does not need to code them separately. During the program execution process, when a program invokes any in-built function, the linker transfers the control to that program where the function is defined, by making the addresses of these functions known to the calling program.

(b) Loader: It is a part of the operating system that brings an executable file residing on disk into the memory and starts its execution. It is responsible for loading, linking, and relocation. In computing, a loader is a program that performs the functions of a linker and then immediately schedules the executable code for execution, without necessarily creating an executable file as an output. It performs the following four basic tasks:

  1. Allocation: It allocates memory space for the programs.
  2. Linking: It combines two or more separate object programs and supplies the information needed to allow references between them.
  3. Relocation: It prepares a program to execute properly from its storage area.
  4. Loading: It places data and machine instructions into the memory.

There are two types of loaders:

i. Absolute loader: It loads the file into the memory at the location specified by the beginning portion (header) of the file and then passes control to the program. If the memory space specified by the header is currently in use, execution cannot proceed, and the user must wait until the requested memory becomes free. Moreover, this type of loader performs only loading function. It does not perform linking and program relocation.

ii. Relocating loader: It loads the program in the memory, altering the various addresses as required to ensure correct referencing. It is a more efficient loader, but there is a slight overhead in terms of a small delay whilst all the relative offsets are calculated. It can only relocate code that has been produced by a linker capable of producing relative code.

16. Explain the features of a good programming language.

Ans.: The features of one programming language may differ from the other. One can be easy and simple while others can be difficult and complex. One judges the success and strength of a programming language with respect to standard features. The various features of a good programming language are as follows:

images Ease of use: It should be easy in writing codes for the programs and executing them. The ease and clarity of a language depends upon its syntax. It should be capable enough to provide clear, simple, and unified set of concepts. The vocabulary of the language should resemble English (or some other natural language). Symbols, abbreviations, and jargon should be avoided unless they are already known to most people. Any concept that cannot easily be explained to amateurs should not be included in the language. Simplicity helps in the readability of the language. A simple language is easier to grasp and code. Developing and implementing a compiler or interpreter is also easier for simple languages as compared to complex ones.
images Portability: It should support the construction of code in a way that it could be distributed across multiple platforms (operating systems). Computer languages should be independent of any particular hardware or operating system, that is, programs written on one platform should be able to be tested or transferred to any other computer or platform and there it should perform accurately.
images Naturalness for the application: It should have a syntax that allows the program structure to show the underlying logical structure of algorithm. It should provide a conceptual framework for thinking through algorithms and means of expressing those algorithms through flowcharts. The advantage of displaying a program in algorithms and flowcharts is that even a novice can understand them easily without learning any programming language.
images Reliability: It should support the construction of components that can be expected to perform their intended functions in a satisfactory manner throughout its lifetime. Reliability is concerned with making a system failure-free, and thus is concerned with all possible errors. It should provide support for preventing errors. For example, some errors can be prevented by a strict syntax checking. Apart from prevention, it should also be able to detect and report errors in the program. For example, errors such as arithmetic overflow and assertions should be detected properly and reported to the programmers immediately so that the error can be rectified. It should provide reliability by supporting explicit mechanisms for dealing with problems that are detected when the system is in operation (exception handling).
images Safety: It is concerned with the extent to which the language supports the construction of safety-critical systems, yielding systems that are fault-tolerant, fail-safe, or robust in the face of systemic failures. The system must always do what is expected and be able to recover from any situation that might lead to a mishap or actual system hazard. Thus, safety tries to ensure that any failure that results in minor consequences and even potentially dangerous failures are handled in a fail-safe fashion. A programming language can facilitate this through such features as built-in consistency checking and exceptional handling.
images Performance: By performance, we mean that it should not only be capable of interacting with the end users, but also with the hardware. It should also support software engineering mechanism, discourage or prohibit poor practices, and support maintenance activities. Nowadays, the hardware has become very sophisticated and quiet fast. Hence, the application developed using a good language should tap the maximum resources of the available hardware power in terms of speed and memory efficiency.
images Reusability: It should facilitate the adaptation of code for use in other applications. Code is reusable when it is independent of other codes. It is very common, for example, to reuse common data structures such as stacks, queues, and trees. When these have been defined with common operations on the structures, these abstract data types are easy to reuse.

17. What is a UGL?

Ans.: A UGL (stands for Universal Graphics Library) is a high-level FORTRAN scientific graphics library that is designed to support various applications in scientific, engineering, and business environments. The integrated routines in the UGL allow programmers to generate graphics, charts, surfaces, contours, and two- and three-dimensional (3D) designs, including logarithmic axes, polar coordinates, 3D mesh-and-line plots, and error-bar charts. The high-level applications that are included in the UGL are X-Y graphics, X-Y-Z graphics, contours, histograms, scatter diagrams, streamlines, vector graphics 3D solids, and world map plots.

18. Write an algorithm to find the sum of N natural numbers.

Ans.:

1. Start
2. Initialize Total_Num, Num, Sum to zero
3. While numbers to be added are less than Total_Num, follow these two subtasks repeatedly
      Read number and store it in Num
      Add Num to Sum (Sum = Sum + Num)
4. Display Sum
5. Stop

19. Write an algorithm to find the smallest of three numbers.

Ans.:

1. Start
2. Read three numbers A, B, and C
3. Find the smaller number between A and B and store it in MIN_AB
4. Find the smaller number between MIN_AB and C and store it in MIN
5. Display MIN
6. Stop

20. Write an algorithm to find the largest of three numbers.

Ans.:

1. Start
2. Read three numbers A, B, and C
3. Find the larger number between A and B and store it in MAX_AB
4. Find the larger number between MAX_AB and C and store it in MAX
5. Display MAX
6. Stop

21. Write an algorithm to find simple interest.

Ans.:

1. Start
2. Read three variables Principle, Time, and Rate
3. Find the simple interest as Simple Interest = (Principle * Rate * Time)/100
4. Display Simple Interest
5. Stop

22. Draw a flowchart to sum 1 to N natural number (1 + 2 + 3 + 4 + … + N).

Ans.:

images

23. Draw a flowchart to generate N natural numbers.

Ans.:

images

Multiple-choice Questions

1.  The semantic and syntax errors in the program are checked in __________.

(a) Coding phase

(b) Testing phase

(c) Implementation phase

(d) Analysis phase

2.  Choose the odd one out.

(a) Abstraction

(b) Encapsulation

(c) Inheritance

(d) Linking

3.  __________ is the ability of a program to ignore some aspects of the information and focus only on the essential elements.

(a) Abstraction

(b) Encapsulation

(c) Inheritance

(d) None of these

4. What consists of instructions written as a series of 1’s and 0’s?

(a) Query language

(b) Assembly language

(c) Machine language

(d) None of these

5. The complexities of machine language led to the search of another language called __________.

(a) High-level language

(b) Very high-level language

(c) Assembly language

(d) None of these

6. A high-level language is a programming language that is __________.

(a) Easier to use

(b) Considered as a first generation language

(c) Considered as a third generation language

(d) Both (a) and (c)

7.  Which of the following was a disadvantage of the first generation language?

(a) Machine dependency

(b) Less efficient

(c) Need for more disk space

(d) All of these

8.  __________ is a kind of translator that translates a program into another program, known as target language.

(a) Compiler

(b) Assembler

(c) Loader

(d) Linker

9.  A good programming language should be __________.

(a) Portable and safe

(b) Reliable and good performance

(c) High cost

(d) Both (a) and (b)

10.  UGL stands for __________.

(a) Universal Graphics Library

(b) Uniform Graphics Library

(c) Unified Graphics Library

(d) None of these

Answers

1. (b)

2. (d)

3. (a)

4. (c)

5. (c)

6. (d)

7. (a)

8. (a)

9. (d)

10. (a)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.50.60