String pool (String intern pool) is a special storage area in Java heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference.
Showing posts with label Java fundamentals. Show all posts
Showing posts with label Java fundamentals. Show all posts
Java Interview Questions and Answers Part 1
1.
What is the most important feature of Java?
Java is a platform independent language.
2. What do you mean by platform independence?
Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg (Linux,Solaris,etc).
3. What is a JVM?
JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
4. Are JVM's platform independent?
JVM's are not platform independent. JVM's are platform specific run time implementation provided by the vendor.
5. What is the difference between a JDK and a JVM?
JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.
6. What is a pointer and does Java support pointers?
Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesn't support the usage of pointers.
Java is a platform independent language.
2. What do you mean by platform independence?
Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg (Linux,Solaris,etc).
3. What is a JVM?
JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
4. Are JVM's platform independent?
JVM's are not platform independent. JVM's are platform specific run time implementation provided by the vendor.
5. What is the difference between a JDK and a JVM?
JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.
6. What is a pointer and does Java support pointers?
Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesn't support the usage of pointers.
7.
What is the base class of all classes?
java.lang.Object
8. Does Java support multiple inheritance?
Java doesn't support multiple inheritance.
9. Is Java a pure object oriented language?
Java uses primitive data types and hence is not a pure object oriented language.
10. Are arrays primitive data types?
In Java, Arrays are objects.
11. What is difference between Path and Classpath?
Path and Classpath are operating system level environment variales. Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.
12. What are local variables?
Local varaiables are those which are declared within a block of code like methods. Local variables should be initialised before accessing them.
java.lang.Object
8. Does Java support multiple inheritance?
Java doesn't support multiple inheritance.
9. Is Java a pure object oriented language?
Java uses primitive data types and hence is not a pure object oriented language.
10. Are arrays primitive data types?
In Java, Arrays are objects.
11. What is difference between Path and Classpath?
Path and Classpath are operating system level environment variales. Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.
12. What are local variables?
Local varaiables are those which are declared within a block of code like methods. Local variables should be initialised before accessing them.
Why Should we use collection frame even though we have an array to store group of objects?
It is possible to store a group of onjects into an array
Let us take an example where we want to store 100 object
of employee class into an array.
Why should we use Interface in java?
By default all variables are static final variables
By default all methods are abstract methods
Interface having only abstract methods
The class which is going to inherit from the interface that class should provide implementation to all abstract methods.
Why use Interface?
There are mainly three reasons to use interface. They are given below.
It is used to achieve fully abstraction.
By interface, we can support the functionality of multiple inheritance.
It can be used to achieve loose coupling.
By default all methods are abstract methods
Interface having only abstract methods
The class which is going to inherit from the interface that class should provide implementation to all abstract methods.
Why use Interface?
There are mainly three reasons to use interface. They are given below.
It is used to achieve fully abstraction.
By interface, we can support the functionality of multiple inheritance.
It can be used to achieve loose coupling.
What is an abstract class in java, and when we should it be used?
Class(don't turn without reading important note given below)
Abstract classes are classes that contain one or more abstract methods.
·
Abstract classes may not be instantiated,
and require subclasses to provide implementations for the abstract methods.
Method
An abstract method is a method that is declared, but contains no implementation means the child which is going to inherit from the parent should provide implementation.
Let's look at an example of an
abstract class, and an abstract method.
For example[optional means for understand sake]
Suppose we were modeling the
behavior of animals, by creating a class hierachy that started with a base
class called Animal. Animals are capable of doing different things like flying,
digging and walking, but there are some common operations as well like eating
and sleeping. Some common operations are performed by all animals, but in a
different way as well. When an operation is performed in a different way, it is
a good candidate for an abstract method (forcing subclasses to provide a custom
implementation). Let's look at a very primitive Animal base class, which
defines an abstract method for making a sound (such as a dog barking, a cow
mooing, or a pig oinking).
public abstract Animal
{
public void eat(Food food)
{
// do something with
food....
}
public void sleep(int
hours)
{
try
{
// 1000
milliseconds * 60 seconds * 60 minutes * hours
Thread.sleep (
1000 * 60 * 60 * hours);
}
catch
(InterruptedException ie) { /* ignore */ }
}
public abstract void
makeNoise();
}
Note that the abstract keyword is
used to denote both an abstract method, and an abstract class. Now, any animal
that wants to be instantiated (like a dog or cow) must implement the makeNoise
method - otherwise it is impossible to create an instance of that class. Let's
look at a Dog and Cow subclass that extends the Animal class.
public Dog extends Animal
{
public void makeNoise() {
System.out.println ("Bark! Bark!"); }
}
public Cow extends Animal
{
public void makeNoise() {
System.out.println ("Moo! Moo!"); }
}
Very important note:
Now you may be wondering why not
declare an abstract class as an interface, and have the Dog and Cow implement
the interface. Sure you could - but you'd also need to implement the eat and
sleep methods. By using abstract classes, you can inherit the implementation of
other (non-abstract) methods. You can't do that with interfaces - an interface
cannot provide any method implementations.
Difference between final, finally and finlize() method
Final and finally is keywords where as finalize() is a method
Finalize():
Finalize() is a method, used to call garbage collector to reclaim the memory.
Finally:
Irrespective of the exception raised, finally block will be executed.
Final:
In java, final is a keyword used to define constants.
Final keyword can be applied in 3 levels mainly.
1 Class level
2 Method level
3 Variable level
Class level:
Final class:
The class declared as final can't be subclass or extend the class
The final class declared as follows :
public final class MyFinalClass
Method level:
final method:
when we declare a method as final, Then final method can't be override in a subclass.
The final method can be declare as follows:
public final String convertCurrency()
Variable level
Final variables:
When we declare a variable as final, it behaves like a constant.
Means once it is declared, it can't be changed.
Attempt to change it's value lead to exception or compile time error.
You can declare the final fields as :
public final double radius = 126.45;
Why String is an immuatable?
String pool (String intern pool) is a special storage area in Java heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference.
For example:-
We have an employee named as Pawan, company HR department decided to give the bonus for incredible performance in the project development while firm team in crucial period.
So, for that I need to add bonus to his salary for encouraging him, now if String is not immutable then that bonus will be added to the so many employees of whose named as Pawan, because lot of people exist in a firm/company/organization with same name.
In order to avoid the above problem String is set was an immutable, if it is not immutable the company should lose huge money in term of crores.
String handling
- String is an immutable that means we can define string but we can not change.
String s2="java smart answers";
- If two or more string object contents are equal then there will be only one object. From the above, both s1 and s2 referring only one object
- In case of "==" , we will check the reference or hash code of the object.
- In case of equals() method, it will check the content of object, it will returns a boolean value i.e true or false.
Constructor in java
- Constructor is a method .
- Constructors are used to initialize the object.
- Constructors are called when object is created.
- Constructor name and class name should be the same.
- Constructors are return a value of its same type.
- Constructor should not have any return type.
What is initialization in java?
The process of "initializing the object i.e. values will be assigned to the variables under that particular object."
What is an instantiation in java?
Instantiation is the process of "allocating memory for an object i.e. memory will be allocated for variables under that particular object."
What is Type conversion?
- Type conversion is the process of primitive types into their respective data type value
- In this we use wrapper class
What is inheritance?
- The process of “obtaining one object properties into another object” is called inheritance.
- It supports reusability.
- We can create objects only for child classes.
Or
Inheritance is the process of “acquiring the properties of super/base/parent class into sub/derived/child class”
Example:
class parent
{
int x;
public
void readx(int a)
{
X=a;
}
}
class child
extends parent
{
int y;
public void ready(int b)
{
Y=b;
}
public
int getSum()
{
Return x+y;
}
public static void main(Sting a[])
{
child c=new child();
c.readx(7);
c.ready(9);
int sum=c.getSum();
System.out.println(“sum
is”+sum);
}
}
What is polymorphism?
Polymorphism is the process of “an object can exhibit different type of behavior at different instances.”
This polymorphism has 2 types.
This polymorphism has 2 types.
1. Function overloading :
The process of defining multiple methods “with
same name and different signatures(parameter type/list)” Or
A class can have more than one function “with
same name and different signatures(parameter type/list)”
2. Function overriding:
The process “redefining super class methods into sub classes”
called function overriding
Or
A class can have more than one function “with
same name and same signatures(parameter type/list)”.
What is abstraction?
The process of hiding essential details is known as abstraction.
Abstraction has 2 types, they are
Eg: vehicle engine no, chasis no
Abstraction has 2 types, they are
1. Data abstraction:
We know that
there will be some kind of data and we can expressEg: vehicle engine no, chasis no
2. Functional abstraction:
We can feel the behavior of an action
but we cannot express what exact process going inside the method.
Eg: Applying acceleration and
breaks to the vehicle.
Subscribe to:
Comments (Atom)