Top 150+ Java Interview Questions and Answers

    58 Votes

112. What is an assertion?

An assertion is a statement which is used to test a program based on certain assumptions. The boolean expression inside assertion is supposed to return during execution. If the expression fails, Java will throw an AssertionError.

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.

113. What are the different forms of assertion statements?
The assertion statement has two forms.
assert Expression1 ;
Where Expression1 is a boolean expression. When the system runs the assertion, it evaluates Expression1 and if it is false throws an AssertionError with no detail message.
assert Expression1 : Expression2 ;

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?

ByteArrayOutputStream writes data into a byte array. The byte array will then be written to multiple streams. FilterOutputStream class is used to filter various output streams.
128. What is BufferedOutputStream and PrintStream?

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.

129. What is object serialization?
Serializing objects is the process of writing objects to a file. When an object is serialized, it's state and other attributes are converted into an ordered series of bytes. This byte series can be written into streams.
Page 11 of 14

Related Articles

Java OOPS Interview Questions and Answers
Top Advanced Java Interview Questions and Answers
Java Collections Interview Questions and Answers
Java Thread Interview Questions and Answers

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know