Java Interview Questions for Junior Developers — Part 3

Uğur Taş
Codimis
Published in
4 min readAug 3, 2023
Question mark
Photo by Emily Morter on Unsplash

Welcome back to the third installment of our series on core Java interview questions for junior developers. In this article, we will explore another set of essential topics that often feature in Java interviews. Through a series of insightful questions and explanations, we will unravel the intricacies of these topics and provide you with the tools to ace your Java interviews. So, let’s dive into the world of core Java and take your Java skills to the next level!

If you don’t have a medium membership, you can use this link to reach the article without a paywall.

What are varargs in Java? Provide an example.

  • Varargs (variable arguments) allow methods to accept a variable number of arguments of the same type.
public void printNumbers(int... numbers) {
for (int num : numbers) {
System.out.println(num+ " ");
}
}

What is the purpose of the static block in Java? When is it executed?

  • The static block is used to initialize static variables or perform one-time initialization tasks for a class.
  • It is executed when the class is loaded by the JVM, before any static methods or the creation of any objects of the class.

How does Java handle garbage collection? Explain the mark-and-sweep algorithm briefly.

  • Garbage collection in Java automatically reclaims memory from objects that are no longer referenced.
  • The mark-and-sweep algorithm involves two phases: marking and sweeping.
  • In the marking phase, the Garbage Collector traverses all live objects and marks them as reachable.
  • In the sweeping phase, it identifies and frees memory occupied by objects that are not marked as reachable.

Explain the difference between checked and unchecked exceptions in Java. Provide examples of each.

  • Checked exceptions are checked at compile-time, and the programmer is required to handle or declare them.
  • Unchecked exceptions are not checked at compile-time, and the programmer is not required to handle or declare them.
  • Example of checked exception: IOException
  • Example of unchecked exception: NullPointerException

What is the purpose of the “volatile” keyword in Java?

  • The “volatile” keyword is used to ensure that a variable’s value is always read from and written to main memory, rather than CPU cache.
  • It guarantees visibility and ordering of variable updates in multi-threaded environments.

Explain the concept of method references in Java. Provide an example.

  • Method references allow us to refer to methods or constructors without invoking them.
List<String> names = Arrays.asList("John", "Jane", "Bob");
names.forEach(System.out::println);
// Using method reference to System.out.println

What is the purpose of the super keyword in Java? Give an example.

  • The super keyword is used to refer to the superclass or parent class.
public class Animal {
public void eat() {
System.out.println("Animal is eating");
}
}

public class Dog extends Animal {
public void eat() {
super.eat(); // Calling the eat() method of the superclass
System.out.println("Dog is eating");
}
}

What is the difference between static and non-static nested classes in Java?

  • A static nested class is a static member of the outer class and can be accessed without instantiating the outer class.
  • A non-static nested class (inner class) is associated with an instance of the outer class and cannot exist without an instance of it.

What is the purpose of the getClass() method in Java? Provide an example.

  • The getClass() method is used to get the runtime class of an object.
String name = "John";
Class<? extends String> clazz = name.getClass();
System.out.println(clazz.getName());

Explain the difference between the finalize() method and the finally block in Java.

  • The finalize() method is called by the Garbage Collector before an object is garbage collected.
  • The finally block is used in exception handling to ensure that certain code is always executed, regardless of whether an exception is thrown or caught.

As we wrap up our exploration of core Java interview questions for junior developers in this article, we hope you have gained valuable insights. Now that you have a solid grasp of core Java, continue practicing coding exercises and experimenting with different scenarios to strengthen your understanding.

In the next article, we will explore more core Java interview questions. So, keep your Java passion alive and get ready for another exciting chapter in our series!

👏 Thank You for Reading!

👨‍💼 I appreciate your time and hope you found this story insightful. If you enjoyed it, don’t forget to show your appreciation by clapping 👏 for the hard work!

📰 Keep the Knowledge Flowing by Sharing the Article!

✍ Feel free to share your feedback or opinions about the story. Your input helps me improve and create more valuable content for you.

✌ Stay Connected! 🚀 For more engaging articles, make sure to follow me on social media:

🔍 Explore More! 📖 Dive into a treasure trove of knowledge at Codimis. There’s always more to learn, and we’re here to help you on your journey of discovery.

--

--

Uğur Taş
Codimis

A software developer with a high passion for learning, improving skills, and sharing knowledge. My motto is “fail million times if you take lessons every time”