• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Some basic questions - part 3

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Q5)True or False
a] Default constructor is provided for all classes
b] Default constructor is provided for the class which does define any constructor.
Q6)What would be the possible output?
1.class SampleConstructor
2.{
3.public static void main(String args[])
4. {
5. Parent p=new Parent(10);
6. Child c=new Child();
7.}
8.}
9.class Parent
10.{
11. public Parent(int i)
12. {
13. System.out.println("Value of i is "+i);
14. }
15.}
16.class Child extends Parent
17.{
18. public Child()
19. {
20. System.out.println("Constructor's do not have return type");
21 }
22}
a] compiles and runs with output Constructor's do not have return type
b] compiler error in line 18 prevents compilation
c] compiler error in line 11
d] compiles and runs with output Value of i is10
I am really confused.
Thanks in advance.
Bye.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think the answerr is b
the reason being that the class Child extends Parent
The class Parent has a constructor which takes an int
You have to call the Parent constructor explicitly as the compiler will not provide a default constructor for that class
If you add the sentence
super(10);
as the first line in the Child constructor it will compile and run
thank you
------------------
Andy
 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Anand ,
I think you are right.

What about the 5th question?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi !
iam Golu.Your 5th question is very obscure.The fact is the default constructor is provided for all the classes till you
dont define the explicit constructor.
we have 2 types of the constructors:-
1.)
classname(){
//your code.
}
2.)
classname(parameters){
//your code.
}
the first one is default constructor and the second is not the default one.
If you define the second constructor in your class the
default constructor is not added by the compiler.
hope that i have helped you somehow.
GOLU_JAIN
[This message has been edited by Golu_Jain (edited January 09, 2001).]
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What about the 5th question?[/B]
5 th question is very ambiguous.
Default constructor is provided for all classes
this is true if an only if you donot define any costructors for
a class other wise it is false
Default constructor is provided for the class which does define any constructor.
If a class is defining a constructor a default constructor is NOT
provided, we have to explicitly define a default constructor depending on whether we are going to use it or not. like in Q.6 where we are required to define a default constructorin parent class.
HTH
sasi
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think the 5th question is ambiguous.
(A) is false since default construcors are not provided for all classes. Create a class and give it a constructor that takes an int and not default will be created.
(B) is true, a default constructor will be created for all classes if no other constructor is defined.
Bill
[This message has been edited by bill bozeman (edited January 09, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic