Download Java Interview Questions and Answers and more Study Guides, Projects, Research Computer Science in PDF only on Docsity! JAVA INTERVIEW QUESTION Q-1. What is meant by looping? Ans: Loops are used to repeatedly execute a certain statement or block of statements. They 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 that only one thread should be able to make database connection at a time. How can 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 obtain. 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?