• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SCJP Brainteaser (4)

 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Above,

Answer F. None of the above.

Regards,
Jothi Shankar Kumar. S
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Yes the answer is F.

Regards

Nikhil
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I do agree.F is the correct answer. All but F will fail the compilation.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
F will fail compliation too
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jae Stryker:
F will fail compliation too



 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answer is F- none of the above...

because ++ can be applied to variables.. not to values...
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
elems[i] = ++elems[i]+1;

yes, thanks Valentin!


Yours,
Bu.
 
What's that smell? Hey, sniff this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic