Here we will discuss the explanation of the answers of the questions shared in the post
Logic for the first three questions :
The method lookup used by javac chooses the most specific valid overload.
It will go to the most specific case as possible. Since null matches every possible subclass of object and object itself, it will always go to the most specific method possible. But if you have 2 classes that have split paths in the inheritance tree, it will throw the compiler time exception that it is too ambiguous.
Read Also : JDBC Interview questions and answers for Java Developers
------------------------------------------------------------------------------------------------------------
The above question is valid one , there is no compile time error . The most important thing is that the expression in the if block should return a boolean value , so above null==null returns true , which is boolean so , the if block body executes and Java Hungry Blogspot prints
------------------------------------------------------------------------------------------------------------
Thanks Elias Onal for the answer
If block expression should always return a boolean value . In line 6 , x=2 , assign value 2 to the variable x. x is an int variable . So , if block expression ends up with int value while it is expecting boolean value hence compiler error .
----------------------------------------------------------------------------------------------------------
if the Thread acquires the monitor first, then it will call notifyAll() before main even starts wait()ing. When the thread releases the monitor, main will start wait()ing forever and never get to print the result.
------------------------------------------------------------------------------------------------------------
Logic for the above questions :
Main reason to show above is that finally block always execute and it will execute last in try/catch/finally block , irrespective of flow of exception generation and handling .
Above example shows that ,String is immutable and string buffer is mutable . So string s2 and s1 both pointing to the same string abc . And , after making the changes the string s1 points to abcd and s2 points to abc , hence false .
While in string buffer , both sb1 and sb2 both point to the same object , As string buffer are mutable , so making changes in one string also make changes to the other string . So both string still pointing to the same object after making the changes to the object (here sb2)
------------------------------------------------------------------------------------------------------------
If you have any doubts then please express it in the comments .
Logic for the first three questions :
The method lookup used by javac chooses the most specific valid overload.
It will go to the most specific case as possible. Since null matches every possible subclass of object and object itself, it will always go to the most specific method possible. But if you have 2 classes that have split paths in the inheritance tree, it will throw the compiler time exception that it is too ambiguous.
Read Also : JDBC Interview questions and answers for Java Developers
------------------------------------------------------------------------------------------------------------
The above question is valid one , there is no compile time error . The most important thing is that the expression in the if block should return a boolean value , so above null==null returns true , which is boolean so , the if block body executes and Java Hungry Blogspot prints
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
There are a number of things going on in the question.
- Line 12 The "" in the println causes the numbers to be automatically cast as strings. So it doesn't do addition, but appends together as string.
- Line11 the += does an automatic cast to a short. However the number 123456 can't be contained within a short, so you end up with a negative value (-7616)
- Those other two are red herrings however as the code will never compile due to line 8. Any number beginning with zero is treated as an octal number (which is 0-7).
-----------------------------------------------------------------------------------------------------------
If block expression should always return a boolean value . In line 6 , x=2 , assign value 2 to the variable x. x is an int variable . So , if block expression ends up with int value while it is expecting boolean value hence compiler error .
----------------------------------------------------------------------------------------------------------
if the Thread acquires the monitor first, then it will call notifyAll() before main even starts wait()ing. When the thread releases the monitor, main will start wait()ing forever and never get to print the result.
------------------------------------------------------------------------------------------------------------
Logic for the above questions :
Main reason to show above is that finally block always execute and it will execute last in try/catch/finally block , irrespective of flow of exception generation and handling .
---------------------------------------------------------------------------------------------
In line 11 , the must know thing in java, The local variable always need to be initialized before using it in the code .
------------------------------------------------------------------------------------------------------------
Above example shows that ,String is immutable and string buffer is mutable . So string s2 and s1 both pointing to the same string abc . And , after making the changes the string s1 points to abcd and s2 points to abc , hence false .
While in string buffer , both sb1 and sb2 both point to the same object , As string buffer are mutable , so making changes in one string also make changes to the other string . So both string still pointing to the same object after making the changes to the object (here sb2)
------------------------------------------------------------------------------------------------------------
If you have any doubts then please express it in the comments .