Multiple choice questions..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carly
    New Member
    • Oct 2006
    • 37

    Multiple choice questions..

    hi,

    i've been solving these multiple choice questions, and i'd like to know if what i answered is right ot wrong......... just to double check :)

    I) Consider the code given below.

    Code:
       class Base{} 
        public class MyCast extends Base{ 
            static boolean b1=false; 
            static int i = ‐1; 
            static double d = 10.1; 
            public static void main(String argv[]){ 
                 MyCast m = new MyCast(); 
                 Base b = new Base(); 
                 //………..Here 
        }}
    Which of the following, if inserted at the comment //Here will allow the code to compile and run without error
    (a) b=m;
    (b) m=b;
    (c) d =i;
    (d) b1 =i;

    - chose (a) because a variable which is declared as a base class can also take instances of subclasses, whereas a variable that's declared as a subclass cannot be set to a superclass.


    (II) 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;

    (a) j= i <<s;
    (b) j= i<<j;
    (c) j=i<<d;
    (d) j=i<<l;

    - I chose (b) because you can't shift by a long, double, or a string.


    thanks,

    carly :D
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by carly
    hi,

    i've been solving these multiple choice questions, and i'd like to know if what i answered is right ot wrong......... just to double check :)

    I) Consider the code given below.

    Code:
    class Base{} 
    public class MyCast extends Base{ 
    static boolean b1=false; 
    static int i = ‐1; 
    static double d = 10.1; 
    public static void main(String argv[]){ 
    MyCast m = new MyCast(); 
    Base b = new Base(); 
    //………..Here 
    }}
    Which of the following, if inserted at the comment //Here will allow the code to compile and run without error
    (a) b=m;
    (b) m=b;
    (c) d =i;
    (d) b1 =i;

    - chose (a) because a variable which is declared as a base class can also take instances of subclasses, whereas a variable that's declared as a subclass cannot be set to a superclass.


    (II) 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;

    (a) j= i <<s;
    (b) j= i<<j;
    (c) j=i<<d;
    (d) j=i<<l;

    - I chose (b) because you can't shift by a long, double, or a string.


    thanks,

    carly :D
    For the first one, with a 1.5 compiler you also get
    d =i; as valid. An integer can be stored in a double reference

    For the second one, You can shifta long!

    Comment

    Working...