Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes of a default constructor question required 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 "of a default constructor question required" Watch "of a default constructor question required" New topic
Author

of a default constructor question required

Java Lea
Greenhorn

Joined: Sep 26, 2004
Posts: 1
Which two statements are true regarding the creation of a default constructor? (Choose
Two)
A. The default constructor initializes method variables.
B. The compiler always creates a default constructor for every class.
C. The default constructor invokes the no-parameter constructor of the superclass.
D. The default constructor initializes the instance variables declared in the class.
E. When a class has only constructors with parameters, the compiler does not create
a default constructor.
Answer: D, E
I believe the right answer is :C,E.
Do u follow me ?
Thanks !!!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Java Lea,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

We shy against obviously fictitious names.

Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I'd pick C, D, and E.

C. The first thing any constructor does (excepting whatever magic occurs by the constructor in the Object class) is to invoke another constructor - either another constructor in the same class, or a parent class constructor. If the programmer doesn't explicitly invoke another constructor as the very first thing the constructor does, then the compiler steps in and makes an invocation of the parent class no argument constructor. In the case of the default constructor, which is provided by the compiler when no constructor has been explicitly defined by the programmer, this default constructor invokes the super class no argument constructor as the very first thing it does.

D. It might initialize them to null, zero and false, but it still initializes them.

E. The compiler doesn't create a no argument default constructor if any constructor has been explicitly defined by the programmer.
Andy Chang
Greenhorn

Joined: Mar 24, 2004
Posts: 5
I'd be agree with you, Java
I'd pick C, E

D is not correct. Why? The Java language has three mechanisms dedicated to ensuring proper initialization of objects: instance initializers (also called instance initialization blocks), instance variable initializers, and constructors. It is initializers doing that, not default constructor initializing the instance variables declared in the class.

Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
and why not option B here..

I was reading "one always gets a default zero parameter constructor if one does not create any of his/her own"
the compiler always inserts this invisible zero parameter constructor "behind the scenes".
Andy Chang
Greenhorn

Joined: Mar 24, 2004
Posts: 5
B is absolutely not correct.
Java doesn't always create a default constructor for you.
It will create one as long as you didn't have any constructors in your class. So......B is wrong.............

k duffy
Greenhorn

Joined: Aug 29, 2004
Posts: 11
D is wrong.

In "The Java Programming Language Third Edition" pg 44 it says "Constructors are invoked after the instance variable of a newly created object of the class have been assigned their default values and after their explicit initializers are executed"..
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
Which two statements are true regarding the creation of a default constructor? (Choose
Two)
A. The default constructor initializes method variables.
B. The compiler always creates a default constructor for every class.
C. The default constructor invokes the no-parameter constructor of the superclass.
D. The default constructor initializes the instance variables declared in the class.
E. When a class has only constructors with parameters, the compiler does not create
a default constructor.
Answer: D, E
I believe the right answer is :C,E.
Do u follow me ?
Thanks !!!


The way that I think about the questions is "Is the answer to option <A-E> ALWAYS correct?" Following that advice here is my 2 cents:

A. The default constructor initializes method variables.
absolutely wrong. The constructor is called when the class is be instatiated.

B. The compiler always creates a default constructor for every class.
If no constructor is defined for the class, then the compiler will create a default no arg constructor.

C. The default constructor invokes the no-parameter constructor of the superclass.
This is only partially true. If you DO NOT reference this.<something> in your constructor then this statement becomes true. However, if you reference this.<constructor method> or super.<constructor method> then this statement is not true.
Using the example code below, you will see that SubClass c = new Subclass(22); calls the default no arg constructor from Base(), which sets the name to Fred. However, in SubClass b = new SubClass("John", 22); we directly reference a specific super constructor to use to initialize this instance.

D. The default constructor initializes the instance variables declared in the class.
Partially True. Some variables may not be initialized if enough information is not provide upon instatiation. Lets add the string attribute HairColour to the class below. The default value will be null until we address the value of the attribute in a constructor or a get/set pair of methods. (eg, pulic String getHairColour() { return this.oHairColour; } and public void setHairColour(String sHairColour) { this.oHairColour = sHairColour; } )

E. When a class has only constructors with parameters, the compiler does not create a default constructor
Let's refine this statement. When a class has 1 or more constructors defined, the compiler will not create a default or implicit constructor.

My answer(s) to this question would be D and E.

Hope this helps.



SCJP 1.4 SCWCD 1.4
Marcus Cain
Greenhorn

Joined: Sep 27, 2004
Posts: 1
I believe the answer to be C, E.
C will always be True. Colin in your example you have created Constructors and therefore the Default Constructor does not exist, as you state in your answer to E.
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
Originally posted by Marcus Cain:
I believe the answer to be C, E.
C will always be True. Colin in your example you have created Constructors and therefore the Default Constructor does not exist, as you state in your answer to E.


agreed Colin in C your assuming you have a constructor already. The question is talking about the default constructor.

C and E are correct.

no way D is correct unless you explicitly tell the constructor to do anything it doesnt. Since the default constructor is not specified by the user it doesnt do anything other than call the super() constructor if one exists

the java spec says so

# If the class being declared is the primordial class Object, then the default constructor has an empty body.
# Otherwise, the default constructor takes no parameters and simply invokes the superclass constructor with no arguments.




java spec
[ September 27, 2004: Message edited by: Inuka Vincit ]

MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
Sim Kim
Ranch Hand

Joined: Aug 06, 2004
Posts: 268
Hey all,

what do we think will be the answer to this question ?

C D
D E
C E
C D E
Peter Laurinec
Ranch Hand

Joined: Sep 13, 2004
Posts: 52
Well is was said many time here before so I will no repeat the agruments already said.
Correct is: C, E


SCJP 1.5 (done, at last)<br /> <br />"If everything seems to be going well you have obviously overlooked something."
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: of a default constructor question required
 
Similar Threads
a question about default constructor
a question about default constructor
Default constructor ?
"default" constructor
question about default constructor