Win a copy of Microservices Testing (Live Project) this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Operator mock exam question

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This mock exam question is from javachampion.com.
The output is 1 1 2
Can someone explain how the result is produced?
I don't know how to trace it on paper.

Thanks,

Connie



 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are some explanations here.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look on this also.



interpreted as
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int k = 12;

Line 7:
k/=--k;

or, k = k / --k
or, k = 12 / 11
or, k = 1
Line 8:
Print 1

Line 9:
k*=k++;

or, k = k * k++
or, k = 1 / 1
or, k = 1
Line 10:
Print 1

Line 11:
k*=++k;

or, k = k * ++k
or, k = 1 * 2
or, k = 2
Line 12:
Print 2
 
Ranch Hand
Posts: 74
Netbeans IDE Tomcat Server Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
will the exam contain complex arithmetic questions?

like this one for instance
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Unmesh Chowdhury wrote:

or, k = k / --k
or, k = 12 / 11 // Check here!
or, k = 1


It should be 12

 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Abimaran Kugathasan
Explain how?

There is no scope of should be.
 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let’s see how the k = k / --k statement is evaluated as k = 12 / 11 statement.

Initially, k = 12.

k = k / --k, is an assignment statement so the right hand expression will be evaluated first.

Now, k / --k expression will be evaluated from left to right.

k is evaluated as 12 and the value is loaded into the processor register or into the processor cache memory (according to the processor architecture) from the physical memory (RAM).

--k is a pre-decrement and single expression. When it is evaluated, there are two things will be happened – the value of its will be decreased by 1 and a value will be returned. Since it is a pre-decrement expression so the new value will be returned, that is, 11 will be returned. So, --k is evaluated as 11 and the value is loaded into the processor register as usual.

So, the k / --k expression is evaluated as 12 / 11.

(The rest of the story, that is, the execution of the simple integer arithmetic is done by the processor and returns the value accordingly which is 1 and the value is assigned into the k variable.)

In the case of post-increment or post-decrement, the evaluation is done in the same way but return the previous value.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:

Unmesh Chowdhury wrote:

or, k = k / --k
or, k = 12 / 11 // Check here!
or, k = 1


It should be 12



Sorry, it's my mistake. It should be 11. because of the implicit casting, 1 will assign to k, even though the the result if float value.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic