Java
Java
When same method name but different When same method name, same argument
parameters and return type is used in a single and same return type is used in a sub class
class then it is known as Method Overloading. and superclass is called Method Overriding.
Method Overloading can be done in same Method Overriding can be done in different
class. class.
Method Overloading is the example of compile Method overriding is the example of run
time Polymorphism. time polymorphism.
Return type can be same or different in Return type must be same in Method
method Overloading. overriding.
Example:
class WrapperDemo
{
public static void main(String args[ ])
{
int a=10;
Integer i=Integer.valueOf(a);
System.out.println(“a=”+a+”i=”+i);
}
}
11. Explain Garbage Collection.
Ans:
Java does not support Destructor to free up the memory. For that java support
Garbage Collection.
A Java runtime environment delete object periodically when it determine that they
are no longer being used. This process is known as Garbage Collection.
In a java Garbage Collection process is done by Garbage Collector () and Finalizer () for
memory relocation.
Java Garbage Collection is the process by which java program perform automatic
memory management.
12. Give the Difference between String Class and String Class Buffer.
Ans:
String class StringBuffer class
Example:
class a
{
int i;
}
class b extends a
{
Void display()
{
System.out.println(“the value of I is =”+i);
}
}
class singledemo
{
public static void main(String args[])
{
b b1=new b();
b1.i=7;
b1.display();
}
}
2. Multilevel Inheritance:
New class is derived from derive class.
Java uses this type of inheritance mainly implementing its class library.
C
Syntax:
class a
{
//statement
}
class b extends a
{
//statement
}
class c extends b
{
//statement
}
Example:
class a
{
int I;
}
class b extends a
{
int j;
void display()
{
System.out.println(“I in class b”+j);
}
}
class c extends b
{
void sum()
{
System.out.println(“the sum is ”+(i+j));
}
}
class MultilevelDemo
{
public static void main(String args[])
{
b b1=new b();
b1.i=5;
c c1=new c();
c1.display();
c1.i=10;
c1.j=20;
c1.sum();
}
}
interface interfaceName
{
Variable Declaration;
Method Declaration;
}
Example:
interface A
{
int i=10;
}
interface B
{
int j=20;
}
class C implements A,B
{
void sum()
{
System.out.println(“the sum is ”+(i+j));
}
}
class InterFaceDemo
{
public static void main(String args[])
{
C c1=new C();
c1.sum();
}
}
18. List out System Define Package.
Ans:
Package name Description
When we create a thread object, the thread is born and is said to be in newborn
state.
The thread is not still scheduled for running.
2) Runnable state
Running means that processor has given its time to the thread for its execution.
A running thread may change its state to another state using resume (), modify
(), sleep (), wait () methods etc.
4) Blocked state
Every thread has a life cycle. A running thread ends its life when it has
completed executing its run ( ) method.
It is natural death. However we can kill it by sending the stop message to it as
any state thus causing a premature death for it.
Throws
The Java throws keyword is used to declare an exception.
If a method does not handle a checked exception, the method must declare it using the
throws keyword.
The throws keyword appears at the end of a method’s signature.
You can declare multiple exceptions.
Syntax:
return_type method_name () throws exception_class_name
{
//method code
}
Example:
import java.io.*;
class M
{
void method()throws IOException
{
throw new IOException("device error");
}
}
public class Testthrows2
{
public static void main(String args[])
{
try
{
M m=new M ();
m.method();
}
catch(Exception e)
{
System.out.println("exception handled");
}
System.out.println("normal flow...");
}
}
Errors which are detected by javac at the Errors which are detected by java
compilation time of program are known as interpreter at the time of program are
compile time errors. known as run time errors.