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

2 questions from Jxam

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


I like jxam however i believe these 2 questions had wrong answers:
1)Consider the following piece of code and select the correct statements.

1. Object o = new String("abcd");
2. String s = o;
3. System.out.println(s);
4. System.out.println(o);
this is the correct answer:
The code fails to compile at line 2
this answer was wrong://I believe this answer is also correct
The code can be made to compile by changing line 1 to the following:
String o = new String("abcd");
2)Given the following definition:
String s = null;

Which code fragment will cause an exception of type NullPointerException to be thrown.
if ((s != null) & (s.length()>0))
if ((s != null) && (s.length()>0))
if ((s != null) | (s.length()>0))
if ((s != null) | | (s.length()>0))
None of the above.
This question revolves around the short-circuit logical operators (at least some of it does!).

For the following, LHS is left-hand side, RHS = right hand side.

Answer 1 must be fully evaluated. The LHS is true and so the RHS needs to be evaluated to ensure the entire expression is true. This is an AND operation, and if the LHS is true, then the RHS must be evaluated for the expression to be true.

Answer 2 is the same as above. Both sides need to be evaluated for an AND expression to be true (if the LHS is true, that is)..

Answer 3 must be fully evaluated because an OR expression is only true if one or the other is true, but not both. Therefore, since the LHS is true, the RHS must be evaluated.

Answer 4 is similar to answer 3 above.

For someone like me with 14+ years of software experience, this was a tough question! It requires that you are familiar with AND and OR truth tables, as well as Java.
first of all the left hand side evaluates to false not true!
second an OR expression is true if either or both are true!
therefore the correct answers are 1, 3, and 4
not 1, 2, 3, and 4

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Randall,
1 ) Answer Is ,

It'll give compile error in Line 2 !
You cannot assign Object type reference to String type variable.
By explicit casting you can make it to compile correctly,
Object o = new String("abcd");
String s = (String)o;
2)
Line 1 ,
first operand is false ,since the operator is bitwise AND it'll evaluate the second operand as well, which gives a NullPointerException.
Line 2,
first operand is false , since the operator is short circuit AND it'll not evalute the second operand & will not give a NullPointerException.
Line 3,
first operand is false , since the operator is bitwise OR operator it'll evalute both of the operands which cause to give NullPointerException.
Line 4,
first operand is false , since the operator is short circuit OR operator it should evaluate the second operand as well which again gives a NullPointerException.

So answer should be 1,3 & 4
Rgds,
Shan.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you misunderstood the first question. it said choose all correct answers and I chose two answers but it said only one was right.
 
Shan Karawita
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops .. Yeh I have miss read...
But Randall you havent mentioned the given answers there ..
According to the code fragment only problem I found is in line 2.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i didnt think the wrong answers were important so i didnt paste them. I think the two answers i posted are both correct:
this is the correct answer:
The code fails to compile at line 2
this answer was wrong://I believe this answer is also correct
The code can be made to compile by changing line 1 to the following:
String o = new String("abcd");
here is result of changing line 1:
1. String o = new String("abcd");
2. String s = o;
3. System.out.println(s);
4. System.out.println(o);
If this wont compile im taking my ball and going home!
 
Shan Karawita
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Definitely now it should work fine !
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those questions have already been moved to the Mock Exam Errata forum. You may want to check there first, if you have doubts on any mock exam questions/answers.
Bosun
 
What? What, what, what? What what tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic