rakhee gupta

Ranch Hand
+ Follow
since May 01, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by rakhee gupta

While going through some mock test i found this question.
This is not at all clear that when we pass null in the method the subtester version of the method is being called.


class Tester {
void test(Object s) { System.out.println ("Object version"); }
void test(Tester s) { System.out.println ("Tester version"); }
void test(SubTester s) { System.out.println ("SubTester version"); }

// Not compilable any more if you uncomment the line
// since String and Tester are siblings
// void test(String s) { System.out.println ("String version"); }

public static void main (String args[]) {
Tester c = new Tester ();
// Ambiguous, the most specific one which fit will be call
c.test (null); // SubTester version
c.test (new Object()); // Object version
}
}

class SubTester extends Tester{
}
Hi,

I have a doubt regarding autoboxing.
When we try to add an primitive to vectors doesn't autoboxing automatically converts the primitive to wrapper class?
like is this not a valid statement:
2) Vector v=new Vector();
v.addElement(99);

What are rules of autoboxing.When it comes into play?
Thanks in advance.
Hi,
In K&B page 646 it is mentioned that "the same rules apply to
method-local inner classes as to local variable declarations. You can't, for example,mark a method-local inner class coderanch, private, protected, static, transient,and the like. The only modifiers you can apply to a method-local inner class are abstract and final, but as always, never both at the same time.

But when I tried this piece of code with different modifiers it worked:
public class MyClass1 {
public static void main(String argv[]){ }
/*Modifier at XX */ class MyInner {}
}

What modifiers would be legal at XX in the above code?

1) public
2) private
3) static
4) friend

Class MyInner is in main() method so it should only allow final or abstract modifier but it is allowing pub;ic and private modifier also.

Can anyone explain this.Is this a error in K&B
Hi, I was going through K&B chapter-6 the following question:

Given:
import java.util.regex.*;
class Regex2 {
public static void main(String[] args) {
Pattern p = Pattern.compile(args[0]);
Matcher m = p.matcher(args[1]);
boolean b = false;
while(b = m.find()) {
System.out.print(m.start() + m.group());
}
}
}
And the command line:
java Regex2 "\d*" ab34ef
What is the result?
A. 234
B. 334
C. 2334
D. 0123456
E. 01234456
F. 12334567
G. Compilation fails.

The answer to this question is : E.
Till 0123445 its clear to me but after that where this digit 6 is coming from because index 6 is not there in the input string.

Thanks
Hi,

So does this means that declaring a final variable after defining it means that it is no longer a final variable.
What the difference between the 2 types of assignment?
Hello,

This is going over my head.This is not clear to me at all.After all value 10 means 10 isn't it? How does it matters in which way it is assigned to a variable ie:

final int a =10;

or

final int a;
a=10;
What I understand is Compile time constants are variables declared as final and initialized at the time of definition like

final int a =10; ---> this is a compile time constant.Right?

But what about this code:

final int a;
a=10;

In this case is 'a' not a compile time constant.
Please explain.

Thanks
Why not objects referenced by e2 and e3 along with e3 are eligible for GC after line 3 after all their references(e2,e3) also being set to null?
Hi Bob,

But after line 8 the reference e3.e is on the heap and not on stack(because e is an instance variable of e3) and e2 has no contact will the external world although it has an active reference e3.e so it is eligible for GC.

Please correct me if my understanding is wrong.
Hi,
Sorry i didn't knew this convention.I copied it from K&B scjp certification guide chapter 3.
Thank you.Understanding is clear now.
Hi,
Please see the following code and the explanation:

Given:
1. class Eco {
2. public static void main(String[] args) {
3. Eco e1 = new Eco();
4. Eco e2 = new Eco();
5. Eco e3 = new Eco();
6. e3.e = e2;
7. e1.e = e3;
8. e2 = null;
9. e3 = null;
10. e2.e = e1;
11. e1 = null;
12. }
13. Eco e;
14. }

At what point is only a single object eligible for GC?
A. After line 8 runs.
B. After line 9 runs.
C. After line 10 runs.
D. After line 11 runs.
E. Compilation fails.
F. Never in this program.
G. An exception is thrown at runtime.
Answer:
G is correct. An error at line 10 causes a NullPointerException to be thrown because e2 was set to null in line 8. If line 10 was moved between lines 7 and 8, then F would be correct, because until the last reference is nulled none of the objects is eligible, and once
the last reference is nulled, all three are eligible.

My question is if I remove line number 10 from the code will the answer to this question will be choice A.I mean if there was supposed to be no runtime error after removing line number 10 from the above code then the answer to the question 'At what point is only a single object eligible for GC?' would be A.

Thanks
Please see the following code:Concept of arrays is clear to me but when it comes to these kind of questions i am stuck.I think I am messed up with arrays.Please clear my understanding.

1. class Dims {
2. public static void main(String[] args) {
3. int[][] a = {{1,2,}, {3,4}};
4. int[] b = (int[]) a[1];(---> not clear a[1] which is the 2nd element of 2 dimensional array 'a' should be a ref to an array of int then how this assignment is valid)
5. Object o1 = a;( a is a reference to 2 dim array and its being assigned to an object o1)
6. int[][] a2 = (int[][]) o1;(---> again the same doubt as above)
7. int[] b2 = (int[]) o1;
8. System.out.println(b[1]);
9. } }

The output is:
A ClassCastException is thrown at line 7 because o1 refers to an int[][]
not an int[]. If line 7 was removed, the output would be 4.
Ok that means a class with private constructor cannot be instantiated from outside the class.But what is the use of having a private constructor.What purpose does it serves?