wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor 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 "constructor" Watch "constructor" New topic
Author

constructor

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477


What will happen if you try to compile and run above program?

.a It will compile successfully. It will output "Created Parent" and then "Creating child"

b. It will compile successfully. It will output "Creating child" and then "Created Parent"

c. It will not compile giving warning, "Explicit constructor invocation must be first statement in constructor."

d. It will not compile giving warning, "Expression is not a valid block statement."

Answers are c and d.I know c is the correct answer and what is the meaning of option d.i don't get the meaning?
please help on this.
Line 1 and line2 will give compile error since Superclass didn't have no-arg constructor.But which Expression is not a valid block statement.
marcos rodrigues
Greenhorn

Joined: Mar 08, 2007
Posts: 7
d
public Child(int x, int y){//line1

}

dont have super();

dont have a default constructor


public Child(int x, int y, int z){//line2
System.out.println("Creating child");
this(x,y);
}

this(x,y) need going first.


a good code its here




second class


output
Created Parent
Creating child
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I've tried a little Google for fun, and found the following:
http://www.javabeat.net/javabeat/scjp2/mocks/scjp_1_4_mock_exam_questions_7.php

Question 12, same as yours. And only one answer, the third one


[My Blog]
All roads lead to JavaRanch
Bunty Naagar
Greenhorn

Joined: Feb 27, 2007
Posts: 14
If you compile this, you will find only option (c) is the correct answer. Also you get following errors , but that is not mentioned in any options. This error is because of absence of a no argument constructor for parent & the fact that child constructor does not calls the super constructor with arguments

cannot find symbol
symbol : constructor Parent()
location: class Parent
[ March 09, 2007: Message edited by: Bunty Naagar ]

SCJP 5, SCWCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor