Вы находитесь на странице: 1из 12

FullStack.

Cafe - Never Fail Tech Interview


(https://www.fullstack.cafe)
42 Advanced Java Interview Questions For Senior Developers
Originally published on 42 Advanced Java Interview Questions For Senior Developers | FullStack.Cafe
(https://www.fullstack.cafe\blog\advanced-java-interview-questions)

Q1 : What is Function Overriding and Overloading in Java?

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

Q3 : What differences exist between HashMap and Hashtable?

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

Q5 : What is reflection and why is it useful?

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

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

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

Q9 : What is structure of Java Heap?

Q10 : What is the difference between throw and throws?

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

Q12 : What is a JavaBean exactly?

Q13 : Can == be used on enum?

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

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

Q16 : Why does Java have transient fields?

Q17 : What is static initializer?

Q18 : Is there anything like static class in java?

Q19 : What do the ... dots in the method parameters mean?

What do the 3 dots in the following method mean?

public void myMethod(String... strings){


// method body
}

Q20 : How can I synchornize two Java processes?

Q21 : What is the JIT?


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 Marshalling and demarshalling.

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

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?

Q27 : What is Double Brace initialization in Java?

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

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?

Q30 : Explain a use case for the Builder Design Pattern

Q31 : Is null check needed before calling instanceof?

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?

Q33 : What exactly is marker interface in Java?

Q34 : What does 'synchronized' mean?

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

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

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

Q38 : Explain what will the code return

Consider:

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

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

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

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

Q42 : Compare volatile vs static variables in Java


## 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.

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.

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.
A class may implement a number of Interfaces, but can extend only one abstract class.
In order for a class to implement an interface, it must implement all its declared methods. However,
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.
Variables declared in a Java interface is by default final. An abstract class may contain non-final
variables.
Members of a Java interface are public by default. A member of an abstract class can either be
private, protected or public.
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.

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."
An Exception "indicates conditions that a reasonable application might want to catch."

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-
basics/exceptions/java-util-concurrentmodificationexception-how-to-handle-concurrent-modification-
exception/), while fail-safe iterator never throws such an exception.

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), that
has "properties" whose getters and setters are just methods with certain names (like, say, getFoo()
is the getter for the "Foo" property), and
has a public 0-arg constructor (so it can be created at will and configured by setting its properties).

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); // runs fine
if (nothing.equals(Color.BLACK)); // throws NullPointerEx

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.

StringBuilder is faster than StringBuffer because it's not synchronized. Using synchronized methods
in a single thread is overkill.

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
sense
Make the constructor private - Prevents instantiation by client code as it makes no sense to
instantiate a static class
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 accessed
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 member

Q19: What do the ... dots in the method parameters mean? ★★★

Topic: Java

Details:
What do the 3 dots in the following method mean?

public void myMethod(String... strings){


// method body
}

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.
You can call winapi from Java (on windows).

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

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

Q24: Explain Marshalling and demarshalling. ★★★★

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)


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:

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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:

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Details:
Will

null instanceof SomeClass


return false or throw a NullPointerException?

Answer:

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Details:
Consider:

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

Answer:

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)


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

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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


★★★★★

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

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

Topic: Java

Read Full Answer on FullStack.Cafe (https://www.fullstack.cafe\blog\advanced-java-interview-questions)

Вам также может понравиться