You are on page 1of 18

Page 1 of 18

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION


(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer

Q1. A. Attempt any three of the following.

a. (Half mark for each data type and its size)


Type Size
byte One byte
short Two bytes
int Four bytes
long Eight bytes
float Four Bytes
double Eight Bytes
char Two bytes
boolean One bit

b. (Definition of Thread: 1 mark, Thread Priority 3 marks)


A thread is a program that has single flow of control. It has a beginning, a body and an end and
executes commands sequentially.
Thread Priority:
A thread can be assigned a priority, which affects the order in which it is scheduled for
running. Thread with same priority are given equal treatment by the Java scheduler and
hence they share the processor on a first come first serve basis.
The priority of a thread can be set using setPriority(() method as follows
ThreadName.setpriority(intNumber);
The Thread class defines priority constants:
MIN_PRIORITY = 1
NORM_PRIORITY = 5
MAX_PRIORITY = 10
In a multithreading system, the Java system chooses the highest priority thread and executes it. The
highest priority thread always preempts the lower priority threads.

c. (1 mark for each point, minimum 4 points)

To be truly considered "object oriented", a programming language should support at a minimum four
characteristics. Java supports all of these features.
Objects and Classes—Objects are the basic run-time entities. The entire set of data and
the code of an object are defined in a class.
Data Abstraction and Encapsulation—wrapping up of data and methods in a single
unit called class.
Polymorphism--the same message sent to different objects results in behavior that's
dependent on the nature of the object receiving the message
Inheritance—It is the process by which objects of one class acquire the properties of
objects of another class.
Page 2 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
Dynamic binding— The code associated with a given procedure call is not known until
the time of the call at run time.
Message Communication: A message for an object is a request for execution of a
procedure, and therefore will invoke a method in the receiving object that generates the
desired result.
d. Built in Packages from Java API (1 mark for each package and its use, Any four)
Sr. No. Package Name Use
1. Java.lang Language support classes
2. Java.util Language utility classes
3. Java.io Input/Output support classes
4. Java.awt Set of classes for implementing graphical user interface
5. Java.net Classes for networking
6. Java.applet Classes for creating and implementing applets

Q1 (B)
a. Wrapper Classes (3 marks for wrapper classes, 3 marks for methods)
1. Wrapper Classes for converting simple types
2. Converting Primitive Numbers to Object Numbers using Constructor methods
3. Converting Object Numbers to Primitive Numbers using typeValue() method
4. Converting Numbers to strings using toString() method.
5. Converting String Objects to Numeric Objects using static method valueOf()
6. Converting Numeric Strings to Primitive Numbers using parsing methods

Methods of Integer wrapper class (Any four)


1. Int compareTo(int i)
2. Boolean equals(Object i)
3. String toString()
4. static Integer valueOf(String str) throws NumberFormatException
5. static Int parseInteger(String str) throws NumberFormatException
6. static Strinf toString(int num)
7. static String toBinaryString(int num)
8. static String toHexString(int num)
9. static String toOctalString(int num)

b. (logic 2 marks, Syntax 2 marks, Result 2 marks) (Any other suitable program based on the same
concept can also be considered.)

class percentage
{
public static void main(String args[])
{
int sub1 = 90;
int sub2 = 95;
int sub3 = 79;
Page 3 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
int sub4= 90;
double total = sub1+ sub2 + sub3 + sub4;
double per = ( total)/4;
System.out.println("Percentage = "+per);
}
}
Q.2

a. (4 marks for overloading, 4 marks for overriding)

Sr. overloading overriding


No.

1. It happens within the same class It happens between super class and subclass

2. Method signature should not be Method signature should be same.


same

3. Method can have any return type Method return type should be same as super class
method

4. Method can have any access level Method must have same or wide access level than
super class method access level

5. Overloading is early binding or static Overriding is late binding or dynamic binding


binding

6. They have the same name but, have They have the same name as a superclass method.
different parameter lists, and can They have the same parameter list as a superclass
have different return types. method. They have the same return type as a
superclass method

7. It is resolved at compile time It is resolved at runtime.

8. For e.g. Simple program showing For e.g. Simple program showing overriding
overloading

b. (2 mark for Exception, 2 mark for handling exceptions, 4 marks for any four exceptions)
Exception is a condition that is caused by a run-time error in the program. When Java interpreter
encounters an error, it creates an exception object and throws it.
Why handle exceptions?
If the exception object is not caught and handled properly, the interpreter displays an error message and
will terminate the program. If the execution of the remaining code is to continue, then exception must
be caught and handled properly.
Page 4 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
List of exceptions
1. ArithmeticException
2. ArrayIndexOutOfBoundsException
3. ArrayStoreException
4. FileNotFoundException
5. IOException
6. NullPointerException
7. NumberFormatException
8. OutOfMemoryException
9. SecurityException
10. StackOverFlowException
11. StringIndexOutOfBoundsException

c. (Logic 3 marks, Syntax 3 marks, Design 2 marks ) (Any other suitable program based on
the same concept can also be considered.)

class thread1 extends Thread


{
public void run()
{
for (int i = 1; i <= 10; i++)
{
System.out.println("Thread 1 = "+i);
}
}
}
class thread2 extends Thread
{
public void run()
{
for (int j = 10; j >= 1; j--)
{
System.out.println("Thread 2 = "+j);
}
}
}
class ThreadTest
{
public static void main(String args[])
{
new thread1().start();
new thread2().start();
}
}
Page 5 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
Q.3

(a) Any four features of JAVA

1 M <-

1. Compile & Interpreted:

JAVA is a two –stage system. It combines both approaches. First java compiler translates source code
into byte code instruction. Byte codes are not machine instructions. In the second stage java
interpreter generates machine code that can be directly executed by machine machine . Thus, java is
both compile & interpreted language.

1 M <-

2. Platform independent & portable

Java programs are portable i.e. it can be easily moved from one computer system to another. changes
in OS, processor, system resources won’t force any change in java programs. Java compiler generates
byte code instructions that can be implemented on any machine as well as the size of primitive data
type is machine independent.

1 M <-

3. Object oriented

Almost everything in java is in form of object. All program codes & data reside within objects & classes
Similar to other oop language java also have basic oop properties such as encapsulation,
polymorphism, data abstraction, inheritance etc. Java comes with an extensive set of classes in
packages.

1 M <-

4. Multithreaded

It can handle multiple tasks simultaneously. Java makes this possible with the feature of
multithreading. This means that we need not wait for the application to finish one task before
beginning other.

(Any other valid feature can be considered, for each feature, 1 mark)

Q.3

(b) Bytecode (Explanation 4 marks)

To solve security & portability problems output of java compiler is not executable code. Java compiler
produces intermediate code known as bytecode , for a machine that does not exit. This machine is
called as ‘Java Virtual Machine ‘& it only exist inside the computer memory.
Page 6 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
Bytecode is a highly optimized set of instructions designed to be executed by the java run-time
system which is called the JVM (Java Virtual Machine). JVM is an interpreter for the bytecode. The
virtual machine code generated by java interpreter.
By translating java program into bytecode makes it easier to run the program on a variety of
environment thus JVM provides the most convenient way of making portable program. The bytecode
requires the JVM for execution it makes the secure.
Q.3
(c) Define Interface ( 2 marks)
Java does not support multiple Inheritance i.e. classes in java con not have more than one super
classes.
eg. class A extends B extends C//multiple
{ } Inheritance is not supported
Java provides an alternate approach known as interface to support concept of multiple inheritance.
An interface is basically a kind of class. It can define only abstract method & final fields/data
members. The datafields contain constant value.
Structure of Interface (2 M)
access interface Interfacename
{
returntype method-name1(parameter list);
returntype method-name2(parameter list);
type final-variable1=value1;
type final-variable2=value2;
:
:
:
returntype method-name n(parameter list);
type final-variable n=value n;
}
where,
access is either public or not used
interface is the keyword
Interface name is the valid java identifier

Q. 3
(d) (Package and its creation 2 marks , example 2 marks)
Java provides a mechanism for partitioning the class name space into more manageable parts.
This mechanism is the package. The package is both naming & visibility controlled mechanism.
Package can be created by including ‘package’ as the first statement in java source code. Any
classes declared within that file will belong to the specified package. Package defines a namespace in
which classes are stored. The syntax for defining a package is ,
package package-name;
For e.eg. package mypack;
Page 7 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
Java uses file system directories to store packages e.g. The .class files of any classes which are
declared in a package must be stored in a directory which has same name as package name. The
directory name must match with the package name exactly. A hierarchy can be created by seperaring
package name & subpackage name by a(.) as mypack.mypack1.mypack2; which requires a directory
structure as mypack1\mypack2\mypack3.
Example:
package mypack;
public class balance
{
int bal;
public balance (int b)
{
bal=b;
}
public void show( )
{
system.out.println(“balance:”+bal);
import mypack.*;
class test
{
public static void main ( string args[ ] )
{
balance b = new balance (1200);
b.show( )
}
}
(Any other suitable program based on the same concept can also be considered.)

Q.3
(e) (4 marks)
import java.io.*;
class myfile
{
public static void main(String args[])
{
int ch=0;
File f=new File("abc.txt");
try
{
FileReader in=new FileReader(f);
while(ch!=-1)
{
ch=in.read();
System.out.println((char)ch);
}
Page 8 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
}
catch(IOException e)
{}
}
}

(Any other suitable program based on the same concept can also be considered.)

Q.4
(A)
(a) Four methods from graphics class
1. drawLine( ) : (1 Mark)
The drawLine( ) method is used to draw line which takes pair of coordinates (x1,y1) & (x2,y2)
as arguments. The graphics object g is passed to paint ( ) method. The syntax is
g.drawLine (x1, y1, x2, y2)
For e.g. g.drawLine (20, 20, 60, 60);
2. drawRect( ): (1 Mark)
To draw a rectangle drawRect( ) method is used , which takes four arguments. The first two arguments
represents x & y coordinates of top left corner of rectangle & the height of rectangle in pixels.
syntax:
g.drawRect ( x, y , width , height);
3. drawRoundRect ( ) : (1 mark)
This method is used to draw rectangle with rounded corner. This method takes 6 parameters
first four are similar to drawRect( ) method . The two extra arguments represent the width & height
of angle of corners. These extra parameters indicate how much of corners will be rounded.
For e.g. g.drawRoundRect (10, 100, 80, 50, 10,100)
4. drawOval( ): (1 mark)
The drawOval( ) method is used to draw circle & ellipse . The drawOval( ) method takes four
arguments, first two represents the top left corners & other two represents width & height of Oval.
For example: g.drawOval(20,20,200,120)
If width & height arguments are same , then oval becomes circle.
For example:
g.drawOval(70,30,100,100)
(Any other valid method can be considered. For each method, 1 M)

(b) Serialization:
(Explanation 4 marks)

It’s a process of write state of an object to a byte stream. These objects may deserialize later on for
restoring.
Serialization is also needed to implement Remote Method Invocation (RMI). RMI allows a java
object on one machine to invoke a method of java object on a different machine. An object may be
supplied as an argument to remote method. The sending machine serializes the object & transmits it.
The receiving machine deserializes it.
Only an object that implements Serializable interface can be saved & restored by the
serialization methods. Serializable interface does not define any member. It is simply used to indicate
that the class may be serialized.
If a class is serializable all its subclasses are also serializable.
Required classes & Interfaces
Page 9 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer

Object output Interface With WriteObject( ) method


of this interface object can
be serialized
ObjectOutputStream Class For writing objects to stream.
ObjectInput With readObject( ) objects
can be deserialized
ObjectInputStream Class For reading objects from a
stream

Q:4 (A)
(c) (Any other suitable program based on the same concept can also be considered.)

import java.lang.*;
import java.io.*;
import java.util.*;
class palindrome
{
public static void main(String arg[ ]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter String:");
String word=br.readLine( );
int len=word.length( )-1;
int l=0;
int flag=1;
int r=len;
while(l<=r)
{
if(word.charAt(l)==word.charAt(r))
{
l++;
r--;
}
else
{
flag=0;
break;
}
}
if(flag==1)
{
System.out.println("palindrome");
}
else
{
System.out.println("not palindrome");
}
}}
Page 10 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
(d) Switch case statement: (2 Marks)
Java has a built-in multi way decision statement known as switch. The switch case statement is useful
when user have multiple choices. Switch statement tests the value of a given variable against a list of
case values & when match is found, a block of statements associated with that case is executed.
Syntax:
switch(expression)
{
case value1:
block1;
break;
case value2:
block2;
break;
-----
----
default :
Default block;
break;
}
statement n;
Example:
class switchdemo (2 Marks)
{
public static void main ( string args[ ] )
{
int a = Integer.parseInt ( args[0 ] ;
switch (a/5)
{
case 1:
system.out.println(“Poor”);
break;
case 2:
system.out.println(“work Hard”);
break;
case 3:

system.out.println(“Good”);
break;

case 4:
system.out.println(“Very Good”);
break;
case 5:

system.out.println(“Excellent”);
break;
default:

system.out.println(“ Invalid value entered”)


}
Page 11 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
}
}

(Any other valid example can be considered)

Q.4
(B)
(a) Difference between Array & Vector (4 M)
Array Vector
1) Array can accommodate only fixed no. of 1) Vectors can accommodate unlimited no. of
elements elements.
2) Array can hold primitive datatype objects 2) Vector can hold only objects
3) The variables in array should be of the same data 3) The objects in Vectors do not have to be
type. ie.homogenous homogenous.
4) syntax: 4) syntax:
Datatype arrayName[ ] ; Vector Variable =
Datatype arrayname [ ] = new Vector( );
new datatype [size]; Vector Variable =
new Vector (size);

Each method 1 mark


1. elementAt( ) method:
This method returns nth object from Vector.
For eg. list.elementAt (n);
2. addElement( );
Adds element specified to the list at the end.
For eg. list.addElement( element );
list is object of vector.

Q.4 (B) (b)(Logic 2 marks, Syntax 2 marks, Design 2 marks ) (Any other suitable program based on the same
concept can also be considered.)

import java.io.*;
import java.lang.*;
class Employee
{
String name;
int empid;
int salary;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
void get()
{
try
{
System.out.println("Enter the Name:");
name=br.readLine();
System.out.println("Enter the empid:");
empid=Integer.parseInt(br.readLine());
System.out.println("Enter the salary:");
salary=Integer.parseInt(br.readLine());
Page 12 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
}
catch(IOException e)
{}
}
void show()
{
System.out.println("Name:"+name);
System.out.println("EmpId:"+empid);
System.out.println("Salary:"+salary);
}
}

class emp
{
public static void main(String args[])
{
Employee e[]=new Employee[5];
for(int i=0;i<5;i++)
{
e[i]=new Employee();
}
for(int i=0;i<5;i++)
{
e[i].get();
}
for(int i=0;i<5;i++)
{
e[i].show();
}
}
}

Q.5
(a) (Explanation 2 marks, 2 marks each for each access specifier)
Packages add another dimension to access control. Java allows many levels of protection to have a
control over visibility of variables and methods within clam, sub clam & package. Clam & package are
both means of encapsulating the namespace & scope of variables & methods. Java addresses 4
categories of visibility for clam members.
1. subclass in same package
2. Non subclass in same package
3. sub class in different package
4. Class that are neither in same package nor in sub class.
The 3 access specifies – public, private & protected provide a variety of ways to produce many
levels of access required by these categories . Anything declared as public can be accessed from
anywhere. Anything declared as private cannot be seen outside of its class. When a member does not
have explicit access specification, it is visible subclasses as well as other classes in same package. This is
the default access. If you want to allow an element to be seen outside your current package, but only
to classes that subclass your class directly then declare it as protected.
Page 13 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer

Access protection matrix can be shown as follows:


Sr. Private Default Protected public
no.
1 Same package yes yes yes yes
2 Same package sub clam No yes yes yes
3 Different package sub No No yes yes
clam
4 Different package non- No No No yes
sub clam

(b) (Explanation 4 marks, program with applet tag 4 marks)


A polygon may be considered as a set of lines connected together. The end of second line is the
beginning of third line and so on. Polygon can be drawn using drawpolygon( ) method of Graphics
Class. It takes 3 arguments
1. An array of integers containing x-coordinates
2. An array of integers containing y-coordinates
3. An integer for total number of points x & y arrays should be of same size & must repeat the 1st
point at the end of array for closing polygon.

Import java.awt.*;
Import java. applet.*;
Public class drawPoly extends Applet
{
Public void paint(Graphics g)
{
Int xPoints [] = {10, 170, 80, 10};
Int yPoints[] = {20, 40, 140, 20};
Int nPoints[] = xPoints.length;
g.drawPolygon(xPoints, yPoints, nPoints);
}
}
/*<applet code = drawpoly.class height = 400 width = 400></applet>*/
(Students should have full program written as any example, any other example can be considered)
Page 14 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
(c) (Any four differences 1 mark each, program with applet tag 4 marks)
Applet Application
1) Applet gives the result in GUI form 1) Application gives output on console
2) Applet does not use main( ) method 2) Application cannot be complete without main ( )
method
3) Applet can not run independently 3) Application can run independently
4) Applets are usually designed for the use on 4) Applications are designed as console application
internet
5) Applet require to be embedded in HTML code 5) No such requirement to run an application

//Applet program to display “welcome to java”


import java.awt *;
import java.applet *;
public clam mayapplet extends Applet
{
string str=”welcome to java”;
public void init ( )
{
setBackground ( color.Blue);
setforeground ( color.pink);
}
public void point (Graphics 9)
{
g.drawString(str,50,50);
}
}
/* applet code=myapplet height = 100 , width =100>
</applet*/

Q.6
(a) (2 marks each)
A thread is similar to a program that has a single flow of control; it has a beginning, a body of execution &
an end. It executes the commands sequentially. A unique property of java is its support for multithreading.
Multithreading is that concept where a program is divided into two or more subprogram which can be
implemented at the same time in parallel.
A thread running in parallel does not really mean that they actually run at the same time.
Because all the threads are running of a single processor, the flow of execution is shared between the threads.
Java interpreter handle switching of control between the threads in such a way that it appears as they are
running concurrently.
During life time of thread, it passes through different stages as –
1. Newborn
2. Runnable
3. Running
4. Blocked
5. Dead
A thread is always in one of these 5 states.
A thread can be temporarily suspended or blocked from entering into the runnable and running state by using
either of the method or suspend( ) , wait ( ) or sleep( ).
Page 15 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
wait( ) & notify( ) if a thread requires to wait until some event occurs, it can be done using wait method & it
can be scheduled to run again by notify( ) method. wait( ) & notify are required to improve interprocess
communication, so all classes should have access to them, that is why they are included in super class of all ,
i.e. ‘object’ and we call them in thread because every process in turn is carried out by thread. Wait ( ) actually
apply lock on the thread, & the locks are always applied on object, not on thread/process, that is why these
methods belong to object class.

(b) (1 mark each)


The mechanism of creating a new class from an old one is called as inheritance. Here the new class can
acquire the properties inherited from old class. The old class is known as base class or super class or parent
class & the new one is called as derived class or subclass or child class.

inheritance can be of different types:

1. A single level inheritance – where there can be one parent or super class to one subclass.

B
A- Base class, B- derived class
2. Multiple inheritance- where is there can be multiple parent/super classes to a derived class.

A B

Where A & B are of super classes & C is a subclass.


In java, only with the help of classes multiple inheritance is not possible. You require another concept
called as ‘interface’ to achieve multiple inheritance.
3. Hierarchical inheritance- where is there can be one super class & many classes.

B c D

Where A is super class & B, C, & D are subclasses.


Page 16 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
4. Multilevel inheritance – where is there can be derived class derived from another sub class.

Where ‘C’ sub class is derived from subclass ‘B’ which is derived from super class ‘A’.
Syntax to implement inheritance in java is class subclass extends super class.
{
variable declaration;
method declaration;
:
:
:
}
e.g. class fruit //base class
{
:
:
:
}
class mango extends fruit // derived class
{
:
:
}

(c) (Explanation 2 marks, example 2 marks)


Java applet provides their output in GUI form. The output of applet can be seen either in appletviewer
window or in any web browser. If we want to see the output of applet in a web browser, we need to
embed applet class name in html code with the help of <apple> tag. <Applet> tag has basic attributes
as
1. Code- Here class file name of applet can be specified
2. Height- defines height of the window in which the output appears.
3. Width- defines width of the window in which the output appears.
Other attributes are
4. Alt- displays short text message if browser is unable to run applet code
5. Align- specifies align of applet such as left, right, top, bottom etc.
6. Codebase – specifies URL where class file resides.
7. Uspace-Hspace-specifies space in pixels to keep below & above the applet area & left & right of applet
area.
8. Program- It allows to pass applet specific argument through HTML page. They can be accessed in an
applet with the help of getparameter ( ) method.
Example:
Page 17 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
<html.
<body>
<applet code=myapplet width=100,Height=100>
<param name=”strl” value = “abc”>
</applet>
</body>
</html>
where myapplet is class file of java code . This file with extension .html can be executed through a web
browser to get the output.
(Any other example can be considered)

(d) (Logic 2 marks. Syntax 2 marks)(Any other program based on same logic may also be considered)
class myclass
{
public static void main(String args[])
{
int a=0,b=1,c;
int i=0;
int n=5;
System.out.print(b+" ");
while(i<=n)
{
c=a+b;
System.out.print(c+" ");

a=b;
b=c;
i++;
}
}
}

(e) (Explanation 2 marks, example 2 marks)(Any other suitable example may also be considered)
A constructor initializes an object immediately upon creation. It has the same name as the class in
which it resides and is syntactically similar to any method. Once defined, constructor is automatically
called immediately after the object is created before new operator completes. Constructions do not
have return value but they don’t require ‘void’ as implicit datatype of class constructor is the class
type.
Parameterized constructor-
In case of java when new operator creates an object , a constructor is called. When you don’t
explicitly define a constructor for a class then java creates a default constructor which automatically
defines all variables to zero , null or spaces. Once you use your own constructor , the default
constructor is no long valid. If the constructor are defined without parameters, variables can have
same values as many times as they are initialized. Instead of this if constructors are defined with
parameters, different values can be provided for different objects. Constructors which has parameters
are called as parameterized construction.
Example:
Class Rect
{
int length , breadth;
Page 18 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 12 EXAMINATIONS
Subject code: 12176 Model Answer
Rect(int l , intb) //parameterized constructor
{
length=l;
breadth=b;
}
public static void main(string args[ ] )
{
Rect r=new Rect(8,5);
System.out.println(“Area=”+(r.length*r.breadth));
Rect r1=new Rect(10,12);
System.out.println(“Area=”+(r.length*r.breadth);
}
}

You might also like