Java Interview Questions And Answers Pdf Yes, We can execute any code, even before the main method. We are using a static block. Java Programming questions and answers with explanation for interview, download Java Programming quiz questions with answers as PDF files and eBooks. 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.
|Language:||English, Spanish, Portuguese|
|Genre:||Business & Career|
|Distribution:||Free* [*Registration Required]|
Dear readers, these Java Interview Questions have been designed the subject and later they continue based on further discussion and what you answer −. 5) Difference between method overloading and method overriding in java? . .. 9. 6) Difference . Core java Interview questions on Coding Standards. 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.
If you don't do that, you will get an incorrect result while parsing or formatting dates in Java. Though, for all practical date time purpose, I highly recommend joda-time library. 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.
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. 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.
I am now fresher So i need java interview questions and answers.
So please forward me. Thank you. Please check your emails, you must have got the link to download. Please subscribe to the email newsletter and you will receive it in the email. Link to Subscribe and get the eBook in the email has been added at the end of post. Please send me the ebook for Java interview questions pdf. Please send me java interview questions ebook thanx. Please send me the java interview questions and answers pdf…..
Hi sir, Plz send me the Ebook of java interview questions. Sir please send me ebook for java question and answer. Pls send the interview besed question in my email. I want to a java-interview-questions-and-answers pdf thanks. Hii sir, Plz send me the Ebook of java interview questions. Please send me interview questions and answers ebook. Please send me core Java interview questions to my email. Please send me ebook i have not got ebook on email. Hi sir plz send me java question answer with example in PDF.
You should get it in email as soon as you will subscribe to newsletter. Please send the book. Your initiative is very much useful to us. I appreciate you so much. Thanks a lot! Please send me copy of Java interview questions pdf. I am following this website and it is very helpful for interviews.
Hi Please send me the ebook for Java interview questions pdf Anil. Please send me, I have already subscribe the page. Thanks gandharav katyal. Automatic type conversion takes place if int is assigned to long because long is larger datatype than int. Widening Conversion comes under Automatic type conversion. We can handle exceptions in either of the two ways: Employee employee; In the above example employee object is not instantiate so it is pointed no where. We can have comments before the package statement.
When we declare variables variables are created in stack. So when the variable is out of scope those variables get garbage collected. When do we use synchronized blocks and advantages of using synchronized blocks? If very few lines of code requires synchronization then it is recommended to use synchronized blocks.
The main advantage of synchronized blocks over synchronized methods is it reduces the waiting time of threads and improves performance of the system. What is the difference between access specifiers and access modifiers in java? But there is no such divison of access specifiers and access modifiers in java. In Java we have access modifiers and non access modifiers.
Access Modifiers: We can use only two access modifiers for class public and default. A class with public modifier can be visible 1 In the same class 2 In the same package subclass 3 In the same package nonsubclass 4 In the different package subclass 5 In the different package non subclass. A class with default modifier can be accesed 1 In the same class 2 In the same package subclass 3 In the same package nonsubclass 4 In the different package subclass 5 In the different package non subclass.
Sometimes we may come across a situation where we cannot provide implementation to all the methods in a class. We want to leave the implementation to a class that extends it. In such case we declare a class as abstract. To make a class abstract we use key word abstract. Any class that contains one or more abstract methods is declared as abstract.
We get the following error. For example if we take a vehicle class we cannot provide implementation to it because there may be two wheelers , four wheelers etc. At that moment we make vehicle class abstract. All the common features of vehicles are declared as abstract methods in vehicle class. Any class which extends vehicle will provide its method implementation. The important features of abstract classes are: Though we cannot instantiate abstract classes we can create object references.
Through superclass references we can point to subclass. But when we cannot instantiate class there is no use in creating a constructor for abstract class.
Abstract method is declared with keyword abstract and semicolon in place of method body. Java Exception Handling Interview questions. NumberFormatException 3 Invalid casting of class Class cast Exception 4 Trying to create object for interface or abstract class Instantiation Exception. In java exception is an object. Exceptions are created when an abnormal situations are arised in our program. Exceptions can be created by JVM or by our application code.
All Exception classes are defined in java. In otherwords we can say Exception as run time error. Error is the subclass of Throwable class in java. When errors are caused by our program we call that as Exception, but some times exceptions are caused due to some environment issues such as running out of memory. Exceptions which cannot be recovered are called as errors in java.
Out of memory issues. It is recommended to handle exceptions with specific Exception instead of handling with Exception root class.
If a method throws an exception and it is not handled immediately, then that exception is propagated or thrown to the caller of that method. This propogation continues till it finds an appropriate exception handler ,if it finds handler it would be handled otherwise program terminates Abruptly. We can create threads in java by any of the two ways: This is first and foremost way to create threads. By implementing runnable interface and implementing run method we can create new thread.
Method signature: If multiple threads tries to access a method where method can manipulate the state of object , in such scenario we can declare a method as synchronized.