Está en la página 1de 3

MBAN-SEM-IV MI0032- SETI

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 used
to declare a class variable is:
<data_type> <variable_name>;
As the braces “{}” are used to mark the beginning and end of a class, a
semicolon “;” is used to mark the end of a statement.

12. What are the different methods under Buffered Input Stream and Buffered Output
Stream? 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. This
class is used to increase the efficiency of input operations. This is done by reading data
from the stream one byte at a time. The BufferedOutputStream class creates and
maintains a buffer for the output stream. Both the classes represents filter streams. The
DataInputStream and DataOutputStream Classes The DataInputStream and
DataOutputStream classes are filter streams that allow the reading and writing of Java
primitive data types. The DataInputStream class provides the capability to read primitive
data types from an input stream. It implements the methods presents in the DataInput
interface.
Methods of the DataInputStream class
Method Explanation
boolean readBoolean Reads one byte and tru i tha byt is
() returns nonzero, false if e f t e
it is zero.
Reads a byte as an 8-bit signed
byte readByte ( )
value.
char readChar ( ) Reads a Unicode character.
int readInt ( ) 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 output
stream.

ROLL-NO-510286414 CENTRE-01976
NAME:-AHUJA PRAKASH DHANRAJ
MBAN-SEM-IV MI0032- SETI

Methods of the DataOutputStream Class


Method Explanation
Method Explanation
void writeChar (int v
Writes a character to the output stream.
)
void writeLong (long
Writes a long integer to the output stream.
v)
void writeInt (int v ) Writes an integer to the output stream.

The term random access means that data can be read from or written to random locations
within a file. In all the above mentioned streams, data is read and written as a continuous
stream of information. Java provides the RandomAccessFile class to perform I/O
operations 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-only
or read/write (rw) access to the file. The RandomAccessFile class has several methods
that allow random access to the content within the file.

Method Explanation
void seek (long pos Sets the file pointer to a particular location inside
) the file.
long getFilePointer
Return the current location of the file pointer.
()
long length () Returns the length of the file, in bytes.

ROLL-NO-510286414 CENTRE-01976
NAME:-AHUJA PRAKASH DHANRAJ
MBAN-SEM-IV MI0032- SETI

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

Ans. The term exception denotes an exceptional event. It can be defined as an abnormal
event 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 care
of unexpected situations. The unexpected situations that may occur during program
execution 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 cannot
afford to have an application stop working or crashing, if the requested file is not present
on the disk. Traditionally, programmers used return values of methods to detect the errors
that occurred at runtime. A variable errno was used for a numeric representation of the
error. When multiple errors occurred in a method, errno would have only one value-that
of 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 is
required. An object that has not been allocated memory holds a null value. The situations
in 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 to
access an array element beyond the index of the array. For example, if you try to access
the eleventh element of an array that’s has only ten elements, the exception will be
thrown.

ROLL-NO-510286414 CENTRE-01976
NAME:-AHUJA PRAKASH DHANRAJ

También podría gustarte