Java5 Organized
Java5 Organized
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
WINTER – 2022 EXAMINATION
Subject Name: Java Programming Model Answer Subject Code: 22412
Ans 2M (1/2 M
Operator Meaning
each)
< Less than Any Four
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
Ans The access specifiers in java specify accessibility (scope) of a data member, 2M (1/2 M
method, constructor or class. There are 5 types of java access specifier: each)
public Any Four
private
Page No: 1 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
Ans 1. Thread is a smallest unit of executable code or a single task is also called as thread. 1 M-
2. Each tread has its own local variable, program counter and lifetime. Define
3. A thread is similar to program that has a single flow of control. Thread
There are two ways to create threads in java:
1M -2ways
1. By extending thread class to create
Syntax: - thread
class Mythread extends Thread
{
-----
}
2. Implementing the Runnable Interface
Syntax:
class MyThread implements Runnable
{
public void run()
{
------
}
f) Distinguish between Java applications and Java Applet (Any 2 points) 2M
Page No: 3 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
2. Attempt any THREE of the following: 12 M
int i,m=0,flag=0;
m=n/2;
if(n==0||n==1){
}else{
for(i=2;i<=m;i++){
if(n%i==0){
flag=1;
break;
}//end of else
Output:
7 is prime number
Page No: 5 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
c) Describe Life cycle of thread with suitable diagram. 4M
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().
Runnable runnable = new NewState();
Thread t = new Thread(runnable); t.start();
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.
Page No: 7 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
Ans 1 M for
each point
(any 2
Applet Application Points)
Applet does not use main() Application use main() method
method for initiating execution of for initiating execution of code
code
Applet cannot run independently Application can run
independently
Applet cannot read from or write Application can read from or
to files in local computer write to files in local computer
Applet cannot communicate with Application can communicate
other servers on network with other servers on network
Applet cannot run any program Application can run any program
from local computer. from local computer.
Applet are restricted from using Application are not restricted
libraries from other language from using libraries from other
such as C or C++ language
Applets are event driven. Applications are control driven.
Ans 2M-Correct
diagram
Page No: 4 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
default (Friendly)
protected
private protected
c) Explain constructor with suitable example. 2M
Ans Constructors are used to assign initial value to instance variable of the class. 1M-
It has the same name as class name in which it resides and it is syntactically similar Explanation
to anymethod. 1M-
Constructors do not have return value, not even ‘void’ because they return the instance if Example
class.
Constructor called by new operator.
Example:
class Rect
{
int length, breadth;
Rect() //constructor
{
length=4; breadth=5;
}
public static void main(String args[])
{
Rect r = new Rect();
System.out.println(“Area : ” +(r.length*r.breadth));
}
}
Output : Area : 20
d) List the types of inheritance which is supported by java. 2M
1 M each
Page No: 2 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
Page No: 8 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
b) Define a class employee with data members 'empid , name and salary. 4M
Accept data for three objects and display it
Ans class employee 4M (for
{ correct
int empid; program
String name; and logic)
double salary;
void getdata()
{
BufferedReader obj = new BufferedReader (new InputStreamReader(System.in));
System.out.print("Enter Emp number : ");
empid=Integer.parseInt(obj.readLine());
System.out.print("Enter Emp Name : ");
name=obj.readLine();
System.out.print("Enter Emp Salary : ");
salary=Double.parseDouble(obj.readLine());
}
void show()
{
System.out.println("Emp ID : " + empid);
System.out.println(“Name : " + name);
System.out.println(“Salary : " + salary);
}
}
classEmpDetails
{
public static void main(String args[])
{
employee e[] = new employee[3];
for(inti=0; i<3; i++)
{
e[i] = new employee(); e[i].getdata();
}
System.out.println(" Employee Details are : ");
for(inti=0; i<3; i++)
e[i].show();
}
}
Page No: 6 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
a) Write a program to find reverse of a number. 4M
while(number !=0)
number = number/10;
} }
Ans Final keyword : The keyword final has three uses. First, it can be used to create the Use of final
equivalent of a named constant.( in interface or class we use final as shared constant or keyword-2
constant.) M
Program-2
Other two uses of final apply to inheritance
M
Using final to Prevent Overriding While method overriding is one of Java’s most powerful
features,
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.");
Page No: 9 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
(x1,y1) and (x2,y2) as arguments and draws a line between them.
The graphics object g is passed to paint() method.
Syntax: g.drawLine(x1,y1,x2,y2);
Example: g.drawLine(100,100,300,300;)
iv) drawArc ()
drawArc( ) It is used to draw arc.
Syntax: void drawArc(int x, int y, int w, int h, int start_angle, int sweep_angle);
where x, y starting point, w & h are width and height of arc, and start_angle is starting
angle of arc sweep_angle is degree around the arc
Ans One
public String getName() Returns the name of the file or directory denoted by this method
abstract pathname.
public String getParent() Returns the pathname string of this abstract pathname's 1M
parent, or null if this pathname does not name a parent
directory
public String getPath() Converts this abstract pathname into a pathname string.
public boolean isAbsolute() Tests whether this abstract pathname is absolute. Returns
true if this abstract pathname is absolute, false otherwise
public boolean exists() Tests whether the file or directory denoted by this abstract
pathname exists. Returns true if and only if the file or
directory denoted by this abstract pathname exists; false
otherwise
public boolean isDirectory() Tests whether the file denoted by this abstract pathname is
a directory. Returns true if and only if the file denoted by
this abstract pathname exists and is a directory; false
otherwise.
public boolean isFile() Tests whether the file denoted by this abstract pathname is
a normal file. A file is normal if it is not a directory and, in
addition, satisfies other system-dependent criteria. Any
nondirectory file created by a Java application is guaranteed
to be a normal file. Returns true if and only if the file
denoted by this abstract pathname exists and is a normal
file; false otherwise.
a) Write all primitive data types available in Java with their storage Sizes in 4M
Page No: 11 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
method to initialize and display the information for three objects.
Ans class Book Correct
{ program- 4
String author, title, publisher; M
Book(String a, String t, String p)
{
author = a;
title = t;
publisher = p;
}
}
class BookInfo extends Book
{
float price;
int stock_position;
BookInfo(String a, String t, String p, float amt, int s)
{
super(a, t, p);
price = amt;
stock_position = s;
}
void show()
{
System.out.println("Book Details:");
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Publisher: " + publisher);
System.out.println("Price: " + price);
System.out.println("Stock Available: " + stock_position);
}
}
class Exp6_1
{
public static void main(String[] args)
{
BookInfo ob1 = new BookInfo("Herbert Schildt", "Complete Reference", "ABC
Publication", 359.50F,10);
BookInfo ob2 = new BookInfo("Ulman", "system programming", "XYZ Publication",
359.50F, 20);
BookInfo ob3 = new BookInfo("Pressman", "Software Engg", "Pearson Publication",
879.50F, 15);
ob1.show();
Page No: 13 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
“Hellojava.java”
import java.awt.*;
import java.applet.*;
public class Hellojava extends Applet
{
public void paint (Graphics g)
{
g.drawString("Hello Java",10,100);
}}
Use the java compiler to compile the applet “Hellojava.java” file.
C:\jdk> javac Hellojava.java
After compilation “Hellojava.class” file will be created. Executable applet is nothing but
the .class file
of the applet, which is obtained by compiling the source code of the applet. If any error
message is
received, then check the errors, correct them and compile the applet again.
We must have the following files in our current directory.
o Hellojava.java
o Hellojava.class
o HelloJava.html
If we use a java enabled web browser, we will be able to see the entire web page containing
the
applet.
We have included a pair of <APPLET..> and </APPLET> tags in the HTML body section.
The
<APPLET…> tag supplies the name of the applet to be loaded and tells the browser how
much space
the applet requires. The <APPLET> tag given below specifies the minimum requirements
to place the
HelloJava applet on a web page. The display area for the applet output as 300 pixels width
and 200
pixels height. CENTER tags are used to display area in the center of the screen.
<APPLET CODE = hellojava.class WIDTH = 400 HEIGHT = 200 > </APPLET>
Example: Adding applet to HTML file:
Create Hellojava.html file with following code:
<HTML>
<! This page includes welcome title in the title bar and displays a welcome message. Then
it specifies
the applet to be loaded and executed.
>
<HEAD> <TITLE> Welcome to Java Applet </TITLE> </HEAD>
<BODY> <CENTER> <H1> Welcome to the world of Applets </H1> </CENTER> <BR>
<CENTER>
Page No: 15 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
bytes.
Page No: 12 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
}
}
class B extends A
{
void meth()
{ // ERROR! Can't override.
System.out.println("Illegal!");
}
}
As base class declared method as a final , derived class can not override the definition of
base class methods.
Page No: 10 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
<APPLET CODE=HelloJava.class WIDTH = 400 HEIGHT = 200 > </APPLET>
</CENTER>
</BODY>
</HTML>
e) Write a program to copy contents of one file to another. 4M
Ans
Page No: 16 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
ob2.show();
ob3.show();
}
}
OUTPUT
Book Details:
Title: Complete Reference
Author: Herbert Schildt
Publisher: ABC Publication
Price: 2359.5
Stock Available: 10
Book Details:
Title: system programming
Author: Ulman
Publisher: XYZ Publication
Price: 359.5
Stock Available: 20
Book Details:
Title: Software Engg
Author: Pressman
Publisher: Pearson Publication
Price: 879.5
Stock Available: 15
d) Mention the steps to add applet to HTML file. Give sample code. 4M
Page No: 14 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
Sr. Array Vector
No.
4 Array can store primitive type data Vector are store non-primitive type data
element. element
8 Array allocates the memory for the Vector allocates the memory
fixed size ,in array there is wastage of dynamically means according to the
memory. requirement no wastage of memory.
9 No methods are provided for adding Vector provides methods for adding and
and removing elements. removing elements.
10 In array wrapper classes are not used. Wrapper classes are used in vector
elementAT( ):
The elementAt() method of Java Vector class is used to get the element at the specified
Page No: 17 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
super(i,n,b);
ta=t;
}
void disp()
{
display();
System.out.println("da of Employee="+da);
}
public void netsalary()
{
double net_sal=basicsalary+ta+hra+da;
System.out.println("netSalary of Employee="+net_sal);
}
}
class Empdetail
{
public static void main(String args[])
{
net_salary s=new net_salary(11, “abcd”, 50000);
s.disp();
s.netsalary();
}
}
c) Define an exception called 'No Match Exception' that is thrown when the 6M
passward accepted is not equal to "MSBTE'. Write the program.
l++;
r--;
else
flag=0;
break;
if(flag==1)
System.out.println("palindrome");
else
System.out.println("not palindrome");
b) Define thread priority ? Write default priority values and the methods to set 6M
and change them.
Ans Thread Priority: 2 M for
define
In java each thread is assigned a priority which affects the order in which it is scheduled for Thread
running. Threads of same priority are given equal treatment by the java scheduler. priority
Default priority values as follows 2 M for
Page No: 21 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
}
c) Design an applet to perform all arithmetic operations and display the result by using 6M
labels. textboxes and buttons.
Ans import java.awt.*; 6 M for
import java.awt.event.*; correct
public class sample extends Frame implements ActionListener { program
Label l1, l2,l3;
TextField tf1, tf2, tf3;
Button b1, b2, b3, b4;
sample() {
l1=new Lable(“First No.”);
l1.setBounds(10, 10, 50, 20);
tf1 = new TextField();
tf1.setBounds(50, 50, 150, 20);
l2=new Lable(“Second No.”);
l2.setBounds(10, 60, 50, 20);
tf2 = new TextField();
tf2.setBounds(50, 100, 150, 20);
l3=new Lable(“Result”);
l3.setBounds(10, 110, 150, 20);
tf3 = new TextField();
tf3.setBounds(50, 150, 150, 20);
tf3.setEditable(false);
b1 = new Button("+");
b1.setBounds(50, 200, 50, 50);
b2 = new Button("-");
b2.setBounds(120,200,50,50);
b3 = new Button("*");
b3.setBounds(220, 200, 50, 50);
b4 = new Button("/");
b4.setBounds(320,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
add(tf1);
add(tf2);
add(tf3);
add(b1);
add(b2);
add(b3);
add(b4);
Page No: 23 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
if (str.compareTo("MSBTE")!=0) // can be done with equals()
throw new NoMatchException("Strings are not equal");
else
System.out.println("Strings are equal");
}
catch(NoMatchException e)
{
System.out.println(e.getMessage());
}
}
}
a) Write a program to check whether the string provided by the user is palindrome 6M
or not.
class palindrome
System.out.println("Enter String:");
String word=br.readLine( );
int l=0;
int flag=1;
int r=len;
while(l<=r)
Page No: 20 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
index in the vector. Or The elementAt() method returns an element at the specified index.
addElement( ):
The addElement() method of Java Vector class is used to add the specified element to the
end of this vector. Adding an element increases the vector size by one.
b) Write a program to create a class 'salary with data members empid', ‘name' 6M
and ‘basicsalary'. Write an interface 'Allowance’ which stores rates of
calculation for da as 90% of basic salary, hra as 10% of basic salary and pf as
8.33% of basic salary. Include a method to calculate net salary and display it.
Ans interface allowance 6 M for
{ correct
double da=0.9*basicsalary; program
double hra=0.1*basicsalary;
double pf=0.0833*basicsalary;
void netSalary();
}
class Salary
{
int empid;
String name;
float basicsalary;
Salary(int i, String n, float b)
{
empid=I;
name=n;
basicsalary =b;
}
void display()
{
System.out.println("Empid of Emplyee="+empid);
System.out.println("Name of Employee="+name);
System.out.println("Basic Salary of Employee="+ basicsalary);
}
}
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1 = tf1.getText();
String s2 = tf2.getText();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
int c = 0;
if (e.getSource() == b1){
c = a + b;
}
else if (e.getSource() == b2){
c = a - b;
else if (e.getSource() == b3){
c = a * b;
else if (e.getSource() == b4){
c = a / b;
}
String result = String.valueOf(c);
tf3.setText(result);
}
public static void main(String[] args) {
new sample();
}
}
Page No: 24 | 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
The thread class defines several priority constants as: - default
priority
MIN_PRIORITY =1 values
NORM_PRIORITY = 5
MAX_PRIORITY = 10
2 M for
Thread priorities can take value from 1-10. method to
set and
getPriority(): The java.lang.Thread.getPriority() method returns the priority of the given change
thread.
import java.lang.*;