Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

some doubts

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
 
We don't have time for this. We've gotta save the moon! Or check this out:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic