aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exam question - constructors- help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exam question - constructors- help!" Watch "Exam question - constructors- help!" New topic
Author

Exam question - constructors- help!

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
could someone please explain the following
Which statements concerning the following code are true?
class A {
public A() {}
public A(int i) { this(); }
}
class B extends A {
public boolean B(String msg) { return false; }
}
class C extends B {
private C() { super(); }
public C(String msg) { this(); }
public C(int i) {}
}
a. the code will fail to compile
b. the constructor in A that takes an int as an argument will never be called as a result of constructing an object of class B or C
c. Class C has three constructors
d.objects of class B cannot be constructed
e.at most one of the constructors of each class is called as a result of constructing an object of class C
Oliver Grass
Ranch Hand

Joined: Nov 02, 2000
Posts: 65
Hi Sarim,
i changed the code a little bit...


a. the code will fail to compile
b. the constructor in A that takes an int as an argument will never be called as a result of constructing an object of class B or C
c. Class C has three constructors
d.objects of class B cannot be constructed
e.at most one of the constructors of each class is called as a result of constructing an object of class C

Here are my suggestions
a) FALSE. Compiles without problems
b) TRUE. If you create an Object C with the Constructor in line 11, super() will be implicitly called, BUT this is default constructor of Class B, this one will call default Constructor of Class A. So A(int) will never be called.
NOTE: line 6 is NOT a constructor, it's a method, so the default constructor will be created from the compiler
c) TRUE. line 9, 10, 11 are constructor, so we have three constructors
d) FALSE. You can construct objects of Class B. Default constructor will be created from the compiler
e) FALSE. If you create an object C with C(String msg), the private constructor of C will be called. In this case, two constructors of C were used, so the answer has to be FALSE (Perhaps my english is not so good and i didn't get the meaning of the last answer, but that's my understanding)
Hope that helps,
as always, correct me if i'm wrong
cheers
Oliver
[This message has been edited by Oliver Grass (edited November 10, 2000).]
sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
thanks oliver
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exam question - constructors- help!
 
Similar Threads
questions from khalid's mock exam
Constructor problem
constructor question
constructor
Constructor in parent class