1.
what will happen if you put the return statement or
system.exit() on the 'try' or 'catch'block? will the 'finally '
block execute?
Ans:- In Java, the finally block is designed to execute regardless of whether
an exception is thrown, ensuring that important cleanup code is run. Here’s
how finally interacts with return and System.exit() in try or catch blocks:
I. Return Statement in try or catch Block:
If a return statement is executed in the try or catch block, the finally block
will still be executed before the method actually returns. This is a feature of
the Java language that ensures the finally block is always run.
II. System.exit() in try or catch Block:
If System.exit() is called in the try or catch block, the JVM will terminate
immediately, and the finally block will not execute. This is because System.exit()
halts the JVM, preventing any further code from running.
2.Can you over ride a private or static method in java?
Ans:- In Java, you cannot override private or static methods. Here's why:
Private Methods:
Private methods are not visible outside the class in which they are declared.
Since they are not inherited by subclasses, you cannot override them.
If you define a method with the same name and signature in a subclass, it is
treated as a new method in the subclass, not an overridden method.
Static Methods:
Static methods belong to the class, not to instances of the class. They are
associated with the class itself rather than any object instance.
When you define a static method with the same name and signature in a
subclass, it is not an override but rather method hiding. The method in the
subclass hides the method in the superclass.
Conclusion:
Private methods cannot be overridden because they are not accessible outside
their own class.
Static methods cannot be overridden in the true sense; they can only be hidden
by defining a static method with the same name and parameters in a subclass.
3.Does java support multiple inheritances? Justify your answer.
Ans:- Java does not support multiple inheritance of classes, meaning a class
cannot inherit from more than one class. This design decision was made to avoid
the complexity and potential issues associated with multiple inheritance, such as
the diamond problem.
The Diamond Problem
The diamond problem occurs when a class inherits from two classes that both
inherit from a single superclass. This can create ambiguity and inconsistency in
the inheritance hierarchy.
Java's Approach:-
To avoid such issues, Java uses single inheritance for classes but allows multiple
inheritance of interfaces. This approach ensures a simpler and more consistent
inheritance model.
Conclusion
Java supports multiple inheritance of type through interfaces but does not
support multiple inheritance of implementation through classes. This design
choice simplifies the language and avoids the complexity and potential issues
associated with multiple inheritance.
4.What will happen if we put a key object in a HashMap already
there?
Ans:- In Java, if you put a key-value pair into a HashMap where the key already
exists, the new value will replace the old value associated with that key. The
HashMap will update its entry to reflect the new value, and the old value will be
returned by the put method.
Conclusion
When you put a key-value pair into a HashMap where the key already exists, the
new value will replace the old value associated with that key. The old value will
be returned by the put method, allowing you to know what the previous value
was. This behavior ensures that each key in the HashMap remains unique, with
its corresponding value being the most recently specified one.
5.Can you access a non- static variable in the static context ?
Ans:- In Java, you cannot directly access a non-static (instance) variable from a
static context (e.g., a static method) because non-static variables are
associated with instances of a class, while static methods belong to the class
itself and do not require an instance to be called.
Explanation
Static Context: A static context (like a static method or static block)
can be accessed without creating an instance of the class.
Non-Static Variables: Non-static variables belong to an instance of the
class. Each instance has its own copy of these variables.
Conclusion
In a static context, you cannot directly access non-static variables because
they are tied to specific instances of the class. However, you can access them
by creating an instance of the class and then referencing the variable through
that instance. This ensures that each instance maintains its own state
independently.