0% found this document useful (0 votes)
11 views10 pages

6th Cs

Uploaded by

youthfulwiles
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

6th Cs

Uploaded by

youthfulwiles
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SUMMER– 18

(d) Explain byte stream class in detail. 4M


Ans:
 It is used for creating & manipulating streams and files for reading and writing bytes.

 Since the steams are unidirectional, they can transmit bytes in only one direction and,

therefore, java provides two kinds of byte stream classes –

1. Input Stream classes

2. Output Stream classes

1. Input stream classes :


 Input stream classes are used to read 8-bit bytes include a super class known as InputStream
and a number of subclasses for supporting various input-related functions.
 The superclass InputStream is an abstract class and therefore, we cannot create instances of
this class. Rather we must use the subclasses that inherit from this class.

The InputStream includes methods that are designed to perform the following tasks:

 Reading bytes
 Closing streams
 Making position in the streams
 Finding the number of bytes in stream
 Methods of InputStream & FileInputStream class
2. Output Stream classes:
 Output Stream classes are used to write 8-bit bytes
 Output Stream classes are derived from base class OutputStream.
 Like InputStream, the OutputStream is an abstract class and therefore we cannot instantiate it.
 The several subclasses of the OutputStream can be used for performing the output operations.

The OutputStream includes methods that are designed to perform the following task:

1. Writing bytes

2. Closing streams

3. Flushing streams

Methods of InputStream & fileInputStream class :

(e) Explain Arrary list & Iterator methods of collections with an example.
Ans:
 Methods of ArrayList class :

1. void add(int index, Object element) :

a. Inserts the specified element at the specified position index in this list.
b. Throws IndexOutOfBoundsException if the specified index is out of
range (index < 0 ||index >size()).

2. boolean add(Object o) :
Appends the specified element to the end of this list.
3. boolean addAll(Collection c) :
a. Appends all of the elements in the specified collection to the end of this list, in the
order that they are returned by the specified collection's iterator.
b. ThrowsNullPointerException if the specified collection is null.

4. booleanaddAll(int index, Collection c) :


a. Inserts all of the elements in the specified collection into this list, starting at the
specified position.
b. Throws NullPointerException if the specified collection is null.

5. void clear() :
Removes all of the elements from this list.

6. Object clone():
Returns a shallow copy of this ArrayList.

7. boolean contains(Object o) :
a. Returns true if this list contains the specified element.
b. More formally, returns true if and only if this list contains at least one element e such that
(o==null ? e==null : o.equals(e))

8. void ensureCapacity(int minCapacity)


a. Increases the capacity of this ArrayList instance.
b. if necessary, to ensure that it can hold at least the number of elements specified by the
minimum capacity argument.

9. Object get(int index)


a. Returns the element at the specified position in this list.
b. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 ||
index >= size()).

10. int indexOf(Object o):


Returns the index in this list of the first occurrence of the specified element, or -1 if the List
does not contain this element.

11. int lastIndexOf(Object o):


Returns the index in this list of the last occurrence of the specified element, or -1 if the list
does not contain this element.

12. Object remove(int index):


a. Removes the element at the specified position in this list.
b. Throws IndexOutOfBoundsException if index out of range (index < 0 || index >= size()).

13. protected void removeRange(int fromIndex, int toIndex) :


Removes from this List all of the elements whose index is between fromIndex, inclusive and
toIndex, exclusive.

14. Object set(int index, Object element):


a. Replaces the element at the specified position in this list with the specified element.
b. Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 ||
index >= size()).

15. int size():


Returns the number of elements in this list.

16. Object[] toArray() :


a. Returns an array containing all of the elements in this list in the correct order.
b. Throws NullPointerException if the specified array is null.

17. Object[] toArray(Object[] a):


Returns an array containing all of the elements in this list in the correct order; the runtime type
of the returned array is that of the specified array.

18. void trimToSize():


Trims the capacity of this ArrayList instance to be the list's current size.

 Methods of Iterator class :

1. booleanhasNext( ) :
Returns true if there are more elements. Otherwise, returns false.

2. Object next( ):
Returns the next element. Throws NoSuchElementException if there is not a next element.
3. void remove( ):
a. Removes the current element.
b. Throws IllegalStateException if an attempt is made to call remove( ) that is not preceded
by a call to next( ).

(c) Write any four methods of File Input stream class give their syntax.
Ans.:
 Java File Input Stream class methods:
Summer 2017

(b) Write any four methods of file class with their use. (Note:
Any four methods may be considered)
Ans.:
1) public String getName():
Returns the name of the file or directory denoted by this abstract pathname.

2) public String getParent():


Returns the pathname string of this abstract pathname's parent, or null if this pathname does not
name a parent directory.

3)public String getPath():


Converts this abstract pathname into a pathname string.

4)public boolean isAbsolute():


Tests whether this abstract pathname is absolute.

5)public String getAbsolutePath():


Returns the absolute pathname string of this abstract pathname.

6)public boolean canRead():


Tests whether the application can read the file denoted by this abstract pathname.

7)public boolean canWrite():


Tests whether the application can modify the file denoted by this abstract pathname.

8)public boolean exists():


Tests whether the file or directory denoted by this abstract pathname exists.

9)public boolean isDirectory():


Tests whether the file denoted by this abstract pathname is a directory.
10) public boolean isFile():
Tests whether the file denoted by this abstract pathname is a normal file.

11) public boolean isHidden():


Tests whether the file named by this abstract pathname is a hidden file

12) public long lastModified():


Returns the time that the file denoted by this abstract pathname was last modified.

13) public long length()


Returns the length of the file denoted by this abstract pathname

14) public boolean createNewFile() throws IOException :


Atomically creates a new, empty file named by this abstract pathname if and only if a file with
this name does not yet exist

15) public boolean delete():


Deletes the file or directory denoted by this abstract pathname

16) public String[] list():


Returns an array of strings naming the files and directories in the directory denoted by this
abstract pathname.

17) public boolean mkdir():


Creates the directory named by this abstract pathname.

18) public boolean renameTo(File dest):


Renames the file denoted by this abstract pathname.

19) public boolean setLastModified(long time):


Sets the last-modified time of the file or directory named by this abstract pathname.

20) public boolean setReadOnly():


Marks the file or directory named by this abstract pathname so that only read operations are
allowed
21) public boolean setWritable(boolean writable, boolean ownerOnly):
Sets the owner's or everybody's write permission for this abstract pathname.

22) public boolean equals(Object obj):


Tests this abstract pathname for equality with the given object

23) public String toString():


Returns the pathname string of this abstract pathname.

(c) What is Iterator class? Give syntax and use of any two
methods of Iterator class.
Ans.:
Iterator enables you to cycle through a collection, obtaining or removing elements. Each of the
collection classes provides an iterator( ) method that returns an iterator to the start of the collection.
By using this iterator object, you can access each element in the collection, one element at a time

 Syntax :
Iterator iterator_variable = collection_object.iterator();

 Methods:
1. Boolean hasNext( ):
Returns true if there are more elements. Otherwise, returns false.

2. Object next( ):
Returns the next element. Throws NoSuchElementException if there is not a next element.

3.void remove( ):
Removes the current element. Throws IllegalStateException if an attempt is made to call remove( )
that is not preceded by a call to next( ).

You might also like