Answered step by step
Verified Expert Solution
Question
1 Approved Answer
QUESTION 5 Consider the following Java code. class MyTest { public static void main(String[] args) { SubClass s = new SubClass(1); } } class SuperClass{
QUESTION 5
- Consider the following Java code. class MyTest { public static void main(String[] args) { SubClass s = new SubClass(1); } } class SuperClass{ private int superClassField; public SuperClass(int f){ superClassField = f + 1; System.out.println("SuperClass constructor, field = " + superClassField); } } class SubClass extends SuperClass{ public SubClass(int f){ super(f); System.out.println("SubClass constructor, field = " + f); } } What output will be produced?
A. | SuperClass constructor, field = 2 SubClass constructor, field = 1 |
B. | SuperClass constructor, field = 1 SubClass constructor, field = 1 |
C. | SuperClass constructor, field = 2 SubClass constructor, field = 2 |
D. | SuperClass constructor, field = 1 SubClass constructor, field = 2 |
QUESTION 6
- Consider the following Java code. class MyTest { public static void main(String[] args) { print(new Object()); print(new SuperClass()); print(new SubClass()); print(new SubSubClass()); } public static void print(SubClass s){ System.out.println(s.toString()); } } class SuperClass{ public String toString(){ return "Superclass"; } } class SubClass extends SuperClass{ public String toString(){ return "Subclass"; } } class SubSubClass extends SubClass{ } Which (if any) lines of code will produce a compile time error (try to avoid copying and pasting the code!).
A. | print(new SuperClass()); only. |
B. | print(new Object()); and print(new SuperClass()); |
C. | print(new Object()); only. |
D. | The code will not produce a compile time error. |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started