Logo F2FInterview

Coldfusion Interview Questions

Q   |   QA

<cfcomponent>
     <cffunction access="public" output="false">
        <cfargument required="true"/>
        <cfargument required="true"/>
     <cfquery datasource="SecurityDB">
        SELECT username
        FROM Security
        WHERE username = (#arguments.user#)
        AND password = (#arguments.passwd#)
     </cfquery>
   </cffunction>
</cfcomponent> 


1. A) By uing cfinvoke :
<cfinvoke
         component="security"
         method="authenticate"
         returnVariable="authenticated"
         user="#form.username#"
         passwd="#form.password#"
>

 
1. B) By using CFscript tag:
<cfscript>
            objSecurity = createObject("component","security");
            bAuthorized = objSecurity.authorize(form.user,form.groupID);
</cfscript>

 
And other ways of calling a coldfusion component:
C direct URL
D form post
E Macromedia Flash movies through Macromedia Flash MX and Flash Remoting
F as a web service (with auto-generating WSDL)
onApplicationStart(): run once when an application starts, before the first request. It can
access that request’s variables and write output to its response.

onApplicationEnd(): run when an application is explicitly shut down. This is not associated
with any request.
onSessionStart(): run when session handling is enabled and a new session starts. Associated
with request.
onSessionEnd(): run when a session times out or is cancelled. Not associated with request.

onRequestStart(): called before every request. Associated with request.
onRequest(): called after onRequestStart code ends. Associated with request.
onRequestEnd(): called after request has been processed. Associated with request.
onMissingTemplate(): called when an unknown page has been requested. Associated with
request.

onError(): called when an uncaught exception occurs. This may or may not be associated with
a request. 

To sum it up, CFCs and custom tags are quite different. Although their functionality does overlap a little, they really do not solve the same problems at all.
By using CFC(coldfusion components) we can develop structured applications. Being reusability is the main and important feature of CFC and Custom Tags, there are few differences between CFC and Coldfusion Custom tags.
Custom tags have a single entry point and CFCs have multiple entry points.
In custom tags there is no validation process and in CFCs we have the validation process for parameters that are passed.
Custom tags are blocks of code that will be executed as it is. Where as CFCs are objects.
Custom tags can be accessed only locally by coldfusion and on the other hand, CFCs can be accessed by web services, i.e CFCs can be used by out side world also.

it is very easy to do using cf.

if you are on cf8+, then you can use <cffeed> tag to read an rss into a query and then output the top 4 rows of the query. in its basics, this is all the code you need:

<cffeed source="url-of-the-twitter-feed-here" query="mytweets">
<cfoutput query="mytweets" maxrows="4">
<a href="#rsslink#">#title#</a><br />
</cfoutput>

exactly the same to wp blog rss, since both twitter and wp publish rss 2.0 feeds.

if you can't use <cffeed> tag, you can get the feed using <cfhttp> tag, then parse the xml and output the feed items:

<cfhttp url="url-of-feed-rss-here" result="myfeed">
<cfset feedxml = xmlparse(myfeed.filecontent)>
<cfset items = feedxml.rss.channel.item>
<cfoutput>
<cfloop from="1" to="#arraylen(items)#" index="x">
<a href="#items[x].link.xmltext#">#items[x].title.xmltext#</a><br />
<cfif x gt 4><cfbreak></cfif>
</cfloop>
</cfoutput>

Method Overloading:
method overloading is when you have two methods with the same name, but they accept different parameters.  The parameters used in the call to the method determine which method is used.
Ex:
DeleteContact ( contact_id int)
--- this method will delete the contact using the contact's ID
DeleteContact ( firstname varchar, lastname varchar)
-- this method will delete the contact using his first and last name

Both methods have the same name, the right method is called by using which parameters are used in the call...
ColdFusion does not support method overloading.  In CF, method/function names must be unique.  You cannot define two functions with the same name, in the same scope.  Even if they have different arguments.
 
Method Overriding:
If you choose to "override" one or more of the functions in the parent cfc, that is called "overriding".
 
What is Inheritance in Coldfusion components? Give an example…
The concept of inheritance is similar to that of other languages.  You create a component (MySubcomponent.cfc) that _extends_ another cfc.  It then "inherits" all accessible variables and functions from the parent cfc.
 
Eg:
Example.cfm
=============
Sample code to test the 3 components

Order.cfc:
=============
Basic parent component representing a sales order. It contains two functions: one that returns the total amount and one that calculates the percent tax on the order.  By default the getTax function calculates 5% tax on all orders.

(These two components both _extend_ Order.cfc.  So they "inherit" the properties of Order.cfc.)

PhoneOrder.cfc:
=============
Subcomponent that extends from "Order.cfc".  It represents a phone sales order. In this example, phone orders are only charged 2% tax.  This is achieved by _overriding_ the getTax function inherited from Order.cfc

InStoreOrder.cfc:
=============
Subcomponent that inherits from "Order.cfc".  It represents a sales order placed in a store location. This component does _not_ override any of the functions inherited from the parent component.  So store orders are charged the default 5% tax.

<!---
Example.cfm
--->
<!---
Create a phone order for $100.00
--->
<cfset phoneOrder = createObject("component", "PhoneOrder").init(100.00, "888-123-1234") />
 
<h2>Phone Order</h2>
<cfoutput>
getPhoneNumber = #phoneOrder.getPhoneNumber()#<br/>
getAmount = #phoneOrder.getAmount()#<br/>
getTax = #phoneOrder.getTax()#<br/>
** The tax for phone orders is 2%
</cfoutput>
 
<!---
Create an In Store order for the same amount
--->
<cfset storeOrder = createObject("component", "InStoreOrder").init(100.00, "Store 85") />
<h2>In Store Order</h2>
<cfoutput>
getStoreNumber = #storeOrder.getStoreNumber()#<br/>
getAmount = #storeOrder.getAmount()#<br/>
getTax = #storeOrder.getTax()#<br/>
** Default 5% tax is used
</cfoutput>
 
 
<!--- ORDER.cfc --->
<cfcomponent>
<!--- create a structure for storing instance variables --->
<cfset variables.instance = structNew()>
 
<cffunction returntype="Order">
<cfargument />
<cfset variables.instance.orderAmount = arguments.orderAmount />
 
<cfreturn this />
</cffunction>
 
<cffunction returntype="numeric" access="public">
<cfreturn variables.instance.orderAmount />
</cffunction>
 
<!---
By default calculate the 5% tax on all orders
--->
<cffunction returntype="numeric">
<cfreturn getAmount() * 0.05 />
</cffunction>
 
</cfcomponent>
 
<!--- PHONEORDER.cfc --->
<cfcomponent extends="Order">
<cffunction returntype="PhoneOrder">
<cfargument />
<cfargument />
 
<!--- call the parent component first --->
<cfset super.init(arguments.orderAmount)>
<cfset variables.instance.getPhoneNumber = arguments.customerTelephoneNumber />
 
<cfreturn this />
</cffunction>
 
<!---
Independent function that applies only to Phone Orders
--->
<cffunction returntype="string" access="public">
<cfreturn variables.instance.getPhoneNumber />
</cffunction>
 
<!---
Example of overriding the parent function.
For PhoneOrders, charge 2% tax, instead of 4%
--->
<cffunction returntype="numeric">
<cfreturn getAmount() * 0.02 />
</cffunction>
</cfcomponent>
 
 
<!--- INSTOREORDER.cfc --->
<cfcomponent extends="Order">
<cffunction returntype="InStoreOrder">
<cfargument />
<cfargument />
<!--- call the parent component first --->
<cfset super.init(arguments.orderAmount)>
<cfset variables.instance.storeNumber = arguments.storeNumber />
 
<cfreturn this />
</cffunction>
 
<!---
Independent function that applies only to IN STORE Orders
--->
<cffunction returntype="string" access="public">
<cfreturn variables.instance.storeNumber />
</cffunction>
 
</cfcomponent>
 

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: