Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!

Ajit Deshpande

Greenhorn
+ Follow
since Jun 15, 2000
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ajit Deshpande

public class Casting{
public static void main(String argv[]){
byte b=0;
int i = 4999;
b = (byte) i;
System.out.println(b);
}
}
If this code is compiled and run the value of b is 3 which I expected.
However when I use 'int i = 5000;' the value of b comes to -120 which is unexpected.
Can anybody please explain this strange output.
Thanks
please consider this code:
class vector1 {
public static void main(String args[]) {
Vector a = new Vector();
int i = 10;
Integer Int = new Integer(i);
a.addElement(Int);

System.out.println(a.elementAt(0));
}
}
I think this should compile but my compiler is complaining that"Class Vector not found". I am using jdk1.2.1.
Could someone tell me if there is something wrong with the code or with the compiler.
Thanks
I am confused about when and how static references can be made to non static variables and methods.
Consider this bit of code:
class Box {
double width;
double height;
double depth;
double volume() {
return width * height * depth;
}
void setDim(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
}
class BoxDemo {
public static void main(String args[]) {
Box myBox1 = new Box();
Box myBox2 = new Box();
double vol;
myBox1.setDim(10, 20, 15);
myBox2.setDim(4, 9, 16);
vol = myBox1.volume();
System.out.println("Volume of myBox1 = " + vol);
vol = myBox2.volume();
System.out.println("Volume of myBox2 = " + vol);
}
}
This code compiles and runs fine. However in the main method of the BoxDemo class a reference is made to the non static variables and method of the Box class. But the compiler does not protest.
Now consider this piece of code:
class MyPoint1 {
int x, y;
void myMethod() {
System.out.print( " ( " + x + ", " + y + " ) " );
switchCoords( x, y );
System.out.print( " ( " + x + ", " + y + " ) " );
}
void switchCoords( int x, int y ) {
int temp;
temp = x;
x = y;
y = temp;
System.out.print( " ( " + x + ", " + y + " ) " );
}
}
class MyPoint {
public static void main(String args[]) {
MyPoint1 MP1 = new MyPoint1();
MyPoint1.x = 5;
MyPoint1.y = 3;
MyPoint1.myMethod();
System.out.println();
}
}
Here the compiler protests the it can't make static reference to nonstatic variables and methods.

Can anybody please help me understand the difference between the two codes.
Thanks
This a question from a mock test:
import java.io.*;
class Super
{
int methodOne( int a, long b ) throws IOException
{ // code that performs some calculations
}
float methodTwo( char a, int b )
{ // code that performs other calculations
}
}
public class Sub extends Super
{
}
Which of the following are legal method declarations to add to the class Sub? Assume that each method is the only one being added.
a) public static void main( String args[] ){}
b) float methodTwo(){}
c) long methodOne( int c, long d ){}
d) int methodOne( int c, long d ) throws ArithmeticException{}
e) int methodOne( int c, long d ) throws FileNotFoundException{}
The answer given is a, b, e.
But what is wrong with c & d. ArithmeticException() is a runtime exception and the rules for overriding apply to checked exception.
Can anybody please help?
Hi Nirmal,
Thanks for the explanation.
It has made things clearer.
But d is not correct if the first element of the array is 0?
What then?
This question is in amock exam:
Can anybody please explain what is the right answer ans and why?
public int guessWhat( int arr[] )
{ int x= 0;
for( int i = 0; i < arr.length; i++ )
x = x < arr[i] ? arr[i] : x;
return x;
}
a) Returns the index of the highest element in the array
b) Returns true/false if there are any elements that repeat in the array
c) Returns how many even numbers are in the array
d) Returns the highest element in the array
e) Returns the number of question marks in the array
Thanks
Can an Inner Class extend another class or implement an Interface?
Hi Carl,
Is there any chance of such a question being asked in the exam.
On an average only 2 minutes are given to answer each question.
For this question to be read, digested and put on the paper the way you have done would take at least 10mins. That would make a person appearing in the exam, very nervous to have to spend so much time on one question.
The wording of Question #8 is not very clear. It says:
When you pass a reference to an array as an argument to a method call, you pass a new reference to the original array.(True or False)
You actually pass a copy of the reference and not a new reference.
20 years ago
Hi Carl, Thanks a lot.
I tried with long, double and float.
With long the output is 9 and
with double and float it is 9.0.
Now it clears things up.
Thanks again
20 years ago
Hi Anil,
It compiles fine. Try again.
Kavitha,
If you put the value 99 at exp1 then the output is 9 and not 9.0
The question is that if you put 999999999999999 at exp1 why does it give a compilation error?
Both expressions should convert to double. But they don't
20 years ago
This question is in several mock tests however there is a problem:
Q: What is the output of the following code:
int x = 6;
double d = 7.7;
System.out.println((x > d) > 99.9 : 9);
a. 99.9
b. 9
c. compilation error
d. 9.0
The answer is 9.0 and the reason given is that int x is promoted to a double.
However if you replace 99.9 with 99 then the output is 9. Which means that the return type of the println statement depends on the type of sub-expressions on either side of the colon.
Now if you put a value which is greater than the greatest int value(say 9999999999999999999) on the left of the colon, it gives a compilation error. Why?
Can anybody help?
20 years ago
Chapter 9 of the RHE book under the section 'Containers and Components' says "The container class is abstract: its most commonly used concrete subclasses are Applet, Frame and Panel." Patrick Naughtons book on Java2 does'nt say anything about the class being abstract.
Confused?
Can anybody help please?
another question on shift operators:-
for a primitive data type long if I want to left shift(<<) more than 63 times(say 64 times) how can I get a result. I tried it and it gives a -ve number.
I am using windows 98 and jdk1.2.1
it goes into an infinite loop