Вы находитесь на странице: 1из 15

PPL - November 2017

1.a) List the programming paradigms. For any three state which programming languages
are based on them and how? (6 Marks)

ANSWER:

Name of programming paradigms-

1. Procedural Programming
Language Construct used: procedures

2. Functional Programming
Language Construct used: functions

3. Abstract Data Type Programming


Language Construct used: definition of abstract data types

4. Module Based Programming


Language Construct used: modularization (grouping of entities) of
Variables
Functions
Procedures
Types

5. Object oriented Programming


Language Construct used: classes and objects

6. Generic Programming
Language Construct used: templates in C

7. Declarative or logical Programming

b) What are benefits of implementing built-in data types in programming languages?


State the built-in data types implemented by C++. (7 Marks)

ANSWER:

Hiding underlying representation

Correct use of variables which can be checked at compile time

Resolution of overloaded operators


Accuracy control

 booleans, i.e., truth values TRUE and FALSE, along with the set of operations defined
by Boolean algebra;
• characters, e.g., the set of ASCII characters;
• integers, e.g., the set of 16-bit values in the range <-32768, 37767>; and
• reals, e.g., floating point numbers with given size and precision.

OR

2.a) What is interpretation and translation process? With neat diagram state the purpose
of each activity in language processing with interpretation and translation. (6 Marks)

ANSWER:

Interpretation
In this solution, the actions implied by the constructs of the language are executed directly.
Usually, for each possible action there exists a subprogram–written in machine language–to
execute the action. Thus, interpretation of a program is accomplished by calling subprograms in
the appropriate sequence.
More precisely, an interpreter is a program that repeatedly executes the following
sequence.
1. Get the next statement;
2. Determine the actions to be executed;
3. Perform the actions;
This sequence is very similar to the pattern of actions carried out by a traditional
computer, that is:
1. Fetch the next instruction (i.e., the instruction whose address is specified by the instruction
pointer).
2. Advance the instruction pointer (i.e., set the address of the instruction to be fetched next).
3. Decode the fetched instruction.
4. Execute the instruction.

Translation
In this solution, programs written in a high-level language are translated into an equivalent
machine-language version before being executed. This translation is often performed in several
steps. Program modules might first be separately translated into relocatable machine code;
modules of relocatable code are linked together into a single relocatable unit; finally, the entire
program is loaded into the computer’s memory as executable machine code. The translators
used in each of these steps have specialized names: compiler, linker (or linkage editor), and
loader, respectively.

b) What are generic data structures and generic algorithms? How C++ implements this
generic programming constructs? Give example of each. (6 Marks)

ANSWER:

Generic data structures


Let us first consider the development of libraries of standard data structures, for example,
stacks and queues. What should be the types of elements stored in these structures? Early
typed languages such as Pascal and C require the designer to define one structure for each
data type to be supported. This is an unsatisfactory solution for two reasons: one is that the
solution only works for only the types the library designer knows about and not for any types to
be defined by the user of the library; the second is that the solution forces the library designer
towards code duplication. C++ templates and Ada generics allow us to avoid such code
duplication and define a template data structure that is independent of the type of the element to
be stored in the structure. For example, we can define a generic pair data structure in C++:

template <class T1, class T2>


class pair {
public:
T1 first;
T2 second;
pair (T1 x, T2 y) : first(x), second(y) { }
};
The template parameters T1 and T2 stand for any type. We may “instantiate” a particular pair by
supplying concrete types for T1 and T2. For example, we may create a pair of integers or a
string, integer pair or a pair of employees:
pair<int, int> intint(2, 1456);
pair<string, int> stringint(“Mehdi”, 46);
pair<employee_t, employee_t> (jack, jill); /*pair of user-defined type employee_t*/
We may refer to pair as a parameterized or generic type. The template of C++ allows us to
define such a parameterized type which may later be used to create concrete types such as
pair<int, int>. C++’s template facility is particularly general because it uses classes as
parameters and classes represent types uniformly: we may instantiate a template with either
user-defined or primitive types. Eiffel supports a similar scheme for generic classes, with which,
for example, we can define a class stack [T] and then instantiate an intstack from stack[integer].

Generic algorithms
Templates may also be used to define generic algorithms. For example, in the following generic
function swap which interchanges the values of its two parameters:
template <class T>
void swap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
This function may be used for any two parameters of the same type that support the “=”
operation. Therefore, we can use it to swap integers, reals, and even user-defined types such
as pairs. This is quite a useful facility because it gives us the possibility to write higher-level
generic functions such as sort if they only use generic functions. The ability to write such
generic functions is helped in C++ by the fact that generic functions do not have to be
instantiated to be used. To use a template data structure, C++ requires explicit instantiation of
the structure, as we saw, for example, in pair<int,int>. For functions, on the other hand, explicit
instantiation is not necessary.
The same swap function is defined in Ada as:
generic
type T is private;
procedure swap (x, y: T) is
begin
temp: T = x;
x = y;
y = temp;
end swap;
The generic is explicitly stated to be based on a type T which is private
b) Justify the meaning of each characteristic of Java in the statement “Java is simple,
architecture neutral, portable, interpreted and robust and secured programming
language”. (6 Marks)

ANSWER:

1. Simple

According to Sun, Java language is simple because:

 syntax is based on C++ (so easier for programmers to learn it after C++).
 removed many confusing and/or rarely-used features e.g., explicit pointers, operator
overloading etc.
 No need to remove unreferenced objects because there is Automatic Garbage
Collection in java.

2. architecture neutral
 There is no implementation dependent features e.g. size of primitive types is fixed.
 In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4
bytes of memory for 64-bit architecture. But in java, it occupies 4 bytes of memory for
both 32 and 64 bit architectures.

3. portable

We may carry the java bytecode to any platform.

4. Interpreted
Because Java byte code is interpreted by JVM.

5. Robust

Robust simply means strong. Java uses strong memory management. There is lack of pointers
that avoids security problem. There is automatic garbage collection in java. There is exception
handling and type checking mechanism in java. All these points make java robust.

6. Secured

Java is secured because:

 No explicit pointer
 Java Programs run inside virtual machine sandbox
 Classloader: adds security by separating the package for the classes of the local file
system from those that are imported from network sources.
 Bytecode Verifier: checks the code fragments for illegal code that can violate access
right to objects.
 Security Manager: determines what resources a class can access such as reading and
writing to the local disk.

These security are provided by java language. Some security can also be provided by
application developer through SSL, JAAS, Cryptography etc.

OR

4.a) What are challenges for programming in Large? How these are addressed by programming
languages?

b) Write a program in Java to perform the addition of two matrices (multidimensional


array) and set the diagonal elements of resultant matrix to 0. (6 Marks)

ANSWER:

import java.util.Scanner;

class AddTwoMatrix
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows and columns of matrix");


m = in.nextInt();
n = in.nextInt();

int first[][] = new int[m][n];


int second[][] = new int[m][n];
int sum[][] = new int[m][n];

System.out.println("Enter the elements of first matrix");

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
first[c][d] = in.nextInt();

System.out.println("Enter the elements of second matrix");

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
second[c][d] = in.nextInt();

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
{
if(c==d)
sum[c][d] = 0;
}

System.out.println("Sum of entered matrices:-");

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+"\t");

System.out.println();
}
}
}

5.a) Explain the concept of dynamic dispatch while overriding method in inheritance.
Give example and advantages of doing so. (5 Marks)

ANSWER:

Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved
at run time, rather than compile time. Dynamic method dispatch is important because this is how
Java implements run-time polymorphism. Let’s begin by restating an important principle: a
superclass reference variable can refer to a subclass object. Java uses this fact to resolve calls
to overridden methods at run time. Here is how. When an overridden method is called through a
superclass reference, Java determines which version of that method to execute based upon the
type of the object being referred to at the time the call occurs. Thus, this determination is made
at run time.
When different types of objects are referred to, different versions of an overridden method will
be called. In other words, it is the type of the object being referred to (not the type of the
reference variable) that determines which version of an overridden method will be executed.
Therefore, if a superclass contains a method that is overridden by a subclass, then when
different types of objects are referred to through a superclass reference variable, different
versions of the method are executed.
Here is an example that illustrates dynamic method dispatch:
// Dynamic Method Dispatch
class A
{
void callme()
{
System.out.println("Inside A's callme method");
}
}
class B extends A
{
// override callme()
void callme()
{
System.out.println("Inside B's callme method");
}
}
class C extends A
{
// override callme()
void callme()
{
System.out.println("Inside C's callme method");
}
}
class Dispatch
{
public static void main(String args[])
{
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme
r = c; // r refers to a C object
r.callme(); // calls C's version of callme
}
}
The output from the program is shown here:
Inside A's callme method
Inside B's callme method
Inside C's callme method
This program creates one superclass called A and two subclasses of it, called B and C.
Subclasses B and C override callme( ) declared in A. Inside the main( ) method, objects of type
A, B, and C are declared. Also, a reference of type A, called r, is declared. The program then in
turn assigns a reference to each type of object to r and uses that reference to invoke callme( ).
As the output shows, the version of callme( ) executed is determined by the type of object being
referred to at the time of the call. Had it been determined by the type of the reference variable, r,
you would see three calls to A’s callme( ) method.

b)Write a program in Java which defines Class CONVERSION which converts one unit of
length into another using multiplying factor. This class has data members unit_in,
unit_out and multiplier. When user creates object, constructor accepts value of multiplier
and sets this for further conversion of units. The object uses methods to get value of
unit_in and output value of unit_out and stores these in class variables. (8 Marks)

ANSWER:

import java.util.Scanner;

public class CONVERSION


{
float unit_in, unit_out, multiplier;
public static Scanner scan;
public CONVERSION(float multiplier_in)
{
multiplier = multiplier_in;
}
public void get_values()
{
System.out.println("Enter value of unit_in:");
unit_in = scan.nextFloat();
}
public void convert_units()
{
unit_out = unit_in * multiplier;
System.out.println("unit_out="+unit_out);
}
public static void main(String args[])
{
scan = new Scanner(System.in);
CONVERSION obj = new CONVERSION(3.28); //meter to feet
obj.get_values();
obj.convert_units();
}
}

OR
6.a) State two major differences in class and an interface. “Interface gives multiple
inheritance facility just as in C++” justify. (7 Marks)

ANSWER:

BASIS FOR
CLASS INTERFACE
COMPARISON

Basic A class is instantiated to create An interface can never be instantiated as

objects. the methods are unable to perform any

action on invoking.

Keyword class interface

Access specifier The members of a class can be The members of an interface are always

private, public or protected. public.

Methods The methods of a class are The methods in an interface are purely

defined to perform a specific abstract.

action.

Implement/Extend A class can implement any An interface can extend multiple

number of interface and can interfaces but can not implement any

extend only one class. interface.

Constructor A class can have constructors to An interface can never have a

initialize the variables. constructor as there is hardly any

variable to initialize.
When one class extends more than one classes then this is called multiple inheritance. For
example: Class C extends class A and B then this type of inheritance is known as multiple
inheritance. Java doesn’t allow multiple inheritance. In this article, we will discuss why java
doesn’t allow multiple inheritance and how we can use interfaces instead of classes to achieve
the same purpose.

Why Java doesn’t support multiple inheritance?

C++ , Common lisp and few other languages supports multiple inheritance while java doesn’t
support it. Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it. One of the
example of such problem is the diamond problem that occurs in multiple inheritance.

Interface gives multiple inheritance facility just as in C++ because we can implement
more than one interfaces in our program because that doesn’t cause any ambiguity.

interface X
{
public void myMethod();
}
interface Y
{
public void myMethod();
}
class JavaExample implements X, Y
{
public void myMethod()
{
System.out.println("Implementing more than one interfaces");
}
public static void main(String args[]){
JavaExample obj = new JavaExample();
obj.myMethod();
}
}
Output:

Implementing more than one interfaces

b) State the use of the following constructs in Java with example:

1. final method declaration in super class while inheritance

2. abstract class declaration


3. method overriding (6 Marks)

ANSWER:

1. final method declaration in super class while inheritance

While method overriding is one of Java’s most powerful features, there will be times when you
will want to prevent it from occurring. To disallow a method from being overridden, specify final
as a modifier at the start of its declaration. Methods declared as final cannot be overridden. The
following fragment illustrates final:
class A
{
final void meth()
{
System.out.println("This is a final method.");
}
}
class B extends A
{
void meth()
{ // ERROR! Can't override.
System.out.println("Illegal!");
}
}
Because meth( ) is declared as final, it cannot be overridden in B. If you attempt to do so, a
compile-time error will result. Methods declared as final can sometimes provide a performance
enhancement: The compiler is free to inline calls to them because it ―knows‖ they will not be
overridden by a subclass.

7.a) Define the term exception, State the advantage of exception handling. What are types
of exceptions? (6 Marks)

ANSWER:

Exception in Java is an indication of some unusual event. It usually indicates the error.

Advantages of Exception Handling:

1. Using exceptions the main application logic can be separated out from the code which
may cause some unusual conditions.
2. When calling method encounters some error then the exception can be thrown. This
avoids crashing of the entire application abruptly.
3. Using exception handling, various types of errors in the source code can be grouped
together.
4. Problems occurring at the lower level can be handled by the higher methods.
b) State the use of the following methods for programming applet. Give example of using
each of these, init(),. Start(), paint(), stop(), destroy(), update(). (6 Marks)

ANSWER:

 init − This method is intended for whatever initialization is needed for your applet. It is
called after the param tags inside the applet tag have been processed.
 start − This method is automatically called after the browser calls the init method. It is
also called whenever the user returns to the page containing the applet after having
gone off to other pages.
 stop − This method is automatically called when the user moves off the page on which
the applet sits. It can, therefore, be called repeatedly in the same applet.
 destroy − This method is only called when the browser shuts down normally. Because
applets are meant to live on an HTML page, you should not normally leave resources
behind after a user leaves the page that contains the applet.
 paint − Invoked immediately after the start() method, and also any time the applet
needs to repaint itself in the browser. The paint() method is actually inherited from the
java.awt.

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.applet.Applet;
import java.awt.Graphics;

public class ExampleEventHandling extends Applet implements MouseListener {


StringBuffer strBuffer;

public void init() {


addMouseListener(this);
strBuffer = new StringBuffer();
addItem("initializing the apple ");
}

public void start() {


addItem("starting the applet ");
}

public void stop() {


addItem("stopping the applet ");
}

public void destroy() {


addItem("unloading the applet");
}

void addItem(String word) {


System.out.println(word);
strBuffer.append(word);
repaint();
}

public void paint(Graphics g) {


// Draw a Rectangle around the applet's display area.
g.drawRect(0, 0,
getWidth() - 1,
getHeight() - 1);

// display the string inside the rectangle.


g.drawString(strBuffer.toString(), 10, 20);
}

public void mouseEntered(MouseEvent event) {


}
public void mouseExited(MouseEvent event) {
}
public void mousePressed(MouseEvent event) {
}
public void mouseReleased(MouseEvent event) {
}
public void mouseClicked(MouseEvent event) {
addItem("mouse clicked! ");
}
}

OR

8.a) What is difference between byte streams and character streams? Demonstrate the
use of console class to get inputs and show results. (6 Marks)

ANSWER:

Sr. No. Byte Stream Character Steam

The byte stream is used for inputting and The character stream is used for inputting
1
outputting the bytes and outputting the characters
There are two super classes used in byte There are two super classes used in
2 stream and those are- character stream and those are-
InputStream and OutputStream Reader and Writer class
A byte is a 8-bit number type that can
represent values from -127 to 127. Only A character is 16 bit in size that can
3
ASCII values can be represented with represent Unicode.
exactly 8 bits.
4 Data only ASCII format can be handled. Data in Unicode can be handled.

import java.io.Console;
class ReadStringTest{
public static void main(String args[]){
Console c=System.console();
System.out.println("Enter your name: ");
String n=c.readLine();
System.out.println("Welcome "+n);
}
}

b) Write a program in Java to calculate the value of ((x + y)/(x – y)). Program should
prevent the condition x – y = 0. (6 Marks)

ANSWER:

class ExceptionDemo
{
static void fun(int x, int y)
{
int c;
try
{
c = ((x + y)/(x - y));
}
catch(ArithmeticException e)
{
System.out.println("Caught Exception: "+ e);
}
}
public static void main(String args[])
{
fun(2,2);
}
}

Вам также может понравиться