220244-Java Programming - 22412 - 2023 - Summer Model Answer Paper
220244-Java Programming - 22412 - 2023 - Summer Model Answer Paper
OUR SERVICES:
Diploma in All Branches, All Subjects
Degree in All Branches, All Subjects
BSCIT / CS
Professional Courses
i) Class
ii) Object
Ans i) Class: Class is a set of object, which shares common characteristics/ behavior and 1 M for any
common properties/ attributes. suitable class
definition
and 1 M for
ii) Object: It is a basic unit of Object-Oriented Programming and represents real-life any suitable
entities. object
definition
Example:
class Student
{
int id;
String name;
Page No: 1 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
public static void main(String args[])
{
Student s1=new Student(); //creating an object of Student
}
}
In this example, we have created a Student class which has two data members id and
name. We are creating the object of the Student s1 by new keyword.
b) Enlist any two access specifier with syntax. 2M
package nameOfPackage;
Example:
package p1;
Accessing Package:
• Package can be accessed using keyword import.
• There are 2 ways to access java system packages:
o Package can be imported using import keyword and the wild card(*) but
drawback of this shortcut approach is that it is difficult to determine from
which package a particular member name.
Syntax: import package_name.*;
Page No: 2 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
For e.g. import java.lang.*;
o The package can be accessed by using dot(.) operator and can be terminated
using semicolon(;)
Syntax: import package1.package2.classname;
d) Give a syntax of following thread method 2M
i) Notify ( )
ii) Sleep ( )
ii) sleep()
Sleep() causes the current thread to suspend execution for a specified period.
Syntax: public static void sleep(long milliseconds)
Ans User-define Parameter can be applied in applet using <PARAM…> tags. Syntax of
<param> - 1
Each <PARAM…> tag has a name and value attribute.
M
Syntax: <PARAM name = ……… Value = “………” >
And syntax
For example, the param tags for passing name and age parameters looks as shown below: of
getParameter
<param name=”name” value=”Ramesh” />
( )- 1 M
<param name=”age” value=”25″ />
The getParameter() method of the Applet class can be used to retrieve the parameters
passed from the HTML page. The syntax of getParameter() method is as follows:
String getParameter(String param-name);
Example:
n = getParameter("name");
a = getParameter("age");
Page No: 3 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
f) Define stream class and list types of stream class. 2M
Ans A java stream is a group of objects that can be piped together to produce the desired result. Define
Streams are used in Java to transfer data between programs and I/O devices like a file, stream
network connections, or consoles. class=1 M
and
any 2 types
of stream
class=1 M
Ans In Java, type casting is a method or process that converts a data type into another data Definition of
Page No: 4 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
type in both ways manually and automatically. The automatic conversion is done by the type casting-
1M
compiler and manual conversion performed by the programmer.
Type casting is of two types: widening, narrowing. Types of
type
Widening (Implicit)
casting-1 M
• The process of assigning a smaller type to a larger one is known as widening or
implicit.
Byte short int long float double Example-2
M
For e.g.
(1 M for
class widening
each
{ example)
public static void main(String arg[])
{
int i=100;
long l=I;
float f=l;
System.out.println(“Int value is”+i);
System.out.println(“Long value is”+l);
System.out.println(“Float value is”+f);
}
}
Narrowing (Explicit)
• The process of assigning a larger type into a smaller one is called narrowing.
• Casting into a smaller type may result in loss of data.
• double long int short byte
For e.g.
class narrowing
{
Public static void main(String[])
{
Double d=100.04;
Long l=(long) d;
Int i=(int) l;
Page No: 5 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
System.out.println(“Int value is”+i);
System.out.println(“Long value is”+l);
System.out.println(“Float value is”
}
}
b) Differentiate between String and String Buffer Class. (any four points) 4M
Ans Following example shows a user defined exception as ‘Invalid Age’, if age entered by For any
the user is less than eighteen. Correct
import java.lang.Exception; program-4 M
import java.io.*;
class myException extends Exception
{
myException(String msg)
{
super(msg);
}
}
class agetest
{
public static void main(String args[])
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//Scanner class is also valid
try
{
System.out.println("enter the age : ");
int n=Integer.parseInt(br.readLine());
if(n < 18 )
throw new myException("Invalid Age"); //user defined exception
Page No: 6 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
else
System.out.println("Valid age");
}
catch(myException e)
{
System.out.println(e.getMessage());
}
catch(IOException ie)
{}
}
}
d) Write a program for reading and writing character to and from the given files 4M
using character stream classes.
OR
class ArmstrongWhile
{
public static void main(String[] arg)
{
int i=1,a,arm,n,temp;
System.out.println("Armstrong numbers between 0 to 999 are");
while(i<500)
{
n=i;
arm=0;
while(n>0)
{
a=n%10;
arm=arm+(a*a*a);
n=n/10;
}
if(arm==i)
System.out.println(i);
i++;
}
}
}
b) Explain the applet life cycle with neat diagram. 4M
Page No: 8 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
Ans Diagram-2
M and
explanation
–2M
Applet Life Cycle: An Applet has a life cycle, which describes how it starts, how it
operates and how it ends. The life cycle consists of four methods: init(), start(), stop() and
destroy().
Initialization State (The init() method):
The life cycle of an Applet is begin on that time when the applet is first loaded into the
browser and called the init() method. The init() method is called only one time in the life
cycle on an Applet. The init() method is basically called to read the “PARAM” tag in the
html file. The init () method retrieve the passed parameter through the “PARAM” tag of
html file using get Parameter() method All the initialization such as initialization of
variables and the objects like image, sound file are loaded in the init () method .After the
initialization of the init() method user can interact with the Applet and mostly applet
contains the init() method.
We may do following thing if required.
• Create objects needed by the applet
• Set up initial values
• Load images or fonts
• Set up colors
Running State (The start() method): The start method of an Applet is called after the
initialization method init(). This method may be called multiples time when the Applet
needs to be started or restarted. For Example if the user wants to return to the Applet, in
this situation the start() method of an Applet will be called by the web browser and the
user will be back on the applet. In the start method user can interact within the applet.
public void start()
{
……..
……..
}
Idle (The Stop() method): An applet becomes idle when it is stopped from running. The
stop() method stops the applet and makes it invisible. Stopping occurs automatically when
we leave the page containing the currently running applet. We can also do so by calling the
stop() method explicitly. The stop() method can be called multiple times in the life cycle of
applet like the start () method or should be called at least one time. For example the stop()
method is called by the web browser on that time When the user leaves one applet to go
another applet and the start() method is called on that time when the user wants to go back
into the first program or Applet.
public void stop()
Page No: 9 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
{
……..
……..
}
Dead State (The destroy() method): The destroy() method is called to terminate an
Applet. An Applet is said to be dead when it is removed from memory. This occurs
automatically by invoking the destroy() method when we quit the browser. It is useful for
clean-up actions, such as releasing memory after the applet is removed, killing off threads
and closing network/database connections.
Thus this method releases all the resources that were initialized during an applet’s
initialization.
public void destroy()
{
……..
……..
}
Display State (The paint() method): The paint() method is used for applet display on the
screen.
The display includes text, images, graphics and background. This happens immediately
after the applet enters into the running state. Almost every applet will have a paint()
method and can be called several times during an applet’s life cycle. The paint() method is
called whenever a window is required to paint or repaint the applet.
public void paint(Graphics g)
{
……..
……..
}
c) Describe the package in java with suitable example. 4M
Ans • Java provides a mechanism for partitioning the class namespace into more Description-
manageable parts called package (i.e package are container for a classes). 2 M,
• The package is both naming and visibility controlled mechanism. Package can be Example -2
created by including package as the first statement in java source code. M
• Any classes declared within that file will belong to the specified package.
Syntax: package pkg;
pkg is the name of the package
eg : package mypack;
• Java uses file system directories to store packages. 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 must match with the package name exactly.
• A hierarchy can be created by separating package name and sub package name by
a period(.) as pkg1.pkg2.pkg3; which requires a directory structure as
pkg1\pkg2\pkg3.
• The classes and methods of a package must be public.
• To access package In a Java source file, import statements occur immediately
following the package. statement (if it exists) and before any class definitions.
• Syntax: import pkg1[.pkg2].(classname|*);
Page No: 10 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
• Example:
package1:
package package1;
public class Box
{
int l= 5;
int b = 7;
int h = 8;
public void display()
{
System.out.println("Volume is:"+(l*b*h));
}
}
}
Source file:
import package1.Box;
class VolumeDemo
{
public static void main(String args[])
{
Box b=new Box();
b.display();
}
}
d) Enlist types of Byte stream class and describe input stream class and output 4M
stream class.
Page No: 11 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
of bytes that are read. At the end returns –1.
3. int read(byte buffer[ ], int offset, int numbytes)- Attempts to read up to numbytes
bytes into buffer starting at buffer[offset]. Returns actual number of bytes that are
read. At the end returns –1.
4. void close()- to close the input stream
5. void mark(int numbytes)- places a mark at current point in input stream and
remain valid till number of bytes are read.
6. void reset()- Resets pointer to previously set mark/ goes back to stream
beginning.
7. long skip(long numbytes)- skips number of bytes.
8. int available()- Returns number of bytes currently available for reading.
Ans 1. Compile & Interpreted: Java is a two staged system. It combines both approaches. Any four
First java compiler translates source code into byte code instruction. Byte codes are not each features
machine instructions. In the second stage java interpreter generates machine code that can -1 M each
be directly executed by machine. Thus java is both compile and interpreted language.
2. Platform independent and 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.
3. Object Oriented: Almost everything in java is in the form of object. All program codes
and data reside within objects and classes. Similar to other OOP languages java also has
basic OOP properties such as encapsulation, polymorphism, data abstraction, inheritance
etc. Java comes with an extensive set of classes (default) in packages.
4. Robust & Secure: Java is a robust in the sense that it provides many safeguards to
ensure reliable codes. Java incorporates concept of exception handling which captures
errors and eliminates any risk of crashing the system. Java system not only verify all
memory access but also ensure that no viruses are communicated with an applet. It does
not use pointers by which you can gain access to memory locations without proper
Page No: 12 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
authorization.
5. Distributed: It is designed as a distributed language for creating applications on
network. It has ability to share both data and program. Java application can open and
access remote object on internet as easily as they can do in local system.
6. 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.
7. Dynamic and Extensible: Java is capable of dynamically linking new class library’s
method and object. Java program supports function written in other languages such as C,
C++ which are called as native methods. Native methods are linked dynamically at run
time.
b) Explain any four methods of vector class with example. 4M
Example:
Page No: 13 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
import java.io.*;
import
java.lang.*;
import
java.util.*;
class vector2
{
public static void main(String args[])
{
vector v=new
vector(); Integer
s1=new Integer(1);
Integer s2=new
Integer(2); String
s3=new
String("fy"); String
s4=new
String("sy");
Character s5=new
Character('a'); Character
s6=new Character('b');
Float s7=new
Float(1.1f);
Float s8=new Float(1.2f);
v.addElement(s1);
v.addElement(s2);
v.addElement(s3);
v.addElement(s4);
v.addElement(s5);
v.addElement(s6);
v.addElement(s7);
v.addElement(s8);
System.out.println(v);
v.removeElement(s2);
v.removeElementAt(4);
System.out.println(v);
}
}
c) Describe interface in java with suitable example. 4M
Ans Java does not support multiple inheritances with only classes. Java provides an alternate Interface
approach known as interface to support concept of multiple inheritance. An interface is explanation
similar to class which can define only abstract methods and final variables. – 2 M, any
Syntax: suitable
access interface InterfaceName example – 2
{ M
Variables declaration;
Methods declaration;
Page No: 14 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
}
Example:
interface sports
{
int sport_wt=5;
public void disp();
}
class test
{
int roll_no;
String name;
int m1,m2;
test(int r, String nm, int m11,int m12)
{
roll_no=r;
name=nm;
m1=m11;
m2=m12;
}
}
class result extends test implements sports
{
result (int r, String nm, int m11,int m12)
{
super (r,nm,m11,m12);
}
public void disp()
{
System.out.println("Roll no : "+roll_no);
System.out.println("Name : "+name);
System.out.println("sub1 : "+m1);
System.out.println("sub2 : "+m2);
System.out.println("sport_wt : "+sport_wt);
int t=m1+m2+sport_wt;
System.out.println("total : "+t);
}
public static void main(String args[])
{
result r= new result(101,"abc",75,75);
r.disp();
}
}
Output :
D:\>java result
Roll no : 101
Name : abc
sub1 : 75
sub2 : 75
sport_wt : 5
Page No: 15 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
total : 155
d) Write an applet program for following graphics method. 4M
i) Drawoval ( )
ii) Drawline ( )
e) Enlist any four methods of file input stream class and give syntax of any two 4M
methods.
Ans • Input Stream Classes: java.io.InputStream is an abstract class that contains the One method
basic methods for reading raw bytes of data from a stream. The InputStream class –1M
defines methods for performing the input functions like: reading bytes, closing
streams, marking positions in streams, skipping ahead in a stream and finding the
number of bytes in a stream.
• Input stream class methods:
1. int read ()- Returns an integer representation of next available byte of input.-1 is
returned at the stream end.
2. int read (byte buffer[ ])- Read up to buffer.length bytes into buffer & returns
actual number
of bytes that are read. At the end returns –1.
3. int read(byte buffer[ ], int offset, int numbytes)- Attempts to read up to numbytes
bytes
into buffer starting at buffer[offset]. Returns actual number of bytes that are read.
Page No: 16 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
At the
end returns –1.
4. void close()- to close the input stream
5. void mark(int numbytes)- places a mark at current point in input stream and
remain valid till
number of bytes are read.
6. void reset()- Resets pointer to previously set mark/ goes back to stream
beginning.
7. long skip(long numbytes)- skips number of bytes.
8. int available()- Returns number of bytes currently available for reading.
a) Write a program to copy all elements of one array into another array. 6M
System.out.println();
m1, m2, m3
Class: Result
display ()
Fig. No. 1
int sports_mark=20;
class Student
String S-name;
int Roll_no, m1, m2, m3;
Student(String n, int a, int b, int c, int d)
S-name = n;
Roll_no = a;
Page No: 18 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
m1 = b;
m2 = c;
m3 = d;
void showdata()
super(n, a, b, c, d );
void dispaly()
super.showdata();
int total=(m1+m2+m3);
float result=(total+Sports_mark)/total*100;
Page No: 19 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
System.out.println(“result of student is:”+result);
class studentsDetails
r.display();
}
c) Write a program to print even and odd number using two threads with delay 6M
of 1000ms after each number.
Page No: 20 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
}
}
}
}
class even extends Thread
{
public void run()
{
for(int i=0;i<=20;i=i+2)
{
System.out.println("EVEN="+i);
try
{
sleep(1000);
}
catch(Exception e)
{
System.out.println("Error");
}
}
}
}
class oddeven
{
public static void main(String arg[])
{
odd o=new odd();
even e=new even();
Page No: 21 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
o.start();
e.start();
}
}
Ans 2 M for
diagram, 4
M for
explanation
Thread Life Cycle Thread has five different states throughout its life.
1) Newborn State
When a thread object is created it is said to be in a new born state.When the thread is in a
new born state it is not scheduled running from this state it can be scheduled for running
by start() or killed by stop(). If put in a queue it moves to runnable state.
2) Runnable State
It means that thread is ready for execution and is waiting for the availability of the
processor i.e. the thread has joined the queue and is waiting for execution. If all threads
have equal priority then they are given time slots for execution in round robin fashion.The
thread that relinquishes control joins the queue at the end and again waits for its turn. A
thread can relinquish the control to another before its turn comes by yield().
Page No: 22 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
3) Running State
It means that the processor has given its time to the thread for execution. The thread runs
until it relinquishes control on its own or it is pre-empted by a higher priority thread.
4) Blocked State
A thread can be temporarily suspended or blocked from entering into the runnable and
running state by using either of the following thread method.
o wait(): If a thread requires to wait until some event occurs, it can be done using wait
method and can be scheduled to run again by notify().
o sleep(): We can put a thread to sleep for a specified time period using sleep(time) where
time is in ms. It reenters the runnable state as soon as period has elapsed /over.
5) Dead State
Whenever we want to stop a thread form running further we can call its stop(). The stop()
causes the thread to move to a dead state. A thread will also move to dead state
automatically when it reaches to end of the method. The stop method may be used when
the premature death is required
Thread should be in any one state of above and it can be move from one state to another by
different methods and ways.
b) Write a program to generate following output using drawline () method. Refer 6M
Fig. No. 2.
Fig. No. 2
Page No: 23 | 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
g.drawLine(200,100,300,200);
g.drawLine(300,200,100,200);
}
}
/*<applet code="Triangle.class" height=300 width=200> </applet>*/
OR
Using drawPolygon() method
import java.applet.*;
import java.awt.*;
public class Triangle extends Applet
{
public void paint(Graphics g)
{
int a[]={100,200,300,100};
int b[]={200,100,200,200};
int n=4;
g.drawPolygon(a,b,n);
}
}
/*<applet code="Triangle.class" height=300 width=200> </applet>*/
Ans Constructor: A constructor in Java is a special method that is used to initialize objects. 2 M for
definition of
The constructor is called when an object of a class is created. It can be used to set initial constructor
values for object attributes. and types of
constructors
Page No: 26 | 26