Quiz J2-2
Submit your answers via this Google Form
Question 1
Consider the classes Letter
and A
below.
abstract class Letter {
protected boolean uppercase;
abstract String get_name();
abstract int get_alphabet_position();
}
class A extends Letter{
public String toString() {
return "A";
}
protected int get_alphabet_position() {
return 1;
}
private String get_name() {
return "A";
}
}
What is the output of this program?
public class Main{
public static void main(final String args[]) {
A a = new A();
System.out.println("A: " + a.get_alphabet_position());
}
}
Question 2
What is the output of the program above if A
is implemented as shown below
class A extends Letter{
public String toString() {
return "A";
}
public int get_alphabet_position() {
return 1;
}
protected String get_name() {
return "A";
}
}
Question 3
Assume that A
is implemented such that the following program will compile and run. What is the output of the below program?
public class tmp{
public static void foo(A a){
System.out.println("foo1: "+a.get_name());
}
public static void foo(Letter a) {
System.out.println("foo2: " + a.get_name());
}
public static void main(final String args[]) {
Letter a =(Letter) new A();
foo(a);
}
}
Question 4
Consider the Dog
class below with an inner class Breed
:
public class Dog{
private String name;
public class Breed{
private String bname;
public Breed(String b){this.bname=b;};
public String toString(){return name+":"+bname;}
}
public Dog(String n){this.name=n;}
What is the output of the following code snippet?
public static void main(String args[]){
Dog d1 = new Dog("Jack");
Dog d2 = new Dog("Champ");
Dog.Breed b1 = d1.new Breed("pitbull");
Dog.Breed b2 = d1.new Breed("blacklab");
System.out.println(b1 + " " + b2);
}