Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Marcus Green Mock Exam#3 questions #40 & #54

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I had posted this on the errata forum. I did not receive any answer there. So, here it goes......
Question #40 says
Given the following code
class Base {
static int oak=99;
}
public class Doverdale extends Base{
public static void main(String argv[]){
Doverdale d = new Doverdale();
d.amethod();
}
public void amethod(){
//Here
}
}
Which of the following if placed after the comment //Here will compile and modify the value of the
variable oak?
1.super.oak=1;
2.oak=33;
3.Base.oak=22;
4.oak=50.1;
The answer given is 1,2 and 3.
I tried this out. 2 and 3 work fine, but on 1, the compiler complains Undefined variable super. I
also tried it with super() - then it complains constructors can only be called by constructors.
Is 1. a good answer or not?

Question #54
public class Inc{
public static void main(String argv[]) {
Inc inc = new Inc();
int i = 0;
inc.fermin(i);
i = i + 1;
System.out.println(i);
}
void fermin(int i) {
i++;
}
}
1.Compile time error
2.Output of 2
3.Output of 1;
4.Output of 0;
I think the answer is 3- Output of 1. I tried it, and it is correct.
The answer given is 4- Output of 0
How come?
Thanks in advance
Savithri
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For question 40
1 worked OK for me
For question 54
In Marcus' exam it is not
i=i+1;
but
i=i++;
try that and you will get output of 0
The value of i is assigned to i before it is incremented.
You would get an output of 1 if the line was
i++;
or
i=i+1;
If you had several lines of i=i++; you would still get an output of 0
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi justin,
i can't get why in second ques. the output was zero.
i =i++;
so i feel it should be executed as
1)i is assigned the value of zero
2)i will be incremented by 1.(i.e. now i becomes 1)
but it prints zero
please explain why this happens clearly?
thanx in advance
deekasha
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option 1 for question 40 does compile, I have just run it. Try
class Base {
static int oak=99;
}
public class Doverdale extends Base{
public static void main(String argv[]){
Doverdale d = new Doverdale();
d.amethod();
}
public void amethod(){
//Here
super.oak=1;
}
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi savithri devaraj,
the Q.40 is working fine.
i think u might have coded "Super" instead of "super"
keep in mind that java is case sensitive.
 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies.
#40 - It is working fine here at work, I must have used Super instead of super at home as Krishna suggested.
#54 - This baffles me. So, until the variable is used the next time, the value is not incremented. Could anyone give an explanation.
Thanks,
Savithri
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,
I agree with the answer that i = i++ will give 0, but I don't understand why either. I'm in the same boat as Savithri here.
I understand that the post incrementation doesn't occur until after the assignment, but according to what actually occurs here, it is as if the post incrementation either doesn't occur or happens before the assignment. Ack!

Help would certainly be appreciated.
Thanks.
-Paul
[This message has been edited by Paul Caudle (edited July 05, 2000).]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why i=i++ then i is equal to 0 either. Try
the following code:
public class Inc{
public static void main(String argv[]) {
Inc inc = new Inc();
int i = 0;
int j=0;
i = j ++;
//i=i++;
System.out.println("i ="+i+" j="+j);
}
}
The result is i=0, j=1.
This means the value of j is passed to i
and then j++; If this is true, in the case of i=i++, the value of i is first passed to i. Then i++; So finally, i is eaqual to
1.
[This message has been edited by bante (edited July 05, 2000).]
 
Justin Moore
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that in i=i++ the value of i prior to the increment is evaluated, then i is incremented, then the value of i that was previously evaluated is assigned to i so it is as if the increment never happens.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Q40 is fine. But in Q54 ,Is this 'i=i+1 ' means arthmetic statement. It is not Unary operator. That too one of the correct answer given in the possible answers,So I think strongly,the answer is 'output of 1'.Suppose if 'i=i+1' is not there,then the answer is 'output of 0'.
Any suggestions or corrections please.
Thanks
Nirmal
 
Justin Moore
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nirmala,
In Marcus' exam it is i=i++; not i=i+1;

[This message has been edited by Justin Moore (edited July 06, 2000).]
 
Nirmala
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin,
Is '1=1++' a valid statement?.
Thanks.
Nirmal
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the fermin(i) does not return anything and in Java I guess post-increment does not work like in C++, you have to pre-increment and then you get 1, else it is 0 (post-increment does not increment)
 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We still didn't get any authoritative answer as to why
i = i++ behaves as it does, especially when the LHS is the variable incremented on the RHS.
Is it always the case that it will not be incremented until after it is used the next time??

Could anyone give an explanation?
Savithri
 
Justin Moore
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nirmala,
typo sorry
Savithri,
No matter how many times you have the statement i=i++; i will not change!
 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
Missed this in the previous posts completely. This behavior is very strange, not intuitive at all!
Savithri
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Originally posted by Savithri Devaraj:
Thanks,
Missed this in the previous posts completely. This behavior is very strange, not intuitive at all!
Savithri


Please refer to the following link for previous discussion.
http://www.javaranch.com/ubb/Forum24/HTML/001546.html

regds
Ajay Kumar
 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for pointing me to the previous discussion. Very informative and good to know I am not the only one confused!
BTW, how do I get to quote the previous posts when I reply? There must be an option I am not seeing. When I push the "Post Reply" button, I get an empty textarea for my reply.
Thanks,
Savithri
 
Ajay Kumar
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just click on the rightmost icon after the posting timestamp info ( the one with a page and a right arrow attached.

Originally posted by Savithri Devaraj:
Thanks a lot for pointing me to the previous discussion. Very informative and good to know I am not the only one confused!
BTW, how do I get to quote the previous posts when I reply? There must be an option I am not seeing. When I push the "Post Reply" button, I get an empty textarea for my reply.
Thanks,
Savithri


regds
Ajay Kumar
 
Nirmala
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
It makes sense. i=i++ and i=i+1 are different. And so in Savithiri's program it should be i=i++.
Thanks.
Nirmal
 
Paul Caudle
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That did the trick. Thanks everyone for the help. It is much appreciated. I hate it when something I thought I understood completely suddenly does something completely unexpected.
Thanks again, y'all
 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by sdev:
Oh! I am sorry it is taking all that fancy faces from smiles legend . Hope you don't mind . I would have disable smiles in this post.
Savithri, could you please send me the website address for the Marcus green Mock exams.
I will be very thankful to u , if u do me the favour. I am planning to take the SCJP2 exam in this month.
Thanks
Sdev
[This message has been edited by sdev (edited July 12, 2000).]



I quite understand what is happening here. Could we close this thread?
Anyway, the Marcus Green exams are at
http://www.jchq.net/faq/jcertfaq.htm
Savithri
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I'm new to this site and was really impressed to see the discussion going to such minute details ! I'm glad I've found this site .
Let me make my contributions to the ongoing discussion.
in the following statement :
i = i++ + i++;
1. As per percedence rules ++ comes before before + and =. Therefore i++ has to be done before any other evaluation.
2. But post-increment returns the value first and increments later.
Thus i = i++ + i++ evaluates to
i = 0 + i++ ( i is 1 )
i.e i = 0 + 1 ( i is 2 )
i.e i = 1
Therefore i is 1
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Deepak M:
i = i++;
Therefore i is 1



Hello everybody! I tried the code below and the value of i became 0. So the answer given is right, could anyone explain me why?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, I just asked my compiler professor, his answer is following:
when we do i=i++, the process is:
temp = i; // for get the value from i before i is increased
i = i+1; // increase i
i = temp; //assignment
Hope that will help.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think dandan is just right from the point of view of software.
From the view of CPU or processor, I think it will be just same.
1)i's value is put into AU (like tmp). so the value in i's registor and AU are both zero;
2)then i++ is caculated as a register caculation without using AU now the value stored in i's register is one.
3)ok, remember now CPU will put the value in AU back to i's registor, which is still zero.
Ok, now the result is just zero.
(Maybe wrong, I almost forgot everything about Microprocessor and CPU)
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dandan & James Kid, now it�s more clear, I think this is beyond the scope of the exam because it�s a bit tricky and doesn�t follow the meaning of Language Fundamentals.

[This message has been edited by Marcela Blei (edited July 13, 2000).]
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just curious...
So is there an implicit assignment to i after iteration of the for loop...
i.e. for(int i=0; i<10; i++) {}
Sorry if i'm dwelling on something already discussed.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for(int i=0; i<10; i++)
{
//your code here
}
is same as:
{//in block
int i=0;
while(i<10){
//your code here
i++;//just before before go back
}
}
and
for(int i=0; i<10 {
//your code here
i++;//or ++i;
}
But it is different from
for(int i=0; (i++)<10; ){}
or
for(int i=0;i<10 {
i++;//or ++i;
//your code here
}
For each loop, i will be plused 1;
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is an implicit assignment to i after iteration of the for loop. But you can only see i because i is defined in the block of for. If you want to see it after for loop, do this:
int i=0;
for(;i<10;i++){}
//at this point, i should be 10(if no break);
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!