Most Asked JAVA Interview Questions
Q-1. What is meant by looping?
Ans: Loops are used to repeatedly execute a certain statement or block of statements. There are of three types- For Loops, While Loops and Do While Loops.
Q-2. What is the difference between Overloading and Overriding?
Ans: When you have two methods of the same name but having different properties, the case is called Overloading. On the other hand, Overriding refers to a situation where two methods with the same name and properties occur, but the two occurring in a parent and child class respectively.
Q-3. Is it possible to restrict Inheritance?
Ans: Yes, it is. You can restrict Inheritance by:
a. Using the final keyword.
b. Making the method final.
c. Using private constructor.
d. Using (//) Javadoc comment.
Q-4. What is WORA?
Ans: WORA or Write Once Read Anywhere, is the property of a language to run on any platform. Java is allowed this property due to its bytecode nature. This is midway between machine code and source code and is thus not platform specific.
Q-5. Can we have any other return type than void for main method?
Ans: No, Java class main method can have only void return type for the program to get successfully executed. Nonetheless , if you absolutely must return a value to at the completion of main method , you can use System.exit(int status).
Q-6. I want to re-reach and use an object once it has been garbage collected. How it’s possible?
Ans: Once an object has been destroyed by garbage collector, it no longer exists on the heap and it can’t be accessed again. There is no way to reference it again
Q-7. In Java thread programming, which method is a must implementation for all threads?
Ans: Run() is a method of Runnable interface that must be implemented by all threads.
Q-8. I want to control database connections in my program and want thatonly one thread should be able to make database connection at a time. Howcan I implement this logic?
Ans: This can be implemented by use of the concept of synchronization. Database related code can be placed in a method which has synchronized keyword so that only one thread can access it at a time.
Q-9. What are real-world practices of volatile modifier?
Ans: One of the real-world use of the volatile variable is to generate interpretation double and long atomic. Equally double and long are 64-bit extensive and they are reciting in two parts, primary 32-bit first time and following 32-bit another time, which is non-atomic but then again volatile double in addition long read is atomic in Java. Additional use of the volatile variable is to deliver a recall barrier, just like it is cast-off in Disrupter framework.Fundamentally, Java Memory model pull-outs a write barrier subsequently you write to a volatile variable besides a read barrier beforehand you read it. That means, if you inscribe to volatile field then it’s definite that any thread retrieving that variable will see the worth you wrote and everything you did beforehand doing that correct into the thread is certain to have occurred and any rationalized data values will also be noticeable to all threads, since the memory barrier flushed all additional writes to the cache.
Q-10. What’s the variance amid an Abstract Class and Interface in Java?
Ans: The main difference amid an abstract class and interface is that a boundary cans only own assertion of public static approaches with no existing application while an abstract class can have associated with any admittance specifies (i.e. public, private etc.) with or without real implementation. Additional key variance in the usage of abstract classes and lines is that a class which gears an interface must contrivance all the approaches of the interface while a class which receives from an abstract class doesn’t need execution of all the methods of its superclass. A class can instrument numerous interfaces but it can spread only one abstract class.
Q-11.Is there a difference between Object Oriented and Object-Based language?
Ans: Object Oriented languages such as Java and C++ follow all the concepts of an Object Oriented Program and do not have inbuilt objects. Object-Based languages like JavaScript do not follow all OOPs concepts-such as inheritance-and do have I built objects.
Q-12. What is the purpose of composition?
Ans: You can use composition to hold the reference of one class within another class, and in this case, the contained object cannot exist without the class containing it. It is a type of aggregation.
Q-13.What is enumeration?
Ans. It is an interface you can use to access original data structure from which the enumeration is obtained.
Q-14. What’s the base class in Java from which all classes are derived?
Ans: java.lang.object
Q-15. What are Java Packages? What’s the significance of packages?
Ans: In Java, package is a collection of classes and interfaces which are bundled together as they are related to each other. Use of packages helps developers to modularize the code and group the code for proper re-use. Once code has been packaged in Packages, it can be imported in other classes and used.
Q-16. Can we declare the main method of our class as private?
Ans: In java, main method must be public static in order to run any application correctly. If main method is declared as private, developer won’t get any compilation error however, it will not get executed and will give a runtime error.
Q-17. When we should use serialization?
Ans: Serialization is used when data needs to be transmitted over the network. Using serialization, object’s state is saved and converted into byte stream .The byte stream is transferred over the network and the object is re-created at destination.
Q-18. Is String a data type in java?
Ans: String is not a primitive data type in java. When a string is created in java, it’s actually an object of Java.Lang.String class that gets created. After creation of this string object, all built-in methods of String class can be used on the string object.
Q-19. What is multi-threading?
Ans: Multi threading is a programming concept to run multiple tasks in a concurrent manner within a single program. Threads share same process stack and running in parallel. It helps in performance improvement of any program.
Q-20. How garbage collection is done in Java?
Ans: In java, when an object is not referenced any more, garbage collection takes place and the object is destroyed automatically. For automatic garbage collection java calls either System.gc() method or Runtime.gc() method.
Q-21. How can we use primitive data types as objects?
Ans: Primitive data types like int can be handled as objects by the use of their respective wrapper classes. For example, Integer is a wrapper class for primitive data type int. We can apply different methods to a wrapper class, just like any other object.
Q-22. What is the difference between ArrayList and vector?
Ans: An ArrayList is not suitable for working in a thread based environment. A vector is built for thread-based executions. ArrayList does not support legacy functions whereas a vector has support for legacy functions.
Q-23. Why we can’t create the object of abstract class in java?
Ans: Because an abstract class is an incomplete class (incomplete in the sense it contains abstract methods without body and output) we cannot create an instance or object; the same way we say for an interface.
Q-24. How is polymorphism achieved in Java?
Ans: An example of polymorphism is the == operator which can be used to compare both numerics and strings.
Q-25. What happens if we make the constructor final?
Ans: If we make the constructors final then the class variables initialized inside the constructor will become unusable. Their state cannot be changed.