This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marcus Exam Q#60

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 60)
Given the following code
import java.io.*;
public class Ppvg{
public static void main(String argv[]){
Ppvg p = new Ppvg();
p.fliton();
}
public int fliton(){
try{
DataInputStream din = new DataInputStream(System.in);
din.readChar();
}catch(IOException ioe){
System.out.println("flytwick");
return 99;
}finally{
System.out.println("fliton");
}

return -1;
}

}
Which of the following statements are true if you try to compile and run the program?
1) The program will run and output only "flytwick"
2) The program will run and output only "fliton"
3) The program will run and output both "fliton" and "flytwick"
4) An error will occur at compile time because the method fliton attempts to return two values
The answer is 2.
The program will run and output only "fliton"
From the answer it looks that the code in the try block is not throwing any exception ,that's why catch block is being by
passed and it prints the statement in the finally block, how one can know that there is no exception is being thrown from the try block?? if it is printig the statment of finally block , why not it also retuns -1, which is being returned from finally block???
If someone can explain :
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Wali:
You are absolutely correct in saying it will also
return -1. However, IMO we should answer based on the
choices we have. In the available answers, choice 2
best suits the question.
Keep in mind that the author is assuming that
no exceptions are being thrown, though such a
stmt is not mentioned in the qstn. IMO, this qstn should
be worded to include "if the try block does not throw
any exceptions
".
Anyways, the return value is -1 if no exceptions
occur and is 99 if IOException is caught.
Regds.
- satya
 
Herbert Maosa
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If really the question does not mention whether an exception is thrown or not, then there is no way to answer this question and you can not make the darling assumption that an exception is not thrown. In the real exam, you will never be required to make such an unrealistic assumption.The exam will explicitly tell you whether an exception is thrown or not,and you will answer based on accurate facts.
Good luck,
Herbert.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Satya and Herbert..I am now clear about this..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic