java interview questions and answers PDF, java interview questions PDF eBook Free download, core java interview questions pdf, + questions, 60+ pages. ”Hi Sam I just wanted to thank you for writing the book and sharing your experience with Java interviews. It was helpful in guiding me to concentrate on the. CORE JAVA INTERVIEW. QUESTIONS AND ANSWERS. Table of Contents. 1) what are static blocks and static initalizers in Java?.

Core Java Interview Questions Ebook

Language:English, Arabic, Portuguese
Country:Sri Lanka
Genre:Science & Research
Published (Last):04.08.2015
ePub File Size:28.86 MB
PDF File Size:17.37 MB
Distribution:Free* [*Sign up for free]
Uploaded by: EVELIN

Read "CORE JAVA Interview Questions You'll Most Likely Be Asked" by Vibrant Publishers available from Rakuten Kobo. Sign up today and get $5 off your first. Our Java interview questions and answers collection is all about Besides studying them online you may download the eBook in PDF format! .. The most basic interfaces that reside in the Java Collections Framework are. Best of Java - Interview Questions Book It is available as ebook only on leanpub as well as on site. The book covers some of the basic and also advanced questions, thereby making it useful for all candidates of different.

This is called Exception propagation. Then it is moved to the main method and then it will stop the flow of execution. It is called Exception Propagation. Q 37 What is the final keyword in Java? Ans: Final variable: Once a variable is declared as final, then the value of the variable could not be changed. It is like a constant.

Top FREE Training Tutorials

No class can extend the final class. Q 38 What is a Thread? Ans: In Java, the flow of a execution is called Thread. Every java program has at least one thread called main thread, the Main thread is created by JVM. The user can define their own threads by extending Thread class or by implementing Runnable interface. Threads are executed concurrently. Ans: There are two ways available in order to make a thread.

The thread is available in java. We can overload the run method in our class. For that we should provide the implementation for run method which is defined in the interface. Ans: Join method is used to join one thread with the end of the currently running thread.

When it reaches the code t. Once it reaches the code t. It is a non-static method. Join method has overloaded version. Q 41 What does yield method of the Thread class do? So that equal priority threads have a chance to run. It is a static method. Yield method moves the thread back to the Runnable state only, and not the thread to sleep , wait or block.

Ans: wait method is used to make the thread to wait in the waiting pool. When a wait method is executed during a thread execution then immediately the thread gives up the lock on the object and goes to the waiting pool. Wait method tells the thread to wait for a given amount of time. Then the thread will wake up after notify or notify all method is called. Wait and the other above-mentioned methods do not give the lock on the object immediately until the currently executing thread completes the synchronized code.

It is mostly used in synchronization. Ans: Given below are few differences between notify method and notifyAll method notify notifyAll This method is used to send a signal to wake up a single thread in the waiting pool.

This method sends the signal to wake up all the threads in a waiting spool. Q 44 How to stop a thread in java? The code is later build on top of it. Below is an example of an Abstract class providing only the most general definitions without talking anything about how are we ever going to achieve it via code: Encapsulation — Encapsulation is simple, as simple as its name is.

Encapsulation simply says about putting all things in a capsule, exactly what we do while creating Java classes.

People also tend to confuse Encapsulation with Data Hiding. Well everything that is encapsulated may not be hidden. But yes encapsulation can be used to achieve Data Hiding also, by adding private methods or fields etc.

Encapsulation only proposes to keep similar things together in a capsule. Below is a simple example of encapsulation: IS-A is denotes an Inheritance relation between two entities.

IS-A is used to specify a more specific type of the entity. Where the parent classes Animal and SuperHero are more general, we have more specific subclasses Dog and Superman. The concept of IS-A is all about Inheritance. HAS-A Relationship: The Animal can be considered an instance variable inside the Zoo class. USES-A is a simple relationship where one entity is not related to the other entity but just wants to use it for some purpose.

The questions in this section are of intermediate level and you must be very thorough with almost all of these questions. Also not to mention there can be many more questions so emphasize on the concepts instead of trying to memorize the code. Do not be scared with the quantity of questions in the section, you might actually be knowing the answers to most of the questions in the section already, so you just have to skim through the questions in that case.

But, if the questions appear new or different please try the Code Snippet given with the explanations. Typing the code might take a few minutes but you would really be able to understand how the code behaves.

We cannot learn Coding until we Code. A Point blank answer can be -: Abstract class: An abstract class is a class which may have some unimplemented methods along with some implemented methods. An abstract class can have non-final instance variables, and can be extended by any concrete or abstract class. The extending class must implement all the un-implemented methods or has to be an abstract class itself.

Core Java - Interview Questions and Answers

Abstract class can have constructors, but they cannot be directly instantiated. An interface cannot have any implemented methods, only method signature falls in interface. Interface cannot have a private method. Any instance variable in interface has to be final.

Interfaces can neither be instantiated, nor can they have constructors. Explain Synchronization and Multithreading. Traditionally all our programs run only on a single thread main thread and the thread completes when the sequential execution of code is over.

Java permits Multi-Threading, where we can have more than one threads, each executing an independent module of its own to gain parallelization in the code, making it efficient. Then comes the problem of shared resource. When we have multiple threads in our program, and we have a particular variable or piece of code which works on a sensitive data.

Java has a synchronized keyword and a synchronized block to avoid this issue. Any method marked synchronized becomes thread safe and can be accessed only by one thread at a time; similarly any block of code inside the synchronized block becomes thread safe and only one thread can be inside that block at a time.

Threads are integral part of many applications which seek better performance and are extensively used in the IT industry. People would definitely need you to know the In-Outs of threads, if not at least the basics in place.

These are the steps you need to follow to make your class Threaded and run your thread: Making Your Class Threaded This can be done by two ways, either by extending the Thread class or by implementing the Runnable Interface.

Below is a short note on both the approaches: Extending the thread class: One way of making you class a threaded class is extending the Thread class. Extending Runnable Interface: This is another way of making your class threaded. Implementing Runnable interface on the other hand does not stop us from extending an parent class, hence gives a programming advantage over Thread Class.

This is the primary method which Java Interview Anything that comes into this method is run as a new thread by the JVM. Create a thread object and start the new thread Now since we have our thread class ready, we can create an object of the class from anywhere, within the class or outside in another class, and just have to trigger the start method to start our thread.

It is important to use the threadObj. The run method is internally called by the JVM to create a thread and execute the code you provided, directly calling the run method executes you code sequentially as any ordinary method would do.

Related question: Which is better, extending the Thread class or implementing the Runnable Interface? Here is a skeleton showing Threads in Action. Pass by value and Pass by reference is an age old programming concept, where Pass by value passes only the value of a variable in a method call, where pass by reference passes the address of the variable via pointers to the method, hence any changes to the variable in the called function actually reflects the change in the called function.

Java is Pass by Value, and does not support Pass by Reference. Related Questions: Primitive data types are passed by reference or pass by value? Everything in java is passed by Value. Objects are passed by value or by reference? Pass By Value, friends. Differentiate between HashMap and Map? HashMap itself is an unordered collection, and does not follow the order of insertion.

HashMap internally uses the hashing algorithm and buckets to put objects in memory, and gives an O 1 time complexity for insertion and retrieval of values, hence has an clear advantage over the other Data Structures. Differences between HashMap and HashTable? Preferred when we are not Thread Safe and Synchronized.

Performance trade-off due to An equivalent thread safe map can be created by synchronization overhead. No guarantee over the order of insertion in HashMap. All the data is kept in the order of insertion in HashTables.

Suggested by Java. Differentiate between Vector and ArrayList? The Vector class was deprecated by Java when they introduced the ArrayList class.

The differences between Vector and ArrayList are: Vector ArrayList Deprecated Class. New implementation provided by Java. Thread safe and Synchronized. Un-Synchronized, thread unsafe. There is a little performance trade-off with Better performance than Vectors. Differentiate between Map and Set?

Core Java Books

Set holds unique values, whereas the Map holds unique Keys and can have any amount of duplicate Values assigned to the unique keys. Maps are part of java collection framework but do not extend the Collection Interface, Set is a part of collection framework and also extends Collection Interface. Differentiate between AWT and Swing? What is the difference between a constructor and any ordinary method? Constructor is a special method that is used to initialize objects before we start using them.

A constructor does not have a return type and has the same name as the class name itself. Java provides a default constructor to every class unless the programmer has defined any constructor himself. The default constructor enables us to create objects of our class by the line of code: What is an Iterator?

Iterator is just another java object that lets you iterate over an java collection. Yes you can always use our favorite foreach loop to scan over the collection, but sometimes we may need to modify the collection while in iteration, here the iterator comes to our rescue.

We can remove the element from the iterator while being inside the iteration iterators are fail fast , the foreach loop would blow off in this situation with a ConcurrentModificationException. Again we must be clear that there are two obvious ways for accessing Java Interview Again there is a third way with static methods and static variables that can be accessed directly by the class name.

Now discussing about the access modifiers: This is the most relaxed access modifier. It is not available to any class extending it, nor can be accessed by objects of the class. Outside the package protected access specified properties are only accessible via Inheritance. This is another restrictive access modifier.

There is no way to access a default property outside the package. We insist you to type down the code below, and play with it to get your concepts in place.

Typing just two Java classes should not be a very difficult task. What is the significance of a static modifier? On a direct note: Everything is packed inside that little object in the Heap, and if we have 10 such objects created, each of these little objects would have their own properties and methods working on those properties, such that on modifying any property of the first object would have no effect on the other 9 objects.

So basically we have 10 people with their own data, and their own powers. Now say we need to have a data that needs to be shared across these 10 people, what do we do? Just for the information, the static data lands up in the Heap only — a special heap area known as perm heap or permanent generation , but not inside an object or the Heap where objects are present.

This area contains all static data of any class along with other metadata information. We would be talking a little over this in the third section of the book in the Java Memory Allocation question. Greedy for learning more? Go on, do some surfing over it, you might learn something totally awesome. Different usage of static key word: Static variable: One per class, shared variable. Static Method: Belongs to class, similar to static variable. It can access only static data and static methods directly.

Else we need to create object of the class and call the methods from the object exactly what we do in our main method most of the times. A static method cannot use32 the this and super keyword inside itself. It can also be accessed via thePage ClassName.

Java Interview Static Block: Your program can also have an interesting thing known as the static block. A static block is a piece of code which you want to execute before any part of your class is used. A static blocks is the 1st thing that would be executed by the JVM, even before the constructor.

The static block would be executed only once. Open your IDE and try this piece of code instantly and observe the order of execution: Anything that is marked final in java becomes final i. Page committed.

A variable marked final cannot be changed by re-assigning any value to it again. Any Java Interview And any class marked final cannot be extended by any other class. Can main method be declared as private? Why not, there is no compilation error for sure if my main method is private; but then I am not able to run my code. Wondering why? Who calls our program.. Etc etc.. So it requests you — please provide me a main method which I can call. I would be treating this method as the entry point to your program.

Now, How does the JVM make the method call? Simple, it just uses the ClassName. So understanding this, we can now think that if the main is not public, how would it be visible to the JVM. What if the static modifier is removed from the main method signature?

Core Java - Interview Questions and Answers

What if I do not provide the String array as the argument to the method? VM would again Fail, not being able to find the correct method. What if I write static public void main, instead of public static void main? Its just fine. No issues here. What is the first argument of the String array in main method? It just holds the parameters provided to the program. If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?

How can we check if the argument array is empty or NULL? We can check it programmatically: Why not, just while executing you must specify the entry point class name, where it should look for the main method. Generally it makes more sense to have only one main, since there should logically one entry point only in the applications.

So in case of multiple main application, while bundling the application the entry point has to be specified which the JVM would call to use the application.

Generally you would distribute your application as a JAR, and there you have to mention which class is going to be the entry point for the app. So even if you have multiple classes with multiple mains, The JVM knows which class to call. Related Question: Can I have multiple main methods in the same class?

Yes, of course, all you are trying to do is overload the main method, so its all valid as long as they stand upto method overloading rules. Do we have to import java. No, java. You can assume JVM does that for us. Yes, a package can be imported N- number of times without any issue. Instead package import is just a qualified name of the class, i.

Math class. What are Checked and Un-Checked Exception? The exceptions are broadly classified into two types: Checked Exceptions: In general these are the exceptions which the programmer has to handle in his code because they are the ones very liable to occur.

UnChecked Exceptions: This is a relaxation from Java, since these arise only because of the programming logic issue or system errors these are not mandatory to be handled or are un-recoverable and therefore unhand able. The diagram below must download you some more clarity: What are the differences between Error and Exceptions?

Errors unlike exceptions are generally unrecoverable. Exceptions are both Checked and Unchecked, whereas all the Errors are Unchecked. Name an unrecoverable Error? Does it mean that we cannot catch Errors? Yes we can catch them, but still there would be no guarantee of the code being stable, since Errors are mostly Unrecoverable.

What is Dynamic Method Dispatching? Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at Run Time, rather than at Compile Time.

Dynamic method dispatch is important because this is how Java implements Runtime Polymorphism. Method Overriding is a Runtime Polymorphism mechanism in java, where you can override the definition of any method in the parent class and provide your own new definition for the method.

Overriding is also known as Dynamic Method Invocation or Runtime Polymorphism, because the JVM itself does not know which method would be called until its executing the code at runtime.

It would give you the unresolved symbol compilation error. Can a class be declared private or protected? Only access modifiers allowed with a class are public,Page default, abstract or final. What is serialization? Certain times we might just have to save the state of an object in our program. Or sometimes we might have to send objects over the network.

Serialization has a pretty simple syntax and all you need to do is to make your class Serializable. We can make our class serializable by simply implementing the Serializable interface. Serializable is a Marker Interface i. Some imp points about serialization: The inner objects must also implement the Serializable interface for being serialized along with the main object.Published in: It would give you the unresolved symbol compilation error.

All the data is kept in the order of insertion in HashTables. Encapsulation — Encapsulation is simple, as simple as its name is. Please keep a paper and pencil handy, and have your Eclipse IDE ready. Greedy for learning more? Java is a compiled as well as an interpreted language.

Un-Synchronized, thread unsafe. Can main method be declared as private?

Oracle 11g For Dummies.

TOVA from Abilene
I do relish reading books helplessly . Review my other articles. I take pleasure in freestyle skiing.