• 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

interview test

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

The following are the sources for four related Java classes. The task is to study the code, find the bugs and suggest how they should be fixed. Additionally, you can comment on any flaws in the design that aren’t specifically bugs. All source comments have been deliberately removed and shouldn’t be counted! Also, generics have deliberately not been used.

com/result/exam/a/Concept.java

com/result/exam/a/ConceptA.java

com/result/exam/a/ConceptB.java

com/result/exam/a/ConceptC.java

some one please provide answer to me ...i will check how i did that test.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read my post at https://coderanch.com/t/638728/java/java/null#2930326 about your use of the code tags and edit your post accordingly. I've already fixed two of your posts, and Campbell has fixed another one, but you should really use them correctly to begin with.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following are the sources for four related Java classes. The task is to study the code, find the bugs and suggest how they should be fixed. Additionally, you can comment on any flaws in the design that aren’t specifically bugs. All source comments have been deliberately removed and shouldn’t be counted! Also, generics have deliberately not been used.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you sir....no body tell this matter till now and i'm really sorry i did n't read your message at that time
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, only put code tags around code, not comments.

Second, I think if you want a reply you should ask a specific question, not "comment on this."
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following are the sources for four related Java classes. The task is to study the code, find the bugs and suggest how they should be fixed. Additionally, you can comment on any flaws in the design that aren’t specifically bugs. All source comments have been deliberately removed and shouldn’t be counted! Also, generics have deliberately not been used.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:some one please provide answer to me ...i will check how i did that test.


How about we do it the other way around.
You tell us what answer you gave and we can then comment on that.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:First, only put code tags around code, not comments.

Second, I think if you want a reply you should ask a specific question, not "comment on this."



Also, it is almost as if the OP is intentionally making the code unreadable. First, posting the code without code tags. And second, posting code with code tags, but with all formatting removed...

[EDIT: Looks like the OP finally fixed it. Thanks.]

Henry
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first class code i'm placing below...i compiled that code and then it shows no errors and i'm little bit doubt wether that one correct or notplease tell me..i don't have full confidence on that.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:first class code i'm placing below...i compiled that code and then it shows no errors and i'm little bit doubt wether that one correct or not



sujesh Katri wrote:The task is to study the code, find the bugs and suggest how they should be fixed. Additionally, you can comment on any flaws in the design that aren’t specifically bugs.


Bugs don't just consist of compilation errors. You have to be sure that the code does what it is supposed to do.
There's a clue on line 11. Can you see a way in which id could be set to null ?
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think id(data member of the class)==null (not anId==null)is it correct now.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:i think id(data member of the class)==null (not anId==null)is it correct now.


No. There is nothing wrong with the constructor. The message in the exception says that id should not be null, but there is a way of setting id to null. Look at all the methods in the class and think about what they do.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't understnad...it seems to me correct....please tell me answer
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explain to me what lines 22 - 25 do.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we are setting id value...we assign formal parmeter value to datamember.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:we are setting id value...we assign formal parmeter value to datamember.


Okay. So assume that somewhere else in your code you have

Would that code work ?
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no..because concept1 is abstract class..we don't create object directly to that abstract class.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:no..because concept1 is abstract class..we don't create object directly to that abstract class.


Fair enough. I missed that.
What about
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
may be we have to write if condition.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:may be we have to write if condition.


Okay. Where would you write this if condition and what would it check ?
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in setId (..).and i write like if(id==null){sop("...");}...brother please tell me ...if you explain i will understand if i don't then i will ask you please brother..i have to another 3 classes also...i have to submit that test tomorrow...please
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm placing second class and i did n't find any mistakes below code please verify it
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:in setId (..).and i write like if(id==null){sop("...");}


Why sop ? That's not going to stop id being set to null.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
at least gives message to end use id must not be null...what should i do now...can i throw NullPointerException to setId(..).please tell me.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:what should i do now...can i throw NullPointerException to setId(..).please tell me.


The question said to suggest a solution. If you think an exception is a possible solution, then you should suggest it.
If this is a pre-interview exam, then remember that you may be asked to justify your choice in the interview. So why do you think throwing an exception is a possible solution ?
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please provide answer to me.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:please provide answer to me.


I'm afraid that's not how it works here. If I just give you the answers you will pass the test, but you won't have learnt anything.
I've guided you so far but you don't seem willing to ShowSomeEffort - can you seriously not think of a single reason why throwing an exception would be a good solution to the problem we have identified ?
Don't be afraid to state the obvious, it's often the right answer.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think there is no necessary of setId(..).is it correct?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:i think there is no necessary of setId(..).is it correct?



During a job interview, you not only have to provide an answer, but justification for it too.

Henry
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:i think there is no necessary of setId(..).is it correct?


I think that is less likely to be a good solution. setId is a public method so there may be code that is already using it. Removing it would break that code.
Throwing an unchecked exception is a good suggestion - I'm just trying to get you to think of a reason why it's a good suggestion. You should always be able to justify any code you write.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i place my code like that how much good practice that...i'm placing my code below...if we throwing exception is not good practice is n't is...we always keep that code in try and catch block...if we write catch block with our message nothing but if conditio with sop(..);is not it...so i want remove setid(..)is it good or bad
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:if i place my code like that how much good practice that...i'm placing my code below...if we throwing exception is not good practice is n't is...we always keep that code in try and catch block...if we write catch block with our message nothing but if conditio with sop(..);is not it


That's something you could discuss when you are answering the

Additionally, you can comment on any flaws in the design that aren’t specifically bugs.

part of the question. Exceptions have good and bad aspects to them, so be prepared to justify your comments.

sujesh Katri wrote:so i want remove setid(..)is it good or bad

I've already explained why i think that is probably a bad idea, but if you can justify removing it, then that is fine.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm trying to keep some code in try and catch but it shows error..how can i place return other != null && other.getClass().equals( getClass() ) && id.equals( ( (Concept1) other ).id ); in try and catch blocks
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:i'm trying to keep some code in try and catch but it shows error..how can i place return other != null && other.getClass().equals( getClass() ) && id.equals( ( (Concept1) other ).id ); in try and catch blocks


Where are you using try/catch blocks ? We were talking about throwing an exception, not catching one.
And why are you modifying the equals method ? It was the setId method that needed altering.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i alter my whole code i get my desire answer ..i'm placing below the code and please tell me if there is any mistakce
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:please tell me if there is any mistakce


From a quick look I can see the following.
1. Why have you declared your own NullPointerException class. One already exists in the standard API and it is a RuntimeException which means you don't need all thoses throws in your constructor and method signatures.
2. Line 32 is in the wrong place. You set id to null and then throw an exception to say id can't be set to null. You should only set id if the parameter id is not null.
3. On line 96 you set c1 and c to point to the same object and then you test if they are equal. Of course they are going to be equal - they're pointing at the same object.

I'm afraid that's it for me for today. Good luck with the rest of your exam.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no need to give the exception a name. Simply
throw new XYZException("Blahblahblah");
Your lines are too long.
Your indentation and placement of braces are inconsistent.
You are using /* this sort of comment */ for commenting‑out.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i placed in my code like that only ..i changed my i entire code and i'm placing below
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
next time i will place braces correctly..thank you
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mean while i'm placing my second class..in this class i did n't find any bug.
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic