Stream is basically a channel on which the data flow from sender to receiver.An input object that reads the stream of data from a file is called input stream and the output object that writes the stream of data to a file is called output stream.
Two Marks Questions with Answers
Q.1What is stream?
Ans.
Stream is basically a channel on which the data flow from sender to receiver.
Q.2 What is input stream and output stream?
Ans. An
input object that reads the stream of data from a file is called input stream
and the output object that writes the stream of data to a file is called output
stream.
Q.3 What is byte stream? Enlist its super
classes.
Ans. The
byte stream is used for inputting or outputting the bytes. The InputStream and
OutputStream are the superclass of byte stream.
Q.4 What is character stream? Enlist its super
classes.
Ans. The
character stream is used for inputting or outputting the characters. The Reader
and Writer are the superclass of character stream.
Q.5 List the byte stream classes.
Ans. The
Byte Stream classes are -
1.
FileInputStream
3.
FilterInputStream
5.
FileOutputStream
7.
FilterOutputStream
2.
PipedInputStream
4.
ByteArrayInputStream
6.
PipedOutputStream
8.
ByteArrayOutputStream
Q.6 What is the purpose of BufferedInputStream
and BufferedOutputStream classes?
• These are efficient classes used for speedy
read and write operations.
• We can
specify Buffer size while using these classes.
Q.7 Give an example on stream.
• There
are two types of streams - Byte stream and Character stream.
• Stream
classes for byte stream : FileInputStream, PipedInputStream,
BufferedInputStream, FileOutputStream, PipedOutputStream and so on.
• Stream
classes for character stream: FileReader, PipeReader, BufferedReader,
FileWriter, PipeWriter, Buffered Writer and so on.
Q.8 What is absolute file name?
Ans. The
filename that can be specified with the complete path name and drive letter is
called absolute file name.
Q.9 What is relative file name?
Ans. The
file name which is specified as a path relative to the current directory is
called relative file name. For example new File("myprogram.html");
Q.10 What is the use of seek method?
Ans. :
The seek() allows you to specify the index from where the read and write
operations will start. The syntax is
void
seek(long position);
Q.11 Write a Java code to check if the command
line argument is file or not.
Ans.:
class
Test
{
public
static void main(String[] args)
{
File
obj=new File(args[0]);
If(obj.isFile())
{
System.out.println("This
is a file name" + obj.getPath());
}
}
}
Q.12 Enlist the two common constructors of
FileInputStream.
Ans. The
common constructors of FileInputStream are
FileInputStream(String
filename);
FileInputStream(File
fileobject);
Q.13 What
is the use of Input Stream Reader and Output Stream Writer?
Ans. The
InputStreamReader converts byte into character and OutputStreamWriter converts
the characters written into byte.
Q.14 Give an example for reading data from
files using FileInput Stream
Ans;
import
java.io.FileInputStream;
public
class test
{
public
static void main(String args[])
{
try
{
FileInputStream
fin=new FileInputStream("D:\\input.txt");
int i=0;
while((i=fin.read())!=
-1)
{
System.out.print((char)i);
}
fin.close();
}catch(Exception
e) { System.out.println(e);}
}
}
Q.15 Why generic programming is required?
OR What is the need for generic programming?
Ans.
Following some reasons for the need of generic programming -
1. It
saves the programmers burden of creating separate methods for handling data
belonging to different data types.
2. It
allows the code reusability.
3.
Compact code can be created.
Q.16 List out motivation needed in generic
programming.
Ans;
1. Suppose
we want to create a stack and if we create a stack of integers then it will
store only the integer elements, if we try to push any string or any double
type element then a compile time error will occur. If we want to push any
string then we need to create a separate stack, separate class and separate
methods for handling the String elements. Same is true for the elements of any
other data type. This results in complex code building. To avoid such
complexity, a concept of generic programming is introduced.
2. It saves
the programmers burden of creating separate methods for handling data belonging
to different data types.
3.It allows the code reusability..
4. Compact code can be created.
Q.17 With an example define a generic class. Or
Describe generic classes.
Ans. A
generic class contains one or more variables of generic data type. Following is
a simple example which shows how to define the generic class.
public
class Test<T>
{
public
Test(){val=null;}
public
Test(T val)
{
this.val=val;
}
public
getVal()
{
return
val;
}
public
setVal()
{
val=newValue;
}
private
T val; //variable defined as of generic type
}
Q.18 Can generic be used with inheritance in
several ways? What are they?
Ans.
Following are the ways by which generic can be used with inheritance -
•
Consider a class and a subclass such as Employee and Trainee. There are two
pair classes Pair<Employee> and Pair<Trainee> which do not possess
an inheritance relation. That is, Pair<Trainee> is not a subclass of
Pair<Employee> even if these two classes are related to each other.
• The
parameterised raw type can be converted to a raw type.
• The
generic classes can extend to implement other generic classes.
Q.19 List any two advantages of type
parameters.
Ans. :
1.Due to
the use of type parameter it saves the programmers burden of creating separate
methods for handling data belonging to different data types.
2. Due
to type parameter the early error detection at compile time occurs. This avoids
crashing of the code(due to type incompatibility) at run time.
Q.20 State any two challenges of generic
programming in virtual machine.
Ans. :
i) The
virtual machine does not work with generic classes or generic methods. Instead
it makes uses raw types in which the raw types are replaced with ordinary java
types. Each type variable is replaced with its bound or with object, if it is
not bounded. This technique is called type erasure.
Object Oriented Programming: Unit IV: I/O, Generics, String Handling : Tag: : I/O, Generics, String Handling | Object Oriented Programming - Two marks Questions with Answers
Object Oriented Programming
CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation