Difficulty level : Intermediate
Predict the output of following Java Programs.
Explanation: Since, s1 and s2 are two different objects the references are not the same, and the == operator compares object reference. So it prints “Not equal”, to compare the actual characters in the string .equals() method must be used.
Inside static method, People(whoAmI) Kid(who) Kid(whoAreYou)
Explanation: Static binding (or compile time) happens for static methods. Here p.whoAmI() calls the static method so it is called during compile time hence results in static binding and prints the method in People class.
Whereas p.whoAreYou() calls the method in Kid class since by default Java takes it as a virual method i.e, dynamic binding.
First statement of try block 15 finally block Main method
Since there is no exception, the catch block is not called, but the finally block is always executed after a try block whether the exception is handled or not.
Explanation : Initially new Thread is started with name gfg1 then in class Two the first run method runs the thread with the name gfg1, then after that a new thread is created calling run method but since a new thread can be created by calling start method only so the previous thread does the action and again gfg1 is printed.Now a new thread is created by calling the start method so a new thread starts with gfg3 name and hence prints gfg3.
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Output of Java program | Set 17
- Output of Java program | Set 27
- Output of Java program | Set 28
- Output of Java Program | Set 6
- Output of Java Program | Set 7
- Output of Java Program | Set 3
- Output of Java Program | Set 19
- Output of Java Program | Set 9
- Output of Java program | Set 5
- Output of Java Program | Set 4
- Output of Java program | Set 26
- Output of Java Program | Set 2
- Output of Java Program | Set 1
- Output of Java Program | Set 11
- Output of Java program | Set 23 (Inheritance)