It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green's mock up test questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Green Watch "Marcus Green New topic
Author

Marcus Green's mock up test questions

Nina Binde
Ranch Hand

Joined: Sep 24, 2004
Posts: 85
Hi all,

I am pretty impressed the way this forum works and how people have contributed. Never seen a better forum than this
I just went thru one of the test's from Marcus Green. Have a few questions if anyone is willing to answer:
1. What happens when you attempt to compile and run these two files in the same directory?

//File P1.java
package MyPackage;
class P1{void afancymethod(){
System.out.println("What a fancy method");
}

//File P2.java
public class P2 extends P1{
public static void main(String argv[]){
P2 p2 = new P2();
p2.afancymethod();
}
}
1) Both compile and P2 outputs "What a fancy method" when run
2) Neither will compile
3) Both compile but P2 has an error at run time
4) P1 compiles cleanly but P2 has an error at compile time

The answer given is:
The package statement in P1.java is the equivalent of placing the file in a different directory to the file P2.java and thus when the compiler tries to compile P2 an error occurs indicating that superclass P1 cannot be found.

Now the question I have is, I know the number 4) is the right answer. What does the compiler take by default if the package name is not found?Does it assign it to the root directory?


Now for the second question
2. Which of the following statements about threading are true

1) You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable
2) You can obtain a mutually exclusive lock on any object
3) A thread can obtain a mutually exclusive lock on an object by calling a synchronized method on that object.
4) Thread scheduling algorithms are platform dependent

The answer given is: 2,3,4

I am confused as to why answer 1) is not true. If anyone can explain, I would appreciate it.


Third Question:
Given the following code what will be the output?

class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.print( v.i );
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.print(v.i);
System.out.print(i);
}//End of another}

1) 10030
2) 20030
3) 209930
4) 10020

The given Answer is: 4

According to me the answer should have been: 10010
cos when the method another() exits, the value of i in the object v would be 10.

Thanks for your help.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Woah, one question at a time... Here is one question addressed

"You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable"

That statement is not true.A mutually exclusive lock means only one Thread at a time can enter a portion of code. This can be an object created from a class of any type. Of course a Thread that enters this code must have been created by a Thread class, e.g. extends Thread or implements runnable.
[ October 12, 2004: Message edited by: Marcus Green ]

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

Joined: Sep 24, 2004
Posts: 85
Thank you Marcus for responding to my question. I guess I interpreted it wrongly. But, I must say that answer #1 is ambiguous.
Any takers for the other 2 questions of mine? Sorry about the mess, I will create new messages for different questions next time thru
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
Now Question No. 2

package MyPackage;
class P1{void afancymethod(){
System.out.println("What a fancy method");
}

//File P2.java
public class P2 extends P1{
public static void main(String argv[]){
P2 p2 = new P2();
p2.afancymethod();
}
}
1) Both compile and P2 outputs "What a fancy method" when run
2) Neither will compile
3) Both compile but P2 has an error at run time
4) P1 compiles cleanly but P2 has an error at compile time

The answer given is:
The package statement in P1.java is the equivalent of placing the file in a different directory to the file P2.java and thus when the compiler tries to compile P2 an error occurs indicating that superclass P1 cannot be found.

Now the question I have is, I know the number 4) is the right answer. What does the compiler take by default if the package name is not found?Does it assign it to the root directory?
==================

Yes, compiler takes a default package if no package name is found in the java file. No, it does not assign the default package to the root directory.

You must be aware of the rule that Package name and the directory name must be the same in order to run the programs.


Kapil Munjal
SCJP 1.4, SCWCD 1.4
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
Now, Question no. 3
====================

I must tell you that this is one hell of a question......even I answered it wrong but I have been able to understand why output is 10020.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.print( v.i ); //third print statement executed
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;

ValHold vh = new ValHold();
v = vh;
System.out.print(v.i); //first print statement executed
System.out.print(i); //second print statement executed
}//End of another}


Look at first print statement
It is printing v.i = 10, the reason is when you execute another(ValHold v, int i), v has the reference of newly created object of ValHold, which contains the value of i as 10.

Lets examine another(ValHold v, int i),

first line i=0(it assigns 0 to a local varialble i).

second line v.i = 20, it assigns value of 20 to that i which is an instance
variable of an instance of ValHold which has been created in amethod(), so now in amethod(), the value of i is 20(which was previously 30).

third line ValHold vh = new ValHold(), it creates a new instance of ValHold.

fourth line v=vh, it is assigning reference a new instance of class ValHold, so it will covert the value of i to 10.

Now comes the two lines which are printing............

System.out.print(v.i); //first print statement executed
System.out.print(i); //second print statement executed

Now in the fifth line, it is printing 10 because v has a reference to a new created instance of class ValHold.

Sixth line is printing 0 because i is a local variable.


Now the turn of the last print statement which is the third statement...

It is printing 20 because v.i=20 is changing the value of i in amethod();
The reason behind this is that we can change the value of instance variables in a class but we can't change the reference of a class variable.


I think, the mess created by me is more than which you have made...but I hope you will be able to understand the explanation.

Bye
Kaps
Nina Binde
Ranch Hand

Joined: Sep 24, 2004
Posts: 85
Thank you Kapil for answering both my questions. Question# 3: I did not quite understand the last statement where you say "It is printing 20 because v.i=20 is changing the value of i in amethod();The reason behind this is that we can change the value of instance variables in a class but we can't change the reference of a class variable."
My explanation:
When amethod() calls another(v,i), it initially changes the value of i in the object to v's reference to 20. Soon after that, it creates a new ValHold object and assigns its reference to v's reference , isn't it? So now according to me, v will point to a new ValHold object and its 'i' variable will have a value of 10. When the method another(v,i) returns, v will still point to the same object as it was pointing in another(v,i) method, isnt it?
Now the print statement in amethod() should print 10 instead of 20?

Could you please clarify further? Thanks Kapil!
Nina Binde
Ranch Hand

Joined: Sep 24, 2004
Posts: 85
hmm..Rereading your statement about changing instance variables and not able to change reference variables is making more sense to me than before. In another(v,i) method, when we assign v=vh, we are not changing 'v' reference in amethod(), but changing it locally in another(v,i) method, correct? When we change locally, will it create a new reference?
Thanks.
Wes Gill
Greenhorn

Joined: Oct 13, 2004
Posts: 1
The reason 20 is printed at the end is because a copy of reference v is passed to the another method. For example, say that the object referenced by v has an address of 100. When another(v,i) is called, a COPY of the address of v (in this case 100) is passed to the method. Since a copy of v is passed, the original v (which is a method local variable of amethod) does not change and still references the object at address 100 when the another method returns.

-Wes Gill
Nina Binde
Ranch Hand

Joined: Sep 24, 2004
Posts: 85
Thank you Wes. Finally, clears my doubts.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Green's mock up test questions