1).What if the main method is declared as private?
Answer: The program compiles properly but at runtime it will give
"Main method not public" Message.
2).What if the static modifier is removed from the signature of the main method?
Answers: Program compiles. But at runtime throws an error "NoSuchMethodError".
3).What if I write static public void instead of public static void?
Answers: Program compiles and runs properly.
4).What if I do not provide the String array as the argument to the method?
Answers: Program compiles but throws a runtime error "NoSuchMethodError".
5).What is the first argument of the String array in main method?
Answers: The String array is empty. It does not have any element.
This is unlike C/C++ where the first element by default is
The program name.
6).What environment variables do I need to set on my Machine in order to be able to run Java programs?
Answers: CLASSPATH and PATH are the two variables.
7).How can one prove that the array is not null but empty using one line of code?
Answers: Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.
8. Can an application have multiple classes having main method?
Answers: Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.
9. Can I have multiple main methods in the same class?
Answers: No the program fails to compile. The compiler says that the main method is already defined in the class.
10. What are Checked and Unchecked Exception?
Answers:
A checked exception is some subclass of Exception (or Exception itself), excluding class Runtime Exception and its subclasses. Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown.
Example: I Exception thrown by java.io.FileInputStream
read () method.
Unchecked exceptions are Runtime Exception and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the
Compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could
Be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method· Checked exceptions must be caught at compile time. Runtime exceptions do not need To be. Errors often cannot be.
11. What is overriding?
Answers: When a class defines a method using the same name, return type, and arguments as a method in its super class, the method in the class overrides the method in the super class.
When the method is invoked for an object of the class, it is the new definition of the method that is called, and not the method definition from super class. Methods may be overridden to be more public, not more private.
0 comments:
Post a Comment