Top Core Java Interview Questions || Core Java Interview Questions and Answers [MOST ASKED]
Q. What are the types of Exceptions?
Exception is an error event that can happen during the execution of a program and disrupts its normal flow.
1. Checked Exception:
The classes which directly inherit Throwable class except RuntimeException and Error are known as checked exceptions e.g. IOException, SQLException etc. Checked exceptions are checked at compile-time.
2. Unchecked Exception:
The classes which inherit RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime.
3. Error:
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
Q. Explain hierarchy of Java Exception classes?
The java.lang.Throwable class is the root class of Java Exception hierarchy which is inherited by two subclasses: Exception and Error.
Example:
Top 1000 Java Interview Questions & Answers: Includes Spring, Hibernate, Microservices, GIT, Maven, JSP, AWS, Cloud Computing
This specific ISBN edition is currently not available.
This is the ultimate book for interview preparation for Java jobs. It has questions on Java, Stream, Collections, Multi-threading, Spring, Hibernate, JSP, Design patterns, GIT, Maven, AWS and Cloud computing. It is a digest of questions from multiple sources. It covers almost all the technical areas of an interview for Java engineer position. The difficulty level of questions in this book vary from beginner to expert level. Once you go through this book, you will be very well prepared for facing Java interview for an experienced Software Developer. This books also contains Java tricky Interview questions, Java 8, Microserivces and AWS questions. Technical job applicants save pervious time in interview preparation by reading this book. You do not have to waste time in searching for questions and answers online. This books is your main book for Java based jobs.
“synopsis” may belong to another edition of this title.
Shipping: US$ 3.00 Within U.S.A.
Q. What is the difference between aggregation and composition?
1. Aggregation:
We call aggregation those relationships whose objects have an independent lifecycle, but there is ownership, and child objects cannot belong to another parent object.
Example: Since Organization has Person as employees, the relationship between them is Aggregation. Here is how they look like in terms of Java classes
2. Composition:
We use the term composition to refer to relationships whose objects dont have an independent lifecycle, and if the parent object is deleted, all child objects will also be deleted.
Example: Since Engine is-part-of Car, the relationship between them is Composition. Here is how they are implemented between Java classes.
Aggregation | Composition |
---|---|
Aggregation is a weak Association. | Composition is a strong Association. |
Class can exist independently without owner. | Class can not meaningfully exist without owner. |
Have their own Life Time. | Life Time depends on the Owner. |
A uses B. | A owns B. |
Child is not owned by 1 owner. | Child can have only 1 owner. |
Has-A relationship. A has B. | Part-Of relationship. B is part of A. |
Denoted by a empty diamond in UML. | Denoted by a filled diamond in UML. |
We do not use “final” keyword for Aggregation. | “final” keyword is used to represent Composition. |
Examples: – Car has a Driver. – A Human uses Clothes. – A Company is an aggregation of People. – A Text Editor uses a File. – Mobile has a SIM Card. | Examples: – Engine is a part of Car. – A Human owns the Heart. – A Company is a composition of Accounts. – A Text Editor owns a Buffer. – IMEI Number is a part of a Mobile. |
Note: “final” keyword is used in Composition to make sure child variable is initialized.