• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exam Question  RSS feed

 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Just Getting Ready for an exam next Tuesday, and i came across this question from an old paper. and wondering do i have the question right

Please answer true or false to the following questions:

1. A try block must be followed by a catch and a finally block.
file. "TRUE"
2. If you write a method that might cause a compiler-checked exception, you must wrap that risky code in a try I catch block.
3. Catch blocks can be polymorphic. "TRUE"
4. Only 'compiler checked' exceptions can be caught "FALSE"
5. If you define a try / catch block, a matching finally block is optional. "TRUE"
6. If you define a try block, you can pair it with a matching catch or finally block, or
both. "TRUE"
7. If you write a method that declares that it can throw a compiler-checked exception,
you must also wrap the exception throwing code in a try / catch block. "TRUE"
8. The main ( ) method in your program must handle all unhandled exceptions thrown
to it. "FALSE"
9. A single try block can have many different catch blocks. "TRUE"
10. A method can only throw one kind of exception. "FALSE"
11. A finally block will run regardless of whether an exception is thrown. "FALSE"
12. A finally block can exist without a try block."TRUE"
13. A try block can exist by itself, without a catch block or a finally block. "FALSE"
14. The order of catch blocks never matters. "FALSE"
15. A method with a try block and a finally block, can optionally declare the exception."FALSE"
16. Runtime exceptions must be handled or declared. "TRUE"
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
david foley wrote:Just Getting Ready for an exam next Tuesday, and i came across this question from an old paper. and wondering do i have the question right

1. Wrong.
2. You didn't answer.
3. Wrong. Mind you, half the time I don't know what people mean by "polymorphic" any more.
4. Correct.
5. Correct.
6. Correct.
7. Wrong.
8. Correct.
9. Correct.
10. Correct.
11. Wrong.
12. Wrong.
13. Correct.
14. Correct.
15. Wrong.
16. Wrong.

Winston
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that..
Saved me a few hours searching google to see if I was right or not..

Just for question 1 I though a catch block must follow a try block

But then again it's asking you catch and a finally block

Which a final block doesn't need to follow a try block

Just going over a few questions to see if I can example then without the need for google

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
david foley wrote:
Just for question 1 I though a catch block must follow a try block


#1 says that if there's a try, then there must also be a catch and a finally. This is false.

try/catch with no finally is perfectly legal. Sometimes we don't have any cleanup to do.

try/finally with no catch is also perfectly legal. That way, if the try block throws an exception that you don't catch, finally still cleans up.

If we have try, then we must have at least one of catch/finally, and of course we can have both.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
david foley wrote:
Just for question 1 I though a catch block must follow a try block


#1 says that if there's a try, then there must also be a catch and a finally. This is false.

try/catch with no finally is perfectly legal. Sometimes we don't have any cleanup to do.

try/finally with no catch is also perfectly legal. That way, if the try block throws an exception that you don't catch, finally still cleans up.

If we have try, then we must have at least one of catch/finally, and of course we can have both.



Thanks man i was kinda guessing that, the Question was asking for both, just an error on my behalf i was reading it wrong...
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:If we have try, then we must have at least one of catch/finally, and of course we can have both.

Nowadays under Java 7 we can also have a try with neither catch nor finally - provided it's a try-with-resources statement.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that,

That was some good reading..
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of those questions are covered by the Java Tutorials. That might include the link you were given last night.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
david foley wrote:13. A try block can exist by itself, without a catch block or a finally block. "FALSE"

Winston Gutkowski wrote:13. Correct.

No, this is wrong, if we're talking about Java 7 or newer. In Java 7 you can have a try-with-resources statement, in which a catch or finally block is not required.

*edit* Oh, I just saw that Mike already said that...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:No, this is wrong, if we're talking about Java 7 or newer. In Java 7 you can have a try-with-resources statement, in which a catch or finally block is not required.

Ah, but is "try-with-resources" really a "try" block, or does it just look like one?

Anyway, I already gave Mike a +1 for pointing that out.

New-fangled stuff...when did Java stop being simple?...grumble...

Winston
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since there's still a block of code between { and } when you use try-with-resources, I'd still call it a try-block, even though you're using the 'try' keyword for a different main purpose than marking a block of code in which you want to handle exceptions.

It's one of those examples where the Java language designers reused an existing keyword rather than inventing a new keyword for a new feature. Introducing new keywords is much harder than reusing existing keywords, because introducing new keywords can very easily lead to incompatibilities with older versions. (Remember, for example, the 'enum' keyword - people who had used 'enum' as a variable name while using Java 1.4 suddenly got compile errors when upgrading to Java 5).
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is actually called a try statement.
[/pedantry mode]
Whether try‑‑resources counts depends on whether you think it has a finally as well, only implicit and hidden.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Since there's still a block of code between { and } when you use try-with-resources, I'd still call it a try-block, even though you're using the 'try' keyword for a different main purpose than marking a block of code in which you want to handle exceptions.

It's one of those examples where the Java language designers reused an existing keyword rather than inventing a new keyword for a new feature.


Not really. Isn't a try-with-resources just syntactic sugar for


?

That is, using try for a purpose other than marking code where we want to catch exceptions has always been a common use case.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think it is actually called a try statement.
[/pedantry mode]
Whether try‑‑resources counts depends on whether you think it has a finally as well, only implicit and hidden.


Right. A try statement consists of a try block and whatever catch and/or finally blocks are present, at least up through 6. I'm not on 7 yet and haven't really looked at the JLS, so I don't know if that definition has been adjusted to include try-with-resources or if the language considers that a separate beast.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look in the Grammar in the JLS, it says:-
Statement:
  Block
  ;
  Identifier : Statement
  StatementExpression ;
  if ParExpression Statement [else Statement]
  assert Expression [: Expression] ;
  switch ParExpression { SwitchBlockStatementGroups }
  while ParExpression Statement
  do Statement while ParExpression ;
  for ( ForControl ) Statement
  break [Identifier] ;
  continue [Identifier] ;
  return [Expression] ;
  throw Expression ;
  synchronized ParExpression Block
  try Block (Catches | [Catches] Finally)
  try ResourceSpecification Block [Catches] [Finally]
So a try followed by a block followed by catches/finally is a kind of statement, and
a try followed by resource specification followed by a block is another kind of statement.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main grammar (the one used in the main body of the JLS, that's actually coupled to the other rules of the language) has a slightly different take though, here:

This indicates that a TryWithResourcesStatement is one type of TryStatement.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do they manage to have two different grammars?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:How do they manage to have two different grammars?


JLS Chapter 2. Grammars.

http://en.wikipedia.org/wiki/Context_free_grammar
http://en.wikipedia.org/wiki/Lexical_grammar

Not that I claim to understand all that myself. I think what it comes down to is that the lexical grammar defines the legal combinations of characters, and how those characters are lexed into tokens, while the syntactic grammar defines how tokens fit together to form legal Java programs.


 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the first link answered that. Thank you.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!