• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

mock question

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will happen if you compile/run the following code?

1: public class Q21
2: {
3: int maxElements;
4:
5: void Q21()
6: {
7: maxElements = 100;
8: System.out.println(maxElements);
9: }
10:
11: Q21(int i)
12: {
13: maxElements = i;
14: System.out.println(maxElements);
15: }
16:
17: public static void main(String[] args)
18: {
19: Q21 a = new Q21();
20: Q21 b = new Q21(999);
21: }
22: }
A) Prints 100 and 999.
B) Prints 999 and 100.
C) Compilation error at line 3, variable maxElements was not initialized.
D) Compillation error at line 19.
The correct answer is D
Could someone explain why.
thanks in adv
amisha
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amisha,
The Correct answer is D. Compilation Error at Line 19.
This is because there is no default constructor in class Q21. So when u r trying to instantiate an object a at line 19 of class Q21, the compiler will complain regarding no matching constructor found in Class Q21.
Default constructors are provided only if u do not define any other constructors in u're class. The other thing which u must have been confused is the method which is declared at line 5. It is perfectly legal to declare a method as the same name as the class. It is not a constructor. I hope u understand.
All the best.
Amit
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
ans constructors dont have the return type void
please remove the void from the default constructor Q21()
now it will run
 
Amit Punjwani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bala,
I think u r mistaken by line 5. The declaration at line 5 is not a constructor but it is a method. As I said earlier it is perfectly legal to define a method with the same name as the class name.
If u want the above code to run just define a no-argument constructor
<pre>
public class Q21
{
int maxElements;
Q21() {
System.out.println("No argument constructor");
}
void Q21()
{
maxElements = 100;
System.out.println(maxElements);
}

Q21(int i)
{
maxElements = i;
System.out.println(maxElements);
}
public static void main(String[] args)
{
Q21 a = new Q21();
Q21 b = new Q21(999);
}
}
</pre>
The code will now compile fine
Any problems will be pleased to solve.

------------------
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
A Patel
The constructor doesn't have any return type.Constructor can be only public,private,protected and can throw an exception.The name of constructor is same as name of class and doesn't have any return type.
Hope this things cleared your dought.
I'm SCJP now if you need help mail me on this mail id sanjaypts@yahoo.com

Thanx
sp
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic