CORE JAVA INTERVIEW. QUESTIONS AND ANSWERS. Table of Contents. 1) what are static blocks and static initalizers in Java?. Dear readers, these Java Interview Questions have been designed especially during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer −. We assure that you will get here the 90% frequently asked interview questions and answers. The answers to the core java interview questions are short and to.
|Language:||English, Spanish, Japanese|
|Distribution:||Free* [*Register to download]|
java interview questions and answers PDF, java interview questions PDF eBook Free download, core java interview questions pdf, + questions, 60+ pages. Basic Java Interview Questions and Answers for Freshers or Experienced Pdf. This interview section questions contains a brief introduction to the Java. Java Interview Questions And Answers Pdf Yes, We can execute any code, even before the main method. We are using a static block.
An Interface cannot have instance variables An abstract class can have any visibility: An Interface visibility must be public or none. If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method An abstract class can contain constructors An Interface cannot contain constructors Abstract classes are fast Interfaces are slow as it requires extra indirection to find corresponding method in the actual class.
Some of the important benefits of using hibernate framework are:. Hibernate eliminates all the boiler-plate code that comes with JDBC and takes care of managing resources, so we can focus on business logic. Hibernate framework provides support for XML as well as JPA annotations, that makes our code implementation independent.
However, HQL is fully object-oriented and understands concepts like inheritance, polymorphism and association. Hibernate is an open source project from Red Hat Community and used worldwide. This makes it a better choice than others because learning curve is small and there are tons of online documentations and help is easily available in forums.
Hibernate cache helps us in getting better performance. For database vendor specific feature, hibernate is suitable because we can also execute native sql queries. Overall hibernate is the best choice in current market for ORM tool, it contains all the features that you will ever need in an ORM tool.
Throws ObjectNotFoundException if object is not found.
It returns proxy object. It should be used if you are sure that instance exists. Hibernate supports inheritance, associations and collections. Hibernate built-in transaction management removes the usage of try-catch blocks.
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 provide option through which we can create database tables too, for JDBC tables must exist in the database. Hibernate supports JPA annotations, so code is independent of implementation and easily replaceable with other ORM tools. JDBC code is very tightly coupled with the application. In case you are facing any challenges with these java interview questions, please comment your problems in the section below.
Apart from this Java Interview Questions Blog, if you want to get trained from professionals on this technology, you can opt for a structured training from edureka!
Click below to know more. Method Description public void jspInit It is invoked only once, same as init method of servlet. JSP provides 9 implicit objects by default. They are as follows:. Association is a relationship where all object have their own lifecycle and there is no owner.
Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. These relationship can be one to one, One to many, many to one and many to many.
Aggregation is a specialized form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy.
What if the main method is declared as private? What happens when the static modifier is removed from the signature of the main method? Yes we can rethrow the same exception from our catch handler. If we want to rethrow checked exception from a catch block we need to declare that exception.
What are the different ways of implementing thread? Which one is more advantageous? The thread can be implemented by Using runnable interface Inheriting from the Thread class.
The use of the Runnable interface is more advantageous because when going for multiple inheritances, the only interface can help. To know more about diamond problem with example, read Multiple Inheritance in Java.
However multiple inheritance is supported in interfaces. An interface can extend multiple interfaces because they just declare the methods and implementation will be present in the implementing class. So there is no issue of diamond problem with interfaces. The include action includes the content at request time.
The include directive includes the original content of the page so page size increases at runtime. We just need to include it in the JSP page using taglib directive. Apart from this Java Interview Questions Blog, if you want to get trained from professionals on this technology.
Following are the major differences between them: ListIterator can only be used for traversing a List. ListIterator can be used for traversing in both the directions forward and backward. Read more at: ListIterator vs Iterator. JspPage interface whenever a JSP page is about to be destroyed. Servlets destroy methods can be easily overridden to perform cleanup, like when closing a database connection.
Object-relational mapping or ORM is the programming technique to map application domain model objects to the relational database tables. Hibernate is java based ORM tool that provides framework for mapping application domain objects to the relational database tables and vice versa. Hibernate framework provide option to map plain old java objects to traditional database tables with the use of JPA annotations as well as XML based configuration.
Similarly hibernate configurations are flexible and can be done from XML configuration file as well as programmatically. Both are used to pause thread that is currently running, however, sleep is meant for short pause because it does not release lock, while wait is meant for conditional wait. This is why it releases lock, which can then be developed by a different thread to alter the condition of which it is waiting. Thread-local variables are variables restricted to a thread. Java offers a ThreadLocal class to upkeep thread-local variables.
This is one of the many ways to guarantee thread-safety. However, it is important to be mindful while using a thread local variable in a controlled environment, e. Any thread local variable which is not taken away once its work is done can hypothetically cause a memory leak in Java application. Skip to content 1. What are the important features of Java 8 release? Some of the important features of Java 8 are: What is a numeric promotion?
What are the methods used to implement for the key Object in Hash Map? Therefore, the output will be 30Javatpoint.
In the second case, the string Javatpoint is concatenated with 10 to be the string Javatpoint10 which will then be concatenated with 20 to be Javatpoint In the second case, The numbers 10 and 20 will be multiplied first to be because the precedence of the multiplication is higher than addition. The result will be treated as the string and concatenated with the string Javatpointto produce the output as Javatpoint However, they have been categorized in many sections such as constructor interview questions, static interview questions, Inheritance Interview questions, Abstraction interview question, Polymorphism interview questions, etc.
It is a programming paradigm based on objects having data and methods defined in the class to which it belongs.
Object-oriented paradigm aims to incorporate the advantages of modularity and reusability. Objects are the instances of classes which interacts with one another to design applications and programs. There are the following features of the object-oriented paradigm. Follows the bottom-up approach in program design. Focus on data with methods to operate upon the object's data Includes the concept like Encapsulation and abstraction which hides the complexities from the user and show only functionality.
Implements the real-time approach like inheritance, abstraction, etc. The Object is the real-time entity having some state and behavior.
In Java, Object is an instance of the class having the instance variables as the state of the object and the methods as the behavior of the object.
The object of a class can be created by using the new keyword. 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.
Every time, an object is created using the new keyword, the default constructor of the class is called. The name of the constructor must be similar to the class name.
The constructor must not have an explicit return type. Based on the parameters passed in the constructors, there are two types of constructors in Java. Default Constructor: default constructor is the one which does not accept any value.
The default constructor is mainly used to initialize the instance variable with the default values. It can also be used for performing some useful task on object creation. A default constructor is invoked implicitly by the compiler if there is no constructor defined in the class.
Parameterized Constructor: The parameterized constructor is the one which can initialize the instance variables with the given values.
In other words, we can say that the constructors which can accept the arguments are called parameterized constructors.
This is good for Java software engineer having 2 to 3 years experience because the question is neither difficult nor tricky but requires knowledge of OOP design patterns. Why would someone use it? How is it different from an interface?
Why would you use it? An abstract class is a class which can have state, code and implementation, but an interface is a contract which is totally abstract. Since I have answered it many times, I am only giving you the gist here but you should read the article linked to answer to learn this useful concept in much more detail.
Constructor injection guaranteed that class will be initialized with all its dependency, but setter injection provides flexibility to set an optional dependency. Setter injection is also more readable if you are using an XML file to describe dependency. Rule of thumb is to use constructor injection for mandatory dependency and use setter injection for optional dependency. By using dependency injection, your classes are nothing but POJO which only knows about dependency but doesn't care how they are acquired.
In the case of factory pattern, the class also needs to know about factory to acquire dependency. Please see the answer for a more detailed discussion on this topic. The adapter pattern is used to bridge the gap between two interfaces, but Decorator pattern is used to add new functionality into the class without the modifying existing code.
Since both Adapter and Proxy pattern encapsulate the class which actually does the job, hence result in the same structure, but Adapter pattern is used for interface conversion while the Proxy pattern is used to add an extra level of indirection to support distribute, controlled or intelligent access. For examples, you can view a sorting algorithm as a template to sort object.
It defines steps for sorting but let you configure how to compare them using Comparable or something similar in another language. The method which outlines the algorithms is also known as template method.
This pattern uses double dispatch to add another level of indirection. It allows clients treat individual objects and container of objects uniformly.
Use Composite pattern when you want to represent part-whole hierarchies of objects. Code written using Composition is also easier to test than code involving inheritance hierarchies. Though you can overload a method in the same class, but you can only override a method in child classes.
Inheritance is necessary for overriding. Composition and Aggregation are two forms of association in object-oriented programming. The composition is stronger association than Aggregation. If an object A is composed of object B then B doesn't exist if A ceased to exists, but if object A is just an aggregation of object B then B can exists even if A ceased to exist.
I expect them to know about OOP design principles as well as patterns. Open closed design principle asserts that your code should be open for extension but closed for modification. Which means if you want to add new functionality, you can add it easily using the new code but without touching already tried and tested code.
There are several design patterns which are based upon open closed design principle e. Strategy pattern if you need a new strategy, just implement the interface and configure, no need to modify core logic.
I would love to see an example the, which should answer where you should use the Abstract factory pattern and where is the Prototype pattern is more suitable. Many Java developers with 4 to 6 years of experience know the definition but failed to give any concrete example. Since many of you might not have used this pattern, it's better to look examples from JDK. You are more likely have used them before and they are easy to remember as well. Now let's see the answer. Flyweight pattern allows you to share object to support large numbers without actually creating too many objects.
I ask this question only to junior Java developers of 1 to 2 years of experience as it's too easy for an experience Java programmers.
The answer is simple, a public top level class must have the same name as the name of the source file, there is no such requirement for nested static class. A nested class is always inside a top level class and you need to use the name of the top-level class to refer nested static class e. Entry is a nested static class, where HashMap is a top level class and Entry is nested static class. This clearly differentiates an average programmer with a good programmer.
Since one of the traits of a good developer is to know tools, regex is the best tool for searching something in the log file, preparing reports etc. Anyway, answer to this question is, a numeric String can only contain digits i. It's mandatory for a method to either handle the checked exception or declare them in their throws clause. These are the ones which are a sub class of Exception but doesn't descend from RuntimeException.
The unchecked exception is the descendant of RuntimeException and not checked by the compiler at compile time. This question is now becoming less popular and you would only find this with interviews with small companies, both investment banks and startups are moved on from this question. Throwable class, which means you can throw both Error and Exception using throw keyword e.
It's mandatory to declare any unhandled checked exception in throws clause in Java. Like the previous question, this is another frequently asked Java interview question from errors and exception topic but too easy to answer.
The interviewer has been asking this question since the day Serialization was introduced in Java, but yet only a few good candidate can answer this question with some confidence and practical knowledge. Serializable interface is used to make Java classes serializable so that they can be transferred over network or their state can be saved on disk, but it leverages default serialization built-in JVM, which is expensive, fragile and not secure.
Externalizable allows you to fully control the Serialization process, specify a custom binary format and add more security measure. It's rather simple to answer and that's why many interviewers prefers to ask this question on the telephonic round. Fork-Join pool to implement something like the Map-reduce pattern in Java. Allowing String variable and literal into switch statements.
Diamond operator for improved type inference, no need to declare generic type on the right-hand side of variable declaration anymore, results in more readable and succinct code.
Another worth noting feature introduced was improved exception handling e. Java 8 is path breaking release in Java's history, here are the top 5 features from JDK 8 release Lambda expression, which allows you pass an anonymous function as object.
Date and Time API, finally you have a solid and easy to use date and time library right into JDK Extension methods, now you can have static and default method into your interface Repeated annotation, allows you apply the same annotation multiple times on a type What is the difference between Maven and ANT in Java?
It's easy to answer questions from core Java but when you ask about setting things up, building Java artifacts, many Java software engineer struggles. Coming back to the answer of this question, Though both are build tool and used to create Java application build, Maven is much more than that. It provides standard structure for Java project based upon "convention over configuration" concept and automatically manage dependencies JAR files on which your application is dependent for Java application.
Please see the answer for more differences between Maven and ANT tool. That's all guys, lots of Java Interview questions? I am sure if you can answer this list of Java questions you can easily crack any core Java or advanced Java interview. If you think, I have missed any popular Java question here and you think it should be in this list then feel free to suggest me.
My goal is to create the best list of Java Interview Questions with latest and greatest question from recent interviews. Further Learning.