Exam 03:
Constructor Chaining and Other Java Rules
Answer the following questions.
For each question:
- start by anticipating the answer
- run the code, see what the answer is
- explain and compare with your expectations
Put your answers in a .txt file and upload it to OnCourse before the end of the week.
Our hope for this week's lab is that you can do this in lab, before class is over.
Lab instructors will try to guide you through this, in class.
All questions should be discussed in lab. Here they are now:
- You attempt to compile and run this code:
class Alpha {
public static void main(String[] args) {
Beta f = new Beta();
}
Alpha(int i) { }
}
class Beta extends Alpha { }
What is the outcome?
Possible answers:
(a) The code does not compile because Beta does not define a no-args constructor.
(b) The code does not compile because Beta does not define any constructors whatsoever.
(c) The code compiles and runs succesfully, with no output.
(d) The code does not compile because Alpha doesn't define a no-args constructor.
(e) None of the above.
- You attempt to compile and run this code:
class Alpha {
public static void main(String[] args) {
Beta f = new Beta(3);
}
Alpha (int i) { }
}
class Beta extends Alpha {
Beta(int i) {
}
} What is the outcome?
- You attempt to compile and run this code:
class Alpha {
public static void main(String[] args) {
Beta f = new Beta(3);
}
Alpha() { System.out.println(0); }
Alpha(int i) {
System.out.println(i);
}
}
class Beta extends Alpha {
Beta() { }
Beta(int i) { }
} What is the outcome?
- You attempt to compile and run this code:
class Alpha {
public static void main(String[] args) {
Beta f = new Beta(3);
}
Alpha() {
System.out.println(0);
}
Alpha(int i) {
System.out.println(i);
}
}
class Beta extends Alpha {
Beta() {
super(6);
}
Beta(int i) {
this();
}
} What is the outcome?
- You attempt to compile and run this code:
class Alpha {
public static void main(String[] args) {
Beta f = new Beta(3);
}
Alpha() {
System.out.println(0);
}
Alpha(int i) {
System.out.println(i);
}
}
class Beta extends Alpha {
Beta() {
super(6);
}
Beta(int i) {
super(3);
this();
}
} What is the outcome?
- You attempt to compile and run this code:
class Alpha {
public static void main(String[] args) {
Beta f = new Beta(12);
}
Alpha() {
System.out.print(0);
}
Alpha(int i) {
System.out.print(i);
}
}
class Beta extends Alpha {
Beta() {
}
Beta(int i) {
this();
System.out.print(3);
}
} What is the outcome?
- You attempt to compile and run this code:
class Alpha {
public static void main(String[] args) {
Alpha f = new Beta();
System.out.println(f.test(3));
}
String test(int i) {
return (i + 2) + " ";
}
}
class Beta extends Alpha {
String test(int i) {
return (i + 1) + " ";
}
String test(long i) {
return i + " ";
}
} What is the outcome?
- You attempt to compile and run this code:
class Alpha {
public static void main(String[] args) {
Beta f = new Alpha();
System.out.println(f.test(3));
}
String test(int i) {
return (i + 2) + " ";
}
}
class Beta extends Alpha {
String test(int i) {
return (i + 1) + " ";
}
} What is the outcome?
- You attempt to compile and run this code:
class Alpha {
public static void main(String[] args) {
Alpha f = new Alpha();
System.out.println(f.test(3));
}
String test(int i) {
return (i + 2) + " ";
}
}
class Beta extends Alpha {
String test(int i) {
return (i + 1) + " ";
}
} What is the outcome?
- You attempt to compile and run this code:
class Alpha {
public static void main(String[] args) {
Beta f = new Beta();
System.out.println(test(3));
}
String test(int i) {
return (i + 2) + " ";
}
}
class Beta extends Alpha {
String test(int i) {
return (i + 1) + " ";
}
} What is the outcome?
- You attempt to compile and run this code:
abstract class Alpha {
int value;
Alpha (int value) {
this.value = value;
System.out.println(value);
}
public static void main(String[] args) {
Beta f = new Beta(2011);
}
}
class Beta extends Alpha {
Beta (int value) { super(value); }
} What is the outcome?
- You attempt to compile and run this code:
class Alpha {
public static void main(String[] args) {
System.out.println("... won't compile");
}
public static void main() {
System.out.println("... will not run");
}
} What is the outcome?
- You attempt to compile and run this code:
abstract class Alpha {
abstract void complain();
}
class Beta extends Alpha {
void complain(String s) {
System.out.println(s);
}
}
class Tester {
public static void main(String[] args) {
Beta f = new Beta();
f.complain("There's a tomato in every automaton.");
}
} What is the outcome?
- You attempt to compile and run this code:
class Alpha {
String message;
Alpha (String msg) { message = msg; }
}
class Beta extends Alpha {
Beta (String msg) { message = msg; }
}
class Tester {
public static void main(String[] args) {
Beta f = new Beta("Greetings");
System.out.println(f.message);
}
} What is the outcome?
- You attempt to compile and run this code:
class Vegetable { }
class Cabbage extends Vegetable { }
class Kohlrabi extends Cabbage { }
class Kroger {
public static void main(String[] args) {
Cabbage a = new Kohlrabi();
}
} What is the outcome?
- You attempt to compile and run this code:
class Vegetable { }
class Cabbage extends Vegetable { }
class Kohlrabi extends Cabbage { }
class Kroger {
public static void main(String[] args) {
Kohlrabi a = new Vegetable();
}
} What is the outcome?
Updated by © Adrian German for C212/A592 - 6W2 Summer 2018
|