Sunteți pe pagina 1din 12

42 Advanced Java Interview Questions For Senior Developers

Q1 : What is Function Overriding and Overloading in Java?Q2 : What is the difference between an Interface and an

Q2 : What is the difference between an Interface and an Abstract class?Q1 : What is Function Overriding and Overloading in Java? Q3 : What differences exist between

Q3 : What differences exist between HashMap and Hashtable?the difference between an Interface and an Abstract class? Q4 : What is the difference between

Q4 : What is the difference between Exception and Error in java?Q3 : What differences exist between HashMap and Hashtable? Q5 : What is reflection and why

Q5 : What is reflection and why is it useful?What is the difference between Exception and Error in java? Q6 : How does Garbage Collection

Q6 : How does Garbage Collection prevent a Java application from going out of memory?Error in java? Q5 : What is reflection and why is it useful? Q7 : What

Q7 : What is difference between fail-fast and fail-safe?prevent a Java application from going out of memory? Q8 : What is the tradeoff between

Q8 : What is the tradeoff between using an unordered array versus an ordered array?Q7 : What is difference between fail-fast and fail-safe? Q9 : What is structure of Java

Q9 : What is structure of Java Heap?between using an unordered array versus an ordered array? Q10 : What is the difference between

Q10 : What is the difference between throw and throws?an ordered array? Q9 : What is structure of Java Heap? Q11 : Is Java “pass-by-reference”

Q11 : Is Java “pass-by-reference” or “pass-by-value”?Heap? Q10 : What is the difference between throw and throws? Q12 : What is a

Q12 : What is a JavaBean exactly?: Is Java “pass-by-reference” or “pass-by-value”? Q13 : Can == be used on enum? Q14 :

Q13 : Can == be used on enum?or “pass-by-value”? Q12 : What is a JavaBean exactly? Q14 : What are the differences between

Q14 : What are the differences between == and equals?: What is a JavaBean exactly? Q13 : Can == be used on enum? Q15 :

Q15 : What is the main difference between StringBuffer and StringBuilder?enum? Q14 : What are the differences between == and equals? Q16 : Why does Java

Q16 : Why does Java have transient fields?the main difference between StringBuffer and StringBuilder? Q17 : What is static initializer? Q18 : Is

Q17 : What is static initializer?StringBuilder? Q16 : Why does Java have transient fields? Q18 : Is there anything like static

Q18 : Is there anything like static class in java?have transient fields? Q17 : What is static initializer? Q19 : What do the dots in

Q19 : What do theQ18 : Is there anything like static class in java? dots in the method parameters mean?

dots in the method parameters mean?

What do the 3 dots in the following method mean?

public void myMethod(String // method body

}

strings){

Q20 : How can I synchornize two Java processes?3 dots in the following method mean? public void myMethod (String // method body } strings){

Q21 : What is the JIT?method mean? public void myMethod (String // method body } strings){ Q20 : How can I

Q22 : What is the difference between a synchronized method and a synchronized block?Q23 : What is the difference between Serial and Throughput Garbage collector? Q24 : Explain

Q23 : What is the difference between Serial and Throughput Garbage collector?between a synchronized method and a synchronized block? Q24 : Explain Marshalling and demarshalling. Q25 :

Q24 : Explain Marshalling and demarshalling.difference between Serial and Throughput Garbage collector? Q25 : Why is char[] preferred over String for

Q25 : Why is char[] preferred over String for passwords?collector? Q24 : Explain Marshalling and demarshalling. Why does String pose a threat to security when

Why does String pose a threat to security when it comes to passwords? It feels inconvenient to use char[]?

Q26 : When to use LinkedList over ArrayList in Java?it comes to passwords? It feels inconvenient to use char[]? Q27 : What is Double Brace

Q27 : What is Double Brace initialization in Java?char[]? Q26 : When to use LinkedList over ArrayList in Java? Q28 : Is it possible

Q28 : Is it possible to call one constructor from another in Java?in Java? Q27 : What is Double Brace initialization in Java? Is it possible to call

Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how?

Q29 : Does Java support default parameter values?(within the same class, not from a subclass)? If yes how? Q30 : Explain a use

Q30 : Explain a use case for the Builder Design Patternyes how? Q29 : Does Java support default parameter values? Q31 : Is null check needed

Q31 : Is null check needed before calling instanceof?Q30 : Explain a use case for the Builder Design Pattern Will null instanceof SomeClass return

Will

null instanceof SomeClass

return false or throw a NullPointerException?

Q32 : Given two double values d1, d2, what is the most reliable way to test their equality?SomeClass return false or throw a NullPointerException? Q33 : What exactly is marker interface in Java?

Q33 : What exactly is marker interface in Java?d2, what is the most reliable way to test their equality? Q34 : What does 'synchronized'

Q34 : What does 'synchronized' mean?equality? Q33 : What exactly is marker interface in Java? Q35 : Why ArrayList are preferable

Q35 : Why ArrayList are preferable in many more use-cases than LinkedList?in Java? Q34 : What does 'synchronized' mean? Q36 : What's wrong with Double Brace initialization

Q36 : What's wrong with Double Brace initialization in Java?are preferable in many more use-cases than LinkedList? Q37 : Provide some examples when a finally

Q37 : Provide some examples when a finally block won't be executed in Java?: What's wrong with Double Brace initialization in Java? Q38 : Explain what will the code

Q38 : Explain what will the code returnexamples when a finally block won't be executed in Java? Consider: try { return true; }

Consider:

try { return true; } finally { return false; }

Q39 : What is an efficient way to implement a singleton pattern in Java?Consider: try { return true; } finally { return false; } Q40 : What's the difference

Q40 : What's the difference between SoftReference and WeakReference in Java?an efficient way to implement a singleton pattern in Java? Q41 : Why isn’t String‘s .length()

Q41 : Why isn’t String‘s .length() accurate?the difference between SoftReference and WeakReference in Java? Q42 : Compare volatile vs static variables in

Q42 : Compare volatile vs static variables in Javathe difference between SoftReference and WeakReference in Java? Q41 : Why isn’t String‘s .length() accurate?

## Answers

Q1: What is Function Overriding and Overloading in Java? ★★

Topic: Java

Method overloading in Java occurs when two or more methods in the same class have the exact same name, but different parameters.Overriding and Overloading in Java? ★★ Topic: Java class Dog { public void bark (){ System.out.println(

class Dog{ public void bark(){ System.out.println("woof ");

}

//overloading method public void bark(int num){ for(int i=0; i<num; i++) System.out.println("woof ");

}

}

On the other hand, method overriding is defined as the case when a child class redefines the same method as a parent class. Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides.i++) System.out.println( "woof " ); } } class Dog { public void bark (){ System.out.println(

class Dog{ public void bark(){ System.out.println("woof ");

}

}

class Hound extends Dog{ public void sniff(){ System.out.println("sniff ");

}

public void bark(){ System.out.println("bowl");

}

}

public class OverridingTest{ public static void main(String [] args){ Dog dog = new Hound(); dog.bark();

}

}

Q2: What is the difference between an Interface and an Abstract class? ★★

Topic: Java

Java provides and supports the creation both of abstract classes and interfaces. Both implementations share some common characteristics, but they differ in the following features:

All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and non-abstract methods.characteristics, but they differ in the following features: class may implement a number of Interfaces, but

class may implement a number of Interfaces, but can extend only one abstract class.class may contain both abstract and non-abstract methods. order for a class to implement an interface,

order for a class to implement an interface, it must implement all its declared methods. However,of Interfaces, but can extend only one abstract class. A In a class may not implement

A

In

a class may not implement all declared methods of an abstract class. Though, in this case, the sub-

class must also be declared as abstract.

Abstract classes can implement interfaces without even providing the implementation of interface methods.this case, the sub- class must also be declared as abstract. Variables declared in a Java

Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.even providing the implementation of interface methods. Members of a Java interface are public by default.

Members of a Java interface are public by default. A member of an abstract class can either be private, protected or public.final. An abstract class may contain non-final variables. An interface is absolutely abstract and cannot be

An interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be instantiated, but can be invoked if it contains a main method.abstract class can either be private, protected or public. Q3: What differences exist between HashMap and

Q3: What differences exist between HashMap and Hashtable? ★★

Topic: Java

There are several differences between HashMap and Hashtable in Java:

1. Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.

2. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.

3. One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.

Q4: What is the difference between Exception and Error in java? ★★

Topic: Java

An Error "indicates serious problems that a reasonable application should not try to catch."between Exception and Error in java? ★★ Topic: Java An Exception "indicates conditions that a reasonable

An Exception "indicates conditions that a reasonable application might want to catch."that a reasonable application should not try to catch." Q5: What is reflection and why is

Q5: What is reflection and why is it useful? ★★

Topic: Java

The name reflection is used to describe code which is able to inspect other code in the same system (or itself) and to make modifications at runtime.

For example, say you have an object of an unknown type in Java, and you would like to call a 'doSomething' method on it if one exists. Java's static typing system isn't really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called 'doSomething' and then call it if you want to.

Method method = foo.getClass().getMethod("doSomething", null); method.invoke(foo, null);

Q6: How does Garbage Collection prevent a Java application from going out of memory? ★★

Topic: Java

It doesn’t! Garbage Collection simply cleans up unused memory when an object goes out of scope and is no longer needed. However an application could create a huge number of large objects that causes an OutOfMemoryError.

Q7: What is difference between fail-fast and fail-safe? ★★★

Topic: Java

The Iterator's (http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html) fail-safe property works with the clone of the underlying collection and thus, it is not affected by any modification in the collection. All the

collection classes in java.util package are fail-fast, while the collection classes in java.util.concurrent are fail- safe. Fail-fast iterators throw a ConcurrentModificationException (http://examples.javacodegeeks.com/java-

Q8: What is the tradeoff between using an unordered array versus an ordered array? ★★★

Topic: Java

The major advantage of an ordered array is that the search times have time complexity of O(log n), compared to that of an unordered array, which is O (n). The disadvantage of an ordered array is that the insertion operation has a time complexity of O(n), because the elements with higher values must be moved to make room for the new element. Instead, the insertion operation for an unordered array takes constant time of O(1).

Q9: What is structure of Java Heap? ★★★

Topic: Java

The JVM has a heap that is the runtime data area from which memory for all class instances and arrays is allocated. It is created at the JVM start-up. Heap memory for objects is reclaimed by an automatic memory management system which is known as a garbage collector. Heap memory consists of live and dead objects. Live objects are accessible by the application and will not be a subject of garbage collection. Dead objects are those which will never be accessible by the application, but have not been collected by the garbage collector yet. Such objects occupy the heap memory space until they are eventually collected by the garbage collector.

Q10: What is the difference between throw and throws? ★★★

Topic: Java

The throw keyword is used to explicitly raise a exception within the program. On the contrary, the throws clause is used to indicate those exceptions that are not handled by a method. Each method must explicitly specify which exceptions does not handle, so the callers of that method can guard against possible

exceptions. Finally, multiple exceptions are separated by a comma.

Q11: Is Java “pass-by-reference” or “pass-by-value”? ★★★

Topic: Java

Java is always pass-by-value. Unfortunately, when we pass the value of an object, we are passing the reference to it. There is no such thing as "pass-by-reference" in Java. This is confusing to beginners.

The key to understanding this is that something like

Dog myDog;

is not a Dog; it's actually a pointer to a Dog.

So when you have

Dog myDog = new Dog("Rover"); foo(myDog);

you're essentially passing the address of the created Dog object to the foo method.

Q12: What is a JavaBean exactly? ★★★

Topic: Java

Basically, a "Bean" follows the standart:

is a serializable object (that is, it implements java.io.Serializable, and does so correctly), thatJava Basically, a "Bean" follows the standart: has "properties" whose getters and setters are just

has "properties" whose getters and setters are just methods with certain names (like, say, getFoo() is the getter for the "Foo" property), andjava.io.Serializable, and does so correctly), that has a public 0-arg constructor (so it can be created

has a public 0-arg constructor (so it can be created at will and configured by setting its properties).getFoo() is the getter for the "Foo" property), and There is no syntactic difference between a

There is no syntactic difference between a JavaBean and another class - a class is a JavaBean if it follows the standards.

Q13: Can == be used on enum? ★★★

Topic: Java

Yes: enums have tight instance controls that allows you to use == to compare instances. Here's the guarantee provided by the language specification.

Q14: What are the differences between == and equals? ★★★

Topic: Java

As a reminder, it needs to be said that generally, == is NOT a viable alternative to equals. When it is, however (such as with enum), there are two important differences to consider:

1. == never throws NullPointerException

enum Color { BLACK, WHITE };

Color nothing = null; if (nothing == Color.BLACK);

if (nothing.equals(Color.BLACK)); // throws NullPointerEx

// runs fine

2. == is subject to type compatibility check at compile time

enum Color { BLACK, WHITE }; enum Chiral { LEFT, RIGHT };

if (Color.BLACK.equals(Chiral.LEFT)); // compiles fine

if (Color.BLACK == Chiral.LEFT);

// DOESN'T COMPILE!!! Incompatible types!

Q15: What is the main difference between StringBuffer and StringBuilder? ★★★

Topic: Java

StringBuffer is synchronized, StringBuilder is not. When some thing is synchronized, then multiple threads can access, and modify it with out any problem or side effect. StringBuffer is synchronized, so you can use it with multiple threads with out any problem.StringBuffer and StringBuilder? ★★★ Topic: Java StringBuilder is faster than StringBuffer because it's

StringBuilder is faster than StringBuffer because it's not synchronized. Using synchronized methods in a single thread is overkill.you can use it with multiple threads with out any problem. Q16: Why does Java have

Q16: Why does Java have transient fields? ★★★

Topic: Java

The transient keyword in Java is used to indicate that a field should not be part of the serialization.

By default, all of object's variables get converted into a persistent state. In some cases, you may want to avoid persisting some variables because you don't have the need to persist those variables. So you can declare those variables as transient. If the variable is declared as transient, then it will not be persisted.

Q17: What is static initializer? ★★★

Topic: Java

Details:

Answer:

The static initializer is a static {} block of code inside java class, and run only one time before the constructor or main method is called. If you had to perform a complicated calculation to determine the value of x — or if its value comes from a database — a static initializer could be very useful.

Consider:

class StaticInit { public static int x; static { x = 32;

}

// other class members such as constructors and // methods go here

}

Q18: Is there anything like static class in java? ★★★

Topic: Java

Java has no way of making a top-level class static but you can simulate a static class like this:

Declare your class final - Prevents extension of the class since extending a static class makes no senseclass static but you can simulate a static class like this: Make the constructor private -

Make the constructor private - Prevents instantiation by client code as it makes no sense to instantiate a static classof the class since extending a static class makes no sense Make all the members and

Make all the members and functions of the class static - Since the class cannot be instantiated no instance methods can be called or instance fields accessedcode as it makes no sense to instantiate a static class Note that the compiler will

Note that the compiler will not prevent you from declaring an instance (non-static) member. The issue will only show up if you attempt to call the instance memberinstance methods can be called or instance fields accessed Q19: What do the Topic: Java dots

Q19: What do the

Topic: Java

dots in the method parameters mean? ★★★

Details:

What do the 3 dots in the following method mean?

public void myMethod(String // method body

strings){

}

Answer:

That feature is called varargs, and it's a feature introduced in Java 5. It means that function can receive multiple String arguments:

myMethod("foo", "bar"); myMethod("foo", "bar", "baz"); myMethod(new String[]{"foo", "var", "baz"}); // you can eve

Then, you can use the String var as an array:

public void myMethod(String

strings){

for(String whatever : strings){ // do what ever you want

}

// the code above is is equivalent to for( int i = 0; i < strings.length; i++){ // classical for. In this case you use strings[i]

}

}

Q20: How can I synchornize two Java processes? ★★★

Topic: Java

It is not possible to do something like you want in Java. Different Java applications will use different JVM's fully separating themselves into different 'blackbox'es. However, you have 2 options:

Use sockets (or channels). Basically one application will open the listening socket and start waiting until it receives some signal. The other application will connect there, and send signals when it had completed something. I'd say this is a preferred way used in 99.9% of applications.different 'blackbox'es. However, you have 2 options: You can call winapi from Java (on windows). Q21:

You can call winapi from Java (on windows).say this is a preferred way used in 99.9% of applications. Q21: What is the JIT?

Q21: What is the JIT? ★★★

Topic: Java

The JIT is the JVM’s mechanism by which it can optimize code at runtime.

JIT means Just In Time. It is a central feature of any JVM. Among other optimizations, it can perform code inlining, lock coarsening or lock eliding, escape analysis etc.

The main benefit of the JIT is on the programmer’s side: code should be written so that it just works; if the code can be optimized at runtime, more often than not, the JIT will find a way.

Q22: What is the difference between a synchronized method and a synchronized block? ★★★★

Topic: Java

Q23: What is the difference between Serial and Throughput Garbage collector? ★★★★

Topic: Java

Q24: Explain Marshalling and demarshalling. ★★★★

Topic: Java

Q25: Why is char[] preferred over String for passwords? ★★★★

Topic: Java

Details:

Why does String pose a threat to security when it comes to passwords? It feels inconvenient to use char[]?

Answer:

Q26: When to use LinkedList over ArrayList in Java? ★★★★

Topic: Java

Q27: What is Double Brace initialization in Java? ★★★★

Topic: Java

Q28: Is it possible to call one constructor from another in Java? ★★★★

Topic: Java

Details:

Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how?

Answer:

Q29: Does Java support default parameter values? ★★★★

Topic: Java

Q30: Explain a use case for the Builder Design Pattern ★★★★

Topic: Java

Q31: Is null check needed before calling instanceof? ★★★★

Topic: Java

Details:

Will

return false or throw a NullPointerException?

Answer:

Q32: Given two double values d1, d2, what is the most reliable way to test their equality? ★★★★

Topic: Java

Q33: What exactly is marker interface in Java? ★★★★

Topic: Java

Q34: What does 'synchronized' mean? ★★★★★

Topic: Java

Q35: Why ArrayList are preferable in many more use-cases than LinkedList? ★★★★★

Topic: Java

Q36: What's wrong with Double Brace initialization in Java? ★★★★★

Topic: Java

Q37: Provide some examples when a finally block won't be executed in Java? ★★★★★

Topic: Java

Q38: Explain what will the code return ★★★★★

Topic: Java

Details:

Consider:

try { return true; } finally { return false; }

Answer:

Q39: What is an efficient way to implement a singleton pattern in Java? ★★★★★

Topic: Java

Q40: What's the difference between SoftReference and WeakReference in Java? ★★★★★

Topic: Java

Q41: Why isn’t String‘s .length() accurate? ★★★★★

Topic: Java

Q42: Compare volatile vs static variables in Java ★★★★★

Topic: Java