OOPS UNIT 4 Two Marks

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

UNIT-4

1. What is stream?
Stream is basically a channel on which the data flow from sender to receiver.
2. What is input stream and output stream?
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.
3. What is byte stream? Enlist its super classes?
The byte stream is used for inputting or outputting the bytes. The InputStream and
OutputStream are the superclass of byte stream.
4. What is character stream? Enlist its super classes?
The character stream is used for inputting or outputting the characters. The Reader and
Writer are the superclass of character stream.
5. List the byte stream classes?
The Byte Stream classes are
 FileInputStream
 FilterInputStream
 FileOutputStream
 FilterOutputStream
 PipedInputStream
 ByteArrayInputStream
 PipedOutputStream
 ByteArrayOutputStream
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.
7. Give an example of 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.

8. What is absolute file name?


The filename that can be specified with the complete path name and drive letter is called
absolute file name.
9. What is relative file name?
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");
10.What is the use of seek method?
The seek() allows you to specify the index from where the read and write operations
will start. The syntax is

void seek(long position);

11.Write a Java code to check if the command line argument is file or not?
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());

12.Enlist the two common constructors of FileInputStream?


The common constructors of FileInputStream are

FileInputStream(String filename);

FileInputStream(File fileobject);

13.What is the use of Input Stream Reader and Output Stream Writer?
The InputStreamReader converts byte into character and OutputStreamWriter
converts the characters written into byte.
14.Give an example for reading data from files using FileInput Stream?

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);

}
15.Why generic programming is required? Or What is the need for generic
programming? Or List out motivation needed in generic programming?
Following some reasons for the need of generic programming are
a) It saves the programmers burden of creating separate methods
for handling data belonging to different data types.
b) It allows the code reusability.
c) Compact code can be created.
16.With an example define a generic class. Or Describe generic classes?
A generic class contains one or more variables of generic data type. 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

}
17.Can generic be used with inheritance in several ways? What are they?
Following are the ways by which generic can be used with inheritance are
 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.
18.List any two advantages of type parameters.
 Due to the use of type parameter it saves the programmers burden of creating
separate methods for handling data belonging to different data types.
 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.
19.State any two challenges of generic programming in virtual machine?
o 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.
o In order to handle the type erasure methods the compiler has to generate bridge
methods in corresponding class.
20.Difference between String and String Buffer in Java?
21.Difference Between String, StringBuilder and StringBuffer in Java?

22. Write the Classification of java Stream Classes?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy