Department of Computer Science & Engineering B. Tech (CSE ) IMPORTANT QUESTION WITH SOLUTION PRINCIPLE OF PROGRAMMING LANGUAGE Unit-1 1. What are the objectives of PPL? ANS -All programming languages share the objective of being able to create computer programs. This is where programming languages differ. • • • • • Some languages, such as C, are designed to produce efficient code to be run on small computer systems. Some languages, such as Pascal and Simula, are designed to teach programming concepts to beginners. Some languages, such as Fortran, are designed to produce efficient numeric calculations. Some languages, such as COBOL, are designed to provide readable syntax for financial and other business applications. Some languages, such as Ada, are designed to combine reliability, robustness, readability, and execution efficiency. 2. Differentiate between compiler and interpreter. ANS Interpreter Compiler Translates program one statement at a time. Scans the entire program and translates it as a whole into machine code. It takes less amount of time to analyze the source code but the overall execution time is slower. It takes large amount of time to analyze the source code but the overall execution time is comparatively faster. No intermediate object code is generated, hence are memory efficient. Generates intermediate object code which further requires linking, hence requires more
memory. Continues translating the program until the first error is met, in which case it stops. Hence debugging is easy. It generates the error message only after scanning the whole program. Hence debugging is comparatively hard. Programming language like Python, Ruby use interpreters. Programming language like C, C++ use compilers. 3. Compute the weakest precondition of the following assignment: a = 2 * (b-1)-1 (a>0) ANS: a>0 2*(b-1)-1>0 2*(b-1)>1 2*b-2>1 2b>3 b>3/2 4. Explain the evolution of various programming language paradigms in detail with suitable examples. The programming paradigms which are most widely used and implemented by various programming languages are: • • • • • Imperative programming Object-oriented programming (OO) Functional programming (FP) Generic programming (GP) Meta-programming (MP) 1.Imperative programming is a paradigm of computer programming in which the program describes a sequence of steps that change the state of the computer. Unlike declarative programming, which describes "what" a program should accomplish, imperative programming
explicitly tells the computer "how" to accomplish it. Programs written this way often compile to binary executables that run more efficiently since all CPU instructions are themselves imperative statements. To make programs simpler for a human to read and write, imperative statements can be grouped into sections known as code blocks. In the 1950s, the idea of grouping a program's code into blocks was first implemented in the ALGOL programming language. They were originally called "compound statements," but today these blocks of code are known as procedures. Once a procedure is defined, it can be used as a single imperative statement, abstracting the control flow of a program and allowing the developer to express programming ideas more naturally. This type of imperative programming is called procedural programming, and it is a step towards higherlevel abstractions such as declarative programming. Imperative programming languages • • Ada ALGOL 2.The object-oriented programming paradigm introduced mechanisms required to obtain modular software design and reusability compared to universal accessibility of implementations by imperative programming. The main part of the object-oriented paradigm is related to the introduction of classes which cover basic properties of concepts to be implemented. Based on this description of properties, an object is created, which can be briefly explained by a selfgoverning unit of information which actively communicates with other objects. This is the main difference compared to a passive access as used in imperative programming languages. The concept of inheritance was introduced due to the fact that a particular concept, e.g., a mathematical -dimensional vector, cannot be described in its generality by a single class. By inheritance, semantically related concepts can be implemented in a class from which other classes can be derived, which should enable means of code reusability. • The first source snippet presents an implementation of the given problem in a fully object-oriented language, Java. 3.Functional Programming A different type of programming paradigm is functional programming, where computation is treated as the evaluation of functions based on the following properties: • • Objects are provided only as constants or as expressions on objects. Functional programming avoids states of objects and mutable data. Loops therefore have to be replaced by recursion.
An important part of functional programming related to high performance is the delayed or lazy evaluation. This means that the moment in which a function is bound with some of its arguments can be separated from the one in which it is evaluated, thereby supporting the following techniques: • • Partial binding and partial evaluation, e.g., a binary function where one of the arguments is already bound to the function and only one parameter has to be specified to finally evaluate the function. Lambda expressions, based on the lambda calculus , use partial binding to define the • expression , with the reserved prefix , followed by the formal parameters, a dot, and the function definition. Currying, which creates unnamed lambda expressions automatically. An implementation of the given problem is presented in the following source snippet using Haskell as the host language. This programming paradigm, with the corresponding purely functional programming languages, such as Haskell, is currently widely used in the area of mathematical modeling. The static type mechanism and the generic mechanism, e.g., the availability of return type deduction, which goes beyond the capabilities of, e.g., C++, is an important feature and advantage of this language. 4.Generic Programming Generic programming denotes (object-oriented) programming methods based on the mechanism of parametric polymorphism of data structures and algorithms to obtain orthogonality, while avoiding the performance drawback of virtual function call costs in C++. The generic programming paradigm thereby establishes homogeneous interfaces between algorithms and data structures without sub-typing polymorphism by an abstract access mechanism, the STL iterator concept. This concept specifies an abstract mechanism for accessing data structures in a concise way. 5.Meta-Programming