Sei sulla pagina 1di 5

Subject: Programming Languages (PC223CS)

PL – Question and Answers


Branch: CSE
PART-A
1. Define Abstract data type
Data type that satisfies the following conditions:
 -The representation of objects of the type is hidden from the program units that use
the type
 The declarations of the type and the protocols of the operations on objects of the
type, which provide the type’s interface, are contained in a single syntactic unit.
2. What are the advantages of the non pointer concept in Java?
 Instead of complex pointer manipulation on array pointers, we access arrays by their
arithmetic indices. The Java run-time system checks all array indexing to ensure
indices are within the bounds of the array. We no longer have dangling pointers and
trashing of memory because of incorrect pointers, because there are no pointers in
Java.
3. List few characteristics of Python language.
 Easy to learn and program and is object oriented.
 Rapid application development
 Readability is better
 It can work with other languages such as C,C++ and Fortran
 Powerful interpreter
 Extensive modules support is available
4. Distinguish between functional languages and imperative languages

Imperative Functional

1. The same name may be associated 1. A name is only ever associated with
with different values. one value.
2. Efficient execution 2. Inefficient execution
3. Complex semantics 3. Simple semantics
4. Complex syntax 4. Simple syntax
5. Concurrency is programmer 5. Programs can automatically be made
designed concurrent
6. Order of execution is Important 6. Order of execution is low important

PART-B
5. (a) Define subprograms. What are the advantages of subprograms? Explain
different methods of parameter passing mechanisms to subprograms

Ans. A subprogram definition describes the interface to and the actions of the
subprogram abstraction
Advantages:
 They help keep the code simple, more readable;
 They allow the programmer to define needed functions;
 They can be used in other programs.
 provide modularity and promote reusability and maintainability.
Parameter Passing Methods
--Semantics models of parameter-passing methods.
--implementation models for these semantics models

1
Semantic Models
Formal parameters are characterized by one of three distinct semantics models:
(1) They can receive data from the corresponding actual parameter -- in mode
(2) they can transmit data to the actual parameter -- out mode or
(3) they can do both -- inout mode.
Implementation Models of Parameter Passing
A variety of models have been developed by language designers to guide the
implementation of the three basic parameter transmission modes.
1. Pass-by-value
2. Pass-by-result
3. Pass-by-value-result
4. Pass-by-reference
5. Pass-by-name
1. Pass-by-value (In Mode)
•The value of the actual parameter is used to initialize the corresponding formal
parameter
2. Pass-by-Result (Out Mode)
 When a parameter is passed by result, no value is transmitted to the
3. Pass-by-Value-Result (inout Mode)
 •A combination of pass-by-value and pass-by-result
 • •Formal parameters have local storage
4. Pass-by-Reference (Inout Mode)
Also called pass-by-sharing
 •Pass an access path
5. Pass-by-Name (Inout Mode)
 •By textual substitution
 •Formals are bound to an access method at the time of the call, but actual binding to
a value or address takes place at the time of a reference or assignment

b) Describe about ADA’s exception handling mechanism with example.


Ans. Exception Handling in Ada
 The frame of an exception handler in Ada is either a subprogram body, a package
body, a task, or a block
 Because exception handlers are usually local to the code in which the exception can
be raised, they do not have parameters
Ada Exception Handlers
 Handler form:
when exception_choice{|exception_choice} => statement_sequence
...
[when others =>
statement_sequence]

exception_choice form:
exception_name | others
 Handlers are placed at the end of the block or unit in which they occur
Binding Exceptions to Handlers
 If the block or unit in which an exception is raised does not have a handler for that
exception, the exception is propagated elsewhere to be handled
o Procedures - propagate it to the caller
o Blocks - propagate it to the scope in which it appears

2
o Package body - propagate it to the declaration part of the unit that declared the
package (if it is a library unit, the program is terminated)
o Task - no propagation; if it has a handler, execute it; in either case, mark it
"completed"
Other Design Choices
 User-defined Exceptions form:
o exception_name_list : exception;
 Raising Exceptions form:
o raise [exception_name]
o (the exception name is not required if it is in a handler--in this case, it
propagates the same exception)
 Exception conditions can be disabled with:
o pragma SUPPRESS(exception_list)
Predefined Exceptions
 Constraint_Error - index constraints, range constraints, etc.
 Program_Error - call to a subprogram whose body has not been elaborated
 Storage_Error - system runs out of heap
 Tasking_Error - an error associated with tasks
Evaluation
 Ada was the only widely used language with exception handling until it was added to
C++
 The propagation model allows exceptions to be propagated to an outer scope in which
the exception would not be visible
 It is not always possible to determine the origin of propagated exceptions
 Exception handling is inadequate for tasks
6. a) Explain about LISP and HASKELL Functional programming languages

Ans. LISP Data Types and Structures

 •Data object types: originally only atoms and lists


 •List form: parenthesized collections of sublists and/or atoms
 e.g., (A B (C D) E)
 •Originally, LISP was a typeless language
•LISP lists are stored internally as single-linked lists

LISP Interpretation

 Lambda notation is used to specify functions and function definitions. Function


applications and data have the same form.
e.g., If the list (A B C) is interpreted as data it is
a simple list of three atoms, A, B, and C
If it is interpreted as a function application,
it means that the function named A is
applied to the two parameters, B and C
 The first LISP interpreter appeared only as a demonstration of the universality of the
computational capabilities of the notation

Haskell

3
 •Similar to ML (syntax, static scoped, strongly typed, type inferencing, pattern
matching)
 •Different from ML (and most other functional languages) in that it is purely functional
(e.g., no variables, no assignment statements, and no side effects of any kind)

Function Definitions with Different Parameter Ranges

 fact n
 | n == 0 = 1
 | n > 0 = n * fact(n – 1) sub n
 | n < 10 = 0
 | n > 100 = 2 | otherwise = 1
 square x = x * x - Works for any numeric type of x

b) Write the following: (i) Semaphores (ii) Logic Programming


(i) Semaphores
 A semaphore is a data structure consisting of a counter and a queue for storing task
descriptors
 Semaphores can be used to implement guards on the code that accesses shared data
structures
 Semaphores have only two operations, wait and release
 Semaphores can be used to provide both competition and cooperation synchronization
 Semaphores: Wait Operation
wait(aSemaphore)
if aSemaphore’s counter > 0 then
decrement aSemaphore’s counter
else
put the caller in aSemaphore’s queue
attempt to transfer control to a ready task
End
 Semaphores: Release Operation
release(aSemaphore)
if aSemaphore’s queue is empty then
increment aSemaphore’s counter
else
put the calling task in the task ready queue
transfer control to a task from aSemaphore’s queue
end
(ii) Logic Programming
 Logic programming languages, sometimes called declarative programming Languages.
 Express programs in a form of symbolic logic.
 Use a logical inferencing process to produce results.
 Declarative rather that procedural:
Proposition:
 A logical statement that may or may not be true.
o –Consists of objects and relationships of objects to each other.
Symbolic Logic:
 Logic which can be used for the basic needs of formal logic:
o –Express propositions.
o –Express relationships between propositions.

4
o –Describe how new propositions can be inferred from other propositions. (Particular
form of symbolic logic used for logic programming called predicate Calculus)
Object Representation
 •Objects in propositions are represented by simple terms: either constants or variables
 •Constant: a symbol that represents an object
 •Variable: a symbol that can represent different objects at different times

Potrebbero piacerti anche