Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

TPL 6 Chapter (Short Answers) : Use Postfix Notation Two Programming Languages

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

TPL 6th Chapter (Short Answers)

1. Name eight major categories of control-flow mechanisms


Name eight major categories of control-flow mechanisms. Selection, iteration,
procedural abstraction, recursion, concurrency, exception handling, speculation,
nondeterminacy.

2.What distinguishes operators from other sorts of functions? 


Function calls usually consist of function name followed by a parenthsized, comma-
separated list of arguments. Operators are typically simpler taking only one or two
arguments without parens or commas.

3. Explain the difference between prefix, infix, and postfix notation. What is
Cambridge Polish notation? Name two programming languages that use
postfix notation?
In the infix expressions, it is difficult to keep track of the operator precedence whereas
here the postfix expression itself determines the precedence of operators (which is
done by the placement of operators)i.e the operator which occurs first operates on the
operand. The main difference between prefix and postfix is that the prefix is
a notation that writes the operator before operands while the postfix is a notation that
writes the operator after the operands. Notation is the way of writing arithmetic
expressions.

Cambridge Polish notation allows. operators to have a variable number of operands


or. scope, but requires that the operator and all of its. operands be enclosed in a pair of
parentheses. Logic languages are based on predicate logic. A few language use
postfix notation and finally here it is being evaluated in two programming languages:
Scheme (Racket) and Postscript (Ghostscript).

4.Why don’t issues of associativity and precedence arise in Postscript or


Forth?
Postscript and Forth use postfix notation. Smalltalk ... C has so many rules
for precedence and associativity that most programmers don't know them all then an
overflow can occur if b+d doesn't fit in an integer.Hence, this makes for problems.

5.What does it mean for an expression to be referentially transparent?


An expression is called referentially transparent if it can be replaced with its
corresponding value (and vice-versa) without changing the program's behavior. This
requires that the expression be pure, that is to say the expression value must be the
same for the same inputs and its evaluation must have no side effects.
6. What is the difference between a value model of variables and a reference
model of variables? Why is the distinction important?
A Value Type holds the data within its own memory allocation and a Reference Type
contains a pointer to another memory location that holds the real
data. Reference Type variables are stored in the heap
while Value Type variables are stored in the stack.

7. What is an l-value? An r-value?


L-Value: Left hand side of assignment indicating the location of change. Usually variables to
parts of variables.

R-Value: Right hand side of assignment that provides the new value. Usually the result of
evaluation and expressions.

8. Why is the distinction between mutable and immutable values


important in the implementation of a language with a reference model of
variables?
Mutable values can cause multiple variables referring the values to change which can cause
issues.

9. Define orthogonality in the context of programming language design?


Orthogonality in a programming language means that a relatively small set of
primitive constructs can be combined in a relatively small number of ways to build the
control and data structures of the language. It is associated with simplicity; the
more orthogonal the design, the fewer exceptions.

10. What does it mean for a language to be expression-oriented?


An expression-oriented programming language is a programming language where
every (or nearly every) construction is an expression and thus yields a value. ...
Some expression-oriented languages introduce a void return type to be yielded
by expressions that merely cause side-effects.

11. What are the advantages of updating a variable with an assignment


operator, rather than with a regular assignment in which the variable
appears on both the left- and right-hand sides?
12.Given the ability to assign a value into a variable, why is it useful to be
able to specify an initial value?
One downside of assignment is that it requires at least two statements:

One to define the variable, and one to assign the value.

These two steps can be combined. When a variable is defined, you can also provide an
initial value for the variable at the same time. This is called initialization.

C++ supports three basic ways to initialize a variable. First, we can do copy
initialization by using an equals sign:
1 int width = 5; // copy initialization of value 5 into variable width

13.What are aggregates? Why are they useful?


In general, an aggregate is data composed of smaller pieces that form a larger
whole. In programming, aggregation is a type of object composition where not all
the containing objects should be destroyed when the owning object is destroyed.

14. Explain the notion of definite assignment in Java and C#.


At any point a variable is used, Java and C# first check to see if there was an assignment to it. If there is
any point in the code where it is possible a variable is not used, it will not even compile.

E.g.

int i;

int j=5;

if(j > 0)

i=2;

else

print i; //I was never initialized to anything;

compiler will complain

15. Why is it generally expensive to catch all uses of uninitialized variables at


run time?
All computers stores bits in their variables, it is up to us as programmers to give the bits
meaning. A computer does not know if we want to use uninitialized space; uninitialized still
means it has a particular order of bits.
In order to recognize a variable as uninitialized, it must somehow keep track of when a
variable is un/initialized, as well as what are “good” verses “bad” values. This adds extra
memory constraints and computations .

17. Why do most languages leave unspecified the order in which the
arguments of an operator or function are evaluated?
So the code can be generated at compile time for the loop control. The size of the loop is
known , so step-size needs to be available at compile time as well.

18. What is short-circuit Boolean evaluation? Why is it useful?


A compiler that performs short-circuit evaluation of Boolean expressions will generate code
that skips the second half of both of these computations when the overall values can be
determined from the first half. Short-circuit evaluation can save significant amounts of time in
certain situations .

19. List the principal uses of goto, and the structured alternatives to each.
It performs a one-way transfer of control to another line of code; in contrast a function call
normally returns control.

Most modern languages provide an explicit return statement. Where once a goto might have
been used to escape from the middle of a loop , most modern languages provide a break or
exit. More significantly, several languages allow a program to return form a nested chain of
subroutine calls in a single operation that propagates out to some surrounding context s.

20. Explain the distinction between exceptions and multilevel returns.


Both transfer control from inner to outer context and unwind the stack.
In multilevel return, the inner context completes computation and transfers control in a way
that needs no post-processing.
An exception cannot complete its work in the inner context, so the handler in the outer
context must be executed.

21. What are continuations? What other language features do they subsume?
It consists of code address and a referencing environment to be rested when jumping to that
address.
A continuation is an abstraction that captures a context in which execution might continue.

22. Why is sequencing a comparatively unimportant form of control flow in


Lisp?
In common Lisp, the programmer can choose to return the value of the first element, the
second, or the last. Of course, sequencing is a useless operation un less the sub-expressions
that do not play a part in the return value have side effects.
23. Explain why it may sometimes be useful for a function to have side effects.
There are some situations in which side effects in functions are highly desirable. We saw one
example in the label name function. Another arises in the typical interface to a pseudorandom
number generator:

Obviously rand needs to have a side effect, so that it will return a different value each time it is
called. One could always recast it as a procedure with a reference parameter.

24. Describe the jump code implementation of the short-circuit Boolean


evaluation.
In Jump Code inherited attributes of the root of the syntax tree inform it of the address to
which control should branch if the expression is true or false, respectively. Jump code can be
generated quite elegantly by an attribute grammar, particularly on that is not L-attributed.

25. Why do imperative languages commonly provide a case statement in


addition to if... then ... else?

26. Describe three different search strategies that might be employed in the
implementation of a case statement, and the circumstances in which each
would be desirable.
Sequential testing, Hashing, and binary search.

Sequential testing (as in an if… then … else statement) is the method of choice if the total
number of case statement labels is small. It runs in time O(n), where n is the number of labels.

A hash table is attractive if the range of label values is large, but has many missing values and
no large ranges. With an appropriate hash function it will run in time O(1). Unfortunately, A
hash table requires separate entry for each possible value of the tested expression, making with
large value range.

Binary search can accommodate ranges easily . It runs in time O(log n), With a relatively low
constant factor

27. Describe three subtleties in the implementation of enumeration-


controlled loops.
Executed once for every value in a given finite set. Number of iterations is known before first
iteration begins.
28. Why do most languages not allow the bounds or increment of an
enumerationcontrolled loop to be floating-point numbers?
Because of numerical imprecision.

29. Why do many languages require the step size of an enumeration-


controlled loop to be a compile-time constant?
So the code can be generated at compile time for the loop control. The size of the loop is
known, so step-size needs to be available at compile time as well.

30. Describe the “iteration count” loop implementation. What problem(s) does
it solve?
Iteration count involve pre-computing the number of iterations that will take place in the loop
based on the step, and then decrementing this number.

Removes the problem of determining the sign of the step and figuring out how to implement it.
This is precomputed.

31. What are the advantages of making an index variable local to the loop it
controls?

32. Does C have enumeration-controlled loops? Explain.


C, C++,Java do not have enumeration controlled loops although the logically controlled for
statement can be used to create n enumeration- controlled loop:

33. What is a container (a collection)?


A container or collection is a set of well- defined objects of finite length.

34. Explain the difference between true iterators and iterator objects.
True iterators represents a subroutine that is permitted to contain yield statements, each of
which produces loop index value.

An iterator is a separate thread of control with its own program counter.

35. Cite two advantages of iterator objects over the use of programming
conventions in a language like C.
No yield statement and no separate thread-like context to enumerate values. Ordinary objects
that provides methods for initialization, generation of next index value , and texting for
completion.

You might also like