Thursday, November 24, 2022

Java 5 Years Experience Interview Questions

Don't Miss

What Is The Importance Of Reflection In Java

Selected| Capgemini java realtime interview 5 years experience. spring boot, Microservices
  • The term reflection is used for describing the inspection capability of a code on other code either of itself or of its system and modify it during runtime.
  • Consider an example where we have an object of unknown type and we have a method fooBar which we need to call on the object. The static typing system of Java doesn’t allow this method invocation unless the type of the object is known beforehand. This can be achieved using reflection which allows the code to scan the object and identify if it has any method called fooBar and only then call the method if needed.
Method methodOfFoo = fooObject.getClass.getMethod methodOfFoo.invoke 
  • Using reflection has its own cons:
  • Speed Method invocations due to reflection are about three times slower than the direct method calls.
  • Type safety When a method is invoked via its reference wrongly using reflection, invocation fails at runtime as it is not detected at compile/load time.
  • Traceability Whenever a reflective method fails, it is very difficult to find the root cause of this failure due to a huge stack trace. One has to deep dive into the invoke and proxy method logs to identify the root cause.
  • Hence, it is advisable to follow solutions that don’t involve reflection and use this method as a last resort.
  • What Are The Different Types Of Ioc

  • Constructor-based dependency injection: Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on another class.
  • Setter-based dependency injection: Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.
  • Int A = 1l Wont Compile And Int B = 0 B += 1l Compiles Fine Why

    When += is used, thats a compound statement and the compiler internally casts it. Whereas in the first case, the compiler straightaway shouts at you since it is a direct statement.

    Compiler behavior and statement types can be confusing, so questions like this will test a candidates grasp of these concepts.

    Don’t Miss: How To Land A Job Interview

    Basic Java Interview Questions

    Irrespective of your levels of experience or skills, you may encounter these basic Java interview questions:

    • Which Java project did you enjoy working on the most?

    • Have you ever used Java to manage a team?

    • Which object-orientated programming concepts have you used?

    • Have you used a Request Dispatcher in a project?

    • What do you know about JRE, JVM and JDK in Java?

    • What do you know about wrapper classes in Java?

    • Can you list the differences between the Array list and vector?

    • Can you list the differences between Heap and Stack Memory?

    • Does the garbage collection process clean Heap or Stack Memory?

    • What are the advantages of Java packages?

    • How would you define a Java Class?

    • What are the main OOP concepts in Java?

    • How would you reverse a string in Java?

    • How would you compare two strings in Java?

    • What do you know about API in Java?

    What Is A Comparator In Java

    Top 20 Java interview questions for 5 to 6 years experience

    Consider the example where we have an ArrayList of employees like, etc. Now if we want to sort this list of employees based on the names of employees. Then that is not possible to sort using the Collections.sort method. We need to provide something to the sort function depending on what values we have to perform sorting. Then in that case a comparator is used.

    Comparator is the interface in java that contains the compare method. And by overloading the compare method, we can define that on what basis we need to compare the values.

    You May Like: How To Master Interview Questions

    Can You Write A Code For Representing Thread

    A thread-safe singleton class is created which helps in object initialization in the presence of multiple threads. It can be done using multiple ways:

    • Using Enums: Enums are the simplest means of creating a thread-safe singleton class in Java because the synchronization support is inherently done by Java itself. Enums are by default final and this also helps in preventing multiple initializations at the time of serialization.
    publicenumThreadSafeSingleton}// The Singleton class methods can be invoked as belowThreadSafeSingleton.SINGLETON_INSTANCE.show 
    • Using Static Field Initialization: Thread-safe singleton can also be created by creating the instance at the time of class loading. This is achieved by making use of static fields as the Classloader guarantees that the instances are initialized during class loading and the instance is not visible until that has been fully created.
    publicclassThreadSafeSingletonpublicstatic ThreadSafeSingleton getInstancepublicvoiddisplay}ThreadSafeSingleton.getInstance.display 

    But the disadvantage of this way is that the initialization cannot be done lazily and the getInstance method is called even before any client can call.

    publicclassThreadSafeSingleton//synchronized getInstance methodsynchronizedpublicstatic ThreadSafeSingleton getInstancereturnthis.instance  }}
    publicclassThreadSafeSingletonpublicstatic ThreadSafeSingleton getInstance     }   }return instance  }}

    What Is The Difference Between Jdk Jre And Jvm

    JVM has a Just in Time compiler tool that converts all the Java source code into the low-level compatible machine language. Therefore, it runs faster than the regular application.

    JRE has class libraries and other JVM supporting files. But it doesnt have any tool for java development such as compiler or debugger.

    JDK has tools that are required to write Java Programs and uses JRE to execute them. It has a compiler, Java application launcher, and an applet viewer.

    Recommended Reading: How To Write A Thank You Note For An Interview

    What Is The Difference Between Execute Executequery Executeupdate

    Statement execute is used to execute any SQL query and it returns TRUE if the result is an ResultSet such as running Select queries. The output is FALSE when there is no ResultSet object such as running Insert or Update queries. We can use getResultSet to get the ResultSet and getUpdateCount method to retrieve the update count.

    Statement executeQuery is used to execute Select queries and returns the ResultSet. ResultSet returned is never null even if there are no records matching the query. When executing select queries we should use executeQuery method so that if someone tries to execute insert/update statement it will throw java.sql.SQLException with message executeQuery method can not be used for update.

    Statement executeUpdate is used to execute Insert/Update/Delete statements or DDL statements that returns nothing. The output is int and equals to the row count for SQL Data Manipulation Language statements. For DDL statements, the output is 0.

    You should use execute method only when you are not sure about the type of statement else use executeQuery or executeUpdate method.

    Question 1: Which Two Ways You Need To Implement To Use Any Object As Key In Hashmap

    TCS | 5 years experience| real time java interview series| Interview 10

    To use any object as Key in HashMap or Hashtable, it must implement equals and hashcode method in Java.

    You can also read How HashMap works in Java for a detailed explanation of how the equals and hashcode method is used to put and get an object from HashMap.

    Question 20: How would you prevent a client from directly instantiating your concrete classes? For example, you have a Cache interface and two implementation classes MemoryCache and DiskCache. How do you ensure there is no object of these two classes created by the client using new keyword.I leave this question for you to practice and think about before I answer. I am sure you can figure out the right way to do this, as this is one of the critical decisions to keep control of classes in your hand, great from a maintenance perspective.

    Further Learning

    Read Also: How To Prepare For A Phone Job Interview

    How To Decide Young Generation And Old Generation Size For Your Application

    It depends on nature of application.If you have lots of temporary objects then there will be lot of minor gc. You can provide arguments XX:NewRatio=1 to distribute 50% to young generation and 50% to old.By default, NewRatio=2 hence young Generation is 1/3 of total heap.Similarly, If you have too many long-lived objects, then you might need to increase the size of tenure space by putting high value of NewRatio.

    Whats The Difference Between A Classnotfoundexception And Noclassdeffounderror

    A ClassNotFoundException means the class file for a requested class is not on the classpath of the application. A NoClassDefFoundErrormeans that the class file existed at runtime, but for some reason the class could not be turned into a Class definition.

    A common cause is an exception being thrown in static initialization blocks.

    Check out our entire set of software development interview questions to choose and practice those which fit your job search situation best:

    Also, learn the non-technical interview questions commonly asked in the first round by HR recruiters and the questions to ask your interviewer!

    Arc is the radically different remote job search platform for developers where companies apply to you. Well feature you to great global startups and tech companies hiring remotely so you can land a great remote job in 14 days. We make it easier than ever for software developers and engineers to find great remote jobs. .

    You May Like: How To Pass Facebook Interview

    What Are The Advantages Of Hibernate Over Jdbc

    Some of the important advantages of Hibernate framework over JDBC are:

  • Hibernate removes a lot of boiler-plate code that comes with JDBC API, the code looks cleaner and readable.
  • Hibernate supports inheritance, associations, and collections. These features are not present with JDBC API.
  • Hibernate implicitly provides transaction management, in fact, most of the queries cant be executed outside transaction. In JDBC API, we need to write code for transaction management using commit and rollback.
  • JDBC API throws SQLException that is a checked exception, so we need to write a lot of try-catch block code. Most of the times its redundant in every JDBC call and used for transaction management. Hibernate wraps JDBC exceptions and throw JDBCException or HibernateException un-checked exception, so we dont need to write code to handle it. Hibernate built-in transaction management removes the usage of try-catch blocks.
  • Hibernate Query Language is more object-oriented and close to Java programming language. For JDBC, we need to write native SQL queries.
  • Hibernate supports caching that is better for performance, JDBC queries are not cached hence performance is low.
  • Hibernate provides option through which we can create database tables too, for JDBC tables must exist in the database.
  • Hibernate supports JPA annotations, so the code is independent of the implementation and easily replaceable with other ORM tools. JDBC code is very tightly coupled with the application.
  • What Is Method Overloading

    Unisys Round 1 Java developer interview questions Mock Interview for 5 ...

    A class having multiple methods with the same name but different parameters are called Method Overloading

    There are three ways to overload a method.

    • Parameters with different data types
    • Parameters with a different sequence of data types
    • Different number of parameters

    Read more on Method Overloading in Java

    Don’t Miss: How To Test For Attention To Detail In An Interview

    How Can You Differentiate Between Jre Jdk And Jvm

    JVMAs mentioned earlier, JVM stands for Java Virtual Machine. This abstract machine is accountable for providing the runtime environment where it is possible to execute Java bytecode. This specification specifies the functioning of JVM. Oracle plus other enterprises have provided its implementation, which is called JRE.You will come across JVMs for lots of software and hardware platforms. As a result, JVM happens to be platform-independent. This runtime instance is created while running the Java class. You will come across 3 KVM notions, namely, specification, instance, and implementation.JREJRE is the acronym for Java Runtime Environment, which is JVMs implementation. The JRE is a collection of tools used for creating Java apps. It aids in providing the runtime environment. It exists physically and consists of a collection of libraries plus other files used by JVM at runtime.JDKJDK stands for Java Development Kit, which happens to be an application development environment used for creating Java applets and apps. Existing physically, it consists of JRE + development tools. Java Development Kit is an implementation of one of the following Java platforms:

    • Enterprise Edition Java Platform
    • Micro Edition Java Platform

    Q36 What Is Java String Pool

    Java String pool refers to a collection of Strings which are stored in heap memory. In this, whenever a new object is created, String pool first checks whether the object is already present in the pool or not. If it is present, then the same reference is returned to the variable else new object will be created in the String pool and the respective reference will be returned.

    Read Also: How To Score A Job Interview

    What Is Static Import

    If we have to use any static variable or method from other class, usually we import the class and then use the method/variable with class name.

    import java.lang.Math //inside classdouble test = Math.PI * 5 

    We can do the same thing by importing the static method or variable only and then use it in the class as if it belongs to it.

    import static java.lang.Math.PI //no need to refer class nowdouble test = PI * 5 

    Use of static import can cause confusion, so its better to avoid it. Overuse of static import can make your program unreadable and unmaintainable.

    Contiguous Memory Locations Are Usually Used For Storing Actual Values In An Array But Not In Arraylist Explain

    wipro java interview questions for 5 years Experience

    In the case of ArrayList, data storing in the form of primitive data types is not possible. The data members/objects present in the ArrayList have references to the objects which are located at various sites in the memory. Thus, storing of actual objects or non-primitive data types takes place in various memory locations.

    However, the same does not apply to the arrays. Object or primitive type values can be stored in arrays in contiguous memory locations, hence every element does not require any reference to the next element.

    Read Also: How To Be Great In An Interview

    What Is The Inheritance

    Inheritance is a mechanism by which one object acquires all the properties and behavior of another object of another class. It is used for Code Reusability and Method Overriding. The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also. Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

    There are five types of inheritance in Java.

    • Single-level inheritance

    Other Intermediate Interview Questions For Java Developers

    Be sure you are well-versed and able to speak about multithreading, as its one of Javas most important features.

    Here are a few Java multithreading questions you might be asked:

    • How does multithreading work?
    • How to implement a thread in Java?
    • How to create daemon threads?
    • What is thread starvation?
    • What is the ExecutorService interface and how does it work?

    Arc is the radically different remote job search platform for developers where companies apply to you. Well feature you to great global startups and tech companies hiring remotely so you can land a great remote job in 14 days. We make it easier than ever for software developers and engineers to find great remote jobs. .

    Recommended Reading: Java Programs Asked In Interview For Automation Tester

    Explain The Spring Mvc Module

    MVC framework is provided by Spring for building web applications. Spring can easily be integrated with other MVC frameworks, but Springs MVC framework is a better choice since it uses IoC to provide for a clean separation of controller logic from business objects. With Spring MVC you can declaratively bind request parameters to your business objects.

    Why Is The Remove Method Faster In The Linked List Than In An Array

    JPMorgan java interview experience round 2

    In the linked list, we only need to adjust the references when we want to delete the element from either end or the front of the linked list. But in the array, indexes are used. So to manage proper indexing, we need to adjust the values from the array So this adjustment of value is costlier than the adjustment of references.

    Example – To Delete from the front of the linked list, internally the references adjustments happened like this.

    The only thing that will change is that the head pointer will point to the heads next node. And delete the previous node. That is the constant time operation.

    Whereas in the ArrayList, internally it should work like this-

    For deletion of the first element, all the next element has to move to one place ahead. So this copying value takes time. So that is the reason why removing in ArrayList is slower than LinkedList.

    Recommended Reading: How To Prepare For An Exit Interview

    What Is The Difference Between An Object

    There are the following basic differences between the object-oriented language and object-based language.

    • Object-oriented languages follow all the concepts of OOPs whereas, the object-based language doesn’t follow all the concepts of OOPs like inheritance and polymorphism.
    • Object-oriented languages do not have the inbuilt objects whereas Object-based languages have the inbuilt objects, for example, JavaScript has window object.
    • Examples of object-oriented programming are Java, C#, Smalltalk, etc. whereas the examples of object-based languages are JavaScript, VBScript, etc.

    Q1 What Is Polymorphism

    Polymorphism is briefly described as one interface, many implementations. Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts specifically, to allow an entity such as a variable, a function, or an object to have more than one form. There are two types of polymorphism:

  • Compile time polymorphism
  • Run time polymorphism
  • Compile time polymorphism is method overloading whereas Runtime time polymorphism is done using inheritance and interface.

    You May Like: How To Prepare For An Interview At Amazon

    How To Differentiate Between Other Platforms And The Java Platform

    Our next advanced Java questions and answers for experienced Java developers will differentiate between other platforms and the Java platform.

    While other platforms might be software-based or hardware-based platforms, Java happens to be a platform that is software-based. Java will be executed on top of hardware platforms, while other platforms can have hardware components only.

    More articles

    Popular Articles