Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!

niraj singh

Ranch Hand
+ Follow
since Feb 07, 2001
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 niraj singh

Hi !
Answers are -
Q1 - a,c. - An array is a subclass of the Object class and is Cloneable. Cloneable is an interface, so it has to implement it. An array by default is Cloneable.
Q2. - can't read the HTML code.
Q3 - a.
Q4 - c. the object is set up for garbage collection, when it is set to null
Hi
The Reader class is a superclass of the BufferedReader and other such classes. The read(char[] cbuff...) method is abstract in the Reader class, as it is just a template to be defined in a subclass where a buffer is implemented. The method is not abstract in the BufferedReader class. Same applies for InputStream and Writer classes.
FilterInputStream cannot be instantiated as it is an abstract class. It can only be subclasses. The following are types of FilterInputStream's -
DataInputStream
BufferedInputStream
LineNumberInputStream
PushbackInputStream
Hi Anon,
The program will terminate when main() thread terminates, even if other threads have not completed. That is why join() method is used to allow completion of other spawned threads before main() thread completes.
Hi Venky,
Are you sure that the question is correct?
If it correct, then the answer should be abstract.
This is because, any abstract method can only be declared in a class which is declared abstract explicitly.
Now, that we have an abstract class in a package and we want to call the method, we will need to inherit the class. When we inherit the class, we don't need to make an instance of the class. If we want to call a abstract method declared, we will first have to define a body for the method. Then we can call the method in the class.
An abstract class can also contain methods which already have a body definition. IN this case, you can also call those methods by their simple name, as they have been inherited.
Hope it helps.
Niraj
Hi Kuldeep,
This is a simple case of a wait() and notifiy() methods to handle Threads. In addition we have the notifyAll() method.
All these 3 methods are relevant to synchronized methods. They can be called only from within a method which has been declared - synchronized.
When a wait() method is used, it is telling the thread to vacate the synchronized lock(in other words - monitor) and go to sleep till any other thread enters the monitor and calls the notify() method.
When a notify() method is issued, as mentioned above, calls the first thread which had called wait() on a particular object.
Similarly, a notifyAll() wakes up all the threads sleeping due to calling the wait() method.
Hope it helps
Niraj
Hi Kuldeep,
The result is F.
This is because, a Frame by default has BorderLayout. You have not assigned it anyother Layout. so, in this case, the 3 buttons you have created will be added to the Frame sequentially as in the code. Button 1 will be added first and Button 3 will be added last. A Border layout by default will send all the components to teh Center and each component will be put on top of each other. So, you cannot see the 2 buttons below Button3. You can see only the last component added to the BorderLayout, which is Button3. If you had added the buttons to different areas of the BorderLayout..East, West, North...then you could see all the buttons.
HOpe it helps
Niraj
HI Kuldeep,
The "Else" in your code should be "else". In the present code,it will result in a compile error, so I guess the answer should be B.
but, if you have made a typing error, and the "Else" is actually "else", the answer should be C, as you have mentioned.
HI Kuldeep,
All floating-point types are taken as double by default. If you want it to be a float value, you have to specifically cast it using f or F.
this can easily be checked by trying to compile the line -
Float f = 1.0;
It will result in a compile error.
HOpe this helps
Niraj
Hi Siva,
Anonymous classes are used to make coding simple and elegant. Though, it may tend to be confusing if used excessively. As Cindy, mentioned, it is used where you do not need to re-use the code. In that case, there's no need to write extra lines of code.
Anonymous classes can be used commonly in AWT when implementing Listeners. For example, a Listener may be used in 3 ways -
1. class Xyz extends Frame implements MouseListener. In this case you have to override all the 5 methods of the interface in the class Xyz itself. If you are using only 1 of the methods, you have to unnecessarily override and give a body declaration to all the remaining 4 methods.
2. Use MouseAdapter classes. In this case you need to only override the particular method you are using and leave out the rest of the methods as the Adapter class already does the overriding for you. In this case, the methods you are implementing can be declared in another class or in the same class Xyz itself using inner classes. In either case you need to declare a class name using - "extends MouseAdapter".
3. Use anonymous classes and declare the method within the addActionListener method itself. If you are not implementing the mouselistener anywhere else, this is a convenient method.
Hope it helps.
Niraj
19 years ago
Hi Smriti,
I agree with you that no objects will be created, if what you have asked is as entered in the Khalid book. In fact, it should give a compile error.
I do not agree with John, who has mentioned that the following code will compile -
pitBull = new Mammal();
This code cannot compile.
Mammal is the superclass of Dog, so you can write it as
Mammal pitbull = new Dog();//upcasting Dog object to its superclass.
but you can't write -
Dog pitBull = new Mammal(); // downcasting is not allowed
The basic thing to remember is that, you can upcast an object, you cannot downcast it.
The Object class is the superclass of all classes. All classes are subclasses or the class Object. If you define a class which does not extend any other class, such as-
class MyClass {}
Here, though we are not using the extendskeyword to inherit a class, Java implicitly extends the class MyClass with the class Object.
Now, when we write, Object c = new MyClass();
it is correct, but is incorrect to write -
MyClass c = new Object();
Smriti, could you try posting this question in the Mock Errata section and see what other people have to say about it.
I hope that helps.
Niraj
19 years ago
Hi naveen,
I feel the answer would be c.
19 years ago
GC
Line 6 seems right.
Niraj
Hi Junilu,
I think there is one small thing which was wrong in your answer.
When a byte is promoted to an int, and the exteme left digit of the byte is a 1 (which would indicate it is a negative number), the rest of the increased bytes will also be filled with 1's. so,
So, byte -5 = 11111011
but int -5 = 11111111 11111111 11111111 11111011
and not 00000000 00000000 00000000 11111011 as mentioned by you.
This can be checked by the same code above. If we change the
code to -

When we are casting the XOR'd value from int to byte, we are losing the 3 extereme left bytes. But, as per your answer, if the extreme left 3 bytes contain only zero's, then the answer
should not make any difference whether it is a int or a byte.
The actual answer will be as follows -
11111111 11111111 11111111 11111011 //byte -5 after promotion
00000000 00000000 00000000 11111111 //int 0xff
-----------------------------------
11111111 11111111 11111111 00000100 //XOR'd int value = -252
0000100 //casted to byte = 4
Hope that helps
Niraj
Hi Chunky,

I have numbered the complete flow sequence of the program from #1 to #25 at the end of each line in the code.
I am sorry I could'nt get the line numbers in the code aligned up vertically for you, although i tried a few times.
The points to note are in line #17, you are assigning y to 4 and z to 5. You may wonder how, we can
assign y and z when they have not been yet declared. This is because, when a subclass has instance
fields, then before the control goes to the superclass, the fields in the subclass are all automatically
initialized to default values, that is 0 for integer values, 0.0 for float and double values and null for
object references.
Thereafter, in line #18, y is being declared. As y is already initialized to 4, no further assignment is
done. In line #19, z is assigned to 3, hence the value of z is changed from 5 to 3.
Hope that helps.
Niraj
[This message has been edited by niraj singh (edited March 10, 2001).]
[This message has been edited by niraj singh (edited March 10, 2001).]
[This message has been edited by niraj singh (edited March 10, 2001).]