Java Interview Questions and Answers for Senior Core Developers

0
286
Java Interview Questions
Java Interview Questions

As a Java maven, it is essential to know the right buzzwords, learn the right technologies and prepare the right answers to commonly asked Java Interview Questions. Here’s a definitive list of top Java Interview Questions that will guarantee a breeze-through to the next level.

We are sharing 25 java interview questions; these questions are frequently asked by the recruiters. Java questions can be asked from any core java topic. So we try our best to provide you the java interview questions and answers for experienced which should be on your to-do list before facing java questions in a technical interview.

Click here: Java Interview Questions for Freshers

Question 1: What is an immutable object?

Can you write immutable object? Immutable classes are Java classes whose objects cannot be modified once created. Any modification in Immutable objects results in a new object. For example is String is immutable in Java. Mostly Immutable are also final in Java, in order to prevent a subclass from overriding methods in Java which can compromise Immutability. You can achieve the same functionality by making member as non-final but private and not modifying them except in constructor.

Click here to know about DataTypes

Question 2: What is the difference between creating String as new () and literal?

When we create a string with new () Operator, it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in PermGen area of heap.

String s = new String (“Test”);

It does not put the object in String pool, we need to call String.intern () method which is used to put them into String pool explicitly. It’s only when you create String object as String literal e.g. String s = “Test” Java automatically put that into String pool.

Question 3: Outline the major Java features.

Object-Oriented – java is based on object-oriented programming where the class and methods describe the state and behavior of the object.

Portable – Java program gets converted into Java Byte Codes that can be executed on any platform without any dependency.

Platform independent – java works on “write once and run anywhere” as it supports multiple platforms like Windows, Linux, Mac, Sun Solaris, etc.

Robust – Java has a strong memory management as there are no pointer allocations. It has automatic garbage collection that prohibits memory leaks.

Interpreted – java compiler converts the codes into Java Byte Codes which are then interpreted and executed by Java Interpreter.

Question 4: Differentiate between JDK, JRE and JVM.

JVM stands for Java Virtual Machine which provides a runtime environment for Java Byte Codes to be executed.

JRE (Java Runtime Environment) that includes sets of files required by JVM during runtime.

JDK (Java Development Kit) consists of JRE along with the development tools required to write and execute a program.

Question 5: Compare Overloading and Overriding.

 Overloading refers to the case of having two methods of the same name but different properties, but overriding occurs when there are two methods of same name and properties, but one is in the child class and one is in the parent class.

Question 6: Explain the creation of a thread-safe singleton in Java using double-checks locking

Singleton is created with double-checked locking as before Java 5 acts as a broker and it’s been possible to have multiple instances of Singleton when multiple threads create an instance of Singleton at the same time. Java 5 made it easy to create thread-safe Singleton using Enum. Using a volatile variable is essential for the same.

Question 7: Difference between Array list And Vector. 
Array List Vector
Array List is not synchronized. Vector is synchronized.
Array List is fast as it’s non-synchronized. Vector is slow as it is thread safe.
If an element is inserted into the Array List, it increases its Array size by 50%. Vector defaults to doubling the size of its array.
Array List does not define the increment size. Vector defines the increment size.
Array List can only use Iterator for traversing an Array List. Except for Hashtable, Vector is the only other class which uses both Enumeration and Iterator.
Question 8: Can we execute any code, even before the main method? Explain?

Yes! We can execute any code, even before the main method. We are using a static block of code in the class when creating the objects at load time of class. Any statements within this static block of code will get executed one time while loading the class, even before the creation of objects in the main method.

Question 9: Java doesn’t support multiple inheritances. Why?

Java doesn’t support multiple inheritances. Because we cannot use different methods in one class it creates an ambiguity.

class Intellipaat1;  {void test()

{

system.out.println(“test() method”);

}

}class Intellipaat2

{

void test()

{

system.out.println(“test() method”);

}

}Multiple inheritance

class C extends Intellipaat1, Intellipaat2

{

…………………………………………..

}

Intellipaat1 and Intellipaat2 test () methods are inheriting to class C
So which test () method C class will take. As Intellipaat1 & Intellipaat2 class test () methods are different, so here we would face ambiguity.

Question 10: Define JSON?

Expansion of JSON is “JavaScript Object Notation”, It is a much lighter and readable alternative to XML. It is an independent and easily parse-able in all programming languages. It is primarily used for Communicating between client-server or server-server communication. It is a much lighter and readable alternative to XML.

Question 11: What are the advantages of JSON over XML?

The advantages of JSON over XML are

  1.  JSON is Lighter and faster than XML
  2.  Better understandable.
  3.  Easy to parse and conversion to objects for information consumption.
  4.  Support multiple data types – JSON support string, number, array, Boolean but XML data are all strings.
Question 12: Can we import same package/class two times? Will the JVM load the package twice at runtime?

A package or class can be inherited multiple times in a program code. JVM and compiler will not create any issue. Moreover, JVM automatically loads the class internally once regardless of times it is called in the program.

Question 13: Define Abstract class?

A class which contains the abstract keyword in its declaration is known as abstract class. It can have abstract and non-abstract methods (method with the body).

  • This class can have public, private, protected or constants and default variables.
  • It needs to be extended and its method implemented. It cannot be instantiated.
  • If a class has at least one abstract method, then the class must be declared abstract.
Question 14: Distinguish between static loading and dynamic class loading?

Static loading – Classes are loaded statically with operator “new”.

Dynamic class loading – It is a technique for programmatically invoking the functions of a class loader at runtime. The syntax isClass.forName (Test className);

Question 15: What is the difference between the transient and volatile variable in Java?

Transient: In Java, it is used to specify the variable is not being serialized. Serialization is a process of saving an object’s state in Java. When we want to persist and the object’s state by default, all instance variables in the object are stored. In some cases, if we want to avoid persisting few variables because we don’t have the necessity to transfer across the network. So, declare those variables as transient.

If the variable is confirmed as transient, then it will not be persisted. Transient keyword is used with that instance variable which will not participate in the serialization process. We cannot use static with a transient variable as they are part of the instance variable.Volatile: Volatile keyword is used with the only variable in Java and it guarantees that the value of the volatile variable will always be read from main memory and not from Thread’s local cache, it can be static.

Read More

Question 16: Is Java Pass by Reference or Pass by Value?

The Java Spec says that everything in Java is pass-by-value. There is no such thing as “pass-by-reference” in Java. The difficult thing can be to understand that Java passes “objects as references” passed by value. This can certainly get confusing and I would recommend reading this article from an expert: http://javadude.com/articles/passbyvalue.htm Also read this interesting thread for example on StackOverflow: Java Pass By Ref or Value

Question 17: What is OutOfMemoryError in java? How to deal with java.lang.OutOfMemeryError error?

This Error is thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector. Note: Its an Error (extends java.lang.Error) not Exception. Two important types of OutOfMemoryError are often encountered

  1. java.lang.OutOfMemoryError: Java heap space

The quick solution is to add these flags to JVM command line when Java runtime is started:

-Xms1024m -Xmx1024m

  1. java.lang.OutOfMemoryError: PermGen space

The solution is to add these flags to JVM command line when Java runtime is started:

-XX:+CMSClassUnloadingEnabled-XX:+CMSPermGenSweepingEnabled

Long-Term Solution: Increasing the Start/Max Heap size or changing Garbage Collection options may not always be a long-term solution for your Out Of Memory Error problem. The best approach is to understand the memory needs of your program and ensure it uses memory wisely and does not have leaks. You can use a Java memory profiler to determine what methods in your program are allocating a large number of objects and then determine if there is a way to make sure they are no longer referenced, or to not allocate them in the first place.

Question 18: How do you handle error condition while writing a stored procedure or accessing stored procedure from java?

This is one of the tough Java interview question and its open for all; my friend didn’t know the answer so he didn’t mind telling me. my take is that stored procedure should return an error code if some operation fails but if stored procedure itself fail than catching SQLException is the only choice.

Question 19: What are the states of an object in hibernate?

The states of an object in hibernate are-

Transient: When objects are just created having no primary key are in the transient state. Here the objects are associated with any session.

Persistent: When the session of an object is just opened and its instance is just saved or retrieved, it is said to be in the persistent state.

Detached: When the session of an object is closed, it is said to be in detached state

Question 20: Name few Java 8 annotations?
  1. @Functional Interface annotation
  2. @Repeatable annotation,

@Functional Interface annotation: It was introduced in Java SE 8, indicates that the type declaration is intended to be a functional interface, as defined by the Java Language Specification.@Repeatable annotation: introduced in Java SE 8, indicates that the marked annotation can be applied many times to the same declaration or type use.

These are the most frequently asked Java Interview Questions.

Few Tips to Become an Outstanding Java Developer

  • Coding

By doing coding, you also realize your mistakes in designing, error handling, threading and then go back to those respective skills to improve.

  • Reading Technical Books

Coding is easier said than done, and there is a huge difference between good code and bad code, but how do you know? Until you have seen a good code and know why a particular code is good, you cannot understand the difference. This is where books come to help; more often than not authors are great programmers themselves. They offer their experience in the form of a book.

  • Practicing data structure, Algorithms, and Design related problems 

By learning these things, you take better advantage of what is available. Since data structure is a key piece of any program, solid knowledge of them helps during problem-solving. Similarly, knowledge of key programming principles, search and sort algorithms and other well-known algorithms develop programming skills on you.

  • Doing Code review

Like Unit testing, Code review is another thing which helps to become a good solid programmer. Code review helps both reviewer and author, reviewer improves his code sense and offers genuine advice while author learns from his mistakes.

Every programmer wants to become a better programmer, but not everyone succeeds. Apart from the natural talent of programming and problem solving, it requires a lot of hard work, constant learning, and perseverance to become a better programmer. The more you do real work, the better you will become!

Stay tuned with JNTU World & learn more about Java Programming!

Java Interview Questions and Answers for Senior Core Developers
Rate this post

LEAVE A REPLY

Please enter your comment!
Please enter your name here