• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Marcus Exam2 #46

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given,
String s = "Hello";
long l = 99;
double d = 1.11;
int i = 1;
int j = 0;
which lines below would compile?
1) j=i<<s;>
2)j= i<< j;
3)j=i<<d;>
4)j=i<<1;
Answeris 2 and 4
can some one explain??

Also, is this statement true?
"An interface cannot be instantiated"
thank you
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lstrite
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
cowboy55
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your kinda explanations.
I understand it now.
regards
 
reply
    Bookmark Topic Watch Topic
  • New Topic