Andhra University 2007 B.E Computer Science s - Question Paper
Wednesday, 01 May 2013 05:50Web
Page 1 of 3
ques. 1:
What is meant by generic programming and what is the alternative?
Answer:
Generic programming means writing data structures and subroutines that can be used for many various kinds of data. The option would be to write a new data structure or subroutine for every various kind of data, even when they would all be essentially identical other than for the kind name. For example, a single generic sort routine can be used for sorting lists that contain data of any kind. The option is 1 routine for sorting lists of integers, 1 for sorting lists of strings, 1 for storing arrays of real numbers, and so on.
ques. 2:
Why can't you make an object of kind LinkedList
Answer:
LinkedList
ques. 3:
What is an iterator and why are iterators necessary for generic programming?
Answer:
One of the principle features of Java's generic programming framework is Collections. There are several kinds of collection (including LinkedList, ArrayList, TreeSet, and HashSet). In order to deal with all the various kinds of collection in a generic way, we need a generic way to access all the elements in a collection. An iterator makes this possible. An iterator is an object associated with a collection that makes it possible to traverse the collection (that is, to visit every of the items in the collection in turn). Code written using iterators will work for any kind of collection. (Note, however, that explicit use of an iterator can sometimes be avoided by using a for-each loop.)
ques. 4:
Suppose that integers is a variable of kind Collection
Answer:
Using an iterator:
int sum = 0;
Iterator
while ( iter.hasNext() ) {
sum += iter.next();
}
The statement "sum += iter.next()" relies on the automatic conversion from kind Integer to kind int. It could also be written "sum += iter.next().intValue()".
Using a for-each loop:
int sum = 0;
for ( int number : integers ) { // ( Could also use "Integer number : integers". )
sum += number;
}
ques. 5:
Interfaces such as List, Set, and Map describe abstract data kinds. discuss what this means.
Answer:
An abstract data kind is described by the operations that can be performed on it, not by the way the data is truly stored or by the way the operations are implemented. An interface such as List describes operations that can be performed, but says nothing about how they are to be implemented. In fact, there can be many various implementations. For example, both LinkedList and ArrayList implement the List interface. They are various "concrete" data kinds that represent the identical abstract data kind.
Earning: Approval pending. |