Java Core interview questions

Posted by

The repository with all questions https://github.com/anton-liauchuk/java-interview.

Table of Contents

What’s new in Java 8?

  • Lambda expressions, Method Reference , Optional, Streams added.
  • Advanced Date , Time, Zone and Calendar Classes introduced.
  • PermGen Removed.
Relative links:

What are the memory types in Java?

  • Stack. Stack memory is responsible for holding references to heap objects and for storing value types (also known in Java as primitive types), which hold the value itself rather than a reference to an object from the heap.
  • Heap. This part of memory stores the actual object in memory. Those are referenced by the variables from the stack.
Relative links:

What is java agent?

Java agents are able to “intrude” into the execution of Java applications running on the JVM at runtime by performing the direct modifications of the bytecode. Java agents are extremely as powerful as dangerous: they can do mostly everything however if something goes wrong, they can easily crash the JVM.

Relative links:

Possible Performance Tools for Java?

  • Java Profilers
  • Tracing Java Web Requests and Transactions
  • Java Application Performance Management (APM)
  • Real User Monitoring (RUM)
  • JVM Performance Metrics
  • Web Server (Apache/Nginx) Access Logs
  • Tracking All Java Exceptions
  • Memory Analysis
Relative links:

What is java profiler?

A Java Profiler is a tool that monitors Java bytecode constructs and operations at the JVM level. These code constructs and operations include object creation, iterative executions (including recursive calls), method executions, thread executions, and garbage collections.

Relative links:

What is stop the world?

Stop-the-world will occur no matter which GC algorithm you choose. Stop-the-world means that the JVM is stopping the application from running to execute a GC. When stop-the-world occurs, every thread except for the threads needed for the GC will stop their tasks. The interrupted tasks will resume only after the GC task has completed. GC tuning often means reducing this stop-the-world time.

Relative links:

What is the difference between int, Integer and AtomicInteger?

  • int is a primitive type. Variables of type int store the actual binary value for the integer you want to represent. int.parseInt(“1”) doesn’t make sense because int is not a class and therefore doesn’t have any methods.
  • Integer is a class, no different from any other in the Java language. Variables of type Integer store references to Integer objects, just as with any other reference (object) type. Integer.parseInt(“1”) is a call to the static method parseInt from class Integer (note that this method actually returns an int and not an Integer). To be more specific, Integer is a class with a single field of type int. This class is used where you need an int to be treated like any other object, such as in generic types or situations where you need nullability.
  • AtomicInteger is used in multithreaded environments when you need to make sure that only one thread can update an int variable. The advantage is that no external synchronization is requried since the operations which modify it’s value are executed in a thread-safe way.
Relative links:

How i++ will work for Integer?

variable will be changed

Relative links:

What can you say about interface constants?

The constant interface pattern is a poor use of interfaces. That a class uses some constants internally is an implementation detail. Implementing a constant interface causes this implementation detail to leak into the class’s exported API. It is of no consequence to the users of a class that the class implements a constant interface. In fact, it may even confuse them. Worse, it represents a commitment: if in a future release the class is modified so that it no longer needs to use the constants, it still must implement the interface to ensure binary compatibility. If a nonfinal class implements a constant interface, all of its subclasses will have their namespaces polluted by the constants in the interface. There are several constant interfaces in the java platform libraries, such as java.io.ObjectStreamConstants. These interfaces should be regarded as anomalies and should not be emulated.

Relative links:

What is the contract between equals and hashcode?

objects which are .equals() MUST have the same .hashCode().

Relative links:

What are the rules for overriding equals/hashcode methods?

  • Always use same attributes of an object to generate hashCode() and equals() both.
  • equals() must be consistent (if the objects are not modified, then it must keep returning the same value).
  • Whenever a.equals(b), then a.hashCode() must be same as b.hashCode().
  • If you override one, then you should override the other.
Relative links:

Are the same fields needed for equals/hashcode implementation?

The fields don’t have to be the same. The requirement is for two objects that are equal, they must have the same hash code. If they have the same hash code, they don’t have to be equal. For example, you could return 1 as your hash code always, and you would obey the hash code contract, no matter what fields you used in your equals method. Returning 1 all the time would improve the computation time of hashCode, but HashMap’s performance would drop since it would have to resort to equals() more often.

Relative links:

What are the purposes of inner classes?

  • It is a way of logically grouping classes that are only used in one place.
  • It increases encapsulation.
  • It can lead to more readable and maintainable code.
Relative links:

What is better interfaces or abstract classes?

Use abstract classes when you have a class that A is kind of B and interface when A can do B.

  • A class can implement multiple interfaces, but it can only extend one abstract class.
  • Interfaces allow the creation of proxies that encapsulate a concrete class. This is used extensively by frameworks in order to intercept method calls to the concrete class (e.g., for starting a transaction before the method is executed or to write to the log).
Relative links:

Do inner classes have access to private fields from outer class?

Inner classes has access to private fields from outer class.

Relative links:

What are the differences between static nested classes and non-static nested classes?

  • Static nested classes do not directly have access to other members(non-static variables and methods) of the enclosing class because as it is static, it must access the non-static members of its enclosing class through an object. That is, it cannot refer to non-static members of its enclosing class directly. Because of this restriction, static nested classes are seldom used.
  • Non-static nested classes (inner classes) has access to all members(static and non-static variables and methods, including private) of its outer class and may refer to them directly in the same way that other non-static members of the outer class do.
Relative links:

What are the methods of Object class?

toString(), hashCode(), equals(Object obj), getClass(), finalize(), clone(), wait(), notify() notifyAll()

Relative links:

What is Jmeter?

Apache JMeter is an open source, Java-based, load testing tool that can be used to analyze the functional behavior of a system and measure the performance of a system under a load test. A load test will simulate end-user behavior that approach the limits of an application’s specifications. Apache JMeter can be used to simulate varying or heavy loads on singular or multiple servers, networks or objects to test a system’s strength.

Relative links:

Possible Ways to Capture Java Heap Dumps?

Possible options from the link.

Relative links:

What is the result of this code:

public static void main(String[] args) {
    Point pnt1 = new Point(0, 0);
    Point pnt2 = new Point(0, 0);
    System.out.println("X: " + pnt1.x + " Y: " + pnt1.y);
    System.out.println("X: " + pnt2.x + " Y: " + pnt2.y);
    System.out.println(" ");
    tricky(pnt1, pnt2);
    System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);
    System.out.println("X: " + pnt2.x + " Y: " + pnt2.y);
}

public void tricky(Point arg1, Point arg2) {
    arg1.x = 100;
    arg1.y = 100;
    Point temp = arg1;
    arg1 = arg2;
    arg2 = temp;
}

If we execute this main() method, we see the following output:

X: 0 Y: 0
X: 0 Y: 0
X: 100 Y: 100
X: 0 Y: 0

The method successfully alters the value of pnt1, even though it is passed by value; however, a swap of pnt1 and pnt2 fails! This is the major source of confusion. In the main() method, pnt1 and pnt2 are nothing more than object references. When you pass pnt1 and pnt2 to the tricky() method, Java passes the references by value just like any other parameter. This means the references passed to the method are actually copies of the original references.

Relative links:

Are checked exceptions bad?

Relative links:

How var in Java 10 can be used?

Relative links:

What is functional interface?

Relative links:

Functional interfaces vs abstract classes?

Relative links:

Why getters/setters?

Relative links:

Serialization?

Relative links:

Deserialization after changes in class? Possible issues?

Relative links:

What is the difference between cohesion and coupling?

Relative links:

@Override annotation?

Relative links:

Java modifiers?

Relative links:

Exception Hierarchy?

Relative links:

One exception is thrown by catch block and another one is thrown from finally block, which exception will be thrown by method?

Relative links:

Is it possible to serialize lambda expression?

Relative links:

Why do Java Collections can not directly store Primitives types?

It’s a combination of two facts:

  • Java primitive types are not reference types (e.g. an int is not an Object)
  • Java does generics using type-erasure of reference types (e.g. a List<?> is really a List<Object> at run-time)
Relative links:

How is recursion implemented in Java?

Relative links:

Is it needed to document unchecked exceptions?

Relative links:

Java Date classes?

Relative links:

Why Do Local Variables Used in Lambdas Have to Be Final or Effectively Final?

Relative links:

orElse() vs orElseGet() in Optional?

Relative links:

How to filter list of objects using Stream API without .filter()?

public static void main(String[] args) {

       final List<Person> list = Arrays.asList(Person.builder().setAge(25).build(), Person.builder().setAge(10).build(),
               Person.builder().setAge(15).build(), Person.builder().setAge(21).build());

        list.stream()
                .flatMap(person -> person.getAge() < 18 ? Stream.of(person) : Stream.empty())
                .forEach(person -> System.out.println(person.getAge()));
    }

What is String Pool? How do Strings get there?

String pool is the special memory region where Strings are stored by the JVM.

Relative links:

What is the difference between StringBuilder and StringBuffer?

All public methods of StringBuffer are synchronized, it provides Thread safety but on a performance cost.

Difference between <? super T> and <? extends T> ?

Relative links:

How does JIT compiler work?

Relative links:

Mutable vs Immutable object?

Relative links:

Provide some examples when a finally block won’t be executed in Java?

If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.

Relative links:

Leave a Reply