Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

some doubts

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q1)
You need to create a class that will store a unique object elements. You do not need to sort these elements but they must be unique.
What interface might be most suitable to meet this need?
1)Set
2)List
3)Map
4)Vector
ans:1)
what not 3) Map to store unique object elements, and Map is unordered.
This question is from appliedreasoning applet
Q2)Do event adapter classes provide a default behaviour?
Q3)
<code>
public class Demo
{
public static void main(String args[])
{
Demo d = new Demo();
call().run();
}
public static Demo call()
{
return null;
}
public static run()
{
System.out.println("Run");
}
}
</code>
output => Run
I though it causes a NullpointerException
Q4)
Overridding method cannot throw exception which is not thrown in overridden method.(True or false)
ans = true
Q5)
byte b;
double d =417.35;
b=(byte)d;
System.out.println(b);
output is 95
but how to convert a double value into byte type?
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi swati,
i'd answer few of your queries.
Q1)
You need to create a class that will store a unique object elements. You do not need to sort these elements but they must be unique.
What interface might be most suitable to meet this need?
1)Set
2)List
3)Map
4)Vector
ans:1)
what not 3) Map to store unique object elements, and Map is unordered.
This question is from appliedreasoning applet


Map stores a pair i.e. a key and a value. so map can not be the right answer.
--------------------------------------------------------
Q2)Do event adapter classes provide a default behaviour?
No they don't. they just provide an empty body to all the functions of an interface.
--------------------------------------------------------
Q3)
<code>
public class Demo
{
public static void main(String args[])
{
Demo d = new Demo();
call().run();
}
public static Demo call()
{
return null;
}
public static run()
{
System.out.println("Run");
}
}
</code>
output => Run
I though it causes a NullpointerException

in case of static methods even if the reference is null no null pointer exception is thrown . had it been an instance method it would haveresulted in NullPointerException..
----------------------------------------------------------
Q4)
Overridding method cannot throw exception which is not thrown in overridden method.(True or false)
ans = true


yes it is true.
just think that if the overriding method can throw more checked exceptions when the bold line gets executed then u can get a compile exception at runtime.which certainly can not be accepted.
------------------------------------------
Q5)
byte b;
double d =417.35;
b=(byte)d;
System.out.println(b);
output is 95
but how to convert a double value into byte type?

here only the 8 least significant bits will be taken into account and it seems that the 8 lsb have value equal to 95.

regards
deeksha
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
swati
Answer to ur first Question
Map is not a collection. It does not contain any elements which is the requirment of any collection interface. Map contains key/value pairs.
mahesh
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swati,
Your questions are well answered above,
only thing I would like to add thatthe overridding method can not throw more checked exception than superclass method, but it can throw more runtime exceptions(Unchecked Exception) than superclass method.
eg. ArithmaticException,NullPointerException etc.

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

Originally posted by deekasha gunwant:
hi swati,
i'd answer few of your queries.
[b]Q1)
You need to create a class that will store a unique object elements. You do not need to sort these elements but they must be unique.
What interface might be most suitable to meet this need?
1)Set
2)List
3)Map
4)Vector
ans:1)
what not 3) Map to store unique object elements, and Map is unordered.
This question is from appliedreasoning applet


Map stores a pair i.e. a key and a value. so map can not be the right answer.
[/B]


Map must have unique keys, so it could be used, just set the values to null. However a set is more suitable and the question asked for the most suitable therefore Set is the answer.
 
huiying li
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q4)
Overridding method cannot throw exception which is not thrown in overridden method.(True or false).
This is not clear to me. I think the overriding method can throw unchecked exceptions not thrown in overriden method.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Huiying ,
You are right. An unchecked (Runtime) exception can be thrown at any time by any method. The rule for overriding methods is that they cannot throw checked exceptions not handled in the original method.
See JLS§8.4.4


A method that overrides or hides another method (�8.4.6), including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.


Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic