File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP Brainteaser (4) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP Brainteaser (4)" Watch "SCJP Brainteaser (4)" New topic
Author

SCJP Brainteaser (4)

Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Alright, next one:

Which one of the following statements can be inserted at line 1 in order for the output to be "Sum:12"?


A. elems[i] = ++elems[i]++;
B. elems[i] = ++(elems[i]++);
C. elems[i] = (++elems[i])++;
D. elems[i] = ((elems[i])++)++;
E. elems[i] = ++(++elems[i]));
F. None of the above


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Paras Jain
Ranch Hand

Joined: Feb 26, 2005
Posts: 137
None of the above is the right answer.
As per the specifications, the operand to ++ operator should be a variable not a value
therefore (++(++x)) will result in an error because ++x results in a value

correct me if i am wrong


Paras Jain
SCJP 5.0
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9618
    
    2

Hi Above,

Answer F. None of the above.

Regards,
Jothi Shankar Kumar. S


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Nikhil Bansal
Ranch Hand

Joined: Jan 24, 2005
Posts: 60
Hi,

Yes the answer is F.

Regards

Nikhil


ban$al
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Yes I do agree.F is the correct answer. All but F will fail the compilation.


~Sanjeev Singh<br />SCJP 1.5
Jae Stryker
Greenhorn

Joined: Oct 31, 2006
Posts: 21
F will fail compliation too
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Jae Stryker:
F will fail compliation too




The Best way to predict your future is to create it - Every great individual common man
toukhir mujawar
Ranch Hand

Joined: Oct 11, 2006
Posts: 70
the answer is F- none of the above...

because ++ can be applied to variables.. not to values...
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

when I saw the question I first thought that the creator of this question should be [CENSORED] .

Then I saw, there was no minus in any of the possible answers. So I didn't care of this codes (if you call'em codes) could never be smaller than i.
And when you add up the first five natural numbers, you're above twelve in any case. Compile or not.


Yours,
Bu.


all events occur in real time
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

I did not say, why I wished poor old Valentin should be [SORRY * WE GOT TO CENSOR THAT AGAIN].

Because of cruelty against SCJP examinees!




By the way,
F gives compilation error:


Looking forward for your first generics puzzle,
Yours,
Bu.

---
Spoiler follows:














If you put the obfuscating brackets away, a-c are just like
int x = 2;
x = ++x++;
d is like
x = x++++;
and e is like
x = ++++x;
and they all cannot compile.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
And when you add up the first five natural numbers, you're above twelve in any case. Compile or not.

Burhard, I appreciate the fact you are trying to joke the gallery (yes, I really do) but if you read the code carefully you'd see that your statement above doesn't make any sense. In fact when you create a new int array, all components of that array are initialized to 0 no matter what, thus for each i, elems[i] will be 0 before the increment operator is applied. Note that I'm not summing up the i but what's at the ith position in the elems array, and thus, the 12 could well make some sense there. Nice try, though! I'm sure you'll do better next time
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Valentin:
Nice try, though! I'm sure you'll do better next time


I'll do my best. Next time I'll try logics.

Bu.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi all,

I puzzled around to get a method that returns 12 only with numbers from 0-5 and incrementing but didn't manage. Poor me. I also tried using the ? : operator. But it seems, in the following code there is missing something.
Where and why something has to be added to get the dozen for Valentin?

More 1.4 related
Deleting an _underscore and a pair of brackets does not count!
Rancy Chadha
Ranch Hand

Joined: Jul 12, 2006
Posts: 135
Hi Burkhard,
Here is the little modification I did to your code which gives me answer as 12 that you wanted the code to give. Please check it

class Valentins12
{
public static void main(String[] args)
{
System.out.print("Valentin's ");
System.out.println(_12());
}

static int _12()
{
int sum=0;
int [] a = new int[5];
for (int i=0; i<5; i++)
{
a[i]=++i + 1;
}
for (int i=0; i<5; i++)
sum += a[i];
return sum;
}
}

This gives output as: Valentin's 12
Statements marked as Bold are the changes.

Thanks,
Rancy


Thanks,<br />-Rancy
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610


I'm not really sure you guys get the true meaning of those little exercises ???

Besides, Rancy, there is only one change needed in the code, provided you keep the array name as it was, i.e. "elems". Instead of having
elems[i] = ++elems[i]++; //option A
you'd have
elems[i] = ++elems[i]+1;

And to me this looks like a really, really tiny change to get to that famous 12, which doesn't look that ridiculous anymore, does it Burkhard?

I puzzled around to get a method that returns 12 only with numbers from 0-5 and incrementing but didn't manage. Poor me. I also tried using the ? : operator. But it seems, in the following code there is missing something.
Where and why something has to be added to get the dozen for Valentin?


If there are 6 iterations and the goal is to get to a sum of 12, logics tells that you somehow have to add 2 each time, thus the double ++ operators. Of course, this doesn't work as proposed, but the goal was not to provide you with something that compiles, only to provide you with something meaningful enough to hit your braincells against.

And in this case, Paras got the goal of the exercise right!

Relevant pointers to the JLS are:
15.14 Postfix Expressions
15.15.1 Prefix Increment Operator ++
15.15.2 Prefix Decrement Operator --
[ November 01, 2006: Message edited by: Valentin Crettaz ]
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
elems[i] = ++elems[i]+1;

yes, thanks Valentin!


Yours,
Bu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SCJP Brainteaser (4)