Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Mon Aug 13, 2012 12:48 pm
by Javanaut
:o Found another error, 2nd error in my count.

This question says that option b
Insert after line 2 : if(n <= 0) local = "bad";
won't work. I tested the code out in Eclipse and using a text editor and compiling on the command line and it compiles and runs, when including a main method just as the else statement

The explanation says:
Choice "Insert after line 2 : if(n <= 0) local = "bad";" doesn't work because the second 'if' will actually be another statement and is not considered as a part of first 'if'. So, compiler doesn't realize that 'local' will be initialized even though it does get initialized.

Code: Select all

public class SomeClass {

	String s1 = "green mile";
	String local;
	public void generateReport(int n){
	
		if(n>0) local="fun"; // 2
		if(n<=0) local = "bad";
		System.out.println(s1 +"= " + local); // 3
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SomeClass sc = new SomeClass();
		System.out.println(sc.local);
		
		sc.generateReport(8);
		System.out.println(sc.local);
	}
	
	
}

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Mon Aug 13, 2012 8:39 pm
by admin
On a standard Oracle compiler using command line produces this:

Code: Select all

C:\works>javac TestClass.java
TestClass.java:8: variable local might not have been initialized
        System.out.println( s1+" = " + local);//3
                                       ^
1 error
So the given explanation is correct.

Also, the code given in the question is different from the one that you are running. In the question's code, local is a method variable, while in your code it is an instance variable, which is always initialized so there is no compilation error.

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Tue Aug 14, 2012 12:16 pm
by Guest
Interesting... I am using the JRE and SDK for Java 7 on my machine and in Eclipse

This questions asks the test taker to make two changes the first is moving one of the variables outside the method to become an instance member. I think this question asks the test taker to make two changes and not just one change in two different situations.

Whatever though, this is a cool Java practice program. :mrgreen:

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Tue Aug 14, 2012 9:17 pm
by admin
Ok, I see your point now. The question has now been update to include the statement, "Which of the changes given in options can be done (independent of each other) to let the following code compile and run without errors?" That should make it clearer.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Thu Mar 06, 2014 6:15 am
by kasper_341
and there is no call to method "public void generateReport( int n )" either

how can this code run ?

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Thu Mar 06, 2014 10:51 am
by admin
This code is not required to be run from command line directly. There could be some other class that can invoke the given method. Notice that there is an int argument to this method, which could be anything. So the code should work for any value of n without any exception.

Problem statement has now been updated to make it clear.

thank you for your feedback!
Paul.