187.
188.
CHAPTER
18
Miscellaneous
Explain what happens when the following code snippet is
compiled.
public class Test {
public static void main(String argument[]) {
int iValue;
System.out.println(iValue);
}
}
Answer:
This code will cause a compilation error. This is because the
variable iValue is not initialized but used in the Sysout
statement. The error can be fixed
by initializing the variable
iValue before it is used as follows:
int iValue = 0;
System.out.println(iValue);
Explain what happens when the following code is
compiled and executed.
class Calculate {
float fValue = 10.2f;
public static void main(String argument[]) {
System.out.println(“Float value is: “+fValue);
189.
190.
}
}
Answer:
The above code will cause a compilation
error because the main
method tries to access the non–static instance variable fvalue.
Java does not allow accessing a static variable from a non–static
method.
Explain symmetric equals() contract.
Answer:
Suppose that there are two objects v1 and v2. If
v1.equals(v2) returns true then v2.equals(v1) must return
true. This equals() contract is called as symmetric contract.
Consider the following code:
Object v1; Object v2;
v1.equals(v2); //Line 1
v2.equals(v1); //Line 2
So, in
case of a symmetric contract, Line 1 returns true if and
only if Line 2 returns true.
Do'stlaringiz bilan baham: