Questions
Question 1 Multiple Choice (Single Answer)
Which statement is true for the following code?
public class Plant {
private String name;
public Plant(String name) { this.name = name; }
public String getName() { return name; }
}
public class Tree extends Plant {
public void growFruit() { }
public void dropLeaves() { }
}
- The code will compile without changes.
- The code will compile if public Tree() { Plant(); } is added to the Tree class.
- The code will compile if public Plant() { Tree(); } is added to the Plant class.
- The code will compile if public Plant() { this("fern"); } is added to the Plant class.
Question 2 Multiple Choice (Single Answer)
What will the result for following code?
public class Yikes {
public static void go(Long n) {System.out.println("Long ");}
public static void go(Short n) {System.out.println("Short ");}
public static void go(int n) {System.out.println("int ");}
public static void main(String [] args) {
short y = 6;
long z = 7;
go(y);
go(z);
}
}
- int Long
- Short Long
- Compilation fails.
- An exception is thrown at runtime.
Question 3 Multiple Choice (Multiple Answers)
Which two statements are true about has-a and is-a relationships?
- Inheritance represents an is-a relationship.
- Inheritance represents a has-a relationship.
- Interfaces must be used when creating a has-a relationship.
- Instance variables can be used when creating a has-a relationship.
Question 4 Multiple Choice (Multiple Answers)
Which two, independently, will allow Sub to compile?
1. class Super {
2. private int a;
3. protected Super(int a) { this.a = a; }
4. }
...
11. class Sub extends Super {
12. public Sub(int a) { super(a); }
13. public Sub() { this.a = 5; }
14. }
- Change line 2 to: public int a;
- Change line 2 to :protected int a;
- Change line 13 to :public Sub() { this(5); }
- Change line 13 to :public Sub() { super(5); }
Question 5 Multiple Choice (Single Answer)
What will be the output?
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";
}
- foofoofoofoofoo
- foobarfoobarbar
- foobarfoofoofoo
- foobarfoobarfoo
- barbarbarbarbar
Question 6 Multiple Choice (Multiple Answers)
Which two statements are true?
- An encapsulated, public class promotes re-use.
- Classes that share the same interface are always tightly encapsulated.
- An encapsulated class allows subclasses to overload methods, but does NOT allow overriding methods.
- An encapsulated class allows a programmer to change an implementation without affecting outside code.
Question 7 Multiple Choice (Single Answer)
What is the output?
public class Wow {
public static void go(short n) {System.out.println("short");}
public static void go(Short n) {System.out.println("SHORT");}
public static void go(Long n) {System.out.println(" LONG");}
public static void main(String [] args) {
Short y = 6;
int z = 7;
go(y);
go(z);
}
}
- short LONG
- SHORT LONG
- Compilation fails.
- An exception is thrown at runtime.
Question 8 Multiple Choice (Single Answer)
Which statement is true
public class CreditCard {
private String cardID;
private Integer limit;
public String ownerName;
public void setCardInformation(String cardID,
String ownerName,
Integer limit) {
this.cardID = cardID;
this.ownerName = ownerName;
this.limit = limit;
}
}
- The class is fully encapsulated.
- The code demonstrates polymorphism.
- The ownerName variable breaks encapsulation.
- The cardID and limit variables break polymorphism.
- The setCardInformation method breaks encapsulation.
Question 9 Multiple Choice (Single Answer)
What is the output?
interface TestA { String toString(); }
public class Test {
public static void main(String[] args) {
System.out.println(new TestA() {
public String toString() { return "test"; }
});
}
}
- test
- null
- Compilation fails because of an error in line 1.
- An exception is thrown at runtime.
Question 10 Multiple Choice (Multiple Answers)
Which three methods, inserted individually at line 14, will correctly complete class Two?
class One {
void foo() { }
}
class Two extends One {
//insert method here
}
- int foo() { /* more code here */ }
- void foo() { /* more code here */ }
- public void foo() { /* more code here */ }
- private void foo() { /* more code here */ }
- protected void foo() { /* more code here */ }