Java Interview Questions And Answers Pdf Yes, We can execute any code, even before the main method. We are using a static block. In this guide we will discuss about different types of questions that can be used in a Java interview, in order for the employer to test your skills in Java and. 5) Difference between method overloading and method overriding in java? . .. 9. 6) Difference . Core java Interview questions on Coding Standards.

Java Interview Questions Answer Pdf

Language:English, Indonesian, Arabic
Published (Last):28.05.2016
ePub File Size:19.38 MB
PDF File Size:19.27 MB
Distribution:Free* [*Register to download]
Uploaded by: LEONORA

Dear readers, these Java Interview Questions have been designed the subject and later they continue based on further discussion and what you answer −. I was getting a lot of emails and comments to create java interview questions and answers PDF eBook. It was hard and time taking, but finally, it's done. 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.

While exceptions are conditions that occur because of bad input etc. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception probably by giving user a feedback for entering proper values etc.

What is the purpose of finalization? The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.

For example, closing a opened file, closing a opened database Connection.

Java Interview Questions and Answers

What is the difference between yielding and sleeping? When a task invokes its yield method, it returns to the ready state. When a task invokes its sleep method, it returns to the waiting state.

What is the difference between preemptive scheduling and time slicing? Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence.

Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors. What is mutable object and immutable object? If a object value is changeable then we can call it as Mutable object. What is the purpose of Void class?

The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the primitive Java type void. Really, just a very fast compiler… In this incarnation, pretty much a one-pass compiler — no offline computations.

If all the methods of a inner class is static then it is a nested class. What are different types of access modifiers? Any thing declared as public can be accessed from anywhere. Any thing declared as protected can be accessed by classes in the same package and subclasses in the other packages. Can be accessed only to classes in the same package. A constructor is a special method whose task is to initialize the object of its class. It is special because its name is the same as the class name.

They do not have return types, not even void and therefore they cannot return values. They cannot be inherited, though a derived class can call the base class constructor. Constructor is invoked whenever an object of its associated class is created. What is an Iterator? The Iterator interface is used to step through the elements of a Collection. Iterators let you process each element of a Collection.

Iterators are a generic way to go through all the elements of a Collection no matter how it is organized. Iterator is an Interface implemented a different way for every Collection. The List interface provides support for ordered collections of objects. Lists may contain duplicate elements. A memory leak is where an unreferenced object that will never be used again still hangs around in memory and doesnt get garbage collected.

The prefix form performs the increment operation and returns the value of the increment operation. The postfix form returns the current value all of the expression and then performs the increment operation on that value. What is the difference between a constructor and a method? A constructor is a member function of a class that is used to create objects of that class.

It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type which may be void , and is invoked using the dot operator.

What will happen to the Exception object after exception handling? Difference between static and dynamic class loading.

Static class loading: The process of loading a class using new operator is called static class loading. Dynamic class loading: The process of loading a class at runtime is called dynamic class loading. Dynamic class loading can be done by using Class. The EJBs can be used to incorporate business logic in a web-centric application.

In Enterprise Application Integration applications, EJBs can be used to house processing and mapping between different applications. It contains Presentation logic and business logic of a web application.

What is the purpose of apache tomcat?

Apache server is a standalone server that is used to test servlets and create JSP pages. It is free and open source that is integrated in the Apache web server. It is fast, reliable server to configure the applications but it is hard to install. It is a servlet container that includes tools to configure and manage the server to run the applications.

It can also be configured by editing XML configuration files.

Pragma is used inside the servlets in the header with a certain value. The value is of no-cache that tells that a servlets is acting as a proxy and it has to forward request. Pragma directives allow the compiler to use machine and operating system features while keeping the overall functionality with the Java language.

These are different for different compilers. Briefly explain daemon thread. Daemon thread is a low priority thread which runs in the background performs garbage collection operation for the java runtime system. A native method is a method that is implemented in a language other than Java.

Explain different way of using thread? A Java thread could be implemented by using Runnable interface or by extending the Thread class. The Runnable is more advantageous, when you are going for multiple inheritance.

What are the two major components of JDBC? One implementation interface for database manufacturers, the other implementation interface for application and applet writers. There are three built-in classloaders in Java. Bootstrap ClassLoader: This is the first classloader which is the superclass of Extension classloader. It loads the rt.

Extension ClassLoader: This is the child classloader of Bootstrap and parent classloader of System classloader. It loads the class files from the classpath.

Top 50+ Core Java Interview Questions and Answers

By default, the classpath is set to the current directory. You can change the classpath using "-cp" or "-classpath" switch. It is also known as Application classloader. Yes, Java allows to save our java file by. It is empty, but not null. The program compiles and runs correctly because the order of specifiers doesn't matter in Java.

Post navigation

The local variables are not initialized to any default value, neither primitives nor object references. In Java, access specifiers are the keywords which are used to define the access scope of the method, class, or a variable. In Java, there are four access specifiers given below.

Public The classes, methods, or variables which are defined as public, can be accessed by any class or method. Protected Protected can be accessed by the class of the same package, or by the sub-class of this class, or within the same class. Default Default are accessible within the package only. By default, all the classes, methods, and variables are of default scope. Private The private class, methods, or variables defined as private can be accessed within the class only.

The methods or variables defined as static are shared among all the objects of the class. The static is the part of the class and not of the object.

The static variables are stored in the class area, and we do not need to create the object to access such variables.

Therefore, static is used in the case, where we need to define variables or methods which are common to all the objects of the class. For example, In the class simulating the collection of the students in a college, the name of the college is the common attribute to all the students. Therefore, the college name will be defined as static. There are various advantages of defining packages in Java.

Packages avoid the name clashes. The Package provides easier access control. We can also have the hidden classes that are not visible outside and used by the package. It is easier to locate the related classes. Now, their sum 30 is treated as the string and concatenated with the string Javatpoint. Therefore, the output will be 30Javatpoint.

DateFormat class allows you to format date on many popular formats. Please see the answer for code samples to format date into different formats e. Date and java. Date in Java? It also contains questions from GOF design patterns.

Why you use it if you cannot write anything concrete on it? The interface is used to define API. It tells about the contract your classes will follow. It also supports abstraction because a client can use interface method to leverage multiple implementations e.

300 Core Java Interview Questions | Set 1

The interface doesn't allow you to write code to keep things abstract but from Java 8 you can declare static and default methods inside interface which are concrete. An abstract class is good to define default behavior for a family of class, but the interface is good to define Type which is later used to leverage Polymorphism. Please check the answer for a more thorough discussion of this question.

This is something you can answer from your experience. You can generally say about dependency injection, factory pattern, decorator pattern or observer pattern, whichever you have used. Though be prepared to answer follow-up question based upon the pattern you choose. Out of 50 candidates, I have almost asked only 5 have managed to answer it. I am not posting an answer to this question as I like you to do some research, practice and spend some time to understand this confusing principle well.

Why it matters? Law of Demeter suggests you "talk to friends and not stranger", hence used to reduce coupling between classes. When to use it? Another frequently asked Java design pattern questions. It provides interface conversion. If your client is using some interface but you have something else, you can write an Adapter to bridge them together. 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.

Java Interview Questions

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.It's not dificult to answer this question but many develoepr get it wrong due to confusion on which one is bigger between double and long in Java.

Check the insightful tutorial to learn more about Java Constructors. We can fetch records from the database by the query method of JdbcTemplate. A constructor is a special method used to initialize objects in java. Ans: Final variable: Once a variable is declared as final, then the value of the variable could not be changed.

Ans: Join method is used to join one thread with the end of the currently running thread. I have multiple constructors defined in a class. Hi sir plz send me java question answer with example in PDF.

Java supports four types of inheritance which are: Single Inheritance: In single inheritance, one class inherits the properties of another i. Java is the software-based platform whereas other platforms may be the hardware platforms or software-based platforms.