Hi, The type of each of the operands in Shift operation must be primitive integral type(short,byte,int,long), otherwise compiler error occurs. 1 & 3 use a String & a double which are not allowed. so the ans 2 & 4 are correct. Hope it helps. Your options are not clear. I had the same problem with one of my Question & Maha anna told me that whenever we use relational operators we should leave space before & after them. otherwise they mess up. Thanks, Kiran.
Here is the actual question: Given the following variables which of the following lines will compile without error? String s = "Hello"; long l = 99; double d = 1.11; int i = 1; int j = 0; 1) j= i << s; 2) j= i << j; 3) j=i << d; 4) j=i << l; 1) Wrong- because the shift cannot be a String 2) Correct- int can be used for both operands 3) Wrong- because the shift cannot be a double 4) Correct- the shift can be a long, the actual shift will be l%32 since the type of i is int. The type of the result depends on the type of the first operand. So if it had been j=l>>i; that would not compile because it would result in a long whereas j is an int. [This message has been edited by lstrite (edited August 05, 2000).]
The reason 1 and 3 are NOT correct is that the result of applying a relational operator is of type boolean. A boolean type cannot be assigned to ANY other type. Interfaces CANNOT be instantiated. A class can implement the interface and that class can then be instantiated.
posted 19 years ago
Answers 1 & 3 were not supposed to be relational, they are supposed to be shift,it just did not print them right. For interfaces, they cannot be instantiated, but you can create a reference and it can hold any object that implements the interface such as this: Runnable r = new myClassThatImplementsRunnable(); But the interface is self cannot be instantiated: Runnable r = new Runnable; //Wrong
posted 19 years ago
Thank you all for your kinda explanations. I understand it now. regards
Of course, I found a very beautiful couch. Definitely. And this tiny ad: