Frequently Asked Java Interview Questions: Google Edition

Raghu Chavva
5 min readJun 14, 2023

--

java programming

1.What are the different types of inner classes in Java?
The different types of inner classes in Java are: static nested classes, non-static nested classes (also known as inner classes), local classes, and anonymous classes.

2.Explain the concept of method overloading and method overriding in Java.
Method overloading is when multiple methods in the same class have the same name but different parameters. The compiler determines which method to invoke based on the arguments provided.
Method overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass.

3.How does Java handle exception handling? Explain the try-catch-finally block.
Java handles exception handling using the try-catch-finally block. The code that may throw an exception is enclosed within the try block. If an exception occurs, it is caught and handled in the catch block. The finally block is executed regardless of whether an exception occurred or not, and it is used for cleanup operations.

4.What are the access modifiers in Java? Provide examples.
The access modifiers in Java are: public, protected, private, and default.
Examples:
public: Allows unrestricted access to the member from anywhere.
protected: Allows access within the same package and subclasses.
private: Restricts access to within the same class.
default: Allows access within the same package.

5.Explain the concept of polymorphism and provide an example in Java.
Polymorphism is the ability of an object to take on different forms or have multiple types.
Example:

class Animal {
public void sound() {
System.out.println("Animal makes a sound.");
}
}
class Dog extends Animal {
public void sound() {
System.out.println("Dog barks.");
}
}
class Cat extends Animal {
public void sound() {
System.out.println("Cat meows.");
}
}
public class Main {
public static void main(String[] args) {
Animal animal1 = new Dog();
Animal animal2 = new Cat();

animal1.sound(); // Output: Dog barks.
animal2.sound(); // Output: Cat meows.
}
}

6.What is the purpose of the “static” keyword in Java? How is it used?
The “static” keyword in Java is used to declare a class-level variable or method that belongs to the class itself rather than an instance of the class. It can be accessed directly using the class name without creating an object.

7.What are the differences between StringBuffer and StringBuilder in Java?
StringBuffer is thread-safe (synchronized), whereas StringBuilder is not thread-safe.
StringBuffer is slower compared to StringBuilder due to synchronization, but it is safe to use in a multi-threaded environment.
StringBuilder is faster than StringBuffer but should be used in a single-threaded environment.

8.What is the “this” keyword in Java? How is it used?
The “this” keyword refers to the current instance of a class. It can be used to access instance variables, invoke constructors, or differentiate between instance variables and parameters with the same name.

9.What is the difference between an ArrayList and a LinkedList in Java?
ArrayList internally uses an array to store elements and provides fast random access but slower insertions and deletions.
LinkedList internally uses a doubly-linked list to store elements and provides fast insertions and deletions but slower random access.

10.How does Java support multithreading? Explain the Thread synchronization mechanisms.
Java supports multithreading through the Thread class and Runnable interface.
Thread synchronization mechanisms like synchronized blocks and methods, as well as locks such as ReentrantLock and synchronized keyword, can be used to control access to shared resources and prevent race conditions.

11.What is the purpose of the “transient” keyword in Java?
The “transient” keyword is used to indicate that a variable should not be serialized when an object is converted into a byte stream.

12.Explain the concept of autoboxing and unboxing in Java?
Autoboxing is the automatic conversion of a primitive type to its corresponding wrapper class type.
Unboxing is the automatic conversion of a wrapper class type to its corresponding primitive type.

13.What is the difference between checked and unchecked exceptions in Java?
Checked exceptions are checked at compile-time and must be handled using try-catch or declared in the method signature using the “throws” keyword.
Unchecked exceptions (RuntimeExceptions) are not checked at compile-time and do not require explicit handling or declaration.

14.How can you prevent a class from being inherited in Java?
To prevent a class from being inherited, declare it as “final.” Final classes cannot be subclassed.

15.Explain the concept of the Java Collections Framework. What are some commonly used classes and interfaces in the framework?
The Java Collections Framework provides a set of classes and interfaces to store, manipulate, and process collections of objects.
Some commonly used classes and interfaces in the framework include ArrayList, LinkedList, HashSet, HashMap, List, Set, and Map.

16.What is the purpose of the “volatile” keyword in Java? How does it work?
The “volatile” keyword is used to indicate that a variable’s value may be modified by multiple threads. It ensures that any read or write to the variable happens directly from the main memory and not from a local cache, ensuring visibility and preventing thread interference.

17.How does Java support networking? Explain the Socket and ServerSocket classes.
Java supports networking through the Socket and ServerSocket classes.
The Socket class represents a client-side endpoint of a connection, allowing communication with a server.
The ServerSocket class represents a server-side endpoint of a connection, waiting for clients to connect.

18.What is the difference between the “finalize()” method and the “finally” block in Java?
The “finalize()” method is a protected method of the Object class that is called by the garbage collector before reclaiming an object’s memory. It is used for cleanup operations.
The “finally” block is used in exception handling and is executed regardless of whether an exception occurred or not. It is used for cleanup operations and releasing resources.

19.How does Java handle memory management? Explain the role of the garbage collector.
Java handles memory management automatically through the garbage collector.
The garbage collector identifies objects that are no longer referenced by the program and reclaims the memory occupied by those objects, freeing the programmer from manual memory deallocation.

20.What are lambda expressions in Java? How are they used in functional programming?
Lambda expressions are anonymous functions that can be treated as variables and passed as arguments to methods or stored in data structures.
They enable functional programming by providing a concise way to write code that can be treated as data, allowing functions to be passed around and used as first-class citizens.

Remember to further explore these topics and practice implementing code related to these concepts to solidify your understanding and enhance your Java skills. Good luck with your interview preparations!

--

--

Raghu Chavva

Hi there!! I am Raghu Reddy, A software engineer at Accenture.Admin of the Instagram account @Java_quizs - 50k (https://www.instagram.com/java_quizs/).