aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sharing Tricky Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sharing Tricky Questions " Watch "Sharing Tricky Questions " New topic
Author

Sharing Tricky Questions

Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Hi,
I ran into this tricky question and i thought of sharing it.
I would be glad if you guys do the same

The following code snippet will not compile...
int i = 10;
System.out.println( i<20 ? out1() : out2() );
Assume that out1 and out2 have method signature: public void out1(); and public void out2();

The comment about it is

Note that it is not permitted for either the second or the third operand expression to be an invocation of a void method.
In fact, it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear.
The first expression must be of type boolean, or a compile-time error occurs.
The conditional operator may be used to choose between second and third operands of numeric type, or second and third operands of type boolean, or second and third operands that are each of either reference type or the null type. All other cases result in a compile-time error.
The type of a conditional expression is determined as follows:
If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression.
Otherwise, if the second and third operands have numeric type, then there are several cases:
If one of the operands is of type byte and the other is of type short, then the type of the conditional expression is short.
If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T.
Otherwise, binary numeric promotion (�5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands.
If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.
If the second and third operands are of different reference types, then it must be possible to convert one of the types to the other type (call this latter type T) by assignment conversion (�5.2); the type of the conditional expression is T. It is a compile-time error if neither type is assignment compatible with the other type.
At run time, the first operand expression of the conditional expression is evaluated first; its boolean value is then used to choose either the second or the third operand expression:
If the value of the first operand is true, then the second operand expression is chosen.
If the value of the first operand is false, then the third operand expression is chosen.
The chosen operand expression is then evaluated and the resulting value is converted to the type of the conditional expression as determined by the rules stated above. The operand expression not chosen is not evaluated for that particular evaluation of the conditional expression.


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
final class F{}
interface P{}
public class Conversion
{
public static void main(String args[])
{
F f1 = new F();
P p1 = (P)f1;
}
}
-------------------------------------------------
What is the output?
a) Compile time error
b) Runtime error
c) No error


Sunita<br />SCJP 1.4
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Answer: Compile Time Error
Because interface P can NOT possibly be implemented in any of the subclasses of F, since F can't be subclassed as it's declared final
If the F was not declared final, then the code would compile fine because the compiler would think "Maybe a subclass of F would implement P and the reference f actually point to an object of runtime type of the subclass that implements P" but when run, it would throw a ClassCastException since F and P are not related.
If P was a class, not an interface and even F would not be declared final but it doesn't extend P, it would still not compile. Why? Cause a subclass of F can never subclass P as well, there is no multiple inheritance in Java
Now what would this code do?:

1- Compile Error
2- Runtime Error
3- No Error
[ October 17, 2002: Message edited by: Alfred Kemety ]
[ October 17, 2002: Message edited by: Alfred Kemety ]

Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sharing Tricky Questions