My first CS problem!

What is the output of the following code?

1
2
3
4
5
6
7
8
9
int t;

int s;

s=6;

t = (8 * s++) % 7;

System.out.print(""+t); 

6 3 2 4 0 5 7 1

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Arulx Z
Dec 2, 2015

In

1
t = (8 * s++) % 7;

The suffix operator in s++ is evaluated after the expression. So the above expression is equivalent to

1
2
t = (8 * s) % 7;
s = s + 1;

Hence the answer is

( 8 6 ) % 7 = 6 \left( 8\cdot 6 \right) \%7=6


Use prefix ++ (for eg ++s ) to evaluate the increment in the expression itself.

The % operator is the operator for modulo.

Moderator note:

In order to read code, you have to know the definition of the terms in the program. Otherwise, you will end up with a different answer. Similarly, when we enter in 1+2 / 3 into a calculation aid, we need to know if it evaluates it as 1 + 2 3 \frac{1+2}{3} or as 1 + 2 3 1+ \frac{2}{3} .

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...