Anda di halaman 1dari 5

11. Explain the various rules for naming a variable in Java? Give one example for each.Ans.

Rules for Naming Variables in Java A variable name: Must not be a keyword in Java. Must not begin with a digit. Must not contain embedded spaces. Can contain characters from various alphabets, like Japanese, Greek,and Cyrillic. Syntax for Defining Variables All the attributes of a class are defined as data members. The syntax usedto declare a class variable is:<data_type> <variable_name>;As the braces {} are used to mark the beginning and end of a class, asemicolon ; is used to mark the end of a statement.12. What are the different methods under Buffered Input Stream and Buffered OutputStream? Explain the uses of random access file over sequential access file?Ans.

The BufferedInputStream and BufferedOutputStream Classes The BufferedInputStream class creates and maintains a buffer for an input stream. Thisclass is used to increase the efficiency of input operations. This is done by reading datafrom the stream one byte at a time. The BufferedOutputStream class creates andmaintains a buffer for the output stream. Both the classes represents filter streams.

The DataInputStream and DataOutputStream Classes The DataInputStream andDataOutputStream classes are filter streams that allow the reading and writing of Java primitive data types. The DataInputStream class provides the capability to read primitivedata types from an input stream. It implements the methods presents in the DataInputinterface. Methods of the DataInputStream class Method boolean readBoolean( ) Byte readByte() Char readChar() int Read int() Explanation Reads one byte and true byte is returns nonzero, false if it is zero Reads a byte as an 8-bit signed value Reads a Unicode character Reads an integer value

The DataInputStream class provides methods that are complementary to the methods of DataInputStream classes. It keeps track of the number of bytes written to the outputstream.

Methods of the DataOutputStream Class Method Explanation Method Void writechar (int v) Void writeLong (long v) Void writeint (int v) Explanation Writes a character to the output stream Writes a long integer to the output stream Writes an integer to the output stream

The term random access means that data can be read from or written to random locationswithin a file. In all the above mentioned streams, data is read and written as a continuousstream of information. Java provides the RandomAccessFile class to perform I/Ooperations a t specified locations within a file.The RandomAccessFile class implements the DataInput and DataOutput interfaces for performing I/O using

the primitive data types. The RandomAccessFile class also supports permissions like read and write, and allows files to be accessed in read-only and read-write modes. Creating a Random Access File There are two ways to create a random access file- using the pathname as a string or using an object of the File class. RandomAccessFile (String pathname, String mode); RandomAccessFile (File name, String mode); Example randomFile = new RandomAccessFile (iotets.txt,rw); or File file1 = new File (iotest.txt); RandomAccessFile randomFile = new RandomAccessFile(file1,rw); The second argument is the mode argument that determines whether you have read-onlyor read/write (rw) access to the file. The RandomAccessFile class has several methodsthat allow random access to the content within the file. Method Void seek (long pos) Long getFilePointer() Long length() Explanation Sets the file pointer to a particular location inside the file Return the current location of the pointer. Returns the length of the file, in bytes.

13. With suitable example, explain the difference between errors and exceptions. Explainthe various types of exceptions.Ans.

The term exception denotes an exceptional event. It can be defined as an abnormalevent that occurs during program execution and disrupts the normal flow of instruction.Error-handling becomes a necessity when you develop applications that need to take careof unexpected situations. The unexpected situations that may occur during programexecution are: Running out of memory

Resource allocation errors. Inability to find a file. Problems in network connectivity.If an above-mentioned situation is encountered, a program may stop working. You cannotafford to have an application stop working or crashing, if the requested file is not presenton the disk. Traditionally, programmers used return values of methods to detect the errorsthat occurred at runtime. A variable errno was used for a numeric representation of theerror. When multiple errors occurred in a method, errno would have only one value-thatof the last error that occurred in the method. The most common exceptions that you may encounter are described below.

Arithmetic Exception This exception is thrown when an exceptional arithmetic condition has occurred. For example, a division by zero generates such an exception.

NullPointer Exception This exception is thrown when an application attempts to use null where an object isrequired. An object that has not been allocated memory holds a null value. The situationsin which an exception is thrown include: Using an object without allocating memory for it. Calling the methods of a null object. Accessing or modifying the attributes of a null object. ArrayIndexOutOfBounds Exception

The exception ArrayIndexOutOfBounds Exception is thrown when an attempt is made toaccess an array element beyond the index of the array. For example, if you try to accessthe eleventh element of an array thats has only ten elements, the exception will bethrown

Anda mungkin juga menyukai