[Logo]
Forums Register Login
of a default constructor question required
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 !!!
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!
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.
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.

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".
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.............

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"..
 

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.

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.
 

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 ]
Hey all,

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

C D
D E
C E
C D E
Well is was said many time here before so I will no repeat the agruments already said.
Correct is: C, E
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 1113 times.

All times above are in ranch (not your local) time.
The current ranch time is
May 25, 2018 02:53:45.