3

I have been pondering a multiple choice question on coercion. One of the 4 examples a,b,c or d is an example of coercion. I narrowed it down to A or B. But I am having a problem choosing between the two. Cane someone please explain why one is coercion and one isn't.

A)

string s="tomat";
char c='o';
s=s+c;

I thought A could be correct because we have two different types, character and string, being added. Meaning that c is promoted to string, hence coercion.

B)

double x=1.0;
double y=2.0;
int i=(int)(x+y);

I also thought B was the correct answer because the double (x+y) is being turned into a int to be placed in i. But I thought this could be wrong because its being done actively through use of (int) rather than passively such as "int i = x + y"

I'll list the other two options, even though I believe that neither one is the correct answer

C)

char A=0x20;
A = A << 1 | 0x01;
cout << A << endl;

D)

double x=1.0;
double y=x+1;
return 0;

I'm not just looking for an answer, but an explanation. I have read tons of things on coercion and A and B both look like the right answer. So why is one correct and the other not.

2 Answers2

0

I don't think type casting is equivalent to type coercion, which is why A would probably be the right answer.

B takes a double and casts it to an int, which is more like a conversion than a coercion. In A you aren't converting anything you're being implicit. You are telling the runtime/compiler/whatever "these 2 things are similar can you figure out how to concatenate them?"

C isn't a conversion or coercion its just bit shifting. Although the cout might be coercion... I am not sure if there is coercion to a string there to write to the console.

D might contain a coercion since 1 is an int and you are adding it to a double. However, you can do floating point math with integers having a decimal is just more explicit.

I think A is the most straight forward example of coercion. Although C's cout statement seems suspicious as well.

Parris
  • 17,833
  • 17
  • 90
  • 133
  • I think that conversion and coercion are pretty much equivalent when it comes to CS. (http://en.wikipedia.org/wiki/Type_conversion) – GJK Dec 09 '12 at 22:49
  • @GJK the definition of coercion is that it is implicit. A cast is explicit: http://stackoverflow.com/questions/8857763/what-is-the-difference-between-casting-and-coercing The word coercion also implies that it is implicative (ha). – Parris Dec 09 '12 at 22:54
  • I see, yes, you're right. I then have to agree with you. Because now that I think of it, these aren't C strings (arrays of characters), they're C++ string (probably), so A is probably the correct answer. – GJK Dec 09 '12 at 22:59
0

I actually think it's B. Even though there's the explicit (int), it's still type coercion (just not automatic type coercion). You're converting a floating point value (probably stored as an IEEE floating point value) to an integer value (probably stored in two's complement).

Whereas A is simply concatenating a character to a string, where a string is just a null terminated array of characters. There's no data type conversion going on there, just a bit of memory manipulation.

I could be wrong though.

EDIT: I would have to agree with Parris. Given that this is a C++ string and not a C array of characters (my mistake), the chracter in A is probably being coerced to a string.

GJK
  • 37,023
  • 8
  • 55
  • 74