File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes sereral doubs... pls clear them all Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "sereral doubs... pls clear them all" Watch "sereral doubs... pls clear them all" New topic
Author

sereral doubs... pls clear them all

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
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 ?


==========


Thanks and Regards, Amit Taneja
karthik venkatesan
Greenhorn

Joined: Dec 31, 2004
Posts: 26
----------------------------------------------------------------------------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

Joined: Dec 31, 2004
Posts: 26
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
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
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.



Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
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 ]

SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
In addition to what Marcus said, you may want to look at this, Amit,

BettyAck

- Soumya.
Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
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.


SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
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?
 
Don't get me started about those stupid light bulbs.
 
subject: sereral doubs... pls clear them all
 
Similar Threads
Exception catching
throw an Exception....
Compile Error
Exception
Reference Variable