1/27
Looks like no tags are added yet.
___________ is attached to the class of the aggregating class to denote the aggregation relationship with the aggregated object.
An empty diamond
An aggregation relationship is usually represented as __________ in ___________.
a data field/the aggregating class
Composition means ______________.
that a class contains a data field that references another object
Object-oriented programming allows you to derive new classes from existing classes. This is called ____________.
inheritance
Which of the following statements are true? Please select all that apply.
"class A extends B" means A is a subclass of B., A subclass is usually extended to contain more methods and more detailed information than its superclass.
Inheritance means ______________.
that a class can extend another class
Which of the statements regarding the super keyword is incorrect?
You can use super.super.p to invoke a method in superclass's parent class.
Which of the following statements are true? Please select all that apply.
Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them.
A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.
To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass.
A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.
It is a compile error if two methods differ only in return type in the same class.
Analyze the following code. What happens when it is executed?
public class Test {
public static void main(String[] args) {
new B();
}
}
class A {
int i = 7;
public A() {
System.out.println("i from A is " + i);
}
public void setI(int i) {
this.i = 2 * i;
}
}
class B extends A {
public B() {
setI(20);
}
@Override
public void setI(int i) {
this.i = 3 * i;
}
}The constructor of class A is called and it displays "i from A is 7".
Analyze the following code. What happens when it is executed?
public class Test {
public static void main(String[] args) {
new B();
}
}
class A {
int i = 7;
public A() {
setI(20);
System.out.println("i from A is " + i);
}
public void setI(int i) {
this.i = 2 * i;
}
}
class B extends A {
public B() {
}
@Override
public void setI(int i) {
this.i = 3 * i;
}
}The constructor of class A is called and it displays "i from A is 60".
Given the following code, find the compile error. Please select all that apply.
public class Test {
public static void main(String[] args) {
m(new GraduateStudent());
m(new Student());
m(new Person());
m(new Object());
}
public static void m(Student x) {
System.out.println(x.toString());
}
}
class GraduateStudent extends Student {
}
class Student extends Person {
@Override
public String toString() {
return "Student";
}
}
class Person extends Object {
@Override
public String toString() {
return "Person";
}
}m(new Object()) causes an error,
m(new Person()) causes an error
Polymorphism means ______________.
that a variable of supertype can refer to a subtype object
Which of the following are Java keywords?
instanceof
Analyze the following code:
public class Test {
public static void main(String[] args) {
String s = new String("Welcome to Java");
Object o = s;
String d = (String)o;
}
}s, o, and d reference the same String object.
Given the following classes and their objects:
class C1 {};
class C2 extends C1 {};
class C3 extends C1 {};
C2 c2 = new C2();
C3 c3 = new C3();
Analyze the following statement:
c2 = (C2)((C1)c3);You will get a runtime error because you cannot cast objects from sibling classes.
Which of the following class definitions defines a legal abstract class?
abstract class A { abstract void unfinished(); }
Which of the following declares an abstract method in an abstract Java class?
public abstract void method();
Which of the following statements regarding abstract methods is false?
An abstract class can have instances created using the constructor of the abstract class.
Which of the following statements regarding abstract methods is false?
A data field can be declared abstract.
Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a no-arg constructor. Which of the following is correct?
A a = new B(); , B b = new B();
What is the output of running class Test?
public class Test {
public static void main(String[] args) {
new Circle();
}
}
public abstract class GeometricObject {
protected GeometricObject() {
System.out.print("A");
}
protected GeometricObject(String color, boolean filled) {
System.out.print("B");
}
}
public class Circle extends GeometricObject {
/** No-arg constructor */
public Circle() {
this(1.0);
System.out.print("C");
}
/** Construct circle with a specified radius */
public Circle(double radius) {
this(radius, "white", false);
System.out.print("D");
}
/** Construct a circle with specified radius, filled, and color */
public Circle(double radius, String color, boolean filled) {
super(color, filled);
System.out.print("E");
}
}BEDC
Which of the following is a correct interface?
interface A { void print();}
_______ is not a reference type.
A primitive type
Show the output of running the class Test in the following code lines:
interface A {
}
class C {
}
class D extends C {
}
class B extends D implements A {
}
public class Test {
public static void main(String[] args) {
B b = new B();
if (b instanceof A)
System.out.println("b is an instance of A");
if (b instanceof C)
System.out.println("b is an instance of C");
}
}b is an instance of A followed by b is an instance of C.
Which of the following statements are true? Please select all that apply.
The Date class implements Comparable.
The BigInteger class implements Comparable.
The String class implements Comparable.
The Double class implements Comparable.
Analyze the following code.
1. public class Test {
2. public static void main(String[] args) {
3. Fruit[] fruits = {new Fruit(2), new Fruit(3), new Fruit(1)};
4. java.util.Arrays.sort(fruits);
5. }
6. }
class Fruit {
private double weight;
public Fruit(double weight) {
this.weight = weight;
}
}The program has a runtime error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable.
Which of the following statements is false?
If you compile a class with errors, a .class file is created for the class.
What is the best suitable relationship between Employee and Faculty?
Inheritance