ColdFusion Quiz
So, you think you know ColdFusion pretty well, huh? Well, maybe you do...and maybe you still have a few things to learn. I've put together a short quiz for you to take. The answers are at the very bottom of this email.
1. Which of these are NOT legal variable names?
a. $myVar
b. my-Var
c. _true
d. my$Var
e. false
2. What will this code produce?
<cfset test = RandRange(0, 1)>
<cfif NOT test>
<cfset x = Val(100/test)>
<cfelse>
<cfset x = Val(100/(test*0))>
</cfif>
3. The custom tag, "QuerySim" (available at my site, halhelms.com), is used to create a query based on information found between theng and closing tags. Within the custom tag code, what CF variable refers to the "ProductDetail" information within the custom tag set?
<cf_querysim>
ProductDetail
productID,description,price
100|Left-handed widget|19.95
</cf_querysim>
4. Given a query, "Products", with the fields: "id", "price", and "description", how can you output the price of the LAST row in the query without using either a loop or another query?
5. Given a custom tag, "ListNoDupes", that accepts the variables: "list" and "returnAs". The last line of the tag is this:
<cfset SetVariable('caller.' & attributes.returnAs, newList) />
What will happen on the page that calls this custom tag?
a. Nothing
b. A variable called "caller.returnAs" will be set as a local variable on the calling page
c. A request-scoped variable called "newList" will be created
d. A local variable will be set on the calling page with the same name as the value of the "returnAs" parameter passed into the custom tag.
e. If no variable called "returnAs" exists on the calling page, it will be created. If such a variable does exist, nothing will happen.
6. Given this CFC:
<cfcomponent displayname="Classroom">
<cfset variables.students = ArrayNew() />
<cfset variables.teacher = "" />
<cffunction name="addStudent" returntype="void">
<cfargument name="student" type="Student" required="true">
<cfset ArrayAppend(getStudents(), arguments.student) />
</cffunction>
<cffunction name="getStudents" returntype="array">
<cfreturn variables.students />
</cffunction>
</cfcomponent>
You create 1 "Classroom" object, "classroom", and 3 Student objects. You call the "addStudent()" method, passing in each of the three new students. At the end of this, what will this code produce?
#ArrayLen(classroom.getStudents())#
7. You've been given a text file, "CityTemperatures.txt". Each line in the file contains the temperatures for each day in the same week for different cities.
Atlanta: 54,57,52,34,45,49,53
Baltimore: 67,72,69,77,79,71,74
Chicago: 81,72,79,74,76,67,72
Denver: 68,64,75,69,72,63,68
Can you read and display the low, high, and average temperatures for each city in 10 lines of code or less?
8. You have a query, "Products", with the following columns: "id", "price", "description", and "quantityOnHand". You need to create a form with a checkbox for each row in the Products query, all using the name, "selectedProducts". The problem is that you need to send information about both the id of the product chosen AND the quantity on hand. At first, you think of putting a hidden form field for each query row that would contain this info, but your co-worker casually remarks that she always does this without using any more form inputs than there are rows. What's her secret?
9. Look at this code:
<cfset susie = StructNew() />
<cfset susie.employer = "ibm" />
<cfset me = StructNew() />
<cfset me.spouse = susie />
<cfset me.spouse.employer = "self-employed" />
What's the value of susie.employer?
10. You've produced a two-dimensional array, "arr", that you need to save as a client variable. What's the code for this?
Training news
January has been a busy training month. I did training for eBay, UPS, and Macromedia. (For a client's perspective on the training, go to www.corfield.org/blog and find his January 9, 2004 entry.)
The next training is going to be in Las Vegas. I'll be doing a Fusebox 4 class with John Quarto and two classes, Mach-II and Java for ColdFusion Programmers, with Ben Edwards. The dates are:
FB4: Feb 23-25
Mach-II: Feb 25-27
Java: Mar 1-5
More info at halhelms.com/training. I hope I'll see you in Las Vegas.
Answers
1. b and e are bogus. You can't have dashes in variable names and you can't use boolean names for variables.
2. It will always throw a "can't divide by zero" exception. Use of RandRange(0,1) is useful when you need to simulate a true/false decision. This works because CF evaluates all non-zero numbers to true.
3. ColdFusion creates an internal variable called "thisTag.generatedContent" to refer to content within a custom tag pair.
4. You can address the table row directly:
#Products['price'][Products.recordCount]#
Occasionally, this turns out to be very useful.
5. d is right. If "ListNoDupes" is called like this...
<cf_ListNoDupes list="a,b,a,c,d" returnAs="purgedList">
a local variable called "purgedList" will be created and accessible to the calling page.
6. It produces zero. The problem is that arrays are passed by value -- that is, a copy of the array is made and passed. The "addMember()" method is therefore NOT manipulating the instance variable of the CFC, but a copy. (For one solution to this problem, see the next issue of "ColdFusion Developer's Journal" where I discuss the problem more fully and present one solution.)
Computers are incorporated in modern ice cream vending machines to enhance their functionality. Ice Cream Vending machines are manufactured by many companies. Your competition will try to overcome all requests for high-tech ice cream vending machines and credit card acceptors
|
|
7. This is one area where lists are wonderfully helpful. Treat the entire file like a newline-delimited list, then use ListToArray() along with CF's built-in math functions for array.
<cffile action="READ" file="#ExpandPath('CityTemperatures.txt')#" variable="aFile">
<cfoutput>
<cfloop list="#aFile#" index="aCityTemp" delimiters="#chr(10)##chr(13)#">
<cfset city = Trim(GetToken(aCityTemp, 1, ':')) />
<cfset temps = Trim(GetToken(aCityTemp, 2, ':')) />
<#city# : High: #ArrayMax(ListToArray(temps))# -- Low: #ArrayMin(ListToArray(temps))# -- Avg: #ArrayAvg(ListToArray(temps))#
</cfloop>
</cfoutput>
In fact, there's no reason (other than clarity) to create separate "city" and "temps" variables.
8. One simple way of doing this is to "pack" the value of each checkbox with multiple pieces of information. In this example, it's simple enough to create a value for checkbox like this: <value="#productID#_#quantityOnHand#">. You can then decode this on your form processing page.
9. The value of "susie.employer" is now "self-employed". Unlike arrays, structures are passed by reference. In this example, both "susie.employer" and "me.spouse.employer" are pointers to the same memory address. There may be many handles on the underlying structure, but whatever handle was used to initiate the change will affect all other references to the structure.
10. This one uses WDDX to serialize the complex variable into an XML-based, self-describing string:
<cfwddx action="CFML2WDDX" input="#arr#" output="client.arr">
|