• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Are my answers correct ?

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not going to read through all that. I skimmed to the bottom and saw that you wanted to see a code example of javac barking about using an uninitialized variable. Here you go...

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a lot of questions there. I would like to remind you of the JavaRanch rule of mentioning the source of such mock questions. So please do so.
Thanks.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
steve ..he is talking about the member variable ..they can b used before intialisation.....
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

This is alright�����..

_________________________________________________________________________________
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.

The Ans is 2,3,4�. Not 1.

The general contract of hashCode is:
�Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
�If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
�It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

__________________________________________________________________________________
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.
Ans: Assuming that all three classes are in three java files since no public class is defined.( if it is in a single java file then option 1 is right)
The answer is 2,3,5
 
reply
    Bookmark Topic Watch Topic
  • New Topic