Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Are my answers correct ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Are my answers correct ?" Watch "Are my answers correct ?" New topic
Author

Are my answers correct ?

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
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??


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
steve ..he is talking about the member variable ..they can b used before intialisation.....


Thanks & Regards<br /> <br />-Srikanth
Purujit Saha
Ranch Hand

Joined: Nov 01, 2005
Posts: 86
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
 
 
subject: Are my answers correct ?
 
Similar Threads
about static variable question
Overriding,Overloading - Question
Mock Questions
Exam questions on inner classes -help!
mughal mock question