jntu anan 2 2 it ps set 1

9
S.1 Principles of Programming Languages (April/May-2012, Set-1) JNTU-Anantapur B.Tech. II-Year II-Sem. ( JNTU-Anantapur ) Code: 9A15403/R09 II B.Tech II Semester Regular & Supplementary Examinations April/May - 2012 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) 6 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) 8. (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) Set-1 Solutions

Upload: raju-raju

Post on 18-Jan-2016

4 views

Category:

Documents


0 download

DESCRIPTION

os

TRANSCRIPT

Page 1: Jntu Anan 2 2 It Ps Set 1

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

B.Tech. II-Year II-Sem. ( JNTU-Anantapur )

Code: 9A15403/R09

II B.Tech II Semester Regular & Supplementary Examinations

April/May - 2012

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 tospecify 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)

6 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)

8. (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)

S e t - 1S o l u t i o n s

Page 2: Jntu Anan 2 2 It Ps Set 1

S.2 Spectrum ALL-IN-ONE Journal for Engineering Students, 2013

B.Tech. II-Year II-Sem. ( JNTU-Anantapur )

Q1. Explain in detail the reasons for studying the programming language concepts.

Answer : April/May-12, Set-1, Q1

For answer refer Unit-I, Q1.

Q2. (a) Explain the concept of token of a language with an example.

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

For answer refer Unit-II, Q1, Topics: Lexemes, Example.

(b) Distinguish between static and dynamic semantics.

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

Difference between Static and Dynamic Semantics

Static Semantic Dynamic Semantic

1. Static semantics are indirectly related to the meaning 1. Dynamic semantic refers to the meaning of theof an executing program, but is usually associated expression, statements and program units of awith the syntax rather than semantics. programming language.

2. The semantic rules or specifications are checked 2. The semantic rules or specifications are checkedduring compile time. during execution time.

3. These rules are enforced by a compiler during the 3. These rules are enforced by a compiler by generatingcompilation process. 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 5. It enables the programmer to add explicit dynamicstatic semantic check. check in the form of assertion.

6. Examples 6. Examples

(i) Type checking (i) Array subscript values are within bounds.

(ii) Check labels. (ii) Arithmetic errors.

7. Errors are detected by semantic analysis at compile 7. Errors are detected by the compiler generated codetime. at execution time.

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

Answer : April/May-12, Set-1, Q3

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( ); ....

}

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

Page 3: Jntu Anan 2 2 It Ps Set 1

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

B.Tech. II-Year II-Sem. ( JNTU-Anantapur )

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 localvariables “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

Returnfrom y

Returnfrom p From y

Return from y Stop

Lifetime(g)

Lifetime(p, q)

Lifetime(r, s)

Lifetime(t)

Lifetime(t)

Start Call x Call y

Returnfrom y

Returnfrom p From y

Return from y Stop

Lifetime(g)

Lifetime(p, q)

Lifetime(r, s)

Lifetime(t)

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 program’s execution. Theyare 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 componentsof composite values etc. They are used as connections between nodes of a complex data structure. Pointer can be manipulatedfor 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

Page 4: Jntu Anan 2 2 It Ps Set 1

S.4 Spectrum ALL-IN-ONE Journal for Engineering Students, 2013

B.Tech. II-Year II-Sem. ( JNTU-Anantapur )

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 typeint_node. Then it initializes it and derives a pointer to the newly created heap variable. This pointer is returned by the consfunction.

Proc1 makes the variable “even” to hold a pointer to a list with integers 12 and 14. It makes “even” point to a list withinteger 10 followed by the integers 12 and 14. The following figure shows the heap variables created with proc1.

Even 10 12 14

Composite

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 removingthe 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 localvariables.

Even 10 12

Composite

Even 10 12

Composite

Figure: Pointer Manipulation

Page 5: Jntu Anan 2 2 It Ps Set 1

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

B.Tech. II-Year II-Sem. ( JNTU-Anantapur )

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

Start Call proc1

Returnfrom proc1 Call proc2

Return From proc2 Stop

Lifetime(composite)

Lifetime(even)

Lifetime(14)

Lifetime(10)

Lifetime(12)

Start Call proc1

Returnfrom proc1 Call proc2

Return From proc2 Stop

Lifetime(composite)

Lifetime(even)

Lifetime(14)

Lifetime(10)

Lifetime(12)

Persistent Variables

Persistent variable is a variable whose lifetime exceeds the activation of any program. In contrast to that, a transientvariable is a variable whose lifetime is set by the activation of its program. In general, persistent variables possess arbitrarylifetimes. But in some systems they possess nested lifetimes. They have secondary storage system unlike transient variablesthat 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 componentfor 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 typeare sequential files with the components of statistics along with opening, closing, reading and writing procedures. The codethat 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;

Page 6: Jntu Anan 2 2 It Ps Set 1

S.6 Spectrum ALL-IN-ONE Journal for Engineering Students, 2013

B.Tech. II-Year II-Sem. ( JNTU-Anantapur )

The execution of this code is as follows,1. Opens statistics.dat by placing a pointer to it in statistics file.2. Reads its components and saves it in transient array.3. Closes the file.Q4. 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, Q4Design 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-timesystem is a program that initializes and manages the run-time stack. It is also responsible for managing the execution ofprograms. 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 MethodIn this method, the values of actual parameters are stored in the stack. The calling program can retrieve these valueswhen the called sub-program terminates.

3. Implementation of Pass-by-Value-Result MethodThe 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 correspondingformal parameters. These variables then act as local variables in the called sub-program.

4. Implementation of Pass-by-Reference MethodThe implementation of pass-by-reference method is simple. Only the address of the actual parameter irrespective ofits type is stored in the stack. For literals, the address of a literal is stored in the stack. For an expression, the compilermust first evaluate the expression and then the address where the result of this evaluation is placed should be storedin the stack.The following figure shows the implementation of various parameter passing methods using a run-time stack.

Ref. to A

Assign to B

Ref. to c

Assign to C

Ref. to D

A’s value

B’s value

C’s value

Address (D)

Code

P

Q

R

S

at start

at end

at start

at end

Address (at start)

Function sub Stack Main

Ref. to A

Assign to B

Ref. to c

Assign to C

Ref. to D

A’s value

B’s value

C’s value

Address (D)

Code

P

Q

R

S

at start

at end

at start

at end

Address (at start)

Function sub Stack Main

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

Page 7: Jntu Anan 2 2 It Ps Set 1

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

B.Tech. II-Year II-Sem. ( JNTU-Anantapur )

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

(b) Explain why parameter passing is moreflexible than direct access to non-localvariable.

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

Through direct access to non-local variables,subprograms can acquire non-local variables and can assignnew values to them. The changes made to the values of thenon-local variables are visible throughout the program. Ifthese variables are accessed frequently then they lose theirvariability.

On the other hand, data passed through parameterscan be acquired using the name that are local to thesubprogram, thereby making it flexible than direct access tonon-local variables. In addition to the above, the scope oflocal variables is limited only to the subprogram. Hence, thechanges made in the values of local variables are not visiblethroughout the program.

Q6. Explain abstract data types in C# withexamples.

Answer : April/May-12, Set-1, Q6

Abstract Data Types in C#

C# follows the features of C++ and Java includingsome of its own constructs. In addition to the modifierspublic, private and protected of Java, it adds two of its ownmodifiers – internal and protected internal. All of its classinstances are heap dynamic. The default constructorsresponsible for providing the initial values for instance dataare given to all the classes. The initial values for integers is0 and for boolean types it is false. A constructor providedfor any class can assign initial values to data instances ofthe class. Instance variables which are not initialized inuser_defined constructors are assigned with values by thedefault 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 ofobjects must not be visible to the clients. But in somesituations the clients are in need of those data members. Insuch cases, accessor methods like getters and setters areused to acquire the data directly.

Using the properties of Delphi, C# brings propertiesthat implements getters and setters without the interventionof explicit method calls. These properties provide access toprivate data instances implicitly. Consider the following classand 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 accessthe private data member “CSEmarks”. This data member istreated as public member variable implicit variable “M” inthe setter method is used to refer new value of the property.

Structs in C# are lightweight classes havingconstructors, methods, properties and data fields. They arecapable of implementing interfaces without providing anyassistance to inheritance. However, there is a noticeabledifference between structs and classes. Structs are valuetypes and are allocated on the run-time stack instead ofheap passed by value method is used for passing them asparameters. Creation of objects is possible with the “new”operator which was previously used in creation of classobjects.

Page 8: Jntu Anan 2 2 It Ps Set 1

S.8 Spectrum ALL-IN-ONE Journal for Engineering Students, 2013

B.Tech. II-Year II-Sem. ( JNTU-Anantapur )

Q7. (a) Explain the applications of logic programming.

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

For answer refer Unit-VII, Q42.

(b) Explain how backtracking work in prolog.

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

Backtracking is the strategy of returning to previous goals. If a unification operation is undone then a different pathhave to be chosen in the search tree. The variables that are assigned with values or associated with other variable arereturned 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).

Snowy(X)(AND)

Snowy (Y)

India (X)(OR)

Winter (X)

Winter (cool)India (cool)India (hot)

Winter (hot)Lead to file,

Perform's backtrack

Success-Y = -X →

[Y and X areinstantiated Such that if

either of themreceives value

then that value is shared by both Y and X]

Original Goal

Candidate Clause

Sub Goals

Candidate Clauses

x = hotx = cool

Snowy(X)(AND)

Snowy (Y)

India (X)(OR)

Winter (X)

Winter (cool)India (cool)India (hot)

Winter (hot)Lead to file,

Perform's backtrack

Success-Y = -X →

[Y and X areinstantiated Such that if

either of themreceives value

then that value is shared by both Y and X]

Original Goal

Candidate Clause

Sub Goals

Candidate Clauses

x = hotx = cool

Backtracking to India (X) subgoal results in breaking of the binding of X to hot similar effect is seen in imperativelanguages 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.

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

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

(b) Discuss in detail about the data abstraction using python.

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

Data Abstraction in Python

Data abstraction in python is relative to the following three constructs,

1. Packages

2. Modules

3. Classes.

Page 9: Jntu Anan 2 2 It Ps Set 1

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

B.Tech. II-Year II-Sem. ( 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 packageonly if it contains a file with _init_.py. On importing this file just like an ordinary module, all its contents can be consideredas 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 themodules “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 user’s 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.