Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Doubts on the mock Exam

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,
Here are some questions for which I am not able to get the right answer.
Which statements are accurate:
a) >> performs signed shift while >>> performs an unsigned shift.
b) >>> performs a signed shift while >> performs an unsigned shift.
c) << performs a signed shift while <<< performs an insigned shift.
d) <<< performs a signed shift while << performs an unsigned shift
I think it is b.
But says it is wrong..
--------------------------------------------------------------
class Super
{ int index = 5;
public void printVal()
{ System.out.println( "Super" );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( "Sub" );
}
}
public class Runner
{ public static void main( String argv[] )
{ Super sup = new Sub();
System.out.print( sup.index + "," );
sup.printVal();
}
}
What will be printed to standard output?
a) The code will not compile.
b) The code compiles and "5, Super" is printed to standard output.
c) The code compiles and "5, Sub" is printed to standard output.
d) The code compiles and "2, Super" is printed to standard output.
e) The code compiles and "2, Sub" is printed to standard output.
f) The code compiles, but throws an exception
correct answer is c. Not able to understand how.
----------------------------------------------------------------
Any light would be appreciated.
Kaushik
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are three shift operators
left shift <<<br /> signed right shift >>
unsigned right shift >>>

so only (a) is the correct answer
 
rajani peddi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the second question-

System.out.print( sup.index + "," ); // sup.index will point to its instance variable which is 5

sup.printVal(); // sup is a reference to its subclass and the
method printVal() is overriden. so when it calls the method
with the reference sup the method in the subclass is invoked.
Hence 5,sub is printed out
correct me if i am wrong
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first one a) is the only correct answer.
The second question, remember this:
For methods, it's the methods of actual object that the reference denotes at runtime that gets execute.
For variable, it's the variables of object that the reference denotes at compile time that gets execute.
I know it's confusing, it took me awhile to understand it.
Let me know if you let more help.
 
Kaushik Subramanya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adrian Yan:
The first one a) is the only correct answer.
The second question, remember this:
For methods, it's the methods of actual object that the reference denotes at runtime that gets execute.
For variable, it's the variables of object that the reference denotes at compile time that gets execute.
I know it's confusing, it took me awhile to understand it.
Let me know if you let more help.


 
Kaushik Subramanya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all...
My doubts are clear
~Kaushik

Originally posted by Adrian Yan:
The first one a) is the only correct answer.
The second question, remember this:
For methods, it's the methods of actual object that the reference denotes at runtime that gets execute.
For variable, it's the variables of object that the reference denotes at compile time that gets execute.
I know it's confusing, it took me awhile to understand it.
Let me know if you let more help.


 
reply
    Bookmark Topic Watch Topic
  • New Topic