Answers to Core Java Coding / Programming Questions and Answers : Technical Interview in Java

Here we will discuss the explanation of the answers of the questions shared in the post


answers to the core java interview questions

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.



answers to the tricky core java interview questions

 answers to the technical interview questions of core java




Read Also :      JDBC Interview questions and answers for Java Developers

------------------------------------------------------------------------------------------------------------




answers to the java interview questions  


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



------------------------------------------------------------------------------------------------------------




core java technical interview questions answers


Thanks Elias Onal for the answer





  • Java passes objects by reference and not by value. In other words , Java is always pass-by-value.

    The difficult thing can be to understand that Java passes objects as references and those references are passed by value.

    In Java strings can never change, because they're immutable. arg1 initially is a copy of the reference pointing to the "I am born new!" string. "Am I going to disappear?" is simply a different string whose reference gets assigned to arg1. Changing arg1 has no effect on the original, since arg1 is just a copy of the iAmOfAnArgumentativeNature reference.
      ------------------------------------------------------------------------------------------------------------



      answers to coding java interview questions

      There are a number of things going on in the question.
      1. 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.
      2. 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)
      3. 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).




      -----------------------------------------------------------------------------------------------------------  



      core java technical interview answers
























      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 .


      ----------------------------------------------------------------------------------------------------------




      java interview questions and answers




      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. 



      ------------------------------------------------------------------------------------------------------------




      java interview questions and answers

      java tricky questions and answers


       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 .





      ---------------------------------------------------------------------------------------------




      java interview answers



      In line 11 , the must know thing in java, The local variable always need to be initialized before using it  in the code .



      ------------------------------------------------------------------------------------------------------------





      interview questions and answers



                          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 .