Junior Java Dev interview questions

  1. What is Java?

“Java is a high-level, object-oriented programming language. It is designed to be platform-independent through the use of the Java Virtual Machine (JVM). Java is widely used for building enterprise-scale applications, mobile applications, and large systems.”

  1. What are the main features of Java?

  • Object-Oriented: Everything is an object in Java.

  • Platform-Independent: Java code runs on any machine by using a JVM.

  • Multithreaded: Java supports multithreaded programming.

  • Robust: Java manages memory automatically and provides error handling and type-checking.

  1. Explain the concept of Object-Oriented Programming (OOP). NEED TO REVIEW AND REWRITE IN MY OWN WORDS

  • Object-Oriented Programming (OOP) is a style of programming based on the concept of objects, which can contain data and code to manipulate that data. I.e. State in instance variables and behaviour in methods. The four main principles of OOP are:

  • Encapsulation: Wrapping data and methods that operate on data within a single unit or class.

  • Inheritance: Mechanism where one class can inherit fields and methods from another class.

  • Polymorphism: Ability to process objects differently based on their data type or class.

  • Abstraction: Hiding complex implementation details and showing only the necessary features of an object.

FOLLOW UP, WHAT IS FUNCTIONAL PROGRAMMING?

  1. What is the difference between JDK, JRE, and JVM?

  • Understanding these components is essential for any Java developer. Example Answer: “The Java Development Kit (JDK) is a complete software development kit that includes the JRE, compilers, and tools like JavaDoc and Java Debugger. E.g. when I install the Oracle Java17 JDK or Amazon Java 17 JDK

  • The Java Runtime Environment (JRE) provides libraries, Java Virtual Machine (JVM), and other components to run applications written in Java.

  • The Java Virtual Machine (JVM) is the engine that provides a runtime environment to drive the Java code or applications. It converts Java bytecode into machine language.

  1. What are the different types of memory areas allocated by JVM?

  • JVM allocates memory into several different areas:

  • Heap: Used for dynamic memory allocation for Java objects and JRE classes.

  • Stack: Stores local variables and function call information for methods.

  • Method Area: Stores class structures like metadata, constant runtime pool, and code for methods.

  • Program Counter (PC) Registers: Contain the address of the JVM instruction currently being executed.

  • Native Method Stack: Contains all native method information used in the application.

  1. Explain the difference between == and equals() method

  • The == operator checks if two references point to the same memory location, whereas the equals() method is used to check the equivalence of two objects based on their content.

  • The .equals() method can be overriden

  • Why does hashcode need to be overriden

  1. What is an Exception in Java? What are the types of exceptions?

  • I was right, but my example was poor need to memorise an example of each

  • An exception is an event that disrupts the normal flow of the program. In Java, exceptions are divided into:

  • Checked Exceptions: Checked at compile-time. Example: IOException, SQLException.

  • Checked exceptions are typically used for errors that are predictable but potentially recoverable, such as file I/O errors, database connectivity issues, or network problems

  • These exceptions will need to be handled with a try-catch block or else the application will stop

  • Unchecked Exceptions: Checked at runtime. Typically issues with logic, that the compiler wouldn’t have known/guessed Example: NullPointerException, ArrayIndexOutOfBoundsException

  • Errors: Serious issues that are not typically caught by the application. Example: OutOfMemoryError, StackOverflowError

  1. How does the final keyword work in Java?

  • The final keyword in Java can be used with variables, methods, and classes,

  • Final Variables: Values cannot be changed once initialised,

  • Final Methods: Cannot be overridden by subclasses,

  • Final Classes: cannot be subclassed

  1. What is the purpose of the static keyword?

  • The static keyword in Java indicates that a particular member belongs to the type itself, rather than to instances of the type. This means:

  • Static Variables are shared among all instances of a class

  • Static Methods can be called without creating an instance of the class.

  • Static Blocks are used to initialise static variables.

  1. Explain the concept of garbage collection in Java

  • Garbage collection in Java is the process of reclaiming the runtime unused memory automatically. The JVM tracks the objects that are no longer in use and removes them to free up memory. This process helps to prevent memory leaks and optimize the application’s performance. There are different types of garbage collectors, such as Serial GC, Parallel GC, CMS (Concurrent Mark-Sweep), and G1 (Garbage-First) GC