Java basics interview questions and answers


Why should I choose Java software development? What are the Pros and Cons?

Java Pros

  1. It’s free cost, download it and start creating application.
  2. Open source with quite large community base.
  3. Lots of available third party libraries, frameworks & IDE for fast development cycles(Spring, Hibernate, Struts, Eclipse, IntelliJ).
  4. Platform independent, write once run on most modern platfrom(Windows, Unix, Linux, Mac & 32/64 bit hardware.)
  5. Supports Object Oriented Programming, easy to model real life scenarios into object model.
  6. very good support for internationalization.
  7. Memory management is automatic by use of garbage collector(G1, Concurrent Mark Sweep, Parallel scavenge garbage collector etc..)
  8. In built support for multi-threading. Java can very efficiently utilize the maximum of given hardware(Thread, Fork/Join, Executores, etc..)
  9. Pure java byte code running on 32 bite JVM works perfectly fine on 64 bit platform.
  10. It’s improving year by year.

Java Cons

  1. Is not good for desktop applications because of heavy memory footprint and huge VM startup time compared to any C/C++ written applications.
  2. Normal Java is not good for real time systems because of stop the world garbage collector pause.

What is difference between an executable file and .class file?

.exe file contains machine language instructions for the microprocessor and is system dependent, .class file contains byte code instructions for the JVM and is system independent.

Why java is suitable for internet?

Java is suitable for internet because of two main reasons.

  1. It is system independent and hence its programs can run on any type of computer system on internet.
  2. It eliminates a lot of security problems for data on internet.

What is difference between a fuction and a method?

A method is a function that is written in a class. We do not have functions in Java, instead we methods. This means whenever a function is written in Java. It should be written inside the class only. But if we take C++, we can write the functions inside as well as outside the class. So in C++, they are called member functions and not methods.

Which part of JVM will allocate the memory for a java program?

Class loader sub sytem of JVM will allocate the necessary memory needed by the Java program.

Why Pointers are eliminated in java?

  1. Pointers lead to confusion for a programmer.
  2. Pointers may crash a program easily, for example, when we add two pointers, the program crashes immediately. The same thing could also happen when we forgot to free the memory allotted to a variable and re-allot it to some other variable.
  3. Pointers break security. Using pointers, harmful program like virus and other hacking programs can be developed.

Because of the above reasons, pointers have been removed from Java.

Which algorithm is used by garbage collector to remove the unused variables or objects from memory?

Garbage collector uses many algorithms but the most commonly used algorith is mark and sweep.

How can we call the garbage collector?

A) Garbage collector is automatically invoked when the program is being run. IT can be also called by calling gc() method of Runtime class or System class in Java.

 

What is JIT compiler?

JIT compiler is the part of JVM which increases the speed if execution of a Java program.

How many parts in Java?

Sun Microsystems Inc. has divided Java into 3 parts. They are 1. Java SE 2. Java EE 3. Java ME. Let us discuss then in brief here.

Java SE

It is the Java Standard Edition that contains basic core Java classes. This edition is used to develop standard applets and applications

Java EE

It is the Java Enterprise Edition and it contains classes they are beyond Java SE. In fact, we need Java SE in order to use many of the classes in Java EE. Java EE mainly concentrates on providing business solutions on a network.

Java ME

It stands for Java Micro Edition. Java ME is for developers who develop code for portable device, such as a PDA or a cellular phone. Code on these devices needs to be small in size and take less memory

What is the difference between static and instance methods?

instance method belongs to the instance of a class therefore it requires an instance before it can be invoked, whereas static method belongs to the class itself and not to any class instance so it doesn’t need an instance to be invoked.

Instance methods use dynamic (late) binding, whereas static methods use static (early) binding.

When the JVM invokes a class instance method, it selects the method to invoke based on the type of the object reference, which is always known at run-time. On the other hand, when the JVM invokes a static method, it selects the method to invoke based on the actual class of the object, which may only be known at compile time.

What is difference between abstract class and interface?

A class is called abstract when it contains at least one abstract method. It can also contain n numbers of concrete method.Interface can contain only abstract( non implemented) methods.

The abstract class can have public,private,protect or default variables and also constants. In interface the variable is by default public final. In nutshell the interface doesnt have any variables it only has constants.

A class can extend only one abstract class but a class can implement multiple interfaces.

If an interface is implemented its compulsory to implement all of its methods but if an abstract class is extended its not compulsory to implement all methods.

The problem with an interface is, if you want to add a new feature (method) in its contract, then you MUST implement those method in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass.

Explain with example to describe when to use abstract class and interface?

Consider a scenario where all Cars will have 4 tyres and other features can be different.

In this case any subclass of Car has to have 4 tyres. This is a case where abstract class will be used and a default implementaion for tyres will be provided.

public abstract class Car { 

private final static TOTAL_TYRES=4; 

public abstract String getCarName(); 

public final int getNoOfTyres(){ 

        return TOTAL_TYRES; 

     } 
}

Consider a scenario where Cars can have any number of tyres and other features can also be different. In this case interface will be created.

public interface Car { 

public abstract String getCarName(); 

public abstract int getNoOfTyres();
 
}

Which class is the superclass of all classes?

java.lang.Object is the root class for all the java classes and we don’t need to extend it.

Can we override static method?

We cannot override static methods. Static methods are belogs to class, not belongs to object. Inheritance will not be applicable for class members

what is proxy class in java?

It is a helper class to give duplicate objects.

What is the difference between super() and this()?

super() is used to call super class constructor, whereas this() used to call constructors in the same class, means to call parameterized constructors.

When to use String and StringBuffer?

We know that String is immutable object. We can not change the value of a String object once it is initiated.

If we try to change the value of the existing String object then it creates new object rather than changing the value of the existing object.

So incase, we are going to do more modifications on String, then use StringBuffer. StringBuffer updates the existing objects value, rather creating new object.

How to prevent a method from being overridden?

By specifying final keyword to the method you can avoid overriding in a subcalss.

Similarly one can use final at class level to prevent creating subclasses.

Does system.exit() in try block executes code in finally block?

It will not execute finally block. The program will be terminated after System.exit() statement.

Example

try{ 

System.out.println("I am in try block"); 

System.exit(1); 

} catch(Exception ex){ 

ex.printStackTrace(); 

} finally { 

System.out.println("I am in finally block!!!"); 

}

output 
I am in try block

 

Leave a Reply