Home Java Technology Java Basics Java Questions - 7

Login Form




Java Questions - 7 Print E-mail

1. What is inheritance? Explain with an example.

Inheritance in object oriented programming means that a class of objects can inherit properties and methods from another class of objects.

 

2. What is abstract class?

Abstract class is a class that needs to be extended and its methods implemented, aclass has to be declared abstract if it has one or more abstract methods.

 

3. Write the Java code to declare any constant (say gravitational constant) and to get its value?

Class ABC
{
static final float GRAVITATIONAL_CONSTANT = 9.8;
public void getConstant()
{
system.out.println("Gravitational_Constant: " + GRAVITATIONAL_CONSTANT);
}
}

 

4. What is a object?

An object is a software bundle of variables and related methods.An instance of a class depicting the state and behavior at that particular time in real world.

 

5. What is a static variable and static method? What’s the difference between two?

The modifier static can be used with a variable and method. When declared as static variable, there is only one variable no matter how instances are created, this variable is initialized when the class is loaded. Static method do not need a class to be instantiated to be called, also a non static method cannot be called from static method.

 

6. What is multi-threading?

Multi-threading as the name suggest is the scenario where more than one threads are running.

 

7. Is multiple inheritance allowed in Java?

No, multiple inheritance is not allowed in Java.

 

8. What is JVM?

The Java interpreter along with the runtime environment required to run the Java application in called as Java virtual machine(JVM)

 

9. What is the difference between final, finally and finalize?

The modifier final is used on class variable and methods to specify certain behaviour explained above. And finally is used as one of the loop in the try catch blocks, It is used to hold code that needs to be executed whether or not the exception occurs in the try catch block. Java provides a method called finalize( ) that can be defined in the class. When the garbage collector is ready to release the storage ed for your object, it will first call finalize( ), and only on the next garbage-collection pass will it reclaim the objects memory. So finalize( ), gives you the ability to perform some important cleanup at the time of garbage collection.

 

10. What are the access modifiers in Java?

There are 3 access modifiers. Public, protected and private, and the default one if no identifier is specified is called friendly, but programmer cannot specify the friendly identifier explicitly.

 

11. What are two ways of creating a thread? Which is the best way and why?

Two ways of creating threads are, one can extend from the Java.lang.Thread and can implement the rum method or the run method of a different class can be called which implements the interface Runnable, and the then implement the run() method. The latter one is mostly used as first due to Java rule of only one class inheritance, with implementing the Runnable interface that problem is sorted out.

 

12. What is interpreter and compiler?

Java interpreter converts the high level language code into a intermediate form in Java called as bytecode, and then executes it, where as a compiler converts the high level language code to machine language making it very hardware specific.

 

13. What is the difference between an array and a vector?

Number of elements in an array are fixed at the construction time, whereas the number of elements in vector can grow dynamically.

 

14. What is method overloading?

Overloading is declaring multiple method with the same name, but with different argument list.