How To Exam?

a knowledge trading engine...


Andhra University 2007 B.E Computer Science s - Question Paper

Wednesday, 01 May 2013 05:50Web

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? What should you do instead?
Answer:

LinkedList is an attempt to use a generic class with a kind parameter of kind int, which is a primitive kind. Generic programming in Java works only for objects, and not for the primitive types, so a kind parameter of primitive kind is not allowed. However, it is possible to use the wrapper class Integer in place of int. An object of kind LinkedList can be used almost as if it were truly a list of ints.
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. Write a code segment that uses an iterator to calculate the sum of all the integer values in the collection. Write a 2nd code segment that does the identical thing using a for-each loop.
Answer:

Using an iterator:

int sum = 0;
Iterator iter = integers.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.



( 0 Votes )

Add comment


Security code
Refresh

Earning:   Approval pending.
You are here: PAPER Andhra University 2007 B.E Computer Science s - Question Paper