There are some java problems, need help! 1 In this declaration: ArrayList<Point>
ID: 3769362 • Letter: T
Question
There are some java problems, need help!
1 In this declaration:
ArrayList<Point> polygon;
what is the data type of the variable 'polygon'?
Select one:
a. ArrayList
b. Point
c. Array
d. ArrayList<Point>
e. None of the above
2 If this method call: myList.size()
returns the value 7, what is the range of legal values allowed for the variable x in the following method call?
myList.get( x )
Select one:
a. 0 thru 6
b. 1 thru 7
c. 0 thru 7
d. x may be any int value
3 Assume that the following code has been executed (same for Questions 7 thru 10):
What value is returned by the following expression?
plot.size()
Select one: a. 0 b. 2 c. 3 d. 4 e. 5
4 Assume that the following code has been executed (same for Questions 7 thru 10):
Which Point is returned by the following expression?
plot.get( 1 )
Select one: a. 0,0 b. 5,10 c. 4,8 d. 12,16 e. None -- the statement throws a runtime exception
5 Assume that the following code has been executed (same for Questions 7 thru 10):
Which Point is returned by the following expression?
plot.get( 2 )
Select one: a. -5,5 b. 0,0 c. 4,8 d. 12,16 e. None -- the statement throws a runtime exception
Explanation / Answer
1.
polygon is an Arraylist of type Point.
Answer: d. ArrayList<Point>
2.
the function get(x) accepts indices of the arraylist as parameter. So, possible values of x are 0-6(index starts from 0).
Answer: a. 0 thru 6
3.
ArrayList<Point> plot = new ArrayList<Point>(); // Size = 0
Point p = new Point( 12, 16 ); // declared a point object
plot.add( p ); // add the point object to the arraylist. Size = 1
plot.add( new Point( 0, 0 ) ); // add a new point object to the arraylist. Size = 2
plot.add( new Point( 4, 8 ) ); // add a new point object to the arraylist. Size = 3
plot.add( 2, new Point( 5, 10 ) ); // add a new point object to the arraylist at index 2. Size = 4
p = plot.get( 0 ); // get the first element of the arraylist.
p = plot.remove( 1 ); // remove the object at index 1. Size = 3
plot.add( new Point( -5, 5 ) ); // add a new point object to the arraylist at index 2. Size = 4
So, final size = 4.
Answer: d. 4
4.
ArrayList<Point> plot = new ArrayList<Point>();
Point p = new Point( 12, 16 );
plot.add( p ); // index 1 element: NA
plot.add( new Point( 0, 0 ) ); // index 1 element: Point(0, 0)
plot.add( new Point( 4, 8 ) ); // index 1 element: Point(0, 0)
plot.add( 2, new Point( 5, 10 ) ); // index 1 element: Point(0, 0)
p = plot.get( 0 ); // index 1 element: Point(0, 0)
p = plot.remove( 1 ); // index 1 element: Point(4, 8)
plot.add( new Point( -5, 5 ) ); // index 1 element: Point(4, 8)
So, final index 1 element = Point(4, 8)
Answer: c. 4,8