Java Interview Quests
Java Interview Quests
Java Interview Quests
because of its byte codes which can run on any system irrespective of its underlying
operating system.
such as boolean, byte, char, int, float, double, long, short which are not objects.
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.
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.
“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.
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.
Ans: Multi threaded applications can be developed in Java by using any of the
following two methodologies:
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.
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?
Q-17. What's the default access specifier for variables and methods of a
class?
Ans: Default access specifier for variables and method is package protected i.e
variables and class is available to any other class but in the same package,not
outside the package.
Ans: There are no pointers in Java. So we can't use concept of pointers in Java.
Interfaces:
● Connection
● Statement
● PreparedStatement
● ResultSet
● ResultSetMetaData
● DatabaseMetaData
● CallableStatement etc.
Classes:
● DriverManager
● Blob
● Clob
● Types
● SQLException etc.
register and unregister drivers. It provides factory method that returns the instance
of Connection.
application domain model objects to the relational database tables. Hibernate is java
based ORM tool that provides framework for mapping application domain objects to
a great choice as ORM tool with benefits of loose coupling. We can use Hibernate
persistence API for CRUD operations. Hibernate framework provide option to map
plain old java objects to traditional database tables with the use of JPA annotations
Similarly hibernate configurations are flexible and can be done from XML
1. Hibernate eliminates all the boiler-plate code that comes with JDBC and takes
care of managing resources, so we can focus on business logic.
2. Hibernate framework provides support for XML as well as JPA annotations,
that makes our code implementation independent.
3. Hibernate provides a powerful query language (HQL) that is similar to SQL.
However, HQL is fully object-oriented and understands concepts like
inheritance, polymorphism and association.
4. Hibernate is an open source project from Red Hat Community and used
worldwide. This makes it a better choice than others because learning curve is
small and there are tons of online documentations and help is easily available
in forums.
5. Hibernate is easy to integrate with other Java EE frameworks, it’s so popular
that Spring Framework provides built-in support for integrating hibernate with
Spring applications.
6. Hibernate supports lazy initialization using proxy objects and perform actual
database queries only when it’s required.
7. Hibernate cache helps us in getting better performance.
8. For database vendor specific feature, hibernate is suitable because we can
also execute native sql queries.
Overall hibernate is the best choice in current market for ORM tool, it contains all
Q-30. 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-31. 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.
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,
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
Answer- Thread-local variables are variables limited to a thread, it’s like thread’s
individual copy which is not public between numerous threads. Java offers a
Thread Local class to care thread-local variables. It’s one of the numerous ways to
managed environment e.g. with network servers where operative thread out lives
detached once its work is done can possibly reason a memory leak in Java
application.
Java?
Answer- 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
Inner class has admittance privileges for the class which is nesting it and it can
contact all variables and system well-defined in the outer class, whereas a
Sub-class can contact all public and protected approaches and fields of its
superclass.
Q-39 Can we affirm a class as Abstract deprived of having an
abstract method?
Answer- Yes, we can generate an abstract class via abstract keyword beforehand
class name even if it doesn’t have any abstract method. Though, if a class has
give an error.
Answer-
JVM
JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the
runtime environment in which java bytecode can be executed. It is a specification.
JVMs are available for many hardware and software platforms (so JVM is platform
dependent).
JRE
JRE stands for Java Runtime Environment. It is the implementation of JVM.
JDK
JDK is an acronym for Java Development Kit. It physically exists. It contains JRE +
development tools.
Q-45 What gives Java its 'write once and run anywhere'
nature?
The bytecode. Java is compiled to be a byte code which is the intermediate language
between source code and machine code. This byte code is not platform specific and
hence can be fed to any platform.