• 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

sereral doubs... pls clear them all

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kindly clear the doubts of every question below...
i m marking them in serial no. do refer while giving the answer

thanx


----------------




1) public class Test9 extends A{
public static void main(String args[]) throws Exception{
Test9 t = new Test9();
}
}
class A{
A() throws Exception{
System.out.println("A Class");
}
}

���������������.Compile error

why this answer ?
=========================================================


2)
public class ThrowsDemo {
static void throwMethod() throws Exception {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwMethod();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}

Answer :=> The method is throwing Exception but while catching your catching a subclass of Exception.

But the catching class is the same as the orginal one throws ..will it not work ?

=========================================================

3)
public class BettyAck {
public static void main(String argv[]){
BettyAck b =new BettyAck();
}
public BettyAck() {
Integer x = new Integer(10);
findOut(x);
Integer y = new Integer(99);
Integer z = y;
z = null;
findOut(y);
//here
}
public void findOut(Integer y){
y = null;

}
}
No obj are garbage collected.

why ?



============================================================

4)
public class CDummy
{
public static void main(String args[])
{
try {
int i=testX();
}
catch (Exception ee)
{
System.out.println("Exception !");
}
}

public static int testX() throws ArithmeticException
{
try {
throw new ArithmeticException();
}
finally {
System.out.println("Finally");
return 4;
}
}
}


when compile it complains that ...

CDummy.java:22: warning: finally clause cannot complete normally
}
^
1 warning

why ???
can return statement is not allowed in finally block ?


==========================================================

5)
try {
System.out.println("Test !");
}
catch(InterruptedException ee) {
System.out.println("The main thread was interrupted !");
}

what is the result when compile ?
Answer :=
The code does not compile because the exception InterruptedException is never thrown. (InterruptedException is checked)


why ? if it is check then it must be definetly thrown ? confused

========================================================

6)
class A extends Thread {
public void run() {
try {
synchronized (this) {
wait();
}
} catch (InterruptedException ie) {
System.out.print(interrupted());
}
}
public static void main(String[] args) {
A a1 = new A();
a1.start();
a1.interrupt();
}
}



when compile and run the output is "false"

why ? when it is interupted and method interrupted() will return boolean ... as it actually been intruppted then why its false and not true ?


==========
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
----------------------------------------------------------------------------Kindly clear the doubts of every question below...
i m marking them in serial no. do refer while giving the answer

thanx


----------------




1) public class Test9 extends A{
public static void main(String args[]) throws Exception{
Test9 t = new Test9();
}
}
class A{
A() throws Exception{
System.out.println("A Class");
}
}

���������������.Compile error

why this answer ?
------------------------------------------------------------------------

Default Constructor for Test9 cannot handle the exception throwed(if any) by the class A(Since its superclass constructor throwing exception and the default construtor for the subclass is "no-arg" constructor). So u need to specify the explicit constructor as below,


public class Test9 extends A1{
//Explicit Constructor for Test9 Class
Test9() throws Exception
{
}
public static void main(String args[])throws Exception {
Test9 t = new Test9();
}
}
class A1{
A1() throws Exception{
System.out.println("A Class");
}
}


Hope it will be clear for you
 
karthik venkatesan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2)
public class ThrowsDemo {
static void throwMethod() throws Exception {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwMethod();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
---------------------------------------------------------------------

When u r throwing an exception from a method, then your receiving end (catch block) should be the same exception or superclass exception. But it cannot be the subclass exception.

In the following line,

throw new IllegalAccessException("demo");

your method throwing subclass of Exception, but your method may throw other subclass of exceptions also. Due to this reason sun made the above rule.
If you change catch block as below, it will work fine.


catch (Exception e)


regards,
Karthik
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your problem 1, super class constructor is defined as to handle an exception type Exception. So you must declare the subclass constructor
with handle exception type Exception to overcome this problem.

In Problem 2 you must caught the exception type Exception. IllegalAccessException is subclass of Exception class. So you couldn’t caught subclass exception instead of superclass exception.

 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
6)


6)
class A extends Thread {
public void run() {
try {
synchronized (this) {
wait();
}
} catch (InterruptedException ie) {
System.out.print(interrupted());
}
}
public static void main(String[] args) {
A a1 = new A();
a1.start();
a1.interrupt();
}
}



when compile and run the output is "false"

why ? when it is interupted and method interrupted() will return boolean ... as it actually been intruppted then why its false and not true ?


According to JAVA API,


If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.


It clearly says that the interrupt status gets cleared(false) when it is blocked on wait call. And also remember that the return doesnt signify whether its interrupted or not, it returns the interrupt status.

5) Since the exception is a checked one and u havent explicitly thrown it, so u get such a compiler error

4) Thats a warning, u can ignore it. U can very well have a return statement inside ur finally block. That shouldnt be a problem.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The BettyAck (Elizabeth Ackroyd) question
is one of the guest questions from

http://www.jchq.net/phezam
and comes with the following answer explanation.

"A reference passed into a method is passed as if it were a copy of a pointer pointer rather than the actual object. Thus if that reference is assigned to a null it makes no difference to any other copy of that pointer. Thus the code within the method findOut makes no difference to any other references. Although reference z is assigned to null reference y still points to the object so no objects are eligible for garbage collection."

You should always try to read any answers given and it is in your best interest to ask only one question at a time in a forum like this. People are likely to be overwhealmed if you post multiple questions and not answer at all.

Marcus
[ May 04, 2005: Message edited by: Marcus Green ]
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to what Marcus said, you may want to look at this, Amit,

BettyAck

- Soumya.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mr.Marcus,
I've a doubt about the BettyAck question you just explained.When he made "z=null",isn't object "z" available for garbage collection.Previously z pointed to y i.e "z = y",but in the very next statement we made it point to null,so why do we say no objects are available for garbage collection??Please explain me this..i would be obliged.Thanks.
 
Jas Oberai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok..thanks soumya for that link..it was really good.I just want to assure myself that i am goin rite..so please seal it with your certification guys.
When,we do "Integer z"....we just created a reference variable and no actual object on the heap,so when we did "z=null",it means z is not pointing to the object variable created by "y",it just points to nothing.Rite?
 
reply
    Bookmark Topic Watch Topic
  • New Topic