112. What is an assertion?
Assertions help programmers to identify and correct bugs faster. It is one of the efficient ways to detect bugs. Assertions can be used for internal documentation, which enhances code maintainability.
Where Expression1 is a boolean expression and Expression2 is an expression that has a value. This form of assert statement provides a detail message for the AssertionError.
114. How do we enable and disable assertions?
To enable assertions use -enableassertions, or -ea, option at runtime. To disable assertions use -disableassertions, or -da. Specify the granularity with the arguments provided at runtime.
- no arguments - Enables or disables assertions in all classes except system classes.
- packageName - Enables or disables assertions in the named package and any subpackages.
- "-" Enables or disables assertions in the unnamed package in the current working directory.
- className - Enables or disables assertions in the named class
Java I/O Interview Questions
115. What are the basic functionalities provided by io package?
Many Java programs need to read or write data to an external source. Read/Write operations are carried out by classes in java.io package. A program can get input from a data source by reading a sequence of characters from an InputStream attached to the source. A program can produce output by writing a sequence of characters to an OutputStream attached to a destination.
116. What is a Stream?
A stream is a flowing set of data. Streams support different types of data, such as bytes, objects, data types, etc. Streams can be used for simple data transfer, data manipulation or data transformation. We can use an input stream to read data from a source, one at a time and we can write data to the target, one at a time.
117. What is an inputstream?
The Java.io.InputStream class is a superclass which provides an interface and a partial implementation to all input streams. The InputStream class defines methods for reading bytes or arrays of bytes.
118. What is a FileInputStream?
It reads data from a file on the native file system. For example, reading raw bytes of data from an image file.
119. What is ByteArrayInputStream?
A ByteArrayInputStream contains an internal buffer which is used to store the bytes that are read from a stream. ByteArrayInputStream keeps track of the byte that needs to supplied during the next read operation.
120. What is SequenceInputStream, StringBufferInputStream, PipedInputStream?
- SequenceInputStream : It concatenates multiple input streams into one input stream.
- StringBufferInputStream : It allows programs to read from a StringBuffer as if it were an input stream.
- PipedInputStream : In this data can be read from a thread by using PipedInputStream
121. What is FilterInputStream?
FilterInputStream is a subclass of InputStream. FilterInputStream class provides interfaces to process/filter data while the data is read or written to and from a stream.
122. What is BufferedInputStream?
BufferedInputStream is used to buffer input streams. When we create a Buffered Input Stream, an internal array will be created. Advantage of Buffered Stream is that it reads large blocks of data at a time and store it into it's internal buffer. This process is much faster compared to reading a byte at a time.
123. What is an Enumeration interface and what are it's methods?
The Enumeration interface is used to fetch items from a collection of objects, one at a time. To check whether the elements exist in a collection, hasMoreElements() method is used. The method returns true if items exist in the collection. To fetch the next item in the collection, use nextElement() method. If no more items exists in the collection and if nextElement() is called, the NoSuchElementException will be thrown.
124. What is a DataInputStream?
A DataInputStream helps application read primitive data types from an input stream. It reads the data types irrespective of the underlying OS on which it is run. It is not thread safe.
125. What is an OutputStream?
The OutputStream is used to write bytes or an array of bytes to a stream. The OutputStream is an abstract class, which provides an interface and implementations of the output stream.
126. What is a FileOutputStream?
A FileOutputStream is used to write data to a file. While writing data, if the file is not available, it will be created by the stream.
127. What is a ByteArrayOutputStream and FilterOutputStream?
A BufferedOutputStream avoids multiple write to a stream by initially writing the data to a buffer. When the buffer is full/flushed, the data is written to the actual stream. A PrintStream class helps us to write data to another output stream. It can format primitive data types like int, long etc. and write as text to any OutputStream.