Java questions for Exams  

Posted by Rajiv Pandey in , , ,

Q:How can I write a program that takes command line input?

A:Java programs that take input from the command line declare a special static method called main, which takes a String 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. More details available to subscribers:How can I write a program that takes command line input?

Q: What does public static void main(String[]) mean?

A: 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. More details available to subscribers:What does public static void main(String[]) mean?

Q: Why are command line arguments passed as a String?

A: Command line arguments are passed to the application's main method by the Java runtime system before the application 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.

Q: Why doesn't the main method throw an error with no arguments?

A: When you invoke the Java Virtual Machine on a class without any arguments, the class' main method receives a String 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.

Q: Why do we only use the main method to start a program?

A: 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.

Q: Can the main method be overloaded?

A: Yes, any Java method can be overloaded, provided there is no final method with the same signature already। The Java interpreter will only invoke the standard entry point signature for the main method, with a string array argument, but your application can call its own main method as required. Main method modifiers Q: Can the main method be declared final? A: Yes, the static void main(String[]) method can be declared final.

Q: I get an exception if I remove the static modifier from main!

A: The static void main(String[]) method is a basic convention of the Java programming language that provides an entry point into the runtime system। The main method must be declared static because no objects exist when you first invoke the Java Virtual Machine (JVM), so there are no references to instance methods. The JVM creates the initial runtime environment in which this static method can be called, if you remove the static modifier, it will throw a NoSuchMethodException. Application start up

Q: What is threaded programming and when is it used?

A: Threaded programming is normally used when a program is required to do more than one task at the same time। Threading is often used in applications with graphical user interfaces; a new thread may be created to do some processor-intensive work while the main thread keeps the interface responsive to human interaction. The Java programming language has threaded programming facilities built in, so it is relatively easy to create threaded programs. However, multi-threaded programs introduce a degree of complexity that is not justified for most simple command line applications.

Q: Why are wait(), notify() and notifyall() methods defined in the Object class?

A: These methods are detailed on the Java Software Development Kit JavaDoc page for the Object class, they are to implement threaded programming for all subclasses of Object.

Q: Why are there separate wait and sleep methods?

A: The static Thread।sleep(long) method maintains control of thread execution but delays the next action until the sleep time expires। The wait method gives up control over thread execution indefinitely so that other threads can run. More details available to subscribers:Why are there separate wait and sleep methods? Threads and runnable types

Q: What's the difference between Thread and Runnable types?

A: A Java Thread controls the main path of execution in an application। When you invoke the Java Virtual Machine with the java command, it creates an implicit thread in which to execute the main method. The Thread class provides a mechanism for the first thread to start-up other threads to run in parallel with it. The Runnable interface defines a type of class that can be run by a thread. The only method it requires is run, which makes the interface very easy to to fulfil by extending existing classes. A runnable class may have custom constructors and any number of other methods for configuration and manipulation.

Latest Seo Services By Seo Support

This entry was posted on Sunday, January 6, 2008 at Sunday, January 06, 2008 and is filed under , , , . You can follow any responses to this entry through the comments feed .

0 comments

Post a Comment