Can you assign an int value to a variable of type Integer?

              Yes, it is possible (autoboxing in Java 5). Other choices is .valueOf(int).

Can you assign an array of int to an array of Integer

              No, you got incompatible types. You need to convert with loop or stream or smth.

What's the default visibility of methods and fields in a Java class?

              For classes inside the package. link

public, protected, private

              public – everyone can see (the class itself should be seen. Public class – seen outside package).

              Private – only inside class

              Protected – inside package and subclasses outside package

Can an enum implement an interface?

              Yes, Enum types internally extends class Enum – the base class. So they cannot extend classes but can implement interfaces.

Can you mark an interface method as abstract?

              In interface all methods are abstract by definition. So it’s allowed for backward compatibility but obsolete and not recommended.

From a constructor, it is not possible to invoke instance methods on the object being constructed.

              True, it is NOT possible. The instance is not constructed yet, so you cannot call method on instance in constructor. However, you can call static methods using class name.

A static nested class has access to all members of the enclosing class, even if they are declared private

              False. A static nested class do not have access to members of the enclosing class. Only through object reference. However Non-static nested class do have access to members of the enclosing class even private.


Checked and Unchecked Exceptions in Java

A checked exception is caught at compile time whereas a runtime or unchecked exception is, as it states, at runtime.

A checked exception must be handled either by re-throwing or with a try catch block, whereas an unchecked isn't required to be handled.


Comparator is an example for the Strategy pattern


One cannot define a finally block without a preceding catch block.


The code within a finally block will be executed, only if an exception occurs within the try block.


JDBC has been deprecated, to be replaced by JPA

              False ?

When leaving a 'synchronized' block, one has to make sure the lock is released by always using a 'try + finally' construct.

              False. Lock is released by JVM.

Deadlock in Java – use thread dump to detect.

Avoid Nested Locks: This is the most common reason for deadlocks, avoid locking another resource if you already hold one. It’s almost impossible to get deadlock situation if you are working with only one object lock.

Lock Only What is Required: You should acquire lock only on the resources you have to work on, for example in above program I am locking the complete Object resource but if we are only interested in one of it's fields, then we should lock only that specific field not complete object.

Avoid waiting indefinitely: You can get deadlock if two threads are waiting for each other to finish indefinitely using thread join.

can you extend from the String class

              No. It's final