1.
Given:
class One {
void foo() {
}
}
class Two extends One {
// insert method here
}
Which three methods, inserted individually at line 14, will correctly complete class Two? (Choose three.)
Correct Answer(s)
B. Void foo(){/* more code here */}
C. Public void foo(){/* more code here */}
E. Protected void foo(){/* more code here */}
Explanation
The correct answer is void foo(){/* more code here */}, public void foo(){/* more code here */}, and protected void foo(){/* more code here */}. These three methods correctly complete class Two because they have the same name and return type as the foo() method in the superclass One. Additionally, they have different access modifiers (void, public, and protected) which are allowed to be less restrictive than the access modifier in the superclass.
2.
Given:
class X {
X() {
System.out.print(1);
}
X(int x) {
this();
System.out.print(2);
}
}
public class Y extends X {
Y() {
super(6);
System.out.print(3);
}
Y(int y) {
this();
System.out.println(4);
}
public static void main(String[] a) {
new Y(5);
}
}
What is the result?
Correct Answer
C. 1234
Explanation
The code creates two classes, X and Y. Class X has two constructors, one without any parameters and one with an int parameter. The constructor without parameters prints 1. The constructor with the int parameter calls the constructor without parameters using "this()" and then prints 2. Class Y extends class X and has two constructors, one without any parameters and one with an int parameter. The constructor without parameters calls the constructor in class X with the int parameter using "super(6)", which prints 1 and 2. It then prints 3. The constructor with the int parameter calls the constructor without parameters using "this()", which prints 1 and 2. It then prints 4. In the main method, a new object of class Y is created with the int parameter 5. Therefore, the output is 1234.
3.
Given:
1. public class A{
2. public void doit(){
3. }
4. public String doit(){
5. return "a";
6. }
7. public double doit(int x){
8. return 1.0;
9. }
10.}
What is the result?
Correct Answer
C. Compilation fails because of an error in line 4.
Explanation
The compilation fails because of an error in line 4. This is because the method "doit()" in line 4 has the same signature (same name and same return type) as the method "doit()" in line 2. In Java, methods are differentiated based on their signature, which includes the method name and the parameters. Since both methods have the same signature, the compiler cannot determine which method to call and therefore throws a compilation error.
4.
Given:
public class SimpleCalc {
public int value;
public void calculate() {
value += 7;
}
}
And:
public class MultiCalc extends SimpleCalc {
public void calculate() {
value -= 3;
}
public void calculate(int multiplier) {
calculate();
super.calculate();
value *= multiplier;
}
public static void main(String[] args) {
MultiCalc calculator = new MultiCalc();
calculator.calculate(2);
System.out.println("Value is: " + calculator.value);
}
}
What is the result?
Correct Answer
A. Value is: 8
Explanation
The result is "Value is: 8" because the calculate() method in the MultiCalc class subtracts 3 from the value, then the calculate(int multiplier) method calls the calculate() method, adds 7 to the value using the super.calculate() method from the parent class, and finally multiplies the value by the multiplier. In the main method, the calculate(2) method is called, so the value is initially 0, then it becomes -3, then it becomes 4 (after adding 7), and finally it becomes 8 (after multiplying by 2).
5.
Given:
public class Base {
public static final String FOO = "foo";
public static void main(String[] args) {
Base b = new Base();
Sub s = new Sub();
System.out.print(Base.FOO);
System.out.print(Sub.FOO);
System.out.print(b.FOO);
System.out.print(s.FOO);
System.out.print(((Base) s).FOO);
}
}
class Sub extends Base {
public static final String FOO = "bar";
}
What is the result?
Correct Answer
D. Foobarfoobarfoo
Explanation
The correct answer is "foobarfoobarfoo" because when the code is executed, the first print statement "System.out.print(Base.FOO)" prints "foo" because it is accessing the static variable FOO from the Base class. The second print statement "System.out.print(Sub.FOO)" prints "bar" because it is accessing the static variable FOO from the Sub class. The third print statement "System.out.print(b.FOO)" also prints "foo" because it is accessing the static variable FOO from the Base class using an instance of the Base class. The fourth print statement "System.out.print(s.FOO)" prints "bar" because it is accessing the static variable FOO from the Sub class using an instance of the Sub class. The fifth print statement "System.out.print(((Base) s).FOO)" prints "foo" because it is accessing the static variable FOO from the Base class using a reference of the Base class that points to an object of the Sub class.
6.
Given:
class Mammal {
}
class Raccoon extends Mammal {
Mammal m = new Mammal();
}
class BabyRaccoon extends Mammal {
}
Which four statments are true? (Choose four.)
Correct Answer(s)
A. Raccoon is-a Mammal.
B. Raccoon has-a Mammal.
C. BabyRaccoon is-a Mammal.
F. BabyRaccoon is-a BabyRaccoon.
Explanation
The first statement is true because Raccoon is a subclass of Mammal, indicating that Raccoon is a type of Mammal. The second statement is also true because Raccoon has a Mammal instance variable. The third statement is true because BabyRaccoon is a subclass of Mammal, indicating that BabyRaccoon is a type of Mammal. The fourth statement is true because BabyRaccoon is a subclass of Raccoon, indicating that BabyRaccoon is a type of Raccoon.
7.
Which four statements are true? (Choose four.)
Correct Answer(s)
C. Has-a relationships can be implemented using instance variables.
D. Is-a relationships can be implemented using the extends keyword.
E. Is-a relationships can be implemented using the implements keyword.
G. An array or a collection can be used to implement a one-to-many has-a relationship.
Explanation
- Has-a relationships can be implemented using instance variables, as an object can have another object as a member variable.
- Is-a relationships can be implemented using the extends keyword, as a class can inherit from another class.
- Is-a relationships can also be implemented using the implements keyword, as a class can implement an interface.
- An array or a collection can be used to implement a one-to-many has-a relationship, as multiple objects can be stored in an array or collection.
8.
Given:
public class Hello {
String title;
int value;
public Hello() {
title += " World";
}
public Hello(int value) {
this.value = value;
title = "Hello";
Hello();
}
}
And:
Hello c = new Hello(5);
System.out.print(c.title);
What is the result?
Correct Answer
C. Compilation fails.
Explanation
The code fails to compile because there is a syntax error in the constructor. The line "Hello();" is not a valid statement. It seems like the intention was to call the default constructor within the parameterized constructor, but the correct syntax for calling a constructor is "this()".
9.
Given:
public class ItemTest {
private final int id;
public ItemTest(int id) {
this.id = id;
}
public void updateId(int newId) {
id = newId;
}
public static void main(String[] args) {
ItemTest fa = new ItemTest(42);
fa.updateId(69);
System.out.println(fa.id);
}
}
Which four statments are true?
Correct Answer
A. Compilation fails.
Explanation
The compilation fails because the variable "id" is declared as final, meaning it cannot be reassigned a new value once it has been initialized in the constructor. Therefore, the statement "id = newId;" in the "updateId" method causes a compilation error.
10.
Given:
class Foo {
private int x;
public Foo(int x) {
this.x = x;
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
}
public class Gamma {
static Foo fooBar(Foo foo) {
foo = new Foo(100);
return foo;
}
public static void main(String[] args) {
Foo foo = new Foo(300);
System.out.print(foo.getX() + "-");
Foo fooFoo = fooBar(foo);
System.out.print(foo.getX() + "-");
System.out.print(fooFoo.getX() + "-");
foo = fooBar(fooFoo);
System.out.print(foo.getX() + "-");
System.out.print(fooFoo.getX());
}
}
What is the output?
Correct Answer
B. 300-300-100-100-100
Explanation
The output is 300-300-100-100-100.
In the main method, a new instance of the Foo class is created with x = 300. The getX() method is called on this instance, returning 300, which is printed.
Then, the fooBar() method is called with the foo instance as an argument. Inside fooBar(), a new instance of Foo is created with x = 100, and this new instance is returned.
Back in the main method, the getX() method is called on the original foo instance, which still has x = 300, returning 300, which is printed.
Then, the getX() method is called on the fooFoo instance, which was assigned the returned value from fooBar(). Since fooFoo is now referencing the new Foo instance with x = 100, 100 is returned and printed.
Finally, the foo instance is assigned the returned value from calling fooBar() with fooFoo as an argument. This means foo is now referencing the new Foo instance with x = 100. The getX() method is called on foo, returning 100, which is printed.
The getX() method is called on fooFoo again, returning 100, which is printed.