Beruflich Dokumente
Kultur Dokumente
April/May - 2012
Set-1
Solutions
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)
(b)
(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.
( JNTU-Anantapur )
S.2
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)
Answer :
For answer refer Unit-II, Q1, Topics: Lexemes, Example.
(b)
Answer :
Difference between Static and Dynamic Semantics
Static Semantic
Dynamic Semantic
1.
1.
2.
2.
3.
3.
4.
4.
5.
5.
6.
Examples
6.
Examples
7.
Q3.
(i)
7.
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( ); ....
}
( 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.
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
( JNTU-Anantapur )
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
10
12
Composite
( JNTU-Anantapur )
S.5
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;
( JNTU-Anantapur )
S.6
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
( JNTU-Anantapur )
S.7
Answer :
Answer :
April/May-12, Set-1, Q6
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 )
S.8
Q7.
(a)
Answer :
For answer refer Unit-VII, Q42.
(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)
Answer :
For answer refer April/May-11, Set-1, Q7(b).
(b)
Answer :
Data Abstraction in Python
Data abstraction in python is relative to the following three constructs,
1.
Packages
2.
Modules
3.
Classes.
( JNTU-Anantapur )
S.9
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)
(ii)
(iii)
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)
2.
Modules
For answer refer Unit-VIII, Q56.
3.
Classes
For answer refer Unit-VIII, Q57.
( JNTU-Anantapur )