wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Some questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Some questions" Watch "Some questions" New topic
Author

Some questions

denish mehta
Ranch Hand

Joined: Dec 21, 2000
Posts: 135
Hello java ppl
i have came across these questions in a mock i think called "java Quiz"
Please explain me why the questions are giving the answers indicated??
1)System.out.println(Math.abs(Integer.MIN_VALUE)); //why gives -ve value???
=========================================================

2)if("String".replace('g','G') == "String".replace('g','G')) // why false???
=======================================================
3) Please go through the code carefully
try
{
}
catch(java.io.IOException t) //line 1
{
System.out.println("Inside Catch");
}

Compiler error complaining about the catch block where no IOException object can ever be thrown
on the other hand if we use catch (Exception s){} at line 1 argument it compiles alright why???
=========================================================
4)Please go through the code carefully
import java.awt.*;
public class TestFrame extends Frame
{
Button bNorth = new Button("North");
Button bSouth = new Button("South");
Button bEast = new Button("East");
Button bWest = new Button("West");
Button bCenter = new Button("Center");
public TestFrame()
{
setLayout(new FlowLayout());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setLayout(new BorderLayout());
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}
Will compile and run cleanly, but no component is visible. why???
Regards Denish
Seema Das
Ranch Hand

Joined: Mar 05, 2001
Posts: 78
Hi Denish,
Please see my comments regarding few of ur qs.
1) This surprised me too. So I checked Java API and this is what I found.
abs
public static int abs(int a)
Returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.
Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

This is also true when the arg to abs is long.
So the conclusion, if the most negative int or long is given to abs method, it will print the argument itself.
2) replace method of string returns a new string and that is why == operator returns false. If you try .equals method of string, it will give you true since .equals tests for real equality of the strings whereas == checks whether both references are pointing to the same object.
4) This kind of q was posted on javaranch few days back and I remember one of the moderators giving the answer as
BorderLayout implements LayoutManager2 and this layout manager does not remember components previously added to the container and that is why you are not able to see any components. But FlowLayout and GridLayout implement LayourManager which does remmber previously added componenets to the container.

hth,
Seema
denish mehta
Ranch Hand

Joined: Dec 21, 2000
Posts: 135
Hey seema
thanx for reply
Any body have any opinion about the 3rd question???
regarding (2) i also know that replace() returns new string but i also know that java does not create new object if two objects r same e.g
String s="Hi";
String d="Hi";
s==d; //given true ok d also points to s
I am curious why not in this case the new object is created
"String".replace('g','G') == "String".replace('g','G');

Regards
Denish
[This message has been edited by denish mehta (edited April 22, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Some questions
 
Similar Threads
Why the button doesn't appears in the Frame?
java.awt BorderLayout and FlowLayout
Why the button doesn't appear in th frame?
doubts over layouts
Why the button doesn't appear in th frame?