Engineering Full Stack Apps with Java and JavaScript
What will the following code print when run?
public class TestClass{ public static long main(String[] args){ System.out.println("Hello"); return 10L; } }
Select 1 option
A. Hello B. It will print nothing. C. It will not compile D. It will throw a Throwable at runtime. E. None of the above.
D
When the program is run, the JVM looks for a method named main() which takes an array of Strings as input and returns nothing (i.e. the return type is void). But in this case, it doesn't find such a method ( the given main() method is returning long!) so it throws a java.lang.No Such Method Error. Note that java.lang.Error does not extend the Exception class. It extends java.lang.Throwable and so it can be "thrown".
Logged in users see lesser ads and get more features.
If you want to solve the questions together with the experts and understand the concepts better, please contact us using the contact form or WhatsApp numbers. Selected people get to do that without any additional charges.