Followers

Sunday, 5 August 2012

Question Refernce converstion


Question 1

class A {
  public static void main(String[] args) {
    char a = 'a', b = 'b'; // 'a' = 97, 'b' = 98
    System.out.print(a + b + "" + a + b);
}}
What is the result of attempting to compile and run the program?
a.   Prints: 390
b.  Prints: 195195
c.   Prints: 195ab
d.  Prints: ab195
e.   Prints: abab
f.   Run-time error
g.  Compile-time error
h.  None of the above

Question 2

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Red {
  public static void main(String args[]) {
    Sub s1 = new Sub();
    I2 i2 = s1;          // 1
    I1 i1 = s1;          // 2
    Base base = s1;      // 3
    Sub s2 = (Sub)base;  // 4
}}
A compile-time error is generated at which line?
a.   1
b.  2
c.   3
d.  4
e.   None of the above

Question 3

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Orange {
  public static void main(String args[]) {
    Base base = new Base();
    I1 i1 = base;           // 1
    Sub sub = (Sub)base;    // 2
}}
What is the result of attempting to compile and run the program?
a.   Compile-time error at line 1
b.  Run-time error at line 1
c.   Compile-time error at line 2
d.  Run-time error at line 2
e.   None of the above

Question 4

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Yellow {
  public static void main(String args[]) {
    Base base = new Sub();  // 1
    I1 i1 = base;           // 2
    Sub sub = (Sub)base;    // 3
    I2 i2 = (Sub)base;      // 4
}}
A compile-time error is generated at which line?
a.   1
b.  2
c.   3
d.  4
e.   None of the above

Question 5

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Gray {
  public static void main(String []args) {
  Base[] base = {new Base()};  // 1
  Sub sub[] = {new Sub()};     // 2
  Object obj = sub;            // 3
  base = obj;                  // 4
}}
A compile-time error is generated at which line?
a.   1
b.  2
c.   3
d.  4
e.   None of the above

Question 6

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Silver {
  public static void main(String []args) {
    Base[] base = {new Base()};
    Sub sub[] = new Sub[1];    // 1
    Object obj = base;         // 2
    sub = (Sub[])obj;          // 3
    I1 []i1 = (I1[])obj;       // 4
}}
What is the result of attempting to compile and run the program?
a.   Compile-time error at line 1
b.  Run-time error at line 1
c.   Compile-time error at line 2
d.  Run-time error at line 2
e.   Compile-time error at line 3
f.   Run-time error at line 3
g.  Compile-time error at line 4
h.  Run-time error at line 4
i.   None of the above



Answers


No.
Answer
Remark
1
Prints: 195ab 
Both operands of the first addition operator are promoted from type char to int, and are evaluated as integral numeric values. The right hand operand of the second addition operator is of type String, so the result of the first addition operator is converted to type String, and is concatenated with the right hand operand. As evaluation of the expression continues from left to right, the remaining operands are also converted to type String.  
2
None of the above 
Line 4 does not generate a compile-time error. The reference named base actually refers to an instance of type Sub, so the reference may be cast to type Sub.  
3
Run-time error at line 2 
The compiler accepts the explicit cast at line 2, but an error is generated at run-time. Type Base is the super class of type Sub, so an instance of type Base can not be converted to the type of the subclass, Sub.  
4
None of the above 
Although the reference named base is of type Base, the instance that it refers to is of type Sub, and the reference can be cast to type Sub. Since instances of type Sub implement both interfaces, I1 and I2, the Sub type instances can be assigned to references of type I1 and I2 without an explicit cast.  
5
The Object referenced by obj is of type Sub[], and the reference, base, is of type Base[]. The assignment expression, base = obj requires an explicit cast to type Base[] as follows: base = (Base[])obj.  
6
Run-time error at line 3 
Base is the superclass of type Sub, so a reference to an actual instance of type Base can not be cast to type Sub. Therefore, a reference to an array instance of type Base[] can not be cast to type Sub[]. The type of the reference, obj, is Object. Type Sub[] is a subclass of Object. The compiler accepts the cast, because the actual instance referenced at run-time might be of type Sub[]. In this case, the actual type of the instance referenced by obj at run-time is found to be type Base[], so the result is a run-time error.  


No comments:

Post a Comment