wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Marcus mock exam 3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Marcus mock exam 3" Watch "Doubt in Marcus mock exam 3" New topic
Author

Doubt in Marcus mock exam 3

Shyamala S
Greenhorn

Joined: Sep 12, 2000
Posts: 10
Question 25)
Which of the following statements are true?
1) The following statement will produce a result of 1. System.out.println( -1 >>>2);
2) Performing an unsigned left shift (<<<) on a negative number will always produce a negative number result <br /> 3) The following statement will produce a result of zero, System.out.println(1 >>1); <br /> 4) All the integer primitives in java are signed numbers <br /> <br /> Answer to Question 25) <br /> Objective 5.1)<br /> 3) The following statement will produce a result of zero, System.out.println(1 >>1);
Although you might not know the exact result of the operation -1 >>> 2 a knowledge of the way the bits will be shifted will tell
you that the result is not plus 1. (The result is more like 1073741823 ) There is no such Java operator as the unsigned left shift. Although it is normally used for storing characters rather than numbers the char Java primitive is actually an unsigned integer
type.
============
my doubt is the option 4 specifically asks for integer primitives and the answer explains about char Java primitive ....is char an integer primitive ??
-------------------------------------------
Question 57)
Given the following code
class Base {}
class Agg extends Base{
public String getFields(){
String name = "Agg";
return name;
}
}
public class Avf{
public static void main(String argv[]){
Base a = new Agg();
//Here
}
}
What code placed after the comment //Here will result in calling the getFields method resulting in the output of the string "Agg"?
1) System.out.println(a.getFields());
2) System.out.println(a.name);
3) System.out.println((Base) a.getFields());
4) System.out.println( ((Agg) a).getFields());
Answer
4) System.out.println( ((Agg) a).getFields());
The Base type reference to the instance of the class Agg needs to be cast from Base to Agg to get access to its methods.The
method invoked depends on the object itself, not on the declared type. So, a.getField() invokes getField() in the Base class,
which displays Base.
------
Here Marcus says that method invoked depends on the object itself, not on the declared type, which means the object which is an Agg will invoke its method not the method of the declared type Base....am i right in this understanding, pls explain.
Ramesh Donnipadu
Ranch Hand

Joined: Sep 16, 2000
Posts: 100
Syamala,
1) In Java all primitives are divided into two groups. (a) integer type and (2) floating type. byte, short, char, int are integer primitives. float and double are floating point type. So, char is a integer primitive.
2) This is an interesting question. when I first looked at this question I thought a.getFields() also gives desired result. Nope. Compiler didn't even allow me to proceed.
It is true that the method invocation depends on actual object and does not depend on reference type. But this applies only when the method being called is a overridden (and non-static)one. In this case since the method is not defined in base class, it is not a overriding case. So, compiler doesn't look at object type. It looks for the method in the base class. As it can't find any getFields() method in the base class, it flags an error.
Does that sound convincing? The above explanation is only my guess. I would appreciate if someone confirms or gives actual reason.
Thanks,
Ramesh
scott irwin
Ranch Hand

Joined: Aug 07, 2000
Posts: 87
We need to remember compile-time vs. run-time. At compile time, all the compiler knows is the type of the variable (a is of type Base). Base doesn't have the method getFields(), so a compile-time error occurs. The cast says treat Base as an Agg type at compile-time. The compiler says a type of Base COULD be an Agg, okay we'll proceed and find out at run-time.
At run-time, the cast must hold true or a RuntimeException occurs. Only for method calls, the VM does a dynamic look-up for the method. Here is where overriding comes into play.
If you are attempting to get an instance variable however, NO dynamic look-up is done! You get the value from the TYPE.variable.
[CODE]
class Base {
public int x = 10;
public void whereAmI() { System.out.println("in base"); }
}
class Agg extends Base {
public int x = 500;
public void whereAmI() { System.out.println("in Agg"); }
public static void main(String[] args) {
Base b = new Agg();
b.whereAmI(); // dynamic lookup "in base"
System.out.println("x is " + b.x); // x of type Base
}
}
shows
in Agg
10
Shyamala S
Greenhorn

Joined: Sep 12, 2000
Posts: 10
thanks both of you. i now understood this clearly.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
In my answer to Question 57 I mention that it the quesiton is probably more complex than you will find in the real exam, though the question did come out of some real live coding I was doing.
Do you think my explanation is sufficient and accurate.
Here it is as it appears in the answers...
"The Base type reference to the instance of the class Agg needs to be cast from Base to Agg to get access to its methods.The method invoked depends on the object itself, not on the declared type. So, a.getField() invokes getField() in the Base class, which displays Base. But the call to ((Agg)a).getField() will invoke the getField() in the Agg class. You will be unlucky to get a question as complex as this on the exam."


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Ramesh Donnipadu
Ranch Hand

Joined: Sep 16, 2000
Posts: 100
Hi Marcus,
Your explanation to Q 57 is good but I feel incomplete. You do say a.getField() invokes getField() in the Base class, which displays Base , but what is not immediately clear is, what happens after that. May be you might want to mention the fact that getField() is not available in Base class and a compile time error occurs.
Shyamala S
Greenhorn

Joined: Sep 12, 2000
Posts: 10
hi marcus,
i too feel the same as Ramesh. i couldnt at the first glance of the explanation note that it is becoz of the method not available in base class... probably you could modify the explanation a bit.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Marcus mock exam 3