Stump the CFChump – 2
I am doing a "Stump the CFChump" series. I recently passed the ColdFusion 9 exam and this is material I learned while studying. I have several CodeSchool gift cards to giveaway so comments who answer correctly I'll email you the discount code. "Stump the Chumps" is from the "Car Talk" so I am spinning that title for ColdFusion. These are questions that stumped me when studying.
- Please try to not google or run the code before answering.
- Must have your correct email address when commenting to receive a CodeSchool giftcard
- Giftcards are available until I run out for correct answers
Stump the CFChump 1
Answer
Stump the CFChump 2
Question
What will display when this code is ran?
<cfset myvar = "Hello ">
<cfif 2 eq true>
<cfset myvar = myvar & "Carrie">
<cfelseif 2 eq false>
<cfset myvar = myvar & "Brody">
<cfelse>
<cfset myvar = myvar & "Saul">
</cfif>
<cfoutput>
#myvar#
</cfoutput>
Answers
A. Hello Carrie
B. Hello Brody
C. Hello Saul
D. Error




Posted under: 


A - any nonzero number is evaluated in CF as true.
The answer is C.
While 2 is a boolean true the if condition is not doing a boolean check. The if is checking to see if 2 EQ true. Since 2 EQ 2 it is neither true nor false.
If you wanted to do the same condition but do a true boolean check you could do it this way...
<cfif 2>
This will treat 2 as a boolean and then return true as any non-zero number is true.
C. Yo!
I cheated by playing with the code. But found it this interesting to run:
<cfoutput>
true = #true#<br />
false= #false#<br />
2 eq true = #2 eq true#<br />
2 eq false = #2 eq false#<br />
yes = <cftry>#YES#<cfcatch type="Any">#cfcatch.message#</cfcatch></cftry><br />
no = <cftry>#no#<cfcatch type="Any">#cfcatch.message#</cfcatch></cftry><br />
"No" eq false= #"No" eq false#<br />
"No" eq true= #"No" eq true#<br />
"Yes" eq true= #"Yes" eq true#<br />
"Yes" eq false= #"Yes" eq false#<br />
</cfoutput>
No real suprises here, except that I would have expected the output from
2 eq true = #2 eq true#<br />
to be
2 eq true = FALSE
but it is
2 eq true = NO
Ok guys, what will the result be if we change 2 to 1? www.henke.ws/post.cfm/stump-the-cfchump-2b
It 'should' say ::
WARNING !!! CF is NOT type safe ... Do not expect it to handle autoboxing correctly ...