aspose file tools*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes Another badly worded (or wrong) SCJA Mock  question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "Another badly worded (or wrong) SCJA Mock  question" Watch "Another badly worded (or wrong) SCJA Mock  question" New topic
Author

Another badly worded (or wrong) SCJA Mock question

patrick avery
Ranch Hand

Joined: Sep 12, 2008
Posts: 46
I ran across an SCJA mock exam question where the code showed an explicitly coded no-arg constructor, i.e.

public class SomeClass {
private int xx;
SomeClass() {}
}

The question asked if this class contained a default constructor to which I answered 'no' since the explicitly declared no-arg constructor should prevent the compiler from inserting a default constructor.

The Mock quiz said the answer is yes. Sounds like they are saying that any no-arg constructor is considered a "default " constructor, even if it was not inserted by the compiler???

Thanks, Pat


SCJA 96%
SCJP 6 88%
skipping SCJD to work on passing SCWCD
Kah Tang
Ranch Hand

Joined: Sep 10, 2007
Posts: 58
I think there is a misunderstanding of what a default constructor is. Please check out: http://en.wikipedia.org/wiki/Default_constructor
patrick avery
Ranch Hand

Joined: Sep 12, 2008
Posts: 46
Thanks, Kah

Your Wikipedia entry seems to confirm the mock test's definition that a 'default constructor' means a 'no-arg constructor' [regardless of who supplies it].

My interpretation, which seems to be wrong, was that 'default constructor' was equaliviant to 'compiler supplied no-arg constructor'.

Thanks for clearing this up before my real test!!

Pat Avery
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Yes, thats interesting, I was the same as Pat, I thought that a default constructor was that explicitly provided by the JVM, and that NON-Default was ano other coded by the programmer. BUt its great that is cleared up!


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
patrick avery
Ranch Hand

Joined: Sep 12, 2008
Posts: 46
I noticed that when mentioning questions from Mock Exams the policy is that we are supposed to quote the source to insure that the question is not from the real exam...

"I ran across an SCJA mock exam question where the code showed an explicitly coded no-arg constructor, i.e."

I was referring to a question 3-16 from the SCJA.de ebook.

Thanks, Pat Avery
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
After reading a little more, I'm of the opinion that the answer that the exam writers gave is strange as, in my understanding, it doesn't nescessarily follow that a default constructor is just a no arg constructor. If the programmer writes a no-arg constructor, then the jvm will NOT insert one (infact the JVM refrains from inserting one even if there are overloaded Constructors taking arguments). The jvm will however, insert a call to super() as the first argument in a constructor, if the constructor dosen't provide one already.Constructors by default MUST have either a call to super() or to this(). After reading the Wikipedia entry,Im still not convinced that the example contains a default constructor, I am sceptical of the specific Wikipedia entry as it is also describing C++, which leaves room for confusion. I agree with Patrick's first answer, I think the best thing to do is to take a look at Sun's java pages and see the definition there:

Sun Java Tutorials (Constructors)

There is also a great section on constructors in Katherine Sierra and Bert Bates Book

"SCJP Sun Certified Programmer for Java 6 Study Guide (CX-310-065): Exam 310-065"


Steve


[ September 29, 2008: Message edited by: Stephen Davies ]
Absolut Hattusas
Greenhorn

Joined: Oct 04, 2008
Posts: 5
If the programmer writes a no-arg constructor, then the jvm will NOT insert one.

FALSE.

It will insert one!

public class Hattusas {
private int field1 = 0;
}

is compiled as :

public class Hattusas {
private int field1 = 0;
public Hattusas(){
super();
}
}

For this reason we should get used to override our default constructor.

Since JVM creates a default constructor even if we have to , the question's answer is YES.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Originally posted by Absolut Hattusas:
If the programmer writes a no-arg constructor, then the jvm will NOT insert one.

FALSE.

It will insert one!

public class Hattusas {
private int field1 = 0;
}

is compiled as :

public class Hattusas {
private int field1 = 0;
public Hattusas(){
super();
}
}

For this reason we should get used to override our default constructor.

Since JVM creates a default constructor even if we have to , the question's answer is YES.


Well in your example you have not provided a no-arg constructor so the JVM will provide a default constructor in this case. Look at what you have written:



You have no constructor (no-arg or otherwise here) so of course the JVM will provide the following implicitly:



If you had written:



A Default no-arg constructor will ONLY be provided if the Program writer has not explicitly provided any Constructor (not just a noarg). What the JVM will implicitly provide if absent is a call to super() as the first line of the Constructor (whoever created it). So my statement was not FALSE. Take a look at some of the references to constructors given above.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
PS: Please use code tags, it makes things a little easier to read.
[ October 26, 2008: Message edited by: Stephen Davies ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
First off, let me say that writing exam questions and mock questions is HARD
I've written tons of both kinds and it's hard.

For sure you need to be aware that mock questions should NEVER be viewed as absolutely perfect. They should always be viewed with some healthy suspicion

So, it's not unusual to find errors in mock questions - when you find one it means you're learning. That's a good thing! I would recommend that when you have a doubt about a mock exam question, post it here at the ranch and have a discussion - just like this thread. So, if you buy 200 mock questions in a book or some software, expect that you might find a few errors, it's no big deal, and it doesn't mean the source of the questions isn't good.

Also, in this case I would say that this particular question isn't terribly well written - but it's no big deal, it's an opportunity to discuss Java topics!

hth,

Bert

p.s. By the way - I didn't write this one


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another badly worded (or wrong) SCJA Mock question
 
Similar Threads
default constructor?????
Constructor
HF EJB Book - Session Bean Class Constructor Clarification required
constructor
JavaBean Standards and default constructor