Core Java interview questions β Part 11
166. What is aggregation?
It is a special type of composition. If you expose all the methods of a composite class and route the method call to the composite method through its reference, then it is called aggregation
167. What are the different methods available in Object?
clone, equals, wait, finalize, getClass, hashCode, notify, notifyAll, toString
168. What is the relationship between synchronized and volatile keyword?
The JVM is guaranteed to treat reads and writes of data of 32 bits or less as atomic. For long or double variable, programmers should take care in multi-threading environment. Either put these variables in a synchronized method or block, or declare them volatile.
169. What factors are used to decide using synchronized or volatile?
You can make a variable synchronized or volatile under the following cases: 1) if you are not updating many variables often in a multithread environment, consider using volatile. 2) If you are updating many variables, consider using synchronized, because using volatile might be slower.
170. What are the drawbacks of inheritance?
Since inheritance inherits everything from the super class and interface, it may make the subclass too clustering and sometimes error-prone when dynamic overriding or dynamic overloading in some situation. In addition, the inheritance may make peers hardly understand your code if they don’t know how your super-class acts.
171. What is the difference between static synchronized and synchronized methods?
Both are synchronized methods. One is instance method, the other is class method. Method with static modifier is a class method. That means the method belongs to class itself and can be accessed directly with class name and is also called Singleton design. The method without static modifier is an instance method. That means the instance method belongs to its object. Every instance of the class gets its own copy of its instance method.
172. What is the purpose of the Runtime class?
The purpose of the Runtime class is to provide access to the Java runtime system.
173. What is the purpose of the System class?
The purpose of the System class is to provide access to system resources.
174. Does the code in finally block get executed if there is an exception and a return statement in a catch block?
If an exception occurs and there is a return statement in catch block, the finally block is still executed. The finally block will not be executed when the System.exit(1) statement is executed earlier or the system shut down earlier or the memory is used up earlier before the thread goes to finally block.
175. Considering notepad/IE or any other thing as process, What will happen if you start notepad or IE 3 times? Where 3 processes are started or 3 threads are started ?
3 processes will start.
176. What are the restrictions placed on the values of each case of a switch statement?
At compile time, each case values of switch statement must evaluate to a an int value.
177. If aaaa is an array then why aaaa.length why not aaaa.length()?
Because length is a property not a method.
178. What is dynamic typing?
Dynamic typing means type errors are detected at run time by checking the actual data types of the values against the required data types
179. What is static typing?
Static typing means type errors are detected at compile time by checking the inferred data type is a subtype of the required type
180. What is HashMap and Map?
Map is Interface and HashMap is class that implements that.