Sie sind auf Seite 1von 9

S.

Principles of Programming Languages (April/May-2012, Set-1) JNTU-Anantapur


Code: 9A15403/R09
II B.Tech II Semester Regular & Supplementary Examinations

April/May - 2012

Set-1
Solutions

PRINCIPLES OF PROGRAMMING LANGUAGES


( Information Technology )

Time: 3 Hours

Max. Marks: 70
Answer any FIVE Questions
All Questions carry equal marks
---

1.

Explain in detail the reasons for studying the programming language concepts. (Unit-I, Topic No. 1.1)

2.

(a)

Explain the concept of token of a language with an example. (Unit-II, Topic No. 2.1)

(b)

Distinguish between static and dynamic semantics. (Unit-II, Topic No. 2.5)

3.

What is lifetime of a variable? Explain 4 categories of variables according to their lifetimes. (Unit-V, Topic No. 5.2)

4.

What are design issues for arithmetic expressions? Clearly explain how precedence and associativity are used to
specify operator evaluation order. (Unit-IV, Topic No. 4.1)

5.

(a)

Explain stack implementation of common parameter passing methods. (Unit-V, Topic No. 5.5)

(b)

Explain why parameter passing is more flexible than direct access to non-local variable. (Unit-V, Topic No. 5.1)

Explain abstract data types in C # with examples. (Unit-VI, Topic No. 6.9)

7.

(a)

Explain the applications of logic programming. (Unit-VII, Topic No. 7.7)

(b)

Explain how backtracking work in prolog. (Unit-VII, Topic No. 7.6)

(a)

Explain with suitable examples, the python procedural abstraction. (Unit-VIII, Topic No. 8.3.3)

(b)

Discuss in detail about the data abstraction using python. (Unit-VIII, Topic No. 8.3.4)

8.

B.Tech. II-Year II-Sem.

( JNTU-Anantapur )

Spectrum ALL-IN-ONE Journal for Engineering Students, 2013

S.2

SOLUTIONS TO APRIL/MAY-2012, SET-1, QP


Q1.

Explain in detail the reasons for studying the programming language concepts.
April/May-12, Set-1, Q1

Answer :
For answer refer Unit-I, Q1.
Q2.

(a)

Explain the concept of token of a language with an example.


April/May-12, Set-1, Q2(a)

Answer :
For answer refer Unit-II, Q1, Topics: Lexemes, Example.
(b)

Distinguish between static and dynamic semantics.


April/May-12, Set-1, Q2(b)

Answer :
Difference between Static and Dynamic Semantics
Static Semantic

Dynamic Semantic

1.

Static semantics are indirectly related to the meaning


of an executing program, but is usually associated
with the syntax rather than semantics.

1.

Dynamic semantic refers to the meaning of the


expression, statements and program units of a
programming language.

2.

The semantic rules or specifications are checked


during compile time.

2.

The semantic rules or specifications are checked


during execution time.

3.

These rules are enforced by a compiler during the


compilation process.

3.

These rules are enforced by a compiler by generating


code in order to perform the check.

4.

Variables are declared prior to the use.

4.

Variables can be used before initialization.

5.

It does not allow the programmer to explicitly add


static semantic check.

5.

It enables the programmer to add explicit dynamic


check in the form of assertion.

6.

Examples

6.

Examples

7.
Q3.

(i) Type checking

(i)

(ii) Check labels.

(ii) Arithmetic errors.

Errors are detected by semantic analysis at compile


time.

7.

Array subscript values are within bounds.

Errors are detected by the compiler generated code


at execution time.

What is lifetime of a variable? Explain 4 categories of variables according to their lifetimes.


April/May-12, Set-1, Q3

Answer :
For answer refer Unit-V, Q5.
The following example shows the lifetimes of global and local variables in C++.
int a;
void main( )
{
int p;
float q;
... x( ); ....
... y( ); ....
}

B.Tech. II-Year II-Sem.

( JNTU-Anantapur )

Principles of Programming Languages (April/May-2012, Set-1) JNTU-Anantapur

S.3

void x( )
{
float r;
int s;
... y( ); ...
}
void y( )
{
int t;
...
}
This example consists of the blocks whose procedures main, x and y. It has one global variable a and many local
variables p and q in main, r and s in x and t in y.
Calling of procedures is in the order:
1.

Main calls x

2.

x calls y

3.

Main calls y (directly).

The following figure shows the lifetimes of global and local variables for this example.
Start

Call x Call y

Return
from y

Return
from p From y

Return
from y

Stop

Lifetime(g)

Lifetime(p, q)
Lifetime(t)

Lifetime(r, s)

Lifetime(t)

Figure
In addition to the above two variables, lifetime defines two more variables heap variables and persistent variables.
Heap Variables
Heap variables are the variables that can be created and destroyed at any time during the programs execution. They
are anonymous and can be created using an expression or command.
They are accessed through pointers which are the first-class values. Pointers can be saved, utilized as components
of composite values etc. They are used as connections between nodes of a complex data structure. Pointer can be manipulated
for adding or removing the nodes. Consider the following example in Ada that creates and manipulates the lists.
Procedure main is,
type int_node;
type int_list is access int_node;
type int_node is
record

B.Tech. II-Year II-Sem.

( JNTU-Anantapur )

Spectrum ALL-IN-ONE Journal for Engineering Students, 2013

S.4
element:Integer;
successor:int_list;
end record;
even, composite: int_list:=null;
function constant(i = Integer; l = int_list)
return int_list is
begin
return new int_node (i, l);
end;
procedure proc1 is
begin
even: = constant(12, constant (14, null));
composite: = constant(10, even);
end;
procedure proc2 is
begin
even: = even.successor;
end;
begin
... proc1;
... proc2; ...
end;

In this example, the values of the type int_list would be a pointer to an int_node record or to the null pointer.
Each time when the cons function is called, the expression new int_node (i, t) will create a heap variable of the type
int_node. Then it initializes it and derives a pointer to the newly created heap variable. This pointer is returned by the cons
function.
Proc1 makes the variable even to hold a pointer to a list with integers 12 and 14. It makes even point to a list with
integer 10 followed by the integers 12 and 14. The following figure shows the heap variables created with proc1.
Even

10

12

14

Composite

Figure: Heap Variables and Pointers Created by Proc1


Proc2 shows manipulation of pointers. The first node pointer of even points to the third node thereby removing
the second node. The same node also gets removed from the composite too. This node (14) become unreachable now.
Hence, its lifetime is ended. A heap variable is said to be reachable until it can be accessed by pointers from global and local
variables.
Even

10

12

Composite

Figure: Pointer Manipulation

B.Tech. II-Year II-Sem.

( JNTU-Anantapur )

S.5

Principles of Programming Languages (April/May-2012, Set-1) JNTU-Anantapur


The lifetimes of global and heap variables in this program is shown below,
Start

Return
from proc1

Call proc1

Call proc2

Return
From proc2

Stop

Lifetime(composite)

Lifetime(even)
Lifetime(14)

Lifetime(12)
Lifetime(10)

Persistent Variables
Persistent variable is a variable whose lifetime exceeds the activation of any program. In contrast to that, a transient
variable is a variable whose lifetime is set by the activation of its program. In general, persistent variables possess arbitrary
lifetimes. But in some systems they possess nested lifetimes. They have secondary storage system unlike transient variables
that have primary storage system.
Consider the following example in Ada.
type state is(hyd, kak, Ant)
type stud_statistics
record
Marks: Integer;
Percentage: Float;
....
end record;
type stud_StatsTable is array(state) of stud_statistics
If there exists a sequential file statistics.dat whose components are of type stud_statistics with each component
for each value of type state then, the following declaration can be used to instantiate the generic package Ada.sequential_IO:
Package statistics IO is new Ada.sequential_IO
(Elen_Type Statistics);
The package that is the output of the above declaration i.e., states_IO provides a type file_type. Values of this type
are sequential files with the components of statistics along with opening, closing, reading and writing procedures. The code
that calls these procedures is as follows,
Procedure load statistics(statistics:out stats table) is statisticsFile:StatisticIO.File_Type;
begin
statisticsIO.open(statistics File, in_file, statistics.dat);
for cy in state loop
statisticsIO.read(statistics File, statistics(cy));
end loop;
statisticsIO.close(statistics File);
end;

B.Tech. II-Year II-Sem.

( JNTU-Anantapur )

Spectrum ALL-IN-ONE Journal for Engineering Students, 2013

S.6

The execution of this code is as follows,


Opens statistics.dat by placing a pointer to it in statistics file.
Reads its components and saves it in transient array.
Closes the file.
What are design issues for arithmetic expressions? Clearly explain how precedence and
associativity are used to specify operator evaluation order.
Answer :
April/May-12, Set-1, Q4
Design Issues for Arithmetic Expressions
For answer refer Unit-IV, Q9.
Operator Evaluation Order
For answer refer Unit-IV, Q3.
Q5. (a) Explain stack implementation of common parameter passing methods.
Answer :
April/May-12, Set-1, Q5(a)
Implementation of parameter passing in most contemporary languages is done using a run-time stack. A run-time
system is a program that initializes and manages the run-time stack. It is also responsible for managing the execution of
programs. The actual implementation of various parameter passing methods using a run-time stack is done as follows,
1.
Implementation of Pass-by-Value Method
A pass-by-value method is implemented by copying the values of pass-by-value parameters into stack locations,
which are later used to store the corresponding formal parameters.
2.
Implementation of Pass-by-Result Method
In this method, the values of actual parameters are stored in the stack. The calling program can retrieve these values
when the called sub-program terminates.
3.
Implementation of Pass-by-Value-Result Method
The implementation of this method is a combination of pass-by-value and pass-by-result methods. In this method,
the values of actual parameters are placed in the stack locations, which are then used to initialize the corresponding
formal parameters. These variables then act as local variables in the called sub-program.
4.
Implementation of Pass-by-Reference Method
The implementation of pass-by-reference method is simple. Only the address of the actual parameter irrespective of
its type is stored in the stack. For literals, the address of a literal is stored in the stack. For an expression, the compiler
must first evaluate the expression and then the address where the result of this evaluation is placed should be stored
in the stack.
The following figure shows the implementation of various parameter passing methods using a run-time stack.

Code

1.
2.
3.
Q4.

Ref. to A

As value

Assign to B

Bs value

Ref. to c

at start

at end

at start
Cs value

Assign to C
Ref. to D

Address (D)

Function sub

Stack

at end
Address (at start)

Main

Figure: Showing Implementation of One Possible Stack for Common Parameter Passing Method

B.Tech. II-Year II-Sem.

( JNTU-Anantapur )

S.7

Principles of Programming Languages (April/May-2012, Set-1) JNTU-Anantapur


Here, the sub-program, sub( ) is called from the main
with the call sub (P, Q, R, S), where is passed by value Q is
passed by result R is passed-by-value-result and S is passed
by reference. In the figure A, B, C and D are formal parameters.
If care is not taken during the implementation of pass-byreference and pass-by-value-result parameters then an error
may arise that will be subtle but inoperable.
(b)

Explain why parameter passing is more


flexible than direct access to non-local
variable.

Answer :

April/May-12, Set-1, Q5(b)

Through direct access to non-local variables,


subprograms can acquire non-local variables and can assign
new values to them. The changes made to the values of the
non-local variables are visible throughout the program. If
these variables are accessed frequently then they lose their
variability.
On the other hand, data passed through parameters
can be acquired using the name that are local to the
subprogram, thereby making it flexible than direct access to
non-local variables. In addition to the above, the scope of
local variables is limited only to the subprogram. Hence, the
changes made in the values of local variables are not visible
throughout the program.
Q6.

Explain abstract data types in C# with


examples.

Answer :

April/May-12, Set-1, Q6

Abstract Data Types in C#


C# follows the features of C++ and Java including
some of its own constructs. In addition to the modifiers
public, private and protected of Java, it adds two of its own
modifiers internal and protected internal. All of its class
instances are heap dynamic. The default constructors
responsible for providing the initial values for instance data
are given to all the classes. The initial values for integers is
0 and for boolean types it is false. A constructor provided
for any class can assign initial values to data instances of
the class. Instance variables which are not initialized in
user_defined constructors are assigned with values by the
default constructors. Destructors are not much used in C#
because it uses garbage collection for its heap objects.
Abstract data types states that the data members of
objects must not be visible to the clients. But in some
situations the clients are in need of those data members. In
such cases, accessor methods like getters and setters are
used to acquire the data directly.

B.Tech. II-Year II-Sem.

Using the properties of Delphi, C# brings properties


that implements getters and setters without the intervention
of explicit method calls. These properties provide access to
private data instances implicitly. Consider the following class
and client code:
public class college
public int marks
{
get
{
return CSE marks
}
set
{
CSE marks = M;
}
}
private int CSEmarks;

M
}

M
college C = new college( );
int CSE_a_marks, CSE_b_marks;

M
C.CSEmarks = CSE_a_marks;

M
CSE_b_marks = C.CSEmarks;
In this example, college class defines marks
property that provides getter and setter methods to access
the private data member CSEmarks. This data member is
treated as public member variable implicit variable M in
the setter method is used to refer new value of the property.
Structs in C# are lightweight classes having
constructors, methods, properties and data fields. They are
capable of implementing interfaces without providing any
assistance to inheritance. However, there is a noticeable
difference between structs and classes. Structs are value
types and are allocated on the run-time stack instead of
heap passed by value method is used for passing them as
parameters. Creation of objects is possible with the new
operator which was previously used in creation of class
objects.

( JNTU-Anantapur )

Spectrum ALL-IN-ONE Journal for Engineering Students, 2013

S.8
Q7.

(a)

Explain the applications of logic programming.


April/May-12, Set-1, Q7(a)

Answer :
For answer refer Unit-VII, Q42.
(b)

Explain how backtracking work in prolog.


April/May-12, Set-1, Q7(b)

Answer :

Backtracking is the strategy of returning to previous goals. If a unification operation is undone then a different path
have to be chosen in the search tree. The variables that are assigned with values or associated with other variable are
returned back to their uninstantiated or associated state consider the following tree for example.
India (hot).
India (cool).
winter (cool).
Snowy (X) : - India (X), winter (X).

[Y and X are
Origina l Goal
Snowy (Y)
instantiated Such that if
either of them
Success
-Y = -X
receives value
then that value is
Snowy(X)
shared by both Y and X]
Candidate Clause
(AND)

India (X)
(OR)
x = hot
x = cool
India (hot)

Winter (X)

Sub Goals

Winter (hot)
Lead to file,
Perform's backtrack

India (cool)

Winter (cool)

Candidate Clauses

Backtracking to India (X) subgoal results in breaking of the binding of X to hot similar effect is seen in imperative
languages where the bindings between actual and formal parameters are broken.
However, prolog expresses the bindings in terms of unifications instead of subroutine calls.
Q8.

(a)

Explain with suitable examples, the python procedural abstraction.


April/May-12, Set-1, Q8(a)

Answer :
For answer refer April/May-11, Set-1, Q7(b).
(b)

Discuss in detail about the data abstraction using python.


April/May-12, Set-1, Q8(b)

Answer :
Data Abstraction in Python
Data abstraction in python is relative to the following three constructs,
1.

Packages

2.

Modules

3.

Classes.

B.Tech. II-Year II-Sem.

( JNTU-Anantapur )

S.9

Principles of Programming Languages (April/May-2012, Set-1) JNTU-Anantapur


1.

Packages

A package is a type of module that contains a group of modules. It is a directory which python treats as a package
only if it contains a file with _init_.py. On importing this file just like an ordinary module, all its contents can be considered
as the contents of the package.
For example, to treat the file,
value/_init_py with the value x as the package value, perform the following,
import value
print value.x
To place the modules in a package, just place the module files in the package directory. For example, to place the
modules CSE and IT in the package branch, the following files and directories are required,
(i)

~/python/ is a directory in PYTHONPATH

(ii)

~/python/branch/ is a package directory

(iii)

~/python/branch/_init_.py is a package code

(iv) ~/python/branch/IT.py is an IT module


(v)

~/python/branch/CSE.py is a CSE module

Based on the above list, it is supposed that the directory ~/python is in the users PYTHONPATH. However,
windows requires ~/python to be replaced with C:\python (reversing the slashes) making the following statement acceptable.
(a)

Import Branch
This statement imports the branch package. It makes the module present in branch available.

(b)

Import Branch.IT
This statement imports the IT module. It makes the IT module available with its full name i.e., branch.IT

(c)

From Branch Import CSE


This statement imports the CSE module. It makes the CSE module available with its name CSE.

2.

Modules
For answer refer Unit-VIII, Q56.

3.

Classes
For answer refer Unit-VIII, Q57.

B.Tech. II-Year II-Sem.

( JNTU-Anantapur )

Das könnte Ihnen auch gefallen