Académique Documents
Professionnel Documents
Culture Documents
2. What is a class?
In manufacturing a blueprint is a descrioption of a device from which many physical
devices are constructed.
In software, a class is a description of an object.
A class describe the data that each object includes (variables).
A class describe the behaviour of that each object exhibit (methods).
3. What is an object?
An object is an instance of the class. Object store data and provides method for accessing
and modifying this data.
4. What is an instance?
At that particular time what ever the values hold by an object of a class is called as
instatnce. It is the state of an object.
2. Inheritance: process by which properties of one class acquire the another class. The
main purpose is code reusability.
In java, inheritance is achieved by using extends keyword
4. Polymorphism: It come from the two greek words poly meaning many and morphs
meaning forms. The ability to exist in different forms is called polymorphism.
In java, Polymorphism is achieved by using method overloading and overriding.
1
Java solves the problem of platform independence by using byte code. The java compiler
does not produce native executable code for a particular machine like a C compiler does.
Instead it produces intermediate code called byte code.
8. What is the size, range and default values of each primitive data type?
2
Instance variable: if the value of variable varied from object to object such type of
variables are called as instance variables. For every object a separate copy of instance
variables will be created. The scope of instance variables is exactly same as the scope of
object. Instance variables should be declare with in the class directly but outside of any
method or block or constructor. Instance variables can not be accessed from static area
directly. We can access by using object reference. For these variables initialization is not
required explicitly, JVM will provide default values.
Static variables: the value of the static variable is not varied from object to object. For all
the objects only one copy will be created at class level and it will be shared by all object
of that class. The scope of static variables is exactly same as the scope of the class i.e.
they will be created at the time of class loading and destroy at the time of class unloading.
Static variables should be declare with in the class directly but out side of any method or
constructor with static modifier. Static variable can be accessed by using class name or
by using object reference but recommended to use class name. For these variables
initialization is not required explicitly, JVM will provide default values.
3
Output: 10---10
20---20
Local variables: to meat the temporary requirements of the program some times we have
to creare variables inside method or block or constructor such type of variables are called
as local variables. These variables will be created while executing the block in which we
declared it and destroy once the block execution completed. For the local variables JVM
wont provide any default value compulsory we should provide initialization before using
them.
10. Why class name and file name should be same in java?
Java file may contain any number of java classes. If the file doesnt contain any public
class then file name could be anything. But if the file contain a public class (contains
main method) then the file name should be public class name. By which java interpreter
know about the entry point for the program.
4
It is a special method having same name as that class name and called automatically
when an object is created.The main purpose is to initialize the instance variables.
Ex:
public class Student
{
int roll;
int marks;
Student( )
{
roll=1;
marks=10;
}
public static void main(String[] args)
{
Student s=new Student();
System.out.println(s.roll+”---“+s.marks);
}
}
Output: 1---10
14. What is the difference between public, private, protected and default access modifiers ?
These are the keywords used while declaring variables, methods, classes and which will
restrict the accessing scope.
Public: public means accessible and visible to every one in the package and outside the
package.
Protected: protected means accessible and visible to every one in the same package and
only to sub classes outside the package.
Default: default is applied by default. It is package friendly. When we do not apply any
access modifier than it is applied. It means accessible only in the same package.
Private: private means accessible only to the same class.
5
Visibility public Protected Default private
From the same class Yes Yes Yes Yes
From any class in the same Yes Yes Yes No
package
From a subclass in the same Yes Yes Yes No
package
From a subclass outside the Yes Yes, No No
same package through
inheritance
From any no-subclass Yes No No No
outside the package
15. What is a singleton class?
For any java class if we are allowed to create only one object such type of class is called
singleton class.
Ex: Runtime, ActionServlet
6
18. Describe about public static void main (String[] args)
Public access modifier to call main method by JVM from anywhere
static access modifier to call mail method by JVM with out creatin any object to class.
void return type indicates main method wont return anything to JVM
main is a name which is configuried inside JVM
String[] args- command line arguments.
Method overriding: what ever methods parent has by default available to the child
through inheritance. Some times child may not satisfy with parent method
implementation. Then child is allow to redefine that method based on its requirement.
This process is called overriding. It is also called as runtime polymorphism or late
binding.
Ex: class Test
{
public void m1()
{
System.out.println(“ parent implementation”);
}
}
class Test1 extends Test
{
public void m1()
{
System.out.println(“child implementation”);
}
}
7
20. Can we overload the constructor?
Yes, we can overload the constructor.
Ex:
class Box
{
double width, height, depth;
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
Box()
{
width = height = depth = 0;
}
Box(double len)
{
width = height = depth = len;
}
double volume()
{
return width * height * depth;
}
}
public class Test
{
public static void main(String args[])
{
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
vol = mybox1.volume();
System.out.println(" Volume of mybox1 is " + vol);
vol = mybox2.volume();
System.out.println(" Volume of mybox2 is " + vol);
vol = mycube.volume();
System.out.println(" Volume of mycube is " + vol);
}
}
8
Output:
Volume of mybox1 is 3000.0
Volume of mybox2 is 0.0
Volume of mycube is 343.0
9
Ex: class Test
{
public void sum(int…x)
{
int total=0;
for(int y:x)
{
total=total+y;
}
System.out.println(total);
}
public static void main(string[] args)
{
Test t=new Test();
t.sum();
t.sum(10);
t.sum(10,20);
t.sum(10,20,30);
}
}
Output: 0
10
30
60
10
31. Why java won’t support multiple inheritance?
When one class extends more than one classes then this is called multiple inheritance.
Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it. If more than
one base class is having same method signature then it creates ambiguity to child class.
i.e. which method it has to access.
11
Errors are not caused by our program these are due to lack of system resources and these
are non recoverable.
39. What is the difference between checked exceptions and unchecked exceptions?
Checked exceptions: these are the exceptions which are checked by the compiler for
smooth execution of program at runtime.
In the case of checked exceptions compiler will check whether we are handling
exception. If the programmer not handling exception then compiler raise error.
Example: FileNotFoundException, IOException
Unchecked exceptions: these are the exceptions which are not checked by the compiler.
In the case of unchecked exceptions compiler wont chech whether the programmer
hadling exceptions or not.
Example: ArithmeticException, NullpointerException.
Try- While writing a program, if you think that certain statements in a program may raise
an exception, enclosed them in try block and handle that exception
Catch- This block must follow the try block, where you handle the exceptions raised in
the try block. A single try block can have several catch blocks associated with it. You can
catch different exceptions in different catch blocks. When an exception occurs in try
block, the corresponding catch block that handles that particular exception executes. For
12
example if an arithmetic exception occurs in try block then the statements enclosed in
catch block for arithmetic exception executes.
Finally block in java can be used to put "cleanup" code such as closing a file, closing
connection etc.
Ex:
class Test
{
public static void main(String args[])
{
int num1, num2;
try
{
num1 = 0;
num2 = 62 / num1;
System.out.println(num2);
System.out.println("end of try block");
}
catch (ArithmeticException e)
{
System.out.println("divide a number by zero is not possible");
}
catch (Exception e) {
System.out.println("Exception occurred");
}
System.out.println("out of try-catch block ");
}
}
Output:
divide a number by zero is not possible
out of try-catch block
Array Collection
13
1. Arrays are fixed in size and hence once 1. Collections are growable in nature and
we created an array we are not allowed to hence based on our requirement we can
increase or decrease the size based on our increase or decrease the size.
requirement.
2. Performance point of view arrays are 2. Performance point of view collections
recommended to use.(Faster) are not recommended to use.
3. Arrays can hold only homogeneous 3. Collections can hold both homogeneous
elements. and heterogeneous elements.
Arrays can hold both primitives as well as 4. Collections can hold only objects.
objects.
5. Memory point of view arrays are not 5. Memory point of view collections are
recommended to use. recommended to use.
45. What are the interfaces and classes available in Collection framework?
== operator equals()
1. It is a operator applicable for both 1. It is a method applicable for only
primitives and object references references but not for primitives
2. == operator meant for reference 2. It is also meant for reference
comparision comparision but we can override for
content comparision. In String class
equals() is overridden for content
comparision.
14
String is immutable, where as String is mutable.
Once we create a string object we can’t perform any changes in the existing object. If we
try to perform any changes, with those changes a new object will be created. This non
changeable nature is nothing but immutability of the string object.
Once we create a StringBuffer object we can perform any type of changes in the existing
object. This changeable nature is nothing but mutability of the StringBuffer object.
Ex:
class Test
{
public static void main(String[] args)
{
String s=new String(“amar”);
s.concat(“cse”);
System.out.println(s);
StringBuffer sb=new StringBuffer(“amar”);
s.append(“cse”);
System.out.println(sb);
Output: amar
amarcse
49. What is the base class for all the classes in java?
Object class is the parent for all the classes in java
15
52. How many ways we can implement multi threading in java?
In two ways we can implement multi threading.
i. By extending Thread class
ii. By implementing Runnable interface
throw throws
1. It is used to create Exception object 1. It is used to deligate the responsibility
manually & hand over that object to the of exception handling to caller methods in
JVM explicityly case of checked exceptions
2. throw new ArithmeticExcepiton(“/by 2. public static void main(String[]
zero”); args)throws ServletException
16
60. Write program to implement serialization and deserialization.
class Test implements Serializable
{
Int i=10;
Int j=20;
}
Class SerializationDemo
{
public static void main(String[] args)
{
Test t=new Test();
FileOutputStream fos=new FileOutputStream(“abc.txt”);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(t);
FileInputStream fis=new FileInputStream(“abc.txt”);
ObjectInputStream ois=new ObjectInputStream(fis);
Test t1=(Test)ois.readObject();
System.out.println(t1.i+”----“+t1.j);
}
}
Output: 10----20
17
}
}
18