English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

State the values that end up in all variables after execution of the following sequence of C# statements:

int x, y = 4, z ;
double w ;
x = 7/ 3 ;
y +=3 ;
z = (a + 4) / 3 ;
w = y % 3 ;

Why?

2007-03-16 07:49:02 · 5 answers · asked by TheOne 1 in Computers & Internet Programming & Design

5 answers

Line 1: x = garbage, y = 4, z = garbage
Line 2: w = garbage
Line 3: x = 2 (ints are floored, meaning any decimal places are removed).
Line 4: y = 7.
z = Compiler error. There is no declaration for the variable a.
w = 1 (% is a modulus).

2007-03-16 08:00:42 · answer #1 · answered by Anonymous · 0 0

ok... x and z are undefined integers at the beginning (whole numbers, can be negative, no decimal no fraction) and (obviously) y = 4...

w is a double which means it can have a decimal

if x = 7/3... well, 7/3 = 2.33333, but since x is an int it truncates (cuts) off the decimal so x = 2

y+=3 is the same thing as saying y = y +3, so if y was 4, now it is 7.

z = (a+4)/3... i dont know what a is so I can't help you out here...this line would throw and error

w = y % 3... % is the mod operation... this means that w will be the remainder when y is divided by 3... since y is 7, and 7 divided by 3 is 2 with a remainder of 1, w is 1

I teach AP Java in a high school in NY :)

2007-03-16 08:03:28 · answer #2 · answered by jessica6582 2 · 1 0

1)
'x=7/3'
x being integer ( without comma ) means x qill have as value "2".
=> x = 2
2)
"y+=3"
this could be translated in
y = y+3
=> y = 7
3)
"z = (a+4)/3"
a = ????? it's not declared
z being declared integer the same rule aplied on 1)

4)
"w = y % 3"
w = 7 % 3
'%" represent te reminder of divide operation
=> w = 1
meaning that w = 3 x 2 + 1

2007-03-16 08:35:54 · answer #3 · answered by lazaraf78 1 · 0 0

Jeremy is wrong. Uninitialized ints in C# get the value 0.

And really, this is a simple problem, looks like homework. Do your own homework.

2007-03-16 08:08:48 · answer #4 · answered by Anonymous · 0 0

my answer? This program will not even compile in C++ because the variable 'a' is undefined!

2007-03-17 11:17:55 · answer #5 · answered by A.Samad Shaikh 2 · 0 0

fedest.com, questions and answers