It prints 0admin wrote:What happened when you tried it out?
About Question enthuware.ocajp.i.v7.2.1114 :
Moderator: admin
-
- Posts: 8
- Joined: Thu Mar 24, 2016 12:02 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
-
- Posts: 14
- Joined: Mon Jun 20, 2016 11:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
What will the following code print?
Hi,
i couldn't understand how if int x=0 is removed from looper(); method would result in printing 0 and end?Though i have seen the earlier posts i couldnt understand the replies.Please if anyone could help me.I am little new to JAVA.Thank you!
Code: Select all
public class TestClass{
int x = 5;
int getX(){ return x; }
public static void main(String args[]) throws Exception{
TestClass tc = new TestClass();
tc.looper();
System.out.println(tc.x);
}
public void looper(){
int x = 0;
while( (x = getX()) != 0 ){
for(int m = 10; m>=0; m--){
x = m;
}
}
}
}
Hi,
i couldn't understand how if int x=0 is removed from looper(); method would result in printing 0 and end?Though i have seen the earlier posts i couldnt understand the replies.Please if anyone could help me.I am little new to JAVA.Thank you!
Last edited by admin on Sun Jun 26, 2016 1:48 pm, edited 1 time in total.
Reason: Please put code within [code] [/code]
Reason: Please put code within [code] [/code]
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
If you remove int x=0 from looper, there will be only one variable named x in the whole code and that is the instance variable x. This x will be modified in look by the statement x = m; At the end of the loop, x will be assigned 0.
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Mon Jun 20, 2016 11:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Wow!Thank you .I understood it very well!
-
- Posts: 1
- Joined: Fri Aug 12, 2016 8:59 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
I just wonder how this code will compile since x is an instance variable in the class. and the loop declare it with the same name? Is it work?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri Mar 17, 2017 1:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Why this code compiles and do not complain about unreachable System.out.println statement due to infinite loop.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Compiler does not execute code. It does not know the value of the variables (unless they are compile time constants). So it does not know that it is an infinite loop.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri Mar 17, 2017 1:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Thanks a lot Enthuware. Cleared my certification 1Z0-803 . Tests helped me a lot. definitely a must have before actual exams!!!
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Hi!!
I would like to know if an infinite loop generate some kind of exception or error, is there no problem with the memory or the hard disk?
I would like to know if an infinite loop generate some kind of exception or error, is there no problem with the memory or the hard disk?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
A simple for loop that does not consume any memory can run forever -Javier wrote:Hi!!
I would like to know if an infinite loop generate some kind of exception or error, is there no problem with the memory or the hard disk?
for(;;);
If the loop consumes memory, of course, it will eventually cause OutOfMemoryError or StackOverflowError (if there is recursion involved).
If you like our products and services, please help us by posting your review here.
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Thank you so much Admin for your answer!!
Enthuware is great, I learnt a lot of with your explanations, I feel very confident to try to pass the OCA certificate.
Thank you guys!!
Enthuware is great, I learnt a lot of with your explanations, I feel very confident to try to pass the OCA certificate.
Thank you guys!!
-
- Posts: 8
- Joined: Tue Nov 07, 2017 8:23 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Hi There
I'm finding the !=(Not equals) quite confusing .
The interpretation of the != operator is that , if values are not equal then the condition evaluates to true,
or evaluating to true if the values are different.
The while loop has the following condition : while(x = getX() !=0) .This implies 5 !=0 .So if I use the definition
of the != that says if values are not equal the condition evaluates to true ...why does the while loop evaluate
to false.
I'm finding the !=(Not equals) quite confusing .
The interpretation of the != operator is that , if values are not equal then the condition evaluates to true,
or evaluating to true if the values are different.
The while loop has the following condition : while(x = getX() !=0) .This implies 5 !=0 .So if I use the definition
of the != that says if values are not equal the condition evaluates to true ...why does the while loop evaluate
to false.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
You understanding is correct. while loop does not evaluate to false. It evaluates to true. Why do you think it evaluates to false here?
If you like our products and services, please help us by posting your review here.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
So the fact that the correct answer is that it will run forever and not throw an exception (OutOfMemeoryException) at run time, mean that neither the for loop nor the assignment within it (x=m) consume any memory?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Correct. Heap memory is consumed only when you create any object. stack memory is consumed when you create local variables. Neither is happening here.
(If you are not clear about the difference between Heap and Stack, you might want to go through a good book. Section 1.6 of Hanumant's book explains this nicely. )
(If you are not clear about the difference between Heap and Stack, you might want to go through a good book. Section 1.6 of Hanumant's book explains this nicely. )
If you like our products and services, please help us by posting your review here.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Section 1.6 really brought together how JVM stores local variables and objects.
One thing was not explicitly mentioned though, where field variables are stored?
Also, since arrays are objects, they will be stored on the heap whether they are field or local arrays, correct?
One thing was not explicitly mentioned though, where field variables are stored?
Also, since arrays are objects, they will be stored on the heap whether they are field or local arrays, correct?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
It is mentioned quite clearly in point 5 of Points to remember pg 117:
Only temporary variables i.e. variable created in a method (also known as local variables and automatic variables) are created on the stack space. Everything else is created on the heap space. If you have any doubt, ask yourself this question - is this a temporary variable created in a method? Yes? Then it is created on the stack. No? Then it is on the heap.
The same point 5 also says, "Actual objects are ALWAYS created on the heap.". So, there shouldn't be any confusion about where arrays are stored.
If you like our products and services, please help us by posting your review here.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
So that's what 5 was about. Well now I don't have to ask that question Thanks
-
- Posts: 2
- Joined: Sat Feb 16, 2019 6:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
sorry...but shouldn't it print 10 in case of removing the local variable X from looper method? cause instance variable X would be assigned M, which woths 10 at the first and last iteration. why zero??
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Why do you think m is 10 at the last iteration?
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Fri Sep 09, 2022 5:03 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Hello,
The answer explanation says: "If you remove int x = 0; from looper(), it will print 0 and end." Shouldn't it be var x = 0 to be consistent with the question?
The answer explanation says: "If you remove int x = 0; from looper(), it will print 0 and end." Shouldn't it be var x = 0 to be consistent with the question?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Yes, that would be better. Fixed.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 47 guests