Sample Questions Best site for GRE, LSAT, SAT, GMAT, TOEFL, CCNA, CCSA and interview sample questions  

Technical Sample Questions : C |  C++ |  Oracle |  Java | Unix |  Operating Systems |  Data Structure

Java Sample Questions ›› Java questions

Technical General Java Sample Questions

  1. Explain how preconditions can be used as an aid in writing correct programs.

    Answer: Suppose that a programmer recognizes a precondition at some point in a program.This is a signal to the programmer that an error might occur if the precondtion is not met.In order to have a correct and robust program, the programmer must deal with the possible error.There are several approaches that the programmer can take.One approach is to use an if statement to test whether the precondition is satisfied.If not, the programmer can take some other action such as printing an error message and terminating the program.Another approach is to use a try statement to catch and respond to the error.This is really just a cleaner way of accomplishing the same thing as the first approach.The best approach, when it is possible, is to ensure that the precondition is satisfied as a result of what has already been done in the program.For example, if the precondition is that x >= 0, and the preceding statement is "x = Math.abs(y);", then we know that the precondition is satisfied, since the absolute value of any number is greater than or equal to zero.

  2. Java has a predefined class called Throwable.What does this class represent? Why does it exist?

    Answer: The class Throwable represents all possible objects that can be thrown by a throw statement and caught by a catch clause in a try...catch statement.That is, the thrown object must belong to the class Throwable or to one of its (many) subclasses such as Exception and RuntimeException.The object carries information about an exception from the point where the exception occurs to the point where it is caught and handled.

  3. Some classes of exceptions require mandatory exception handling.Explain what this means.

    Answer: Subclasses of the class Exception which are not subclasses of RuntimeException require mandatory exception handling.This has two consequences: First, if a subroutine can throw such an exception, then it must declare this fact by adding a throws clause to the subroutine heading.Second, if a routine includes any code that can generate such an exception, then the routine must deal with the exception.It can do this by including the code in a try statement that has a catch clause to handle the exception.Or it can use a throws clause to declare that calling the subroutine might throw the exception.

  4. Consider a subroutine processData that has the header

    static void processData() throws IOException

    Write a try...catch statement that calls this subroutine and prints an error message if an IOException occurs.

    Answer: try {
    processData();
    }
    catch (IOException e) {
    System.out.println( "An IOException occurred while precessing the data.");
    }

  5. Why should a subroutine throw an exception when it encounters an error? Why not just terminate the program?

    Answer: Terminating the program is too drastic, and this tactic certainly doesn't lead to robust programs! It's likely that the subroutine doesn't know what to do with the error, but that doesn't mean that it should abort the whole program.When the subroutine throws an exception, the subroutine is terminated, but the program that called the subroutine still has a chance to catch the exception and handle it.In effect, the subroutine is saying "Alright, I'm giving up.Let's hope someone else can deal with the problem."

  6. In Java, input/output is done using streams.Streams are an abstraction.Explain what this means and why it is important.

    Answer: A stream represents a source from which data can be read or a destination to which data can be written.A stream is an abstraction because it represents the abstract idea of a source or destination of data, as opposed to specific, concrete sources and destinations such as a particular file or network connection.The stream abstraction is important because it allows programmers to do input/output using the same methods for a wide variety of data sources and destinations.It hides the details of working with files, networks, and the screen and keyboard.

  7. Java has two types of streams: character streams and byte streams.Why? What is the difference between the two types of streams?

    Answer: Character streams are for working with data in human-readable format, that is, data expressed as sequences of characters.Byte streams are for data expressed in the machine-readable format that is used internally in the computer to represent the data while a program is running.It is very efficient for a computer to read and write data in machine format, since no translation of the data is necessary.However, if a person must deal directly with the data, then character streams should be used so that the data is presented in human-readable form.

  8. What is a file? Why are files necessary?

    Answer: A file is a collection of data that has been given a name and stored on some permanent storage device such as a hard disk or floppy disk.Files are necessary because data stored in the computer's RAM is lost whenever the computer is turned off.Data that is to be saved permanently must be stored in a file.

  9. What is the point of the following statement?

    out = new PrintWriter( new FileWriter("data.dat") );

    Why would you need a statement that involves two different stream classes, PrintWriter and FileWriter?

    Answer: The PrintWriter class is being used as a "wrapper" for the FileWriter class.A FileWriter is a stream object that knows how to send individual characters to a file.By wrapping this in a PrintWriter, you get the ability to write other data types such as ints, doubles, and Strings to the file using the PrintWriter's print() and println() methods.Wrapping the FileWriter in a PrintWriter adds capabilities to the file output stream but still sends the data to the same destination.

  10. The package java.io includes a class named URL.What does an object of type URL represent, and how is it used?

    Answer: A url is an address for a web page (or other information) on the Internet.For example, "http://math.hws.edu/javanotes/index.html" is a url that refers to the main page of the current edition of this on-line textbook.A URL object represents such an address.Once you have a URL object, you can call its openConnection() method to access the information at the url address that it represents.

« Previous || Next »

Java Sample Question Number: 1-10| 11-20| 21-30| 31-40| 41-50| 51-60| 61-70| 71-80| 81-90| 91-100| 101-110| 111-120| 121-130| 131-140| 141-150| 151-160| 161-170| 171-180| 181-190| 191-200| 201-210| 211-220| 221-230| 231-240| 241-250| 251-260| 261-270| 271-280| 281-290| 291-300| 301-310| 311-320| 321-330
Sample Test Questions
GRE Sample Questions
CAT Sample Questions
GMAT Sample Questions
TOEFL Sample Questions
ACT Sample Questions
SAT Sample Questions
LSAT Sample Questions
PSAT Sample Questions
MCAT Sample Questions
PMP Sample Questions
GED Sample Questions
ECDL Sample Questions
DMV Sample Questions
CCNA Sample Questions
MCSE Sample Questions
Network+ Sample Questions
A+ Sample Questions
Technical Sample Questions
WASL Sample Questions
CISA Sample Questions

Other Sample Questions
Sample Interview Questions
Sample Teacher Interview Questions
Sample Citizenship Questions
Accuplacer Sample Questions
Science Bowl sample Questions
Driving Test Sample Questions
Sample Survey Questions Sample Essay Questions
Sample Behavioral Interview Questions

Copyright © 2004-2013, Best BSQ. All Rights Reserved.