QUESTION 1
Following is a declaration for a class that will be used to represent points in the x-y-coordinate plane.
public class Point
{
// coordinates
private int x;
private int y;
public Point()
{
x = 0;
y = 0;
}
public Point(int a, int b)
{
x = a;
y = b;
}
// ... assume additional methods not shown
}
Now consider extending this class to give the points a name. This new class is:
public class NamedPoint extends Point
{
private String pointName;
// possible constructors go here
// possible other methods go here
Below are three proposed constructors for the NamedPoint class.
I) public NamedPoint()
{
super();
pointName = "";
}
II) public NamedPoint( int x1, int y1, String name)
{
x = x1;
y = y1;
pointName = name;
}
III) public NamedPoint(int x1, int y1, String name)
{
super(x1, y1);
pointName = name;
}
Which of these constructors would be legal for the NamedPoint class?
a. II only
b. III only
c. I and III
d. I only
e. II and III
f. I and II
g. I and II and III
QUESTION 6
What is the output of this program?
Assuming the class LinkedList is given and provides the following methods defnitions.
boolean add(Object o)
Appends the specified element to the end of this list.
void addFirst(Object o)
Inserts the given element at the beginning of this list.
Object removeFirst()
Removes and returns the first element from this list. Throws NoSuchElementException if this list is empty.
int size()
Returns the number of elements in this list
import java.util.*;
class List
{
public static void main(String args[])
{
LinkedList obj = new LinkedList();
obj.add("A");
obj.add("B");
obj.add("C");
obj.removeFirst();
obj.addFirst("D");
System.out.println(obj + " " + obj.size());
}
}
a. DABC 3
b. [A, B, C] 3
c. ABC 2
d. [D, B, C] 3
e. [A, B, C] 2