Core Java interview questions – Part 7
106. What is java collections?
Java collections is a set of classes, that allows operations on a collection of classes.
107. Can we compile a java program without main?
Yes, we can. In order to compile a java program, we don’t require any main method. But to execute a java program we must have a main in it (unless it is an applet or servlet). Because main is the starting point of a java program.
108. What is a java compilation unit.
A compilation unit is a java source file.
109. What are the restrictions when overriding a method ?
Overridden methods must have the same name, argument list, and return type (i.e., they must have the exact signature of the method we are going to override, including return type.) The overriding method cannot be less visible than the method it overrides( i.e., a public method cannot be override to private). The overriding method may not throw any exceptions that may not be thrown by the overridden method
110. What is static initializer block? What is its use?
A static initializer block is a block of code that declares with the static keyword. It normally contains the block of code that must execute at the time of class loading. The static initializer block will execute only once at the time of loading the class only.
111. How does a try statement determine which catch clause should be used to handle an exception?
When an exception is thrown , the catch block of the try statement are examined in the order in which they appear. The first catch block that is capable of handling the exception is executed. The remaining catch blocks are ignored
112. How parameters are passed to methods in java program ?
All java method parameters in java are passed by value only. Obviously primitives are passed by value. In case of objects a copy of the reference is passed and so all the changes made in the method will persist.
113. If a class doesn’t have any constructors, what will happen?
If a class doesn’t have a constructor, the JVM will provide a default constructor for the class.
114. What will happen if a thread cannot acquire a lock on an object?
It enters to the waiting state until lock becomes available.
115. How does multithreading occurring on a computer with a single CPU?
The task scheduler of OS allocates an execution time for multiple tasks. By switching between different executing tasks, it creates the impression that tasks execute sequentially. But actually there is only one task is executed at a time.
116. What will happen if you are invoking a thread’s interrupt method while the thread is waiting or sleeping?
When the task enters to the running state, it will throw an InterruptedException.
117. What are the different ways in which a thread can enter into waiting state?
There are three ways for a thread to enter into waiting state. By invoking its sleep() method, by blocking on I/O, by unsuccessfully attempting to acquire an object’s lock, or by invoking an object’s wait() method.
118. What are the the different ways for creating a thread?
A thread can be created by sub classing Thread, or by implementing the Runnable interface.
119. What is the difference between creating a thread by extending Thread class and by implementing Runnable interface? Which one should prefer?
When creating a thread by extending the Thread class, it is not mandatory to override the run method (If we are not overriding the run method , it is useless), because Thread class have already given a default implementation for run method. But if we are implementing Runnable , it is mandatory to override the run method. The preferred way to create a thread is by implementing Runnable interface, because it give loose coupling.
120. What is coupling?
Coupling is the dependency between different components of a system