Saturday 31 January 2015

24 TOP JVM Interview Questions and Answers

1)How can I write a program that takes command line input?
Java programs that take input from the command line declare a special static method called main, which takes aString array as an argument and returns void. The example program below loops through any arguments passed to the program on the command line and lists their values.

2)What does public static void main(String[]) mean?
This is a special static method signature that is used to run Java programs from a command line interface (CLI). There is nothing special about the method itself, it is a standard Java method, but the Java interpreter is designed to call this method when a class reference is given on the command line, as below.

3)Why are command line arguments passed as a String?
Command line arguments are passed to the application's main method by the Java runtime system before theapplication class or any supporting objects are instantiated. It would be much more complex to define and construct arbitrary object types to pass to the main method and primitive values alone are not versatile enough to provide the range of input data that strings can. String arguments can be parsed for primitive values and can also be used for arbitrary text input, file and URL references.

4)Why doesn't the main method throw an error with no arguments?
When you invoke the Java Virtual Machine on a class without any arguments, the class' main method receives aString array of zero length. Thus, the method signature is fulfilled. Provided the main method does not make any reference to elements in the array, or checks the array length before doing so, no exception will occur.

5)Why do we only use the main method to start a program?
The entry point method main is used to the provide a standard convention for starting Java programs. The choice of the method name is somewhat arbitrary, but is partly designed to avoid clashes with the Thread start() and Runnable run() methods, for example.
More Questions & Answers :-

No comments:

Post a Comment