Model Question Paper Answer Key -Pp
Model Question Paper Answer Key -Pp
Readability: the ease with which programs can be read and understood.
a) Readability - It is the ability to understand the program, and then modify it.
Readability has strong relation with the language of choice and the application
i. Simplicity
• Programmers usually use the features they are familiar with, which are not
necessarily the ones that the readers of the program are familiar with.
• Most often a large number of features will do the same thing, causing
unnecessary confusion. That is, multiplicity of features does not help.
Example: i = i + 1; i+ = 1; i + +; + + i;
• Assembly languages are simple, but are too low-level (or not “sophisticated”
enough) to express our ideas.
ii. Orthogonality
VAX
through Orthogonality.
• Goto statements should be used with caution. They should precede their
destination, and never jump to distant labels.
• Adequate facilities for defining data types and structures aids readability.
v. Syntax Considerations
• Identifier form - should not be too restrictive on length. The length and valid
combination of characters.
• Special words - Words such as while, if, end, class, etc., have special meaning
within a program. How to express compound statement? Can special words be
used as identifiers?
• Form and meaning - How a statement appears should clear indicate whatit
actually means.
b) Writability - Does the language make it easy to write what you have in
mind?
Readability first, then writability. How easily can the language be used to
create programs for a particular domain? Consider Visual Basic (VB) and C for –
A graphical game program, – An embedded controller for automotive brakes.
• Procedure or Process abstraction isolate what a process does from how the
process does it. Procedural or process abstraction is a specification that:
- ignores implementation
- Can be applied in any language. Classic example: sorting, that is: We just want
to call a sort routine when we need to sort something. We don’t want to
clutter up code by implementing a sort algorithm every time.
• Data abstraction separate what an object should behave given certain events
from how it implements the actions.
iii. Expressivity
➢ Binding time is the time at which a binding is created or, more generally,
the time at which any implementation decision is made.
1. Language design time: In most languages, the control flow constructs, the
set of fundamental (primitive) types, the available constructors for creating
complex types, and many other aspects of language semantics are chosen
when the language is designed.
6. Load time: Load time refers to the point at which the operating system loads
the program into memory so that it can run.
7. Run time: Run time is actually a very broad term that covers the entire span
from the beginning to the end of execution. Bindings of values to variable
occur at run time, as do a host of other decisions that vary from language to
language.
1. Static
2. Dynamic
The terms STATIC and DYNAMIC are generally used to refer to things bound
before run time and at run time, respectively. It is difficult to overstate the
importance of binding times in the design and implementation of
programming languages. In general, early binding times are associated with
greater efficiency.
Later binding times are associated with greater flexibility. Compiled languages
tend to have early binding times. Interpreted languages tend to have later
binding times.
Binding time is the the moment when the binding is performed (compilation,
execution, etc).
• A binding is static if it first occurs before run time and remains unchanged
throughout program execution.
• A binding is dynamic if it first occurs during execution or can change during
execution of the program.
Possible Binding Time: Binding Time is the time at which a binding is created
compile time
link time
load time
Runtime
load time: Refers to the point at which the OS loads the program into memory
so that it can run. Virtual address are chosen at link time and physical
addresses change at run time.bind a variable to a memory cell (ex. C static
variables)
Runtime :refers to the entire span from the beginning to the end of execution.
Virtual functions, values to variables, many more. bind a nonstatic local
variable to a memory cell.
The terms static and dynamic are generally used to refer to things bound
before run time and at run time, respectively
A binding is static if it first occurs before run time and remains unchanged
throughout program execution
An ordinal type is one in which the range of possible values can be easily
associated with the set of positive integers. In Java, for example, the primitive
ordinal types are integer, char, and boolean.
There are two user-defined ordinal types that have been supported by
programming languages:
Enumeration Types
An enumeration type is one in which all of the possible values, which are
named constants, are provided, or enumerated, in the definition.
– integer
– char
– boolean
C# example:
– Ada, C#, and Java 5.0 provide better support for enumeration than C++
because enumeration type variables in these languages are not coerced into
integer types.
Finally, when all local variables are stack dynamic, subprograms cannot be
history sensitive; that is, they cannot retain data values of local variables
between calls.
The third characteristic (after abstract data types and inheritance) of object
oriented programming languages is a kind of polymorphism provided by the
dynamic binding of messages to method definitions. This is sometimes called
dynamic dispatch.
There is a base class, A, that defines a method draw that draws some
figure associated with the base class. A second class, B, is defined as a subclass
of A. Objects of this new class also need a draw method that is like that
provided by A, but a bit different because the subclass objects are slightly
different. So, the subclass overrides the inherited draw method. If a client of A
and B has a variable that is a reference to class A’s objects, that reference also
could point at class B’s objects, making it a polymorphic reference If the
method draw, which is defined in both classes, is called through the
polymorphic reference, the run-time system must determine, during
execution, which method should be called, A’s or B’s (by determining which
type object is currently referenced by the reference).
Polymorphism is a natural part of any object-oriented language that is
statically typed.
Shot note
• When a class hierarchy includes classes that override methods and such
methods are called through a polymorphic variable, the binding to the correct
method will be dynamic
An abstract method is one that does not include a definition (it only defines a
protocol)
A) No. Users do not need to write constructors for every class. A constructor
can be declared using any of the access modifiers. It is mandatory to have a
constructor with the right access modifier. However, the compiler supplies
default if an access modifier is not defined in the class and a constructor is not
declared.
Logic programming can be used in any domain where a large amount of data
must be analyzed to make decisions. However, it is most commonly applied to
a few subjects. Following are some places where logic programming is most
likely to be found.
• Evaluate all expressions, then bind the values to the names; evaluate the
body
(* x y)) =6
(plus +))
(* z x))
= 35
• Let* is similar to let, but the bindings are performed sequentially from left to
(* z x)))
=70
a) Readability - It is the ability to understand the program, and then modify it.
Readability has strong relation with the language of choice and the application
i. Simplicity
• Programmers usually use the features they are familiar with, which are
not necessarily the ones that the readers of the program are familiar with.
• Most often a large number of features will do the same thing, causing
Example: i = i + 1; i+ = 1; i + +; + + i;
• Assembly languages are simple, but are too low-level (or not “sophisticated”
enough) to express our ideas.
ii. Orthogonality
IBM
VAX
• Goto statements should be used with caution. They should precede their
destination, and never jump to distant labels.
• Sufficient data type and data structure support not only improve readability,
but also expressibility.
• Adequate facilities for defining data types and structures aids readability.
v. Syntax Considerations
• Identifier form - should not be too restrictive on length. The length and valid
combination of characters.
• Special words - Words such as while, if, end, class, etc., have special meaning
within a program. How to express compound statement? Can special words be
used as identifiers?
• Form and meaning - How a statement appears should clear indicate what it
actually means.
b) Writability - Does the language make it easy to write what you have in
mind?
Readability first, then writability. How easily can the language be used to
create programs for a particular domain? Consider Visual Basic (VB) and C for –
A graphical game program, – An embedded controller for automotive brakes.
• Procedure or Process abstraction isolate what a process does from how the
process does it. Procedural or process abstraction is a specification that:
- ignores implementation
• Data abstraction separate what an object should behave given certain events
from how it implements the actions.
iii. Expressivity
• The language should have convenient way to express computation, e.g., goto
may be sufficient, but while loop is better.
c) Reliability
i. Type Checking
• Is the process of verifying and enforcing the constraints of types, and it can
occur either at compile time (i.e. statically) or at runtime (i.e. dynamically).
That is, checks errors either at runtime or compile time.
• Early detection is less expensive than correcting released code - But forcing
early detection of type errors reduces writability and expressiveness.
- Many scripting languages freely cast types at runtime which can result in
bizarre and difficult to detect errors later.
Possible in any language, but some languages have better facilities than others.
iii. Aliasing
Two or more distinct names that refer to the same memory location or object.
A reliable program is written with the best method, the best method is
implemented only when we understand what exactly we want to do.
A language that does not support natural ways of expressing an algorithm will
require the use of “unnatural” approaches, and hence reduced reliability.
Cost to execute the program Most likely determined by what language you
use.
y : integer := 4
procedure add
x := x + y
x : integer := 5
P()
procedure first
y : integer := 6
second(add)
first()
write integer(x)
(a) What does this program print if the language uses static scoping? Give
reasons.
(b) What does it print if the language uses dynamic scoping? Give reasons.
12.(a) With respect to storage binding, explain the meanings, purposes,
advantages and disadvantages of four categories of scalar variables.
Object Storage Management
i. Static
ii. Stack
iii. Heap
memory cell. So the lifetime of a var begins when it is bound to a specific cell
and ends when it is unbound from that cell.
1. static
2. stack-dynamic,
3. explicit heap-dynamic
4. implicit heap-dynamic
Static variables
➢ Bound to memory cells before execution begins and remains bound to the
same memory cell throughout execution.
➢ Advantages:
➢ Disadvantage:
o Eg: if two large arrays are used by two subprograms, which are never
active at the same time, they cannot share the same storage for their arrays.
➢ Eg: The variable declarations that appear at the beginning of a Java method
are elaborated when the method is invoked and the variables defined by those
declarations are deallocated when the method completes its execution.
➢ Advantages:
o Allows recursion: each ac tive copy of the recursive subprogram has its
own version of the local variables.
➢ Disadvantages:
➢ In Java, C++, and C#, variables defined in methods are by default stack
dynamic.
➢ These vars, which are allocated from and deallocated to the heap, can only
be referenced through pointers or reference variables.
int *intnode;
// intnode points
➢ Explicit heap-dynamic vars are used for dynamic structures, such as linked
lists and trees that need to grow and shrink during execution.
➢ Advantage
➢ Disadvantage
➢ Bound to heap storage only when they are assigned value. Allocation and
deallocation caused by assignment statements.
➢ All their attributes are bound every time they are assigned. E.g. all variables
in APL; all strings and arrays in Perl and JavaScript.
➢ Advantage
➢ Disadvantages
referencing environment at the indicated program points (1), (2), (3) & (4) for
statically scoped.
program example;
var a, b : integer;
procedure sub1;
var x, y: integer;
begin { sub1 }
........... (1)
end { sub1 }
procedure sub2;
var x : integer;
........
procedure sub3;
var x: integer;
begin { sub3 }
........... (2)
end { sub3 }
begin { sub2 }
........... (3)
end { sub2}
begin {example}
........... (4)
end {example }
Consider the following program; assume that the only function calls are
void sub1( )
int a, b;
…1
} /* end of sub1 */
void sub2( )
int b, c;
…2
sub1;
} /* end of sub2 */
void main ( )
int c, d;
…3
sub2( );
} /* end of main */
3 c and d of main