Friday, July 5, 2019

Basic Java Interview Questions and Answers

1.  How Java enabled High Performance?

Java uses Just-In-Time compiler to enable high performance.

Java source code is first compiled into byte code. These byte codes are then executed by JVM. But execution of byte code is slower than execution of machine language code, because JVM first needs to translate byte code into machine language code. Now here come Java JIT compiler that turns Java bytecode into instructions, compiling it "just in time" to run that can be sent directly to the processor.

2.  Why Java is considered dynamic?

Dynamic refer as the things which are executed when required rather than in advance. Java is considered as dynamic because of Bytecode. A source code written in one platform, the same code can be executed in any platform. Java is designed to adapt to an evolving environment. Java also loads the class files at runtime.

3.  What is JIT compiler?

In Java, a just-in-time (JIT) compiler is a program that turns Java bytecode into instructions that can be sent directly to the processor.  Just-In-Time(JIT) compiler is used to improve the performance.

4. Define class.

A class is a template that is used to create objects. A class contains fields and methods to describe the behavior of an object.

5. What is Singleton class?

Singleton class controls object creation. It allows only one instance of itself to be created. This class is helpful when a user wants to restrict instantiation of a class to only one object. For example, if you want only one connection to a database due to licensing issues, you can use Singleton class in such scenario.

6. What is a static variable?

static variable is a class level variable and common to all the instances of the class. Only a single copy of static variable is created and shared among all the instances of the class.

It is declared with the static keyword in a class, but outside a method, constructor or a block.

7. What is protected access modifier?

Variables, methods and constructors which are declared protected in a superclass are accessible within package and outside the package but through inheritance only.

8. Why is String called immutable?

The String class is considered as immutable because once it is created a String object cannot be changed. If we require to make a lot of modifications to Strings of characters then StringBuffer should be used.

9. Explain the importance of finalize() method.

- Finalize() method is fired when an object is just about to be reclaimed by the garbage collector. When an object needs to perform some action before the object is destroyed, Finalize() method is handy.

- Sometimes an object is holding some non-java resources that should be released before it is destroyed, finalization process of used.

10. Can we force the garbage collection to run?

- Yes, we can force but it is not certain that garbage collector will act upon immediately.
- We can use, System.gc() or runtime.gc()

11. What is the static method?

A static method belongs to the class rather than the object.

There is no need to create the object to call the static methods.

A static method can access and change the value of the static variable.

12. What is the purpose of 'this' keyword?

- 'this' keyword is used to refer current instance of an object.

- It is a non-static method and cannot be used inside main method in Java.

- It invokes current class constructor.

- It can be passed as an argument in the method call.

13. Significance of Super keyword.

- It is used to access the methods of a parent class.

- It is a non-static method and cannot be used inside main method in Java.

- It invokes the constructor of the parent class.

- The Outer.super can be used to get current instance of an outer class and its parent in Java.

14. How objects are stored in Java?

In java, each object gets a memory space from a heap. When an object is destroyed by a garbage collector, the space allocated to it from the heap is re-allocated to the heap and becomes available for any new objects.

15. What is Polymorphism in Java? What are the kinds of Polymorphism?

Polymorphism means the ability to take more than one form. It is the capability of an action or method to do different things based on the object acting upon.

Two types of Polymorphism:

1. Method Polymorphism through overloading 

2. Object Polymorphism by inheritance and interface

16. What is method overloading?

Method overloading allows us to create multiple methods with the same name but different signature. 

We used Method overloading when a couple of methods are needed with conceptually similar functionality with different parameters.

We can achieve method overloading in two ways:

By changing the number of arguments

By changing the return type

17. Can main() method in Java return any data?

In java, main() method is always declared with a void return type, so it can't return any data

18. Is it poosible to pass argument to a function by reference instead of pass by value in Java?

No, we can pass argument to a function only by value and not by reference in Java

19. Can a class have multiple constructors?

Yes, a class can have multiple constructors with different parameters. You can define as many constructors as you need. When a Java class contains multiple constructors, we say that the constructor is overloaded.

20. Is String a data type in java?

String is not a primitive data type in java. It's a class, a reference type. It is an object of Java.Lang.String class.

21. What is multi-threading?

Multi threading is running multiple tasks in a concurrent manner within a single program. Multi-threading is a special form of multitasking. Multi-threading allows you to write very efficient program that makes maximum use of CPU because idle time is kept to minimum.

22. Can we use Pointers in Java class?

We can't use concept of pointers in Java.

23. Can a class in Java be inherited from more than one class?

In Java, a class can be derived from only one class and not from multiple classes. Multiple inheritance is not supported by Java.

24. What is Garbage collector?

Garbage collector is the process of automatically freeing objects that are no longer referenced by the program. It reduces a programmer's work from having to track when to free allocated memory. Java provides memory management through garbage collector.

25. How are destructors defined in Java?

In Java, there are no destructors defined in the class as there is no need to do so. Java has its own garbage collection mechanism which does the job automatically by destroying the objects when no longer referenced.

26. Can we have static methods in an Interface?

Static methods can't be overridden in any class while any methods in an interface are by default abstract and are supposed to be implemented in the classes being implementing the interface. So it makes no sense to have static methods in an interface in Java.

Interface Methods are required to be implemented in the classes that implement the interface. But Static methods can't be overridden in any class. So, we can't have static methods in an Interface.

27. How does Java reduce the chances of a program going out of memory?

Java provides memory management through automatic garbage collection mechanism. This helps in reducing the chances of a program going out of memory. But it doesn't ensure that a Java program will not go out of memory. When Java objects are created at a faster pace compared to garbage collection, a program can go out of memory.

28. Can we increase the size of an array after its declaration?

No, Arrays are static, we can't change the size after its declaration. In this case, we should use vector over array.

29. What is the base class of all exception classes?

In Java, Java.lang.Throwable is the super class of all exception classes and all exception classes are derived from this base class.

30. What is the order of call of constructors in inheritance?

When we create a new object of a derived class, the constructor of the super class is invoked first and then the constructor of the derived class is invoked.


No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...