Thursday, January 26, 2023

Sr Java Developer Interview Questions

Don't Miss

What Is A Composition In Java

Interview with Senior Java Developer in 2022

Composition is a design technique to implement a has-a relationship. Inheritance is used for code reuse purposes and the same can be done using composition. Composition is a way of describing reference between two or more classes using instance variables and an instance should be created before it is used.

Question 1: What Is Immutable Object Can You Write Immutable Class

Immutable classes are Java classes whose objects cannot be modified once created. Any modification in an Immutable object results in a new purpose for example, String is immutable in Java.

Mostly Immutable classes are also final in Java to prevent subclasses from overriding methods, which can compromise Immutability.

You can achieve the same functionality by making member as non-final but private and not modifying them except in constructor.

Apart from obvious, you also need to make sure that you should not expose the internals of an Immutable object, mainly if it contains a mutable member.

Similarly, when you accept the value for the mutable member from client e.g. java.util.Date, use the clone method to keep a separate copy for yourself to prevent the risk of malicious client modifying mutable reference after setting it.

The Same precaution needs to be taken while returning value for a mutable member, return another separate copy to the client, never return original reference held by Immutable class. You can also see my post How to create an Immutable class in Java for step by step guide and code examples.

How Is A Java Program Executed By Jvm

Java class files or bytecodes that are loaded to the runtime data areas are executed by the execution engine, which is a component of the JVM. Before executing, the execution engine reads and transforms the bytecodes into …

*** See complete answer in the Java Interview Guide

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.

Recommended Reading: How To Make Money Doing Interviews

Q 6 What Are The Differences Between Heap And Stack Memory In Java

Stack memory in data structures is the amount of memory allocated to each individual programme. It is a fixed memory space. Heap memory, in contrast, is the portion that was not assigned to the Java code but will be available for use by the Java code when it is required, which is generally during the program’s runtime.

Classes Like Stringbuilder And Stringbuffer In Java

Javarevisited: 10 Hibernate Interview Questions and Answers for Java ...

Check whether a Java developer understands the StringBuilder class. This represents a mutable sequence of characters, and its an alternative to the String Class. On the other hand, the StringBuffer class creates a string that can be modified. Java developers should know the syntax of using these classes.

Read Also: Call To Schedule Interview Script

Key Interview Questions For Senior Java Developers

Hiring junior developers have a lot of benefits. However, sometimes your team needs a seasoned senior-level Java developer that puts all their knowledge on the table. If this is your case, and youre looking to scale your IT team with programmers with years of experience upon their shoulders, we got you covered. These are the critical interview questions for senior-level Java developers:

1. What are the benefits of using Spring?

Using the Spring framework has many benefits, such as:

  • Its lightweight the basic version is around 2MB.
  • Spring supports Aspect-oriented programming and enables cohesive development it separates application business logic from system services.
  • Springs web framework is a well-designed web MVC framework.
  • Spring provides a transaction management interface that can scale to both local and global transactions.
  • Exception handling Spring provides a convenient API to translate technology-specific exceptions into consistent unchecked exceptions.

2. Compare the wait and sleep methods in Java

wait pauses the thread until the specified number of milliseconds have elapsed or until it receives the desired notification from another thread these happen without keeping a hold on the monitor/lock of the shared object.

sleep blocks operations that keep a hold on the monitor/lock of the shared object for the specified number of milliseconds.

3. Read/convert an InputStream into a String in Java

4. Does Java have static class?

2) Make the constructor private.

What Is Spring Bean Lifecycle

A Spring bean needs to be instantiated when the container starts, based on Java or XML bean definition. The framework may also be required to perform some pre- and post-initialization steps to get the bean into a usable state.

After that, when the bean is no longer required, it will be removed from the IoC container. Like the initialization phase, the Spring framework may need to perform pre-and post-destruction steps to free the other system resources.

Don’t Miss: How To Answer Star Interview Questions

What Are The Key Steps To Connect To A Database Using Jdbc Api

There are two key steps to connecting to a database using Java JDBC API

1. Load JDBC Driver – Every database that can be connected using JDBC API must have a corresponding JDBC Driver class that implements java.sql.Driver interface. Before you can make a connection to the database you must load the driver. From JDBC 4.0 onwards any JDBC driver found in the classpath is automatically loaded. Prior to JDBC 4.0 you have to load the driver specifically using Class.forName.

2. Open database connection –After loading the driver, you can open a connection to the database by calling getConnection method on DriverManager class and passing the database connection URL, user id and password as arguments.

Example Java Interview Questions And Answers

Senior Software Engineer Mock Technical Interview (Coding/Algorithms in JavaScript)

Note: Important keywords are underlined in the answers. Look out for them in interviews!

Question 8: What are the differences between vectors and arrays?Requirement: Deep Knowledge of Java, Data structures

Answer: In Java, the main difference is that vectors are dynamically allocated. They contain dynamic lists of references to other objects and can hold data from many different data types. This means their size can easily change as needed. Arrays, on the other hand, are static. They group data with a fixed size and a fixed primitive type.

Question 9: Since Java has a garbage collection feature, does this mean your program can never run out of memory?Requirement: Deep Knowledge of Java

Answer:Not necessarily. Although Java does have an automatic garbage collection feature, this wont do you any good if youre creating objects faster than your garbage collector can work. So while garbage collection does mean its harder for your program to run out of memory, dont push your luck it can still happen.

Question 10: What is an abstract class? Why are they useful and when would you use one?Requirement: Object-Oriented Programming

Answer: Abstract classes contain one or more abstract methods . You cant instantiate your abstract classes, so you need subclasses if you want implementations for your abstract methods.

Question 11: What is method overriding in Java?Requirement: Object-Oriented Programming, Knowledge of Java

Answer:

@Test public void outOfBounds 

Don’t Miss: A Interview With God Movie

What Are Static Variables In Java Programming Language

Static variables are variables declared within the class body, outside of any methods or blocks, and declared with ‘static’ keyword.

Static variables have the longest scope. They are created when the class is loaded, and remain in memory as long as the class remains loaded in JVM.

Static variables are,essentially, global variables. A single copy of the static variable is created and shared among all objects at class level. All instances of the class share the same static variable.

A static variable can be accessed using the class, and without creating an object instance.

Class variables are stored on the heap.

class MyClass {

Question : When Do You Override Hashcode And Equals

Whenever necessary, especially if you want to do an equality check based upon business logic rather than object equality, e.g. two employee objects are equal if they have the same, even though they are two different objects created by a different part of the code.

Also, overriding both these methods is a must if you want to use them as key in HashMap.

Now, as part of the equals-hashcode contract in Java, when you override equals, you must override hashcode as well otherwise, your object will not break invariant of classes, e.g. Set, Map which relies on equals method for functioning correctly.

You can also check my post five tips on equals in Java to understand the subtle issue which can arise while dealing with these two methods.

Question 12: What will be the problem if you dont override hashCode method? If you dont replace the equals method, then the contract between equals and hashcode will not work, according to which two objects which are equal by equals must have the same hashcode.

In this case, another object may return different hashCode and will be stored on that location, which breaks invariant of HashMap class because they are not supposed to allow duplicate keys.

When you add the object using the put method, it iterates through all Map.Entry objects present in that bucket location, and update the value of the previous mapping if Map already contains that key. This will not work if the hashcode is not overridden.

Also Check: Scheduling Phone Interview Email Sample

What Are The Core Implementation Classes Of Queue Interface Defined In Java Collections Framework

Java collections framework provides classes LinkedList and PriorityQueue which are implementations of the Queue interface. In addition the java.util.concurrent package provides classes LinkedBlockingQueue, ArrayBlockingQueue, PriorityBlockingQueue, DelayQueue, SynchronousQueue…

*** See complete answer and code snippet in the Java Interview Guide.

Q2: What Do You Understand By Checked And Unchecked Exceptions

Pin on Interview Questions

Answer: Checked exceptions occur when your code encounters a situation that is out of the ordinary and needs to be handled, which means they inherit from the Exception class. Unchecked exceptions, on the other hand, indicate that some kind of problem has occurred but its not an error – these are subclasses of RuntimeException so there isnt any need to handle them.

Don’t Miss: Inventory Control Supervisor Interview Questions

What Are The Different Garbage Collection Algorithms Which Algorithm Is Better

Following are some of the algorithms available for garbage collection

Serial GC – Designed for single CPU machines. Stops all application processes during garbage collection. Goes through all objects, marks objects for garbage collection, removes them.

Parallel GC – Similar to Serial, except used multiple threads for garbage collection.

Concurrent Mark Sweep – Concurrent Mark Sweep does most of the garbage collection concurrently with the application processes. Hence, the amount of time that all application process are completely stopped are greatly reduced.

G1GC – …

*** See complete answer in the Java Interview Guide

Q: What Is The Difference Between A Jtable And A Tablemodel

Answer: A JTable provides access to data stored in your program, which allows users to search, sort, edit it, etc., by using their mouse or keyboard input – this means you need to provide your TableModel. A TableModel, on the other hand, is responsible for providing access and editing functionality by using its methods and listeners – it also provides information about rows in a JTable so that they appear as expected.

Don’t Miss: When Are Med School Interviews

Using That Model Return A List Of All Cars That Cost Less Than $100

This question asks candidates to use their own model as a basis to do more programming. Candidates have to quickly develop a good understanding of their own design and be comfortable with how classes work in order to return the information they want. Developers should think about what logic should be contained within the class and what logic can take place in external functions, and the tradeoffs associated with those design decisions.

If You Had The Opportunity To Go Back In Time What Skill Might You Learn First About The Program

Morgan Stanley Java Interview | Latest Java Interview Questions Answers

Interviewers may ask about a hypothetical scenario where, if you had the chance to go back to when you were beginning your programming experience, what skill you might learn first. This question can show how much you have learned since you started your career and developed your skill set. To answer this question, think about what you might have found challenging about the program and the abilities you have learned to lessen the challenges.

Example answer:’If I had the chance to teach myself something first when I was starting my education, I might take an introduction class dedicated to the basics of the program. I started self-teaching myself before attending university, and I got overwhelmed trying to learn everything at once. I think I might begin with byte codes and learn how they work together with the program’s structure. Then, I might learn how it compares to other popular coding programs.’

Related:15 Java 8 Interview Questions

Also Check: What To Wear For A Zoom Interview

Q: What Are The Differences Between An Inner Class And An Anonymous Class

Answer: An inner class is defined as a member of another class and has access to all its members – it can be instantiated independently but only if an instance of the outer class exists. A local or anonymous class, on the other hand, does not have any name associated with it so you do need an instance of the enclosing object before creating them.

Can You Think Of A Problem You Faced That You Didn’t Know How To Solve How Did You Overcome This

Yes, this is the time to highlight problem-solving abilities, but it also lets developers call on a number of other skills, like creativity, teamwork and personal accountability. Were you willing to ask for help? Maybe this is a chance to talk about the ability to build relationships and collaborate with others. Were you able to do research to seek out the answer yourself? Perhaps this is a good time to highlight curiosity and a go-getter attitude.

For a junior developer who may be making a significant jump to the next level in their career, this question allows them to spotlight their desire to grow and learn from the people and resources around them. And even for a senior developer, this can be a time to show theyre still enthusiastic about professional growth.

You May Like: How To Wow In An Interview

Q: When Should We Use The Volatile Keyword

A: The volatile keyword in Java is used to mark a Java variable as being stored in memory accessed by multiple threads. This means, that every read of a volatile variable will be read from the computer’s main memory, and not from the CPU cache and that every time volatile variable is changed the change will be written to main memory, and not just to the CPU cache.

Since Java 5 the features around volatile variables are extended comparing to previous versions where they could be only written to and read from shared memory.

Write A Program To Sort An Array Of Integers

20 Java Multi Threading Interview Questions &  Answers for Developers ...

This question tests candidates on their ability to manipulate arrays and code a basic sorting algorithm. The question also offers some flexibility to hiring managers, who can follow it up by asking about the solutions performance and scalability. Java has built-in sorting functions, so be sure to ask the hiring manager whether you can use them to answer this question. If you do use the built-in functions, they may follow up by asking about the drawbacks and limitations of that function.

You May Like: How To Stop Being Nervous For An Interview

What Are Exceptions In Java Programming Language

Java exceptions are problematic events that occur during the execution of Java programs, that disrupt the normal flow program. Examples – A java program that has to reads a file but the file does not exist in the file system during program execution, A java program that tries to get an object from an array at an index position that is greater than the array

In java programing language exceptions are objects of type Exception. The exception object wraps details of the problem such as type of problem, state of program when the problem occurred etc. When an exception happens within a method, an exception object is created send to the Java runtime system

Creating an exception object and handing it over to the runtime system is termed as throwing an exception. A method hands over an exception to the Java runtime system by using the keyword throw followed by exception object. This method must also declare that it throws an exception by using the keyword throws in the method declaration followed by the type of exception that is thrown.

What Do You Understand By Generics In Java Programming Language What Are The Benefits Of Generics

Generics in Java programming language enable Java types, i.e. classes and interfaces, to be declared as parameters to classes, interfaces and methods. Generics provide a way to reuse the same code with different inputs, i.e different types. Code that uses generics has many advantages over code that is non-generic.

  • Compile time type checks – The Java compiler applies type checking to generic code at compile time and throes errors for type safety violations.
  • Eliminates implicit casting – Java code that uses generics do not need explicit casting, where as the code that is non-generic requires explicit casting.
  • Implementation of generic algorithms – By using generics, programmers can develop generic algorithms designed to work on collections of different types that are type safe and easier to use.

Read Also: How To Prepare For Amazon Data Engineer Interview

Q 55 Briefly Explain The Term Spring Framework

Spring is essentially defined as an application framework in Java and inversion of control containers for Java. The spring framework creates enterprise applications in Java. Especially useful to keep in mind that the spring framework’s central features are essentially conducive to any Java application.

What Are Final Variables In Java Programming Language

Java interview questions and answers for experienced | Live Mock | coding interview

Final variables are variables declared with keyword ‘final’. Once a value is assigned to a final variable it cannot be changed. If final variable is a primitive variable, the primitive literal cannot be changed once it is assigned to the primitive variable…

*** See complete answer and code snippet in the Java Interview Guide.

Recommended Reading: Python For Data Engineering Interview Questions

More articles

Popular Articles