• 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

Clarification on Jxam questions

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need a clarification on thhe following two questions from the mock exam - JExam:
Q1)
Suppose a MyException should be thrown if Condition() is true, which statements do you have to insert ?
1: public aMethod {
2:
3: if (Condition) {
4:
5: }
6:
7: }
a)throw new Exception() at line 4 - myans
b)throws new MyException() at line 4 *
c)throw new MyException() at line 6
d)throws new Exception() at line 2
e)throws MyException at line 1 *

My answers are a & e. But as per the exam its b & e. please clarify

Q2)
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);
a)The following is displayed:
abcd
abcd
b)The code fails to compile at line 1.
c)The code fails to compile at line 2
d)The code fails to compile at line 4.
e)The code can be made to compile by changing line 1 to the following:
String o = new String("abcd");
My answers are c & e. But the JExam says only c is correct. Please clarify.
Regards,
JAI
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the first question, I would go with your answers too.
For the second question you have the following:
1. Object o = new String("abcd");
2. String s = o;
Object o is higher up the hierarchy than String s so in order for o to go into s, you will need a cast to confirm it.
String s = (String)o;
I think that would make it compile.
Hope that helps
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I think the answer to the first question is probably overlooked. The question asked for when a MyException will be thrown, not an Exception. Therefore, I think the statement that throws a new MyException should be inserted at statement 4.
I agree with you on the second question. Obviously, once the reference to o is a String. Assigning a String object to a String reference should be no problem.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cameron,
What you say is correct, MyException should be thrown in the method.But look at option b carefully. It says throws new MyException(), whereas it should be throw new MyException().
c would have been correct if the line number was 4 and not 6.
Cheers
Sajida
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic