• 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

Questions from mock Exam !!!

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
q-1)
=====
import java.io.*;
class Super
{
int methodOne( int a, long b ) throws IOException
{ // code that performs some calculations
}
float methodTwo( char a, int b )
{ // code that performs other calculations
}
}
public class Sub extends Super
{
}
which of the following are legal method declarations to
add to the class Sub?
Assume that each method is the only one being added.
a) public static void main( String args[] ){}
b) float methodTwo(){}
c) long methodOne( int c, long d ){}
d) int methodOne( int c, long d ) throws ArithmeticException{}
e) int methodOne( int c, long d ) throws FileNotFoundException{}
the answer given are ---> a,b,e
but according to me 'd' is also valid method if we don't
declare 'e' in sub class so in this case we can choose
either d or e.
so pl.tell me the right answers and correct me if wrong
q-2)
=====
If raf is a RandomAccessFile, what is the result of compiling
and executing the following code?
raf.seek( raf.length() );
a) The code will not compile.
b) An IOException will be thrown.
c) The file pointer will be positioned immediately before the
last character of the file.
d) The file pointer will be positioned immediately after the
last character of the file.
answer given is -->d
it is little bit confusing me . i think ,answer may be 'a' as
"seek(long i)" method throws IoException and i know if a method
may throws Checked exception then it should be in try-catch block
or declared in throws clause of method.in this case there is no
try-catch block . so the code should not compile
d is correct if the code will be written in try-catch block.
pl. tell me the right answer

thanks in adv.
sunil
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to 1) the method is declared in a SubClass. So you cannot throw checked Exceptions which are not thrown in the overriden method.
to 2) For me this is just a code snippet. You should put the line in try - catch if you use it in a program. But they just ask for the method-functionality.
hope this helps
Axel
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sunil,
For q1,i think u r right, since it clearly says to assume that each method is the only one being added.
For q2 ,i would assume that line of code is part of a try-catch block or a method that throws IOException. in the real exam u won't be confused like this.
Elizabeth.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sunil,
For Ques. 1 I guess you are right. d option is one of the right ans. so ans. is a,b,d and e
For ques. 2 I feel ans. should be C becoz if the length of the file is 16 for eg., becoz of zero based starting it will point at 15 i.e. The file pointer will be positioned immediately before the last character of the file.
Please confirm if I am wrong.
Jyotsna
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
surely d is wrong, as a an overriding method cannot throw any exceptions other than that thrown by the overridden method. An arithmetic exception is not a subclass of an IO exception is it?
 
Elizabeth Jacob
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding the Q1, if an overridden method( in the sub class)throws a checked exception it has to be a subclass of the exception thrown in the superclass.Here in option d) an Arithmatic Exception is being thrown which is an 'unchecked' exception and hence it does'nt matter the fact that it is not a subclass of IOException.
Elizabeth.
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are checked Exceptions and unchecked Exceptions?
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic