This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Some questions

 
denish mehta
Ranch Hand
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic