Jpr 22412 Sample Test Paper 70 Marks
Jpr 22412 Sample Test Paper 70 Marks
Jpr 22412 Sample Test Paper 70 Marks
Answer:
i) sqrt () - The java.lang.Math.sqrt() returns the square root of a value of type double
passed to it as argument.
Syntax: public static double sqrt(double a); Output will be: 15
e.g. System.out.println(Math.sqrt(225));
ii) pow () - The java.lang.Math.pow() is used to calculate a number raise to the power of
some other number. This function accepts two parameters and returns the value of first
parameter raised to the second parameter.
Syntax: public static double pow(double a, double b)
e.g. System.out.println(Math.pow(5, 3)); Output will be: 125
1. Integer.parseInt()
Example to convert a String “10” to primitive int.
String number = "10";
int result = Integer.parseInt(number);
System.out.println(result);
Output: 10
2. Integer.valueOf()
Alternatively, you can use Integer.valueOf(), it will returns an Integer object.
String number = "10";
Integer result = Integer.valueOf(number);
System.out.println(result);
Output : 10
The static keyword in Java is used for memory management mainly. We can apply java static
keyword with variables, methods, blocks and nested class. The static keyword belongs to the
class than an instance of the class.
static variable- If you declare any variable as static, it is known as a static variable.
static method- If you apply static keyword with any method, it is known as static method.
o A static method belongs to the class rather than the object of a class.
o It can be invoked without an object/instance of a class.
o It can access static data member and can change the value of it.
The <param> tag contains two attributes: name and value which are used to specify the name of
the parameter and the value of the parameter respectively.
<PARAM> tags are the only way to specify applet-specific parameters. It is used to pass
parameters to the applet. Applets read user-specified values for parameters with
the getParameter() method.
g) Give any two methods from File class with their usage.
1. boolean mkdir() : Creates the directory named by this abstract pathname.
2. boolean renameTo(File dest) : Renames the file denoted by this abstract pathname.
3. long length() : Returns the length of the file denoted by this abstract pathname.
4. boolean delete() : Deletes the file or directory denoted by this abstract pathname.
5. boolean exists() : Tests whether the file or directory denoted by this abstract pathname
exists.
Output:
Largest number between 5 and 10 is 10.
b) Define class Student with suitable data members create two objects using two different
constructors of the class.
class Student{
int id;
String name;
int age;
//creating two arg constructor
Student(int i,String n){
id = i;
name = n;
}
//creating three arg constructor
Student(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+" "+name+" "+age);}
Output:
111 Karan 0
222 Aryan 25
import java.io.*;
public class CopyFile
{
int c;
while ((c = in.read()) != -1)
{
out.write(c);
}
}
finally
{
in.close();
out.close();
}
}
}
interface vehicleone{
int speed=90;
public void distance();
}
interface vehicletwo{
int distance=100;
public void speed();
}
class Multiple{
public static void main(String args[]){
System.out.println("Vehicle");
obj.distance();
obj.speed();
}
}
(i) drawOval( )
Drawing Ellipses and circles: To draw an Ellipses or circles used drawOval() method can be used.
Syntax: void drawOval(int top, int left, int width, int height);
The ellipse is drawn within a bounding rectangle whose upper-left corner is specified by top and
left and whose width and height are specified by width and height to draw a circle or filled circle,
specify the same width and height the following program draws several ellipses and circle.
Example: g.drawOval(10,10,50,50);
(ii) getFont()
The getFont() method gets the font specified by the system property name. If name is not a valid
system property, null is returned.
Syntax: public static Font getFont (String name)
e.g. Font f=g.getFont();
(iii)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
Example: g.drawArc(10, 10, 30, 40, 40, 90);
iv) getFamily()
Returns the font family, as string, for which the font belongs.
Syntax: public String getFamily()
Font f1 = new Font("SansSerif", Font.ITALIC, 18);
String n= f1.getFamily();
d) Enlist types of stream classes and describe methods for reading and writing data for each
type.
There are two kinds of Character Stream classes - Reader classes and Writer classes.
Reader Classes - These classes are subclasses of an abstract class, Reader and they are used to
read characters from a source(file, memory or console).
Writer Classes - These classes are subclasses of an abstract class, Writer and they used to write
characters to a destination(file, memory or console)
These methods are of Reader class.
Methods Description
int read() This method reads a characters from the input stream.
int read(char[] This method reads a chunk of characters from the input stream and store them
ch) in its char array, ch.
This method closes this output stream and also frees any system resources
close()
connected with it.
Local variables are created when the method, constructor or block is entered and the
variable will be destroyed once it exits the method, constructor, or block.
Local variables are visible only within the declared method, constructor, or block.
Instance Variables
Instance variables are declared in a class, but outside a method, constructor or any block.
When a space is allocated for an object in, a slot for each instance variable value is
created.
Instance variables are created when an object is created with the use of the keyword
'new' and destroyed when the object is destroyed.
The instance variables are visible for all methods, constructors and block in the class.
Instance variables can be accessed directly by calling the variable name inside the class.
Class/Static Variables
Class variables also known as static variables are declared with the static keyword in a
class, but outside a method, constructor or a block.
There would only be one copy of each class variable per class, regardless of how many
objects are created from it.
Static variables are created when the program starts and destroyed when the program
stops.
Static variables can be accessed by calling with the class name ClassName.VariableName.
class Student{
int id;
String name;
int age;
Student(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+" "+name+" "+age);}
Output:
222 Aryan 25
c) Write a program to create package Math_s having two classes as addition and subtraction.
Use suitable methods in each class to perform basic operations.
package Mypack;
public class addition
{
public void add(int a, int b)
{
System.out.println("Addition is: "+(a+b));
}
}
package Mypack;
public class subtraction
{
public void subtract(int a, int b)
{
System.out.println("Subtraction is: "+(a-b));
}
}
import java.util.*;
import Mypack.*;
public class basic_operations
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter two integer numbers");
int a=sc.nextInt();
int b=sc.nextInt();
addition a1=new addition();
a1.add(a,b);
subtraction s1= new subtraction();
s1.subtract(a,b);
}
}
d) Differentiate between Java Application and Java Applet (any 4 points)
BASIS FOR APPLET APPLICATION
COMPARISON
Basic It is small program uses An application is the programs
another application program executed on the computer
for its execution. independently.
main() method Do not use the main method Uses the main method for execution
Execution Cannot run independently Can run alone but require JRE.
require API's (Ex. Web API).
Installation Prior installation is not needed Requires prior explicit installation on
the local computer.
Read and write The files cannot be read and Applications are capable of
operation write on the local computer performing those operations to the
through applet. files on the local computer.
Communication with Cannot communicate with Communication with other servers is
other servers other servers. probably possible.
Restrictions Applets cannot access files Can access any data or file available
residing on the local computer. on the system.
Security Requires security for the No security concerns are there.
system as they are untrusted.
e) Write a program to count number of words from a text file using stream classes.
import java.util.*;
import java.io.*;
Two – dimensional array is the simplest form of a multidimensional array. A two – dimensional
array can be seen as an array of one – dimensional array for easier understanding.
Indirect Method of Declaration:
Declaration – Syntax:
data_type[][] array_name = new data_type[x][y];
For example: int[][] arr = new int[10][20];
Initialization – Syntax:
array_name[row_index][column_index] = value;
For example: arr[0][0] = 1;
data_type[][] array_name = {
{valueR1C1, valueR1C2, ....},
{valueR2C1, valueR2C2, ....}
};
Initialization – Syntax:
array_name[array_index][row_index][column_index] = value;
For example: arr[0][0][0] = 1;
Direct Method of Declaration:
Syntax:
data_type[][][] array_name = {
{
{valueA1R1C1, valueA1R1C2, ....},
{valueA1R2C1, valueA1R2C2, ....}
},
{
{valueA2R1C1, valueA2R1C2, ....},
{valueA2R2C1, valueA2R2C2, ....}
}
};
For example: int[][][] arr = { {{1, 2}, {3, 4}}, {{5, 6}, {7, 8}} };
c) Write a program to define two threads for displaying even and odd numbers respectively
with a delay of 500 ms after each number.
import java.io.*;
import java.lang.Thread;
class Even extends Thread
{
public void run()
{
for(int i=1;i<=10;i++)
{
if(i%2==0)
{
System.out.println("Even Thread i="+i);
Thread. sleep(500);
}
}
System.out.println("Exit from Even");
}
}
class Odd extends Thread
{
public void run()
{
for(int j=1;j<=10;j++)
{
if(j%2!=0)
{
System.out.println("Odd Thread j="+j);
Thread. sleep(500);
}
}
System.out.println("Exit from Odd");
}
}
class ThreadTest
{
public static void main(String args[])
{
new Even().start();
Odd ThOdd=new Odd();
ThOdd.start();
}
}
Q.6) Attempt any TWO of the following. 12 Marks
a) Write a program to define class Employee with members as id and salary. Accept data for five
employees and display details of employees getting highest salary.
import java.util.Scanner;
int empid;
String name;
float salary;
e[i].display();
}
}
}
1.Compile-time errors
These errors are errors which prevents the code from compiling because of error in the syntax
such as missing a semicolon at the end of a statement or due to missing braces, class not found,
etc. These errors will be detected by java compiler and displays the error onto the screen while
compiling.
These errors are errors which occur when the program is running. Run time errors are not
detected by the java compiler. It is the JVM which detects it while the program is running.
EXCEPTIONS:
Exception is a runtime error which can be handled or prevented and occurs during the execution
of the program.
Runtime exceptions.
Can be found in the compile time itself. These exceptions are also called as Checked exceptions.
Example – ClassNotFoundException occurs when the programmer has not mentioned the proper
class name in the code, same way FileNotFoundException occurs when the program is trying to
acces the file whose path is given wrong or the name of the file is mentioned wrong.
Checked exceptions must be handled by the programmer either by using try, catch or by
specifying the throws keyword (try, catch, throws are explained in the upcoming topic).
The main thing which has to be remembered is that checked exceptions needs to be handled
(checked).
The compiler forces the programmer to specify ‘throws’ keyword or to handle the exception
(using try, catch), if it is a checked exception.
RUNTIME EXCEPTIONS:
Are the exceptions which cannot be found while compiling the code and occurs during the
execution of the program.
Examples of runtime exceptions are ArithmeticException which occurs due to the divide by zero
condition, ArrayIndexOutOfBoundException which occurs due to the access of element whose
index is out of the array size declared, NullPointerException when the code is trying to access a
null value when an object value is needed in its place.
Compiler does not forces the method to handle the exception, unlike compile time exceptions.
RunTimeException is a special subclass of Exception class, which need not have to be handled
(hence called as unchecked). The reason for this is that JVM will notify the exception to the user
(what made the program to terminate).
RunTime exception occurs due to the bug present in the application code and the programmer
has to fix it.
Errors and the RunTime Exceptions are collectively called as Unchecked Exceptions.
Before deciding the exception handling mechanism, the programmer has to think whether the
client could be able to handle the exception.
Sometimes, even a novice programmer finds the exception handling mechanism a nightmare.
One has to know the best practices to be followed to implement exception handling.