0% found this document useful (0 votes)
70 views

Java Imp

The document provides information about various math functions and Java concepts: 1. It describes the sqrt() and pow() math functions - sqrt() finds the square root and pow() raises a number to a power. 2. It lists the four access specifiers in Java - private, default, protected, and public and how they control access to classes, methods, and variables. 3. It provides the wrapper class methods to convert between strings and primitive ints. 4. It explains that the static keyword is used for memory management and can be used with variables, methods, blocks, and nested classes to share the same variable or method across instances. 5. It lists the keywords used for exception

Uploaded by

Hemil Shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Java Imp

The document provides information about various math functions and Java concepts: 1. It describes the sqrt() and pow() math functions - sqrt() finds the square root and pow() raises a number to a power. 2. It lists the four access specifiers in Java - private, default, protected, and public and how they control access to classes, methods, and variables. 3. It provides the wrapper class methods to convert between strings and primitive ints. 4. It explains that the static keyword is used for memory management and can be used with variables, methods, blocks, and nested classes to share the same variable or method across instances. 5. It lists the keywords used for exception

Uploaded by

Hemil Shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Give Syntax and Example of the Following Math Functions:-

a.) sqrt()
b.) pow()
All the Math functions in Java lie under the Package of java.lang.Math
sqrt():- sqrt() is a Math Function in Java, which is used for finding out the Square- Root of a specific Number. The
Short Form of Square Root, i.e. sqrt is used here.
SYNTAX:- Math.sqrt(a)
pow():- 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)

Enlist Access Specifiers in Java


There are two types of Specifiers in Java: Access Specifiers and Non-Access Specifiers. The access Specifiers in
Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of
fields, constructors, methods, and class by applying the access modifier on it. Java provides four types of access
Specifiers or visibility specifiers i.e. default, public, private, and protected.
1. Private: The access level of a private modifier is only within the class. It cannot be accessed from outside
the class.
2. Default: The access level of a default modifier is only within the package. It cannot be accessed from
outside the package. If you do not specify any access level, it will be the default.
3. Protected: The access level of a protected modifier is within the package and outside the package through
child class. If you do not make the child class, it cannot be accessed from outside the package.
4. Public: The access level of a public modifier is everywhere. It can be accessed from within the class,
outside the class, within the package and outside the package.
There are many non-access Specifiers, such as static, abstract, synchronized, native, volatile, transient, etc.

Name the wrapper class methods for the following:


(i)To convert string objects to primitive int.
(ii)To convert primitive int to string objects.
(i)String str=”5”;
int value = Integer.parseInt(str);
(ii)int value=5;
String str=Integer.toString(value);

State the use of Static Keyword


In Java, Static Keyword is mainly used for Memory Management. It can be used with Variables, Methods, Blocks
and Nested Classes. It is a Keyword which is used to share the same Variable or Method of a given class. Basically,
static is used for a Constant Variable or a method that is same for every instance of a class. The main method of a
class is generally labeled Static.

Enlist the Keywords used for Exception Handling in Java.


The following are the primary keywords used in the process of Exception handling in Java.
Keyword Description
try The “try” keyword is used to specify the exception block
catch The “catch” keyword is used to specify the solution
finally The “finally” keyword has a mandatorily executable code
throw The “throw” keyword throws an exception
throws The “throws” keyword is used to declare an exception 
Give the syntax of <param> tag to pass parameters to an applet.
Syntax:- <param name=”name” value=”value”>
Example:- <param name=”color” value=”red”>

Give any two Methods from File Class with their useage in Java
Method Description Return Type
canRead() Tests whether the application can read the file denoted by this abstract pathname. boolean
canWrite() Tests whether the application can modify the file denoted by this abstract pathname. boolean
createNewFile() Atomically creates a new, empty file named by this abstract pathname. boolean
delete() Deletes the file or directory denoted by this abstract pathname. boolean
getAbsolutePath()  Returns the absolute pathname string of this abstract pathname. String
getFreeSpace() Returns the number of unallocated bytes in the partition. long
getName() Returns the name of the file or directory denoted by this abstract pathname. String
getParent() Returns the pathname string of this abstract pathname’s parent. String
getParentFile() Returns the abstract pathname of this abstract pathname’s parent. File
getPath() Converts this abstract pathname into a pathname string. String
setReadOnly() Marks the file or directory named so that only read operations are allowed. boolean
isHidden() Tests whether the file named by this abstract pathname is a hidden file. boolean
length() Returns the length of the file denoted by this abstract pathname. long
toString() Returns the pathname string of this abstract pathname. String
toURI() Constructs a file URI that represents this abstract pathname. URI

Write a program to find largest between two numbers using "?:" Operator
public class Main
{
public static void main (String[]args)
{
int num1 = 50, num2 = 10, temp;

if (num1 == num2)
System.out.println ("Both are Equal\n");
else
{
temp = num1 > num2 ? num1 : num2;
System.out.println (temp + " is largest");
}

}
}

Define a Class “Student” with suitable Data- Members. Create two Objects using two different Constructors
of the Class.
import java.util.Scanner;
public class Student{
private String name;
private int age;
private int m1;
private int m2;
private int m3;
private int maximum;
private double average;
public Student(String n, int a, int s1, int s2, int s3) {
name = n;
age = a;
m1 = s1;
m2 = s2;
m3 = s3;
}
public Student() {
name = "";
age = 0;
m1 = 0;
m2 = 0;
m3 = 0;
maximum = 0;
average = 0;
}
public void accept() {
Scanner in = new Scanner(System.in);
System.out.print("Enter name: ");
name = in.nextLine();
System.out.print("Enter age: ");
age = in.nextInt();
System.out.print("Enter Subject 1 Marks: ");
m1 = in.nextInt();
System.out.print("Enter Subject 2 Marks: ");
m2 = in.nextInt();
System.out.print("Enter Subject 3 Marks: ");
m3 = in.nextInt();
}
public void compute(){
if (m1 > m2 && m1 > m3)
maximum = m1;
else if (m2 > m1 && m2 > m3)
maximum = m2;
else
maximum = m3;
average = (m1 + m2 + m3) / 3.0;
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Subject 1 Marks: " + m1);
System.out.println("Subject 2 Marks: " + m2);
System.out.println("Subject 3 Marks: " + m3);
System.out.println("Maximum Marks: " + maximum);
System.out.println("Average Marks: " + average);
}
public static void main(String args[]) {
Student obj = new Student();
obj.accept();
obj.compute();
obj.display();
}
}
Describe the Life- Cycle of a Thread with suitable Diagram.

1. New Thread: When a new thread is created, it is in the new state. The thread has not yet started to run when
the thread is in this state. When a thread lies in the new state, its code is yet to be run and hasn’t started to
execute.
2. Runnable State: A thread that is ready to run is moved to a runnable state. In this state, a thread might
actually be running or it might be ready to run at any instant of time. It is the responsibility of the thread
scheduler to give the thread, time to run. 
A multi-threaded program allocates a fixed amount of time to each individual thread. Each and every thread
runs for a short while and then pauses and relinquishes the CPU to another thread so that other threads can get
a chance to run. When this happens, all such threads that are ready to run, waiting for the CPU and the
currently running thread lie in a runnable state.
3. Blocked/Waiting state: When a thread is temporarily inactive, then it’s in one of the following states: 
 Blocked
 Waiting
4. Dead State: A thread terminates because of either of the following reasons: 
 Because it exits normally. This happens when the code of the thread has been entirely executed by the program.
 Because there occurred some unusual erroneous event, like segmentation fault or an unhandled exception.

Write a Program to Copy the Content of one File to Another File.


import java.io.*;
import java.util.*;
class Copyfile
{
public static void main(String arg[]) throws Exception
{
Scanner sc = new Scanner(System.in);
System.out.print("Provide source file name :");
String sfile = sc.next();
System.out.print("Provide destination file name :");
String dfile = sc.next();
FileReader fin = new FileReader(sfile);
FileWriter fout = new FileWriter(dfile, true);
int c;
while ((c = fin.read()) != -1)
{
fout.write(c);
}
System.out.println("Copy finish...");
fin.close();
fout.close();
}
}
Write a Program to Divide any Positive Even Integer by two using Bitwise Shift Operator.

Describe the use of following methods:


(i) drawoval ()
(ii) getFont ()
(iii) drawRect ()
(iv) getFamily ()
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. Example: g.drawOval(10,10,50,50);
getFont():- It is a method of Graphics class used to get the font property Font f = g.getFont();
String fontName = f.getName();
Where g is a Graphics class object and fontName is string containing name of the current font.
drawRect():- The drawRect() method display an outlined rectangle.
Syntax: void drawRect(int top,int left,int width,int height)
The upper-left corner of the Rectangle is at top and left. The dimension of the Rectangle is specified by width and
height. Example: g.drawRect(10,10,60,50);
getFamily():- The getfamily() method Returns the family of the font.
String family = f.getFamily();
Where f is an object of Font class.

Enlist the types of Stream Classes and describe the Methods for Reading and Writing Data for each type.
Based on the data they handle there are two types of streams −
 Byte Streams − These handle data in bytes (8 bits) i.e., the byte stream classes read/write data of 8 bits.
Using these you can store characters, videos, audios, images etc. InputStream is used for Reading and
OutputStream is used for Writing the Data.
 Character Streams − These handle data in 16 bit Unicode. Using these you can read and write text data
only. Reader is used for Reading, and Writer is used for Writing the Data.

Describe Types of Variables in Java with their Scope.


In Java, there are three types of variables based on their scope. in Java -
1. Member Variables (Class Level Scope)
2. Local Variables (Method Level Scope)
Member Variables (Class Level Scope):- These are the variables that are declared inside the class but outside any
function have class-level scope. We can access these variables anywhere inside the class. Note that the access
specifier of a member variable does not affect the scope within the class.
Java allows us to access member variables outside the class with the following rules:
Access Modifier Package Subclass Word
public Yes Yes Yes
protected Yes Yes No
private No No No
default Yes No No
Local Variables (Method Level Scope):- These are the variables that are declared inside a method, constructor,
or block have a method-level or block-level scope and cannot be accessed outside in which it is defined. Variables
declared inside a pair of curly braces {} have block-level scope.

Write a Program to initialize the Object of a Class “Student” using Parameterized Constructor.
class Student4
{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n)
{
id = i;
name = n;
}
//method to display the values
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
}
}

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 Math_s;
public class addition
{
Int a,b;
public add(int x,int y)
{
a=x;
b=y;
return(a+b);
}
}
public class subtraction
{
Int a,b;
public sub(int x, int y)
{
a=x;
b=y;
return(a-b);
}
}
import Math_s.addition;
import Math_s.subtraction;
class Basic
{
public static void main(String args[])
{
addition a=new addition( );
int sum=a.add(10,5);
subtraction s = new subtraction();
int subt=s.sub(10,5);
System.out.println(“Addition= “+sum);
System.out.println(“Subtraction= “+subt);
}
}

Difference between Java Application and Java Applet. (Any 4 Points)


Java Application Java Applet
A Java Application is a type of program that can get independently A Java Applet is a small program that makes use of another
executed on a computer. application program so that we can execute it.
The execution of the Java application begins with the main() method. The Java applet initializes through the init(). It does not require the
The usage of the main() is a prerequisite here. usage of any main() method.
It cannot run alone, but it requires JRE for its execution. It cannot run independently but requires APIs for its execution (Ex.
APIs like Web API).
One needs to install a Java application priorly and explicitly on a local A Java applet does not require any prior installation.
computer.
It is possible to establish communication with the other servers. It cannot really establish communication with the other servers.
The Java applications are capable of performing the read and write A Java applet cannot perform these applications on any local
operations on various files present in a local computer. computer.
These can easily access the file or data present in a computer system or These cannot access the file or data available on any system or local
device. computers.
Java applications are pretty trusted, and thus, come with no security Java applets are not very trusted. Thus, they require security.
concerns.

Write a program to copy content of file “input.txt into file “output.txt”.


import java.io.*;
class copyf
{
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();
}
}
}
}

You might also like