JPR Winter 23
JPR Winter 23
OUR SERVICES:
Diploma in All Branches, All Subjects
Degree in All Branches, All Subjects
BSCIT / CS
Professional Courses
operator : 2 marks
List any two Bitwise
Logical Operators: operator : 2 marks
1. AND Operator ( && ) – if( a && b ) [if true execute else don’t]
2. OR Operator ( || ) – if( a || b) [if one of them is true to execute else don’t]
3. NOT Operator ( ! ) – !(a<b) [returns false if a is smaller than b]
Bitwise Operator:
1. Bitwise OR (|)
2. Bitwise AND (&)
3. Bitwise XOR (^)
4. Bitwise Complement (~)
5. Bitwise left shift(<<)
6. Bitwise right shift(>>)
Page No: 1 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
b) Define constructor. 2M
Ans A constructor in Java is a special method that is used to initialize objects. The Correct/suitable
constructor is called when an object of a class is created. It can be used to set definition- 1 M
initialvalues for object attributes. Syntax or
Page No: 2 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
For Example: Example- 1 M
class Test
{
Test()
{
// constructor body
}
}
c) Write down the syntax of array declaration, initialization. 2M
Here, the datatype is the type of element that will be stored in the array, square and
bracket[] is for the size of the array, and arrayName is the name of the array.
1 M-array
initialization
The syntax of initializing an array is given below.
datatype [] arrayName = new datatype [ size ];
Ans There are three ways to access the package from outside the package. Any 2 correct ways
–2M
• import package.*;
• import package.classname;
• fully qualified name
e) Differentiate between starting thread with run( ) method and start() method. 2M
Creates a new thread and the No new thread is created and the run()
run() method is executed on the method is executed on the calling
newly created thread. thread itself.
Defined
Defined in java.lang.Runnable interface and
in java.lang.Thread class. must be overridden in the
implementing class.
Page No: 3 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
A new thread will be created and No new thread will be created and
it is responsible to complete the main thread will be responsible to
job. complete the job.
Ans Attach a file to a FileInputStream as this will enable us to read data from the file as Correct syntax-2 M
shown below as follows:
FileInputStream input = new FileInputStream("input.txt");
Now in order to read data from the file, we should read data from the
FileInputStream as shown below:
ch=fileInputStream.read();
for(i=0;i<l;i++)
{ //apply bubble sort
for(j=(i+1);j<l;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("Ascending order of numbers:");
for(i=0;i<l;i++)
System.out.println(""+a[i]);
}
}
Output:
Ascending order of numbers:
12
19
45
56
78
78
85
95
c) Define Thread. Draw life cycle of Thread. 4M
Page No: 5 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
Ans Thread is a smallest unit of executable code or a single task is also called as thread. Definition of
thread- 2 M
Each tread has its own local variable, program counter and lifetime.
A thread is similar to program that has a single flow of control.
Diagram: 2M
String line;
int wordCount = 0;
int paraCount = 0;
while ((line = bufferedReader.readLine()) != null)
{
if (line.equals("")) {
Page No: 6 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
paraCount += 1;
}
else {
String words[] = line.split("\\s+");
wordCount += words.length;
}
}
System.out.println("Total word count = "+ wordCount);
}
}
int n = s.nextInt();
switch(n)
{
Page No: 7 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
case 1:
System.out.print("Enter money to be withdrawn:");
withdraw = s.nextInt();
if(balance >= withdraw)
{
balance = balance - withdraw;
System.out.println("Please collect your money");
}
else
{
System.out.println("Insufficient Balance");
}
System.out.println("");
break;
case 2:
System.out.print("Enter money to be deposited:");
deposit = s.nextInt();
balance = balance + deposit;
System.out.println("Your Money has been successfully depsited");
System.out.println("");
break;
case 3:
System.out.println("Balance : "+balance);
System.out.println("");
break;
case 4:
System.exit(0);
}
}
}
b) Differentiate between method overloading and method overriding. 4M
Page No: 8 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
Ans 4 M for any four
correct point
Method Overloading Method Overriding
Method overloading is a compile-time Method overriding is a run-time
polymorphism. polymorphism.
Method overriding is used to grant the
Method overloading helps to increase specific implementation of the method
the readability of the program. which is already provided by its parent
class or superclass.
It is performed in two classes with
It occurs within the class.
inheritance relationships.
Method overloading may or may not Method overriding always needs
require inheritance. inheritance.
In method overloading, methods must In method overriding, methods must
have the same name and different have the same name and same
signatures. signature.
In method overloading, the return type
In method overriding, the return type
can or can not be the same, but we just
must be the same or co-variant.
have to change the parameter.
Static binding is being used for Dynamic binding is being used for
overloaded methods. overriding methods.
It gives better performance. The reason
Poor Performance due to compile time behind this is that the binding of
polymorphism. overridden methods is being done at
runtime.
Private and final methods can be Private and final methods can’t be
overloaded. overridden.
The argument list should be different The argument list should be the same
while doing method overloading. in method overriding.
Page No: 9 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
Ans The applet life cycle can be defined as the process of how the object is created, 2 M for diagram
started, stopped, and destroyed during the entire execution of its application. It and 2 M for
basically has five core methods namely init(), start(), stop(), paint() and explanation
destroy().These methods are invoked by the browser to execute.
Page No: 10 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
• 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.
• Syntax:-
public void start()
{
……..
……..
}
3. 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.
• Syntax:-
public void stop()
{
……..
……..
}
4. 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.
• Syntax:-
public void destroy()
{
……..
……..
}
Page No: 11 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
Page No: 12 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
Ans 4 M for any correct
4 point
Byte Stream Class Character Stream Class
Byte streams access the file byte by byte
A character stream will read a file
(8 bits). character by character (16 bits).
Byte stream classes are classified into:Character stream classes are classified
1. Input Stream Classes into:
2. Output Stream Classes 1. Reader class
2. Writer class
InputStream/OutputStream class is byte- The Reader/Writer class is character-
oriented. oriented.
The methods for byte streams generally The methods for character streams
work with byte data type. generally accept parameters of data
type char parameters.
Byte-stream classes end with the suffix Character-stream classes end with the
InputStream and OutputStream. suffix Reader or Writer.
It is possible to translate character stream It is possible to translate byte stream
into byte stream with into a character stream with
OutputStreamWriter. InputStreamReader.
Byte streams specifically used for The advantage of character streams,
reading and writing data in byte format. that is make it easy to write programs,
which is not dependent upon a specific
character encoding.
No conversion needed. Character streams convert the
underlying data bytes to Unicode,
which is a costly operation.
InputStream and OutputStream are used Reader and Writer uses Unicode, hence
for reading or writing binary data. they can be internationalized. Hence in
some cases they are more efficient than
byte streams.
Page No: 13 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
For e.g.
class widening
{
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.
For e.g.
class narrowing
{
Public static void main(String[])
{
Double d=100.04;
Long l=(long) d;
Int i=(int) l;
System.out.println(“Int value is”+i);
System.out.println(“Long value is”+l);
System.out.println(“Float value is”
}
}
Page No: 14 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
student (student s)//copy constructor
{
id=s.id;
name=s.name;
}
void display()
{
System.out.println(id+“ ”+name)
}
public static void main(String args[])
student s1=new student(111, “ABC”);
s1.display();
student s2= new student(s1);
s2.display();
}
}
c) Write a program to show the Hierarchical inheritance. 4M
Page No: 15 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
{
double area1;
void getd()
{
super.getdata();
}
void area()
{
area1=dim1*dim2;
System.out.println("The Area of Rectangle is: "+area1);
}
}
class triangle extends shape
{
double area1;
void getd()
{
super.getdata();
}
void area()
{
area1=(0.5*dim1*dim2);
System.out.println("The Area of Triangle is: "+area1);
}
}
class methodover1
{
public static void main(String args[])
{
rectangle r=new rectangle();
System.out.println("For Rectangle");
r.getd();
r.disp();
r.area();
triangle t=new triangle();
t.getd();
t.disp();
t.area();
}
}
OR
class A
{
Page No: 16 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
public void methodA()
{
System.out.println("method of Class A");
}
}
class B extends A
{
public void methodB()
{
System.out.println("method of Class B");
}
}
class C extends A
{
public void methodC()
{
System.out.println("method of Class C");
}
}
class D extends A
{
public void methodD()
{
System.out.println("method of Class D");
}
}
class JavaExample
{
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
//All classes can access the method of class A
obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}
d) Explain any four font methods with example. 4M
Ans Font is a class that belongs to the java.awt package. 2 M for any four-
Following are the methods of Font class: font method with
Page No: 17 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
description and 2
Methods Description M for example
String getFamily( ) Returns the name of the font family to which the
invoking font belongs.
static Font Returns the font associated with the system property
getFont(String specified by property. null is returned if property does
property) not exist.
Example:
import java.awt.*;
import java.applet.*;
public class Shapes extends Applet
{
Font f,f1;
String s,msg;
String fname;
String ffamily;
int size;
int style;
public void init()
{
f= new Font("times new roman",Font.ITALIC,20);
setFont(f);
msg="is interesting";
s="java programming";
fname=f.getFontName();
ffamily=f.getFamily();
size=f.getSize();
style=f.getStyle();
Page No: 18 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
String f1=f.getName();
}
public void paint(Graphics g)
{
g.drawString("font name"+fname,60,44);
g.drawString("font family"+ffamily,60,77);
g.drawString("font size "+size,60,99);
g.drawString("fontstyle "+style,60,150);
g.drawString("fontname "+f1,60,190);
}
}
/*<applet code=Shapes.class height=300 width=300></applet>*/
e) Write a program to append content of one file into another file. 4M
Ans import java.io.*; 4 M for correct
class copyf program
{
public static void main(String args[]) throws IOException
{
BufferedReader in=null;
BufferedWriter out=null;
try
{
in=new BufferedReader(new FileReader("input.txt"));
out=new BufferedWriter(new FileWriter("output.txt"));
int c;
while((c=in.read())!=-1)
{
out.write(c);
}
System.out.println("File copied successfully");
}
finally
{
if(in!=null)
{
in.close();
}
if(out!=null)
{
out.close();
}
Page No: 19 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
}
}
}
a) Explain vector with the help of example. Explain any 3 methods of vector 6M
class.
Ans • Vector is a data structure that is used to store a collection of elements. Correct
Elements can be of all primitive types like int, float, Object, etc. Vectors are explaination-2 M
dynamic in nature and accordingly, grow or shrink as per the requirement.
• Vector Class in Java is found in the java.util package. List of constructors
and methods of
• Vector class is a child class of the AbstractList class and implements the List
vector class-2 M
interface. Therefore, we can use all the methods of the List interface.
• Vectors are known to give ConcurrentModificationException when accessed Example – 2 M
concurrently at the time of modification.
• When a Vector is created, it has a certain capacity to store elements that can
be defined initially. This capacity is dynamic in nature and can be increased
or decreased.
• By definition, Vectors are synchronized, which implies that at a time, only one
thread is able to access the code while other threads have to wait.
Vectors are created like arrays. It has three constructor methods
Vector list = new Vector(); //declaring vector without size
Vector list = new Vector(3); //declaring vector with size
Vector list = new Vector(5,2); //create vector with initial size and whenever it
need to grows, it grows by value specified by increment capacity
Example:
import java.util.*;
Page No: 20 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
public class Main
{
public static void main(String args[])
{
Vector v = new Vector();
v.addElement(new Integer(10));
v.addElement(new Integer(20));
v.addElement(new Integer(30));
v.addElement(new Integer(40));
v.addElement(new Integer(10));
v.addElement(new Integer(20));
System.out.println(v.size()); // display original size
System.out.println("Initial Vector: " + v);
v.removeElementAt(2); // remove 3rd element
System.out.println("Current Vector: " + v);
v.removeElementAt(3); // remove 4th element
System.out.println("Current Vector: " + v);
v.insertElementAt(11,2); // new element inserted at 3rd position
System.out.println("Current Vector: " + v);
System.out.println("Size of vector after insert delete operations: " + v.size());
}
}
Output:
6
Initial Vector: [10, 20, 30, 40, 10, 20]
Current Vector: [10, 20, 40, 10, 20]
Current Vector: [10, 20, 40, 20]
Current Vector: [10, 20, 11, 40, 20]
Size of vector after insert delete operations: 5
b) Develop and Interest Interface which contains Simple Interest and 6M
Compound Interest methods and static final field of rate 25%. Write a
class to implement those methods.
class Main
{
public static void main(String args[ ])
{
String str1= new String("India");
String str2= new String("Australlia");
try
{
if(str1.equals("India"))
System.out.println(" String is : "+str1);
else
if(str2.equals("India"))
System.out.println("\n String is : "+str2);
Page No: 22 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
else
throw new NoMatchException(str2);
}
catch(NoMatchException e)
{
System.out.println("\nCaught .... "+e);
}
}
}
OUTPUT:
String is : India
a) Write a program to print the sum, difference and product of two complex 6M
numbers by creating a class named "Complex" with separate methods for
each operation whose real and imaginary parts are entered by user.
// Empty Constructor
Complex()
{
}
// Constructor to accept
// real and imaginary part
Complex(int tempReal, int tempImaginary)
{
real = tempReal;
imaginary = tempImaginary;
}
Page No: 23 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
Complex temp = new Complex();
// Main Class
public class Main
{
// Main function
public static void main(String[] args)
{
Ans An error is an issue in a program that prevents the program from completing its task. Types of errors
There are several types of errors that occur in Java, including syntax errors, runtime with example – 3
errors, and logical errors. They are M
● Syntax Errors or Compilation Errors: These occur when the code violates
the rules of the Java syntax. These errors are usually caught by the Java and
compiler during the compilation phase. thread methods
● Example of compile time error: with any
public class Main
relevant/correct
{
example – 3 M
public static void main(String[] args)
{
int x = "5";
}
}
OUTPUT:
● Runtime Errors: These errors occur when the code encounters an unexpected
behaviour during its execution. These errors are usually caused by flawed
logic or incorrect assumptions in the code and can be difficult to identify and
fix.
● The most common run-time errors are:
a) Dividing an integer by zero
b) Accessing an element that is out of bounds of an array
c) Trying to store value into an array of an incompatible class or type Passing
parameter that is not in a valid range or value for method
d) Trying to illegally change status of thread
e) Attempting to use a negative size for an array
f) Converting invalid string to a number
g) Accessing character that is out of bound of a string
These errors can be handled by uexception handling with help of try-catch- final
block
Page No: 26 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
(i) Priorities in threads
To get and set priority of a thread in java following methods are used,
1. public final int getPriority(): java.lang.Thread.getPriority() method
returns priority of given thread.
2. public final void setPriority(int
newPriority): java.lang.Thread.setPriority() method changes the priority
of thread to the value newPriority. This method throws
IllegalArgumentException if value of parameter newPriority goes
beyond minimum(1) and maximum(10) limit.
Example:
// Java Program to Illustrate Priorities in Multithreading
// via help of getPriority() and setPriority() method
// Main class
class ThreadDemo extends Thread {
// Method 1
// run() method for the thread that is called
// as soon as start() is invoked for thread in main()
public void run()
{
// Print statement
System.out.println("Inside run method");
}
// Thread 1
// Display the priority of above thread using getPriority() method
System.out.println("t1 thread priority : " + t1.getPriority());
// Thread 1
// Display the priority of above thread
System.out.println("t2 thread priority : " + t2.getPriority());
// Thread 3
System.out.println("t3 thread priority : " + t3.getPriority());
Page No: 27 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
// Setting priorities of above threads by passing integer arguments
t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
// Main thread
OUTPUT:
t1 thread priority : 5
t2 thread priority : 5
t3 thread priority : 5
t1 thread priority : 2
t2 thread priority : 5
t3 thread priority : 8
Currently Executing Thread : main
Main thread priority : 5
Main thread priority : 10
c) Write a program to draw a chessboard in Java Applet. 6M
Page No: 28 | 28
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
_________
int x, y;
for (int row = 0; row & lt; N; row++) {
Page No: 29 | 28