Monday, February 20, 2012

Good Flex Interview Questions [Flex Basics (Part 2)]

Part One.
Question: Can we have an abstract class in ActionScript?
Answer: NO, we cannot have. If you don't know about an abstract class then it is a class which provides some properties and methods that must be implemented by the concrete class. It goes beyond an interface by providing some methods that are fully implemented. This is not a feature of ActionScript. You can read more:

Question: What do the following operators do?
>>>, >>>=, ===, !==
Answer:These operators are:
>>> denotes strict equality: Tests two expressions for equality, but does not perform automatic data conversion.
!== denotes opposite of strict equality:Tests for the exact opposite of the strict equality (===) operator.
>>>= denotes bit-wise unsigned right shift and assignment:Performs an unsigned bitwise right-shift operation and stores the result in expression. We can read about more operators here.



Question: Can we say the operator && lazy in action script?
var b:Boolean = condition1() && condition2();
In this statement, if condition1() evaluates to false, will condition2() be evaluated?
Answer: Well, the actual term is short-circuit. And yes this operator is short-circuit. It means if condition1() evaluates to false the condition2() wont be evaluated. Same is the case if we replace the operator with || (OR) and first condition which is condition1() evaluates to true; second one wont be evaluated. This is beacuse:

AND (&&)
F && (T OR F) = F
OR (||)
T || (T OR F) = T

Question: What is difference between x:XClass = XClass(y) and x:XClass = y as XClass? OR
What is the difference between Type Casting and as operator?
Answer: The as operator is introduced in ActionScript 3 and it works pretty much like casting in ActionScript 2.
Type Casting:

var dispVariable:DisplayObject = DisplayObject(objRef);
AS Operator:

var dispVariable:DisplayObject = objRef as DisplayObject;

ActionScript still supports type(object) and both work fine. But there is one important difference (and that in case of failure)- rather than returning null for failed casting, a TypeError is thrown. When you try to cast an object into an incompatible type such as trying to cast one object into a type it is not associated with or inherits from, a failure occurs.
ActionScript also has global conversion functions like:
String (obj) (obj represents the object to be casted to String).
Number(obj) (obj represnts the object to be casted to Number).
Array(obj) (obj represnts the object to be casted to Array).
and they have precedence over type(object) casting. These include String(), Number(), and Array(), etc. More. and More.

Question: What are the differences between instanceof and is operators?
Answer: The is operator is is a new keyword that lets us check whether an instance is of certain object type. Adobe advise us to use is and not instanceof (available in AS1 and AS2).
"The is operator, which is new for ActionScript 3.0, allows you to test whether a variable or expression is a member of a given data type. In previous versions of ActionScript, theinstanceof operator provided this functionality, but in ActionScript 3.0 the instanceof operator should not be used to test for data type membership. The is operator should be used instead of the instanceof operator for manual type checking, because the expression x instanceof y merely checks the prototype chain of for the existence of (and in ActionScript 3.0, the prototype chain does not provide a complete picture of the inheritance hierarchy)."


Question: What is the need of toString() and clone() methods of ObjectUtil?
Answer: The ObjectUtil class is an all-static class with methods for working with Objects within Flex. We do not create instances of ObjectUtil; instead we simply call static methods such as theObjectUtil.isSimple(val:Object) method [For curious cases this method returns true if the object referenced is a simple data type.]
We sometimes do not need the structure of the object (or value-object) passed by back-end code to UI. In these scenarios we can debug in Flash Builder. But if debugging is not possible then we want to print the complete structure of the object using toString() method ObjectUtil like that:
Alert.show(ObjectUtil.show(objectToPrint));
We use clone method to clone the object and return a reference to the cloned copy.More about ObjectUtil here. and http://blog.flexexamples.com/category/objectutil/.


< br /> Question: How to set style values in action script
Answer: We can use setStyle() method to set style in action script. This method can be used to set various properties viz: backGroundColor, contentBackgroundColor etc. We can also use this to set skinClass for Spark component like:

setStyle("skinClass",MyNewSkin);

Question: What are the differences between Object and ObjectProxy?
Answer: The major difference between Object and ObjectProxy class is: The properties specified in Object  class are not bindable, it means if any of the property of object changes it won't dispatch any propertyChangeEvent and binding wont be triggered; whereas in ObjectProxy the properties specified are bindable and any change in any of the property will trigger binding. The down side of using ObjectProxy is  performance issues.
http://blog.flexexamples.com/2007/09/27/detecting-changes-to-an-object-using-the-flex-objectproxy-class/
http://flexpearls.blogspot.com/2008/05/objectarray-and-objectproxyarraycollect.html

When we get a result (ArrayCollection say) as a remote-call then what we get is not Object but ObjectProxy (assume we have not done any mapping of value-objects in our application). Then natural question is can we convert ObjectProxy instances to Object...well we can:
http://blog.olivermerk.ca/index.cfm/2007/6/1/Flex-Converting-ObjectProxies-to-Value-Objects
http://howardscholz.wordpress.com/2007/06/08/object-proxy-cont/

Question: How to use apply() from Function class in ActionScript?
Answer: We know how to write a function and call it in actionscript: 
function dummy(...arg):int

    //some logic which operate on parameters and returns int
}
The usual way to call this is like dummy(11,22,33,44) which is equivalent to dummy.call(null,11,22,33,44) and But here we need to know how many arguments (parameters) we need to pass. We have another method in Function class which can accept an array as parameters: apply()
var params:Array = {0,1,2};
dummy.apply(null,params);
More.

Question: What is e4x? 
Answer: E4x stands for ECMAScript for XML. It is a specification that defines some classes and functionality to work with XML. The main features of e4x are:
1. It defines a new set of classes and functionality to work with XML.
2. It provides dot operator that can be used to manipulate XML data.
3. We can use @ and (.) operators for reading data as well for assigning data.
4. Using e4x is much easier and intuitive than "walking the DOM".
Frameworks
Question: What is Parsley? How to use it?
Answer: A very good article already exists here:

11 comments:

NAVEEN said...

Hi Akhil,

Thanks for your useful flex interview questions.

I need the following concepts if possible with examples.

1.Flex Component Life Cycle
2.What is Custom Component Need of Custom Component with example.
3.What is Custom Event
Need of Custom Event with example.
4.Event Propagation
5.What is e4x?

Thanks,
Naveen.

Unknown said...

Hi Naveen,

Thanks for you feedback!!

Flex Component Life Cycle I have not posted till now. Will post in some time ...keep visiting this blog.

There are some Custom Component examples here in this blog itself(Digital Clock,Workflow etc). I am planning to create one more detailed post about custom comp soon.

Custom Event,Need of it,Event propagation and many event related questions can be found here itself:
http://flex4java.blogspot.com/2012/02/flex-events-part-one.html

Question for E4x is added now.

Anonymous said...

Brilliant work my friend! Keep up the good work.

Anonymous said...

First off, great blog! just what I was after as a refresher/test for myself.

Check your question about operators, there is a small typo: you have listed '>>>' as denoting strict equality. Obviously '===' is strict equality (checks type, no auto conversion) and '>>>' is a bitwise unsigned right shift.

thanks again!

Anonymous said...

Dude.. you rocks..

Anonymous said...

thanks buddy......u r making a new hight in flex blog posts.....

Anonymous said...

plzzz update us abt present good work related to flex also.....if possible......and yes .....abt mobile apps of flex

Unknown said...

Thanks All!! Few things:

1. Please post your name as well while commenting as it will help me answer your queries. Otherwise all appear anonymous to me :)
2. I am extremely busy with my trading application development. So may be I will not be able to answer every thing as of now. But I will do definitely later on.

As far as mobile apps are concerned lots of good applications have already been developed in Flex. But flex apps seem to be slightly heavy so developers are also exploring other technologies (Sencha Ext, JQuery, Andorid SDK, Objective C etc). I will post about it pretty soon.

Anonymous said...

Thank you so much for the questions and answers. Great Job. you blog is very helpful specially for flex developers like me. People like you are keeping the flex community alive.. Keep up the good work.

Monik said...

Thank you very much Akhil. This is really helpful!

Anonymous said...

These are good source of flex questions even now. Though they miss a few, like the components, flex 4 architecture, states, component life cycle, core flex components, spark skins, data services(remote objects), etc.,,