🌙 DARK

Java interview questions and answers for freshers

 

Java Interview Question 

Follow 

@MRProgrammer89

Q-1 Explain public static void main(String args[]). 

● public : Public is an access modifier, which is used to specify who can access this method. Public means that this Method will be accessible by any Class. 

● static : It is a keyword in java which identifies it is class based i.e it can be accessed without creating the instance of a Class. 

● void : It is the return type of the method. Void defines the method which will not return any value. 

● main: It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs. 

● String args[] : It is the parameter passed to the main method. 


Q-2. Why Java is platform independent? 

Platform independent practically means “write once run anywhere”. Java is called so because of its byte codes which can run on any system irrespective of its underlying operating system. 


Q-3. Why java is not 100% Object-oriented? 

Java is not 100% Object-oriented because it makes use of eight primitive datatypes such as boolean, byte, char, int, float, double, long, short which are not objects. 


Q-4. What do you mean by Object?

 An object consists of methods and class which depict its state and perform operations. A java program contains a lot of objects instructing each other their jobs. This concept is a part of core java. 


Q-5 What is association?

 Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. These relationship can be one to one, One to many, many to one and many to many. 


Q-6. What do you mean by aggregation? 

Aggregation is a specialized form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let’s take an example of Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy. 


Q-7. What is composition in Java? 

Composition is again specialized form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object dose not have their lifecycle and if parent object deletes all child object will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete. 


Q-8. What's the difference between an array and Vector? 

Ans: An array groups data of same primitive type and is static in nature while vectors are dynamic in nature and can hold data of different data types. 

@MRProgrammer89 

Q-9. 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-10. Why Runnable Interface is used in Java? 

Ans: Runnable interface is used in java for implementing multi threaded applications. Java.Lang.Runnable interface is implemented by a class to support multi threading. 

@MRProgrammer89

Q-11. What are the two ways of implementing multi-threading in Java? 

Ans: Multi threaded applications can be developed in Java by using any of the following two methodologies: 

1. By using Java.Lang.Runnable Interface. Classes implement this interface to enable multi threading. There is a Run() method in this interface which is implemented. 

2. By writing a class that extend Java.Lang.Thread class. 


Q-12. When a lot of changes are required in data, which one should be a preference to be used? String or StringBuffer? 

Ans: Since String Buffers are dynamic in nature and we can change the values of StringBuffer objects unlike String which is immutable, it's always a good choice to use StringBuffer when data is being changed too much. If we use String in such a case, for every data change a new String object will be created which will be an extra overhead. 


Q-13. What's the purpose of using Break in each case of Switch Statement? 

Ans: Break is used after each case (except the last one) in a switch so that code breaks after the valid case and doesn't flow in the proceeding cases too. If break isn't used after each case, all cases after the valid case also get executed resulting in wrong results. 

@MRProgrammer89

Q-14. 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-15. How we can execute any code even before main method?

 Ans: If we want to execute any statements before even creation of objects at load time of class, we can use a static block of code in the class. Any statements inside this static block of code will get executed once at the time of loading the class even before creation of objects in the main method.

@MRProgrammer89





MOTIVATION :


    “The way we spend our time defines who we are 


Although we may not see each other as often as we’d like, distance is no match for the bond that we share. Thank you for coming to visit. It was fantastic to catch up.


VISITE MORE BLOGS


Post a Comment

Thanks for reading the blog. We hope it was useful to you and that you learned something new. Will always be writing on new and interesting topics, so you can visit our website to know the latest updates of our blogs. Thank You!

Previous Post Next Post

Contact Form