Which class has a default constructor?

Given:

class X {}
class Y {Y() {}}
class Z {z(int i ) {} }

Which class has a default constructor?

Given:

class X {}
class Y {Y() {}}
class Z {z(int i ) {} }

Which class has a default constructor?

A.
X only

B.
Y only

C.
Z only

D.
X and Y

E.
Y and Z

F.
X and Z

G.
X, Y and Z

Explanation:
No constructors are defined for the X, Y, or Z classes. All three have empty declarations. Java will create default constructors for all three classes.
Note: A java constructor has the same name as the name of the class to which it belongs. Constructor’s syntax does not include a return type, since constructors never return a value. Constructors may include parameters of various types. When the constructor is invoked using the new operator, the types must match those that are specified in the constructor definition. Java provides a default constructor which takes no arguments and performs no special actions or initializations, when no explicit constructors are provided. The only action taken by the implicit default constructor is to call the superclass constructor using the super() call. Constructor arguments provide you with a way to provide parameters for the initialization of an object.



Leave a Reply 14

Your email address will not be published. Required fields are marked *


smru

smru

I think the answer should be X only which is A.

Nur

Nur

There is a typo in this line:

class Z {z(int i ) {} }
\\ z should be capital or it should be turned to a method

if Z is capital than the answer is X,Z.

If one user-defined constructor is defined, the compiler will not add a default one.

jud

jud

I agree. I think the answer is : X,Z because the class Z defines : z(int i ) where
z is lower-case letter and the class’s name is Z.
As the class Z doesn’t define a valid constructor , the compiler add a default constructor.

hafid

hafid

Response : A
In the exam question, Z is capital : class Z {Z(int i ) {} }
Y and Z each defines a constructor, so no default constructor is generated for them

lolwut?

lolwut?

I don’t get that X,Z answer.

From what I’ve researched, the original answer provided should be the right one.

X {} // no user-defined constructors were created so a default one is created.

Y { Y(){} } // Last I checked after doing extensive research, Y() *is* a Default Constructor.

Z { Z(int i) {} } // If what hafid said is true, then it is NOT a default constructor.

lolwut?

lolwut?

And what I meant was the “typo” which has now been pointed out where z is actually Z.

Out of curiousity, I entered the original with the typo in Netbeans. After typing it in, this error was displayed : “invalid method declaration; return type required”.

When I changed the z to Z, the error was gone.

So for those of you taking the exam, if you see this problem, look at the question VERY CAREFULLY. Hafid said that in the exam(hopefully he’s speaking from actually seeing this question on the exam), the z is capitalized(Z) so if so, then ‘A’ should be your answer.

Just remember the question : Which Class has a Default Constructor?

It’s not asking which class the compiler would create default constructors in.

It’s asking which class CURRENTLY has(right now) a default constructor in it. X does(no user-defined constructors so default constructor will implicitly be put in there), Y does( Y() is a valid default constructor ), and Z does not because it does provide an argument.

(of course if lower-case z shows up on the exam, then the original answer X,Y,Z applies)

lolwut?

lolwut?

You know what?

Forget I said anything.

I’m just as confused by these typos and word-games that Oracle is playing with us…. *grrr*

lolwut?

lolwut?

I figured out why Y isn’t correct.

It is a Default constructor…but it’s user-defined(meaning that you actually typed it in) whereas in X and Z(provided there’s no typo), none was provided.

Now I see where the X,Z answer came from.

I’ll shut up now.

Jazz

Jazz

“F”

Explanation:
class X {}
class Y {Y() {}} // user Defined
class Z {z(int i ) {} }

X and Z has a default constructor

Jazz

Jazz

The default constructor is the no-argument constructor automatically generated unless you define another constructor. It initialises any uninitialised fields to their default values. For your example, it would look like this assuming that the types are String, int and int:

public Module()
{
super();
this.name = null;
this.credits = 0;
this.hours = 0;
}

This is exactly the same as

public Module()
{}

And exactly the same as having no constructors at all. However, if you define at least one constructor, the default constructor is not generated.

Reference: http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.9

Clarification:
Technically it is not the constructor (default or otherwise) that default-initialises the fields. However, I am leaving this in the answer as a) the question got the defaults wrong and b) including them or not, the constructor has exactly the same effect.

@OrangeDog

James

James

The Answer is A, assuming z(int i) {} is a typo and should be Z(int i) {}.

z(int i) {} cannot be a method, because it is missing a return type. Only if the third class declaration were

class Z { void z(int i) {} } would the answer be F, as the Java compiler would create a default constructor in this case.

Darpan Madlani

Darpan Madlani

Ans is D. X and Y

Explanation:
Because default constructor has no parameter while in class Z even if it is typo then also it is parameterized constructor.

So, X has a default constructor which will be added by compiler while executing the code and class Y has no argument constructor (Which is known as default constructor).

obrain

obrain

The Answer is A, assuming z(int i) {} is a typo and should be Z(int i) {}.