Q- Which statements concerning the correlation between the inner
and outer instances of member classes are true?
(selsect two answers)
1) member variables of outer instance are always accessible to inner instances
regardless of their access modifiers
2) member variables of outer instance can never be accessed using only
the variable name wtihin the inner instance
3) More than on inner instance ca be associated with the same outer instance
4) All variables from the outer instance that should be accessible in the
inner instance must be declared final
5) A class that is declared final cannot have any member classes.
Ans- 1) & 3) are correct
_________________________________________________________________________________
Q- Given the following class, which are correct implementations of the hashCode()
method? (select three correct answers)
class ValuePair {
public int a, b;
public boolean equals(Object other) {
try {
ValuePair o = (ValuePair) other;
return (a == o.a && b == o.b) || (a == o.b && b == o.a);
} catch (ClassCastException cce) {
return false;
}
}
public int hashCode() {
// Provide implementation here.
}
}
1) return 0;
2) return a;
3) return a+b;
4) return a-b;
5) return a^b;
6) return (a 16)|b;
Ans -i am 100% confident for 1). But which other two are correct?
hashcodes always confuse me.
__________________________________________________________________________________
Q-What statements concerning the following code are true?
class A {
public A() {}
public A(int i) { this(); }
}
class B extends A {
public boolean B(
String msg) { return false; }
}
class C extends B {
private C() { super(); }
public C(String msg) { this(); }
public C(int i) {}
}
1) the code fails to compile.
2) the constructor in A that takes an int
will never be called as a result of
constructing an object of class B or C.
3) class C defines 3 constructors
4) Objects of class B cannot be constructed.
5) At most one constructor of each class
is called as a result of constructing an
object of class C.
Dont you guys think that the question must specify where all above instances
are created.
_________________________________________________________________________________
The following code outputs 22.
class Base {
int i;
Base() {
System.out.println("Inside Base:STEP 1");
add(1);//this call add(int) method of class Extension
}
void add(int v) { i += v;System.out.println("Inside Base:add(int)"); }
void print() { System.out.println(i); }
}
class Extension extends Base {
Extension() { add(2); }
void add(int v) {
System.out.println("Inside Extension:add(int):STEP 2");
i += v*2;
}
}
public class Qd073 {
public static void main(String[] args) {
//this calls construcor @ line 6 first,coz constructors are executed
//top from the hierarchy.
bogo(new Extension());//System.out.println() i added
//to get the program control flow.
}
static void bogo(Base b) {
b.add(8);
b.print();
System.out.println(b.i);
}
}
Consider the following two lines
bogo(new Extension) ;
static void bogo(base b), are
These two equivalent to Base b=new Extension();
Suppse we do the following changes in the code above
i) int i=99; //in 2nd line from top
ii)System.out.println(b.i); //add this after last line
then Keeeping in mind 'dynamic binding', b.i should print 99. ISnt it?
But it prints 22. why?
if you dont know 'dynamic binding', here is the code that illustrates it-
class Base {
protected int size = 100;
public int getSize() {
return size;
}
}
class SubClass extends Base {
protected int size = 10;
public int getSize(){
return size;
}
public static void main(String[] args) {
Base b = new SubClass();
System.out.println (b.size + "," + b.getSize());
}
}
/* whats the output ?
Compilation succeeds, the output is 10,100
Compilation succeeds, the output is 100,100
Compilation succeeds, the output is 10,10
Compilation succeeds, the output is 100,10
Compilation fails, the field size is not visible
Answer 4 is correct. In the
Java language, the behaviour of 'overriding' methods and
fields are different. At line 8 of the code example, the compiler associates the
getSize method with the class of b. That is, the getSize method of SubClass will be invoked.
This is called dynamic binding and is how Java implements
polymorphism.
However, for field declarations dynamic binding is not applied.
For the size field, the compiler associates the field with the class type of b (Base).
Therefore, when the program is run, b.size evaluates to 100 and b.getSize() evaluates to 10.
So fields are bound at compile time and methods (non-static ones) are
bound are run time.
*/
___________________________________________________________________________________
Which statements concerning the value of a member variable are true,
when no explicit assignments have been made?
(select 3 correct answers)
1)The value of a memeber variable of type int is
undetermined
2)The value of a member variable of any numeric type
is 0.
3)The compiler issue an error if the member variable
is used before its initialised.
4)member variable of type String will denote the
empty string ("");
5)The value of all variables which are references
is null.
ans-2),5) i am 100% confident it.Eliminate-1) & 4) as
they are nonsense.So final correct ans is 3). But can anyone illustrate
option 3) using a code snippet??