Usha Damarla

Greenhorn
+ Follow
since Aug 29, 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 Usha Damarla

Hi Ashok,
Q17.The private modifier can be applied to ...
a)A variable
b)A method
c)A class
d)All of the above
Ans: I would answer 'd'
I don't mind whether it is a inner class or top level class as far as it is a class.
When they ask question like this "What modifiers can we apply for a top level class?", then you know what to answer (public & default).
Q20.An unary operators operate on a single value
a)True
b)False
Answer: a
Example:
int i = 1;
i = 2 + -3;
System.out.println(i);
Q22.The switch() construct is used to make a choice based upon ...
a)A char value
b)An int value
c)A String value
d)None of the above
Answer: a,b
But Java2 Exam Prep by Bill Brogden does not mention about 'char value' in the book, but JLS does.
May be char gets promoted to int or shorter than int primitive.
Someone has to clear this.
Q23.The circumstances that can prevent execution of the code in a finally block are
a)the death of the thread
b)The use of System.exit()
c)It is always guaranteed to be executed.
Answer: a,b
Q32.Please select invalid statement(s) for a thread
a)You can restart a dead thread
b)You can't call the methods of a dead thread
c)Both of the above
d)None of the above
Answer: a
_______
Q29.An inner class created inside a method can access
a)Any local variables of a method that contain an inner class.
b)Any instance variables of the enclosing class
c)Any final variables of the enclosing class or a method that contain an inner class.
d)None of the above
Answer: a,b,c
I am just looking at one side of the coin in this case.
My answers may be wrong, please correct me.
Regards
Usha
<PRE>
I am still not clear here.
What is the difference between
set1)
a=1;
a = a + 2 + a++;
Output: a =4
set2)
a=1;
a = a++ + 2 + a;
Output: a = 5
Why am I getting two different answers here?
------------------- JLS ------------------
15.7 The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right
15.7.2 Evaluate Operands before Operation
The Java programming language also guarantees that every operand of an operator (except the conditional operators &&, | |, and ? appears to be fully evaluated before any part of the operation itself is performed.
------------------------------------------------
Also, the ++ operator has high precedence over binary operator +
Now my understanding is as follows:
The RHS is evaluated like this:

In Set1,
(a + 2) + a++; // point 15.7 in JLS
= (1 + 2) + a++
= 3 + a++
= 3 + 1
= 4 (a has a value 2 in the buffer, but does not get assigned to the var 'a')
In Set2,
a++ + 2 + a;
= (a++ + 2) + a
= (1 + 2) + a
= 3 + a (also a++ gets evaluated which results in a = 2 in the buffer, not in the variable 'a')
= 3 + 2
= 5
try this stmt
a=1;
a=a++ + 2 + a + a++ + 1 + a++;
results in a = 11
if a++; is a standalone stmt then it increments it by 1 and saves it in the var.
My understanding may be wrong. Please comment.
Regards
Usha
</PRE>
Hi Ernst,
You got this question from Guoqiao Sun's mock exam site, right.
Infact the toString() method gets called here. Guoqiao
actually forgot to concatenate "null" with an empty string. The original stmt
if(i == 0) return null;
was actually returning a null value instead of a string, causing it to fail at runtime.
Change the code like this and try again.
public String toString()
{
if(i == 0) return ""+null;
else return "" + i;
}

Guoqiao, its ur wish to modify either question or your explanation.

Regards
Usha
ps: Removed the < PRE > tags in the post
since it was difficult to read.
- satya

[This message has been edited by Madhav Lakkapragada (edited December 18, 2001).]
Typo error in Q#7 just like question 31.

D: The code does not compiles.
D: The code compiles but cannot run.
Okay, its time to go for lunch, good work Guoqiao.

Hi Guoqiao,
I am back again working on your mock exam. I like the way you presented the answers, placing them next to questions, reduces lot of overhead, looking back and forth etc.
One suggestion regarding Q22.
I don't think you need
"What is the output of trying to compile and run the following code? " at the beginning of question number 22.
Becz there is no code in the question.
I think you copied the tags from the previous questions and made corections to it.

usha

[This message has been edited by Thomas Paul (edited September 04, 2001).]
<PRE>
Small typo error in question # 31.
You got two choices with the same name.
D: The code does not compile because of line //1
D: The code does not compile because of line //2
The last choice should be "E". And the answer for this question
is "The code does not compile because of line //2 "
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
What is the output of trying to compile and run the following code?
(Select one correct answer)
-----------------------------------------------------------------------
public class Test031
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");;
String st = new String("World");

System.out.println(sb.append(st)); //1
System.out.println(st.concat(sb)); //2
}
}
-----------------------------------------------------------------------
A: The code compiles and runs with output:
HelloWorld
WorldHello
B: The code compiles and runs with output:
WorldHello
HelloWorld
C: The code compiles and runs with output:
Hello
World
D: The code does not compile because of line //1
D: The code does not compile because of line //2
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
<PRE>
Hi Guoqiao,

I think the answer for question 42 is D. I got the output "0"
Here is the question. Please correct me if I am wrong.
public class Test042 extends Super
{
private int i = 1; //1

public static void main(String args[])
{
Super s = new Test042(); //2
System.out.println(s.i); //3
}
}
class Super {
protected int i = 0;
}

</PRE>
<PRE>
Hi Guoqiao Sun
Your idea is excellent.
Question number 17 was quite interesting to me.

public class Test017
{
public static void main(String args[])
{
System.out.println(Math.abs(Byte.MIN_VALUE) > 0);
System.out.println(Math.abs(Long.MIN_VALUE) > 0);
System.out.println(Math.abs(Short.MIN_VALUE) > 0);
System.out.println(Math.abs(Float.MIN_VALUE) > 0);
}
}

I was expecting "true, true, true, true"
Refered Sun's web site for abs() method. http://java.sun.com/products/jdk/1.2/docs/api/java/lang/Math.html#abs(long)
What I understood is,
When argument to abs() is either Integer.MIN_VALUE or Long.MIN_VALUE, the result remains same, ie.,
negative
Integer.MIN_VALUE = -2147483648 if we negate this value, which is 2147483648 and
it CANNOT be represented in int type.
The max value that can go into int data type is 2147483647
Same theory applies to abs(Long.MIN_VALUE)

Cheers
Usha
</PRE>
I put indentation in message, but it does not show up after submitting it.
Anyway copy the table to some editor and put some spaces.
Very simple, if you can understand what I wrote here.
Say we have
"answer = M shift_operator N"
where M is the value to be shifted
N is the number of times to be shifted
"answer" is where the result gets stored
To make it simple let us divide the shift
operators into 2 categories
Category1 (right shift) will have
case1: M >> N
case2: -M >> N
case3: M >>> N
Formula:
answer = M/2^N
(In words, m divided by 2 to the power of N
(I don't know how to write power of N here)
Sign of "answer" = sign of M
if answer is not an integer
** take integer part only
(for ex: answer = 123.45, consider 123 only)
** add "1" to answer in case2.

Category2 (left shift) will have:
case4: M << N<br /> case5: -M << N<br /> Formula:<br /> answer = M * (2^N) <br /> (In words, M multiplied by 2 to the power of N)<br /> Sign of "answer" = sign of M<br /> <br /> Covering my explanation with an example:<br /> <br /> M N >> ->> >>> << -<<
-----------------------------------------------------------

129 4 8 -9 8 2064 -2064
272 2 68 -68 68 1088 -1088
745 1 372 -373 372 1490 -1490
123456 3 15432 -15432 15432 987648 -987648

I did not mention about case -M >>> N. I have not worked on it.
It always resuls in large numbers.
Does the exam centres permit to bring calculators?
The solution here is very simple with the use of a calculator.
You can tell the answer in less than 10 seconds.
Hope it is clear now. If it confuses just ignore.
Hi Srini,
SUN does not allow to transfer the voucher on a different person name. The voucher will have almost one year validity from the date of purchase. Postpone and try to prepare well for the exam.
Instead of losing your money for nothing, better give a try. who knows you may come out with a big smile out of the exam room?
Good luck
Usha
The following lines are taken from sun's web site http://java.sun.com/docs/books/jls/first_edition/html/javaio.doc16.html

""""22.16.2
public FileOutputStream(File file)
throws SecurityException, FileNotFoundException
This constructor initializes a newly created FileOutputStream by opening
a connection to an actual file, the file named by file in the file system. A new FileDescriptor object is created to represent this file connection.
First, if there is a security manager, its checkWrite method (�20.17.21) is called with the path represented by the file argument as its argument.
If the actual file cannot be opened, a FileNotFoundException is thrown.""""
My answer is D.
Justification:
The first line creates a new File called "hello.txt" if it does not exist. Nothing will happen if the file already exists.
checkWrite method in Securitymanager class throws SecurityException if writing, modifying, creating (for output), or renaming the specified file or directory is not permitted.
we cannot write data if html.txt is not opened.
FileOutputStream constructor throws a fileNotFoundException if the actual file cannot be opened.
The list of possible answers for this question does not mention anything about exception, it means the file is ready to be written.
Hi Mateen,
Numeric promotion to the operands is applied using Identity
conversion or widening primitive conversion.
In our case it is widening conversion.
"The following conversions on primitive types are called the
widening primitive conversions :
byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double "
Now the operand 'O' will be promoted to int (which is 48 unicode
value) as the other operand 7 is an integer.
The result of the expression 48 - 7 is 41.
Now JVM uses "Narrowing Primitive Conversion" as part of "Assignment Conversion" if all the below mentioned conditions
are met.

1. The expression is a constant expression of type int. (true, 48 is a constant of type int)
2. The type of the variable is byte, short, or char. (true, variable k is of type char)
3. The Value of the expression is representable in the type of the variable. (true, the constant 48 fits in 16 bits, size
of char is 16 bits)

Now in your example all the above mentioned conditions are met, and the conversion is error free. There is no need for explicit
conversion.
I will give you an example where you will need explicit
conversion.
For ex: the result of the expression is 65537
You will get a compile time error "possible loss of precision"
if you write
char k = 65537;
In this case you need explicit conversion i.e., char k = (char) 65537;
Refer 2.6 for more info at http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#16021