Carleton University - SYSC 2004
Part 2: Debugging And Smelly Code
Final Exam Breakdown1) Read these classes and answer the questions below:
class Fizz {
public Fizz() {
System.out.println("Fizz constructor");
}
public void doThis() {
System.out.println("doThis in Fizz");
}
public void doThat() {
System.out.println("doThat in Fizz");
}
}
class Buzz extends Fizz {
public Buzz() {
super();
System.out.println("Buzz constructor");
}
public void doThis() {
super.doThis();
System.out.println("doThis in Buzz");
}
public void doOther() {
System.out.println("doOther in Buzz");
doThat();
}
}A) What will Buzz bz = new Buzz(); do?
(a) This code will not compile.
(b) This code will compile, but causes a runtime error.
(c) This code will run and displays "Buzz constructor".
(d) This code will run and displays "Fizz constructor".
(e) This code will run and displays "Buzz constructor" followed by "Fizz constructor".
(f) This code will run and displays "Fizz constructor" followed by "Buzz constructor".
(g) None of the above.
Correct answer is (f)
When we create a new instance of Buzz, its constructor runs. Inside that constructor, the first line is super();, which calls the constructor of its parent class Fizz.
So in order:
Fizz's constructor runs → prints"Fizz constructor"Then we go back to
Buzz's constructor → prints"Buzz constructor"
So the full output is:
Fizz constructor
Buzz constructorB) What will bz.doThis(); do?
Keep reading this lesson
You've reached the end of the free preview. Unlock the rest of this lesson plus 3 more lessons in Carleton University - SYSC 2004.
- Unlimited coding questions
- Unlimited access to all classroom material
- Weekly live call for questions and guidance
- 3,000 AI tutor tokens / month
- Personal SQL database (3 MB)
- Progress tracking
Cancel any time.