• 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
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JQtest Questions

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please, explain the following problems from JQtest
Q1)
Imagine that there are two exception classes called Exception1 and Exception2 that descend from the Exception class. Given these two class definitions,
1. class First {
2. void test() throws Exception1, Exception2 { . . . }
3. }
4. class Second extends First {
5. void test() { . . . }
Create a class called Third that extends Second and defines a test() method. What exceptions can Third's test method throw? Select one valid answer.
ans) no checked Exception, why?
Q2) "slight modified"
class test {
public static void main(String arg[]) {
String args[]=
{"Mighty", "Mouse"};
Changer c = new Changer();
c.method(args);
System.out.println(args[0] + " " + args[1]);
}
static class Changer {
void method(String s[]) {
String temp = s[0];
s[0] = s[1];
s[1] = temp;
}
}
}
ans) prints Mouse Mighty , but why? I know this seems trival question but I'm thinking in terms of the copy of the reference of original array passed in as an argument. So my guess was that it wouldnt change the actual data values in the object array when using s[] in the local method.
thank you!
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin
for Q1
An overriding method can not throw an checked exceptions not thrown by the method it overrides. It can throw less, like the test method in class Second does when it overides the method from class First, but it can't throw anything higher in the hierarchy. since the method in class Second throws none the method in Third can't either.
for Q2
All variables in java are passed by value. However the value of a refernce variable (like arrays) is the reference to the underlying object. In this case you pass a referenc eto the array to the method and any changes you make to it effect the array located at the memory location pointed to by the reference - in this case it is the original array.
hope that helps
Dave
 
reply
    Bookmark Topic Watch Topic
  • New Topic