Monday, July 29, 2019

What are CoffeeScript Interview Questions ?

A list of frequently asked CoffeeScript Interview Questions are given below:

1) Define CoffeeScript.

The CoffeeScript is a lightweight programing language that compiles into JavaScript. It is getting popular day by day because it provides better and more consistent syntax than JavaScript, still contains the flexibility and other advantages of JavaScript.

2) Who was the founder of CoffeeScript?

The CoffeeScript was designed and developed by Jeremy Ashkenas in December 2009.

3) What was the primary purpose behind the invention of CoffeeScript?

The primary purpose of CoffeeScript was to remove all the rough edges from JavaScript and provides a smooth way of programming in JavaScript. Using CoffeeScript, we can also create classes and inherit them.

4) Which languages have the most impact on CoffeeScript?

Python, Ruby, and Haskell have inspired the CoffeeScript. It adopts syntax & coding styles from them, which makes it unique and useful.

5) What are the basic rules to remember for CoffeeScript?

You must remember these rules for CoffeeScript:
  • You don't need to use curly braces in CoffeeScript.
  • No need of parenthesis for defining a function which has arguments.

6) Explain the advantages of CoffeeScript over JavaScript.

Following is a list of advantages of CoffeeScript over JavaScript:
  • Less coding: A CoffeeScript's program contains very less code than the same JavaScript?s program. It facilitates you to use less code for the same task.
  • Light-weighted: CoffeeScript is light-weighted, small programming language. It contains very lightweight add-ons such as Ruby string Interpolation.
  • Reliable: The CoffeeScript is a programming language which is safe for writing dynamic programs.
  • Readable and easy to maintain: CoffeeScript provide aliases for the operators which makes the code easy to understand and manage.
  • Inheritance: JavaScript does not support class-based inheritance whereas CoffeeScript offers class-based inheritance to the programmer to implement inheritance.
  • No var keyword: In javascript, we have to use the var keyword to create variables, whereas in CoffeeScript there is no need to use the var keyword to create variables.

7) Explain the disadvantages of CoffeeScript over javascript.

Following are some disadvantages of using CoffeeScript over javascript:
  • Sensitive to whitespaces: CoffeeScript is very sensitive to whitespaces. Programming should be careful of using proper indentations. If the programmer could not maintain the appropriate indentation, then the entire code may go wrong.
  • Compilation time required: CoffeeScript needs to be compiled first to the javascript which takes some time and efforts of the computer. Every time CoffeeScript is compiled in javascript code to execute its functionality in the browser.

8) What is the difference between variables in CoffeeScript and JavaScript?

In JavaScript, it is required to declare a variable using the var keyword and initialize it. However, in CoffeeScript, we can create a variable directly, by assigning the value to a literal such as: -
name=variable value

9) Explain functions in CoffeeScript.

The syntax of CoffeeScript's function is more straightforward than JavaScript's function. In CoffeeScript, you have to define function expressions. The function keyword is not used in CoffeeScript. Instead of this, you have to use an arrow symbol (->).

10) Why is CoffeeScript getting popularity day by day?

The CoffeeScript is the 11th most popular language in Github. Its primary purpose is to produce efficient JavaScript without writing much code. It also focuses on highlighting all the functional aspects of JavaScript with a simple syntax.
Reasons behind the popularity of CoffeeScript:
  • Very Little Coding is required when programming in CoffeeScript as compared to JavaScript.
  • CoffeeScript includes all the useful features of JavaScript.
  • You can use any existing JavaScript library seamlessly with CoffeeScript.

11) What are the disadvantages of CoffeeScript?

  • CoffeeScript includes an additional compilation step during compilation.
  • Only a few resources are available for this language.

12) What are transpilers in CoffeeScript?

In CoffeeScript, a tool is required to translate/ convert CoffeeScript codes into JavaScript. This tool is known as transpilers.

13) What is splat in CoffeeScript?

Splat is the term used for (...) operator for var-argument. Splatted arguments can come either before, after or between standard arguments.
For example, (first, rest ....)

14) What is the use of clone-function in CoffeeScript?

In CoffeeScript, clone function is used to clone or duplicate an instance of an object. Create a clone() method which returns a new instance with copied values.
For example:
clone = (obj) -> return obj if obj is null or typeof (obj) isnt "object" temp = new obj.constructor() for key of obj temp[key] = clone(obj[key]) temp

15) Explain class methods in CoffeeScript?

In CoffeeScript, class objects are stored in the object itself rather than on the object prototype. It saves memory and gives a central location to store class-level values.

16) How can you replace a portion of strings with another value?

You can use Regular Expression to match and replace a portion of a string with another value.

17) What is the difference between copying an object through assignment and clone-function?

The main difference between copying an object through assignment and clone-function is the way they handle references. The assignment only copies the reference of the object while clone-function creates an entirely new object.

18) How CoffeeScript interpolates the strings?

The concept of Interpolation in CoffeeScript is same as Ruby. Most expressions of CoffeeScript are valid inside the #{...} interpolation syntax.

19) How do Boolean works with CoffeeScript?

In CoffeeScript, "True" is generally represented as "On" or "Yes" and "False" is represented as "Off" or "No".

20) How can you create and delete a missing function in CoffeeScript?

You can use the existential assignment operator (?=) to create and delete the missing function in CoffeeScript.

21) Is it possible to bind parameters to properties in CoffeeScript?

Yes, You can bind parameters to properties in CoffeeScript by using @ shorthand. It can also be used to define class functions.

22) How can you map an array in CoffeeScript?

You can map an array in the object by using map() with an anonymous function in CoffeeScript. For
simple mapping, the list comprehension is more useful, because CoffeeScript directly support list comprehensions.

23) How can you use arrays to swap variables?

You can use CoffeeScript's destructuring assignment syntax to swap variables using the array. It allows exchanging two values without the use of a temporary variable.

24) Why are Essential Operators are used in CoffeeScript?

Existential Operators are used in CoffeeScript for the following reasons:
  • To check the existence of a variable.
  • For conditional assignment.
  • For function chaining.

25) What is String interpolation in CoffeeScript? Give an example.

The CoffeeScript provides a feature known as string interpolation used to include variables in the string. Ruby programming language inspires this feature.
  1. Syntax: String = "This is a string #{variable_name}"  
String interpolation is done using the above syntax. Using double quotes "", Hashtag #, and a pair of curly braces {}.
For example:
  1. name = "Raju"  
  2. age = 26  
  3. message ="Hello #{name} your age is #{age}"  
  4. console.log message  
  5. On compiling the above coffeeScript code compiler will generate the corresponding javascript:  
  6. // Generated by CoffeeScript 1.10.0  
  7. (function() {  
  8.   var age, message, name;  
  9.   
  10.   name = "Raju";  
  11.   
  12.   age = 26;  
  13.   
  14.   message = "Hello " + name + " your age is " + age;  
  15.   
  16.   console.log(message);  
  17.   
  18. }).call(this);  

26) What is string concatenation in CoffeeScript? How is it done?

In CoffeeScript, we can easily concatenate two strings by using + operator between the two strings.
For example:
  1. new_string = "Hello how are you "+"Welcome to Javatpoint".  
  2. console.log new_String  
  3. On compiling the above CoffeeScript code compiler will generate corresponding javascript code as follows:  
  4. // Generated by CoffeeScript 1.10.0  
  5. (function() {  
  6.   var new_string;  
  7.   
  8.   new_string = "Hello how are you " + "Welcome to Tutorialspoint";  
  9.   
  10.   console.log(new_String);  
  11.   
  12. }).call(this);  

27) What is string object in coffeeScript/javaScript? Explain its methods.

The string object of javascript helps you work with the series of characters. String object provides various of methods to perform various operations in strings.
We can use the javascript library in CoffeeScript code. Hence we can use all the methods of string object in javascript.
Following are some of the methods of string object:
  • charAt(): This method returns the character at the given index value of a string object.
  • charCodeAt(): This method returns the Unicode value of the character at the given index.
  • concat(): This method combines two substrings and returns a superstring.
  • indexOf(): This method returns the index of calling string having the first occurrence of a specified value, It will return -1 if not found.
  • lastIndexOf(): This method returns the index of calling string having the last occurrence of a specified value, It will return -1 if not found.
  • localeCompare(): This method returns a number representing whether a reference string comes before or after or is the same as given string in sort order.
  • match(): This method is used to match a regular expression against a string.
  • search(): This method executes a search for a match between the regular expressions in a specified string.
  • slice(): Extract the section of a string object and returns a new string object.
  • split(): Splits a string object into the array of strings by separating the string into substrings.
  • substr(): This method returns the calling string beginning at the specified location through the specified number of characters.
  • toLocaleLowerCase(): Converts the calling string into the lower case while respecting the current locale.
  • toLocaleUpperCase(): Converts calling string into the upper case while respecting the current locale.
  • toLowerCase(): Returns calling string in lower case.
  • ToUpperCase(): Returns calling string in upper case.

28) What is splat in CoffeeScript? Explain with an example.

The CoffeeScript provides a feature known as splat which is used to pass multiple arguments to a function.
We can use splats in functions by placing three dots after the variable name.
For example:
  1. indian_team = (first, second, others...) ->  
  2.   Captain = first  
  3.   WiseCaptain = second  
  4.   team  = others  
  5.   console.log "Captain: " +Captain  
  6.   console.log "Wise captain: " +WiseCaptain  
  7.   console.log "Other team members: " +team  
  8.   
  9. #Passing 4 arguments  
  10. console.log "############## Four Players ############"  
  11. indian_team "Mahendra Singh Dhoni""Virat Kohli""Shikhar Dhawan""Rohit Sharma"  
  12.   
  13. #Passing 6 arguments  
  14. console.log "############## Six Players ############"  
  15. indian_team "Mahendra Singh Dhoni""Virat Kohli""Shikhar Dhawan""Rohit Sharma""Gurkeerat Singh Mann""Rishi Dhawan"  
  16.     
  17. #Passing full squad  
  18. console.log "############## Full squad #############"  
  19. indian_team "Mahendra Singh Dhoni""Virat Kohli""Shikhar Dhawan""Rohit Sharma""Gurkeerat Singh Mann""Rishi Dhawan""Ravindra Jadeja""Axar Patel""Jasprit Bumrah""Umesh Yadav""Harbhajan Singh""Ashish Nehra""Hardik Pandya""Suresh Raina""Yuvraj Singh""Ajinkya Rahane"  
Explanation of the above example:
In the above case of spats, multiple arguments were being passed to the function. By placing three dots after the argument list of the function indian_team. In the first pass we have passed four arguments to the function, in the second pass we have passed six arguments to the function, and in the last pass, we had passed the names of the full squad to the function.

29) Give an example to demonstrate the use of splats with tailing argument in CoffeeScript. Also, explain.

The splats with tailing argument refer to the argument placed after the splat argument.
For example:
  1. indian_team = (first, second, others...., last) ->  
  2.   Captain = first  
  3.   WiseCaptain = second  
  4.   team  = others  
  5.   Wicketkeeper =last  
  6.   console.log "Captain: " +Captain  
  7.   console.log "Wise captain: " +WiseCaptain  
  8.   console.log "Wicket keeper is:"+last  
  9.   console.log "Other team members: " +team    
  10.     
  11. squad = [  
  12.    "Mahendra Singh Dhoni"  
  13.    "Virat Kohli"  
  14.    "Shikhar Dhawan"  
  15.    "Rohit Sharma"     
  16.    "Gurkeerat Singh Mann"  
  17.    "Rishi Dhawan"  
  18.    "R Ashwin"  
  19.    "Ravindra Jadeja"  
  20.    "Axar Patel"  
  21.    "Jasprit Bumrah"  
  22.    "Umesh Yadav"  
  23.    "Harbhajan Singh"  
  24.    "Ashish Nehra"  
  25.    "Hardik Pandya"  
  26.    "Suresh Raina"  
  27.    "Yuvraj Singh"  
  28.    "Ajinkya Rahane"  
  29.  ]  
In the above example of using tailing with splats, we have placed an additional argument to the argument list of the indian_team function.

30) Give an example to demonstrate the use of comprehensions with splats.

Within the function, we can also iterate the elements of a splat using comprehensions as shown in the following example. Save this code in a file with the name splats_comprehensions.coffee
  1. indian_team = (first, second, others...) ->  
  2.   Captain = first  
  3.   WiseCaptain = second  
  4.   team  = others  
  5.   console.log "Captain: " +Captain  
  6.   console.log "Wise captain: " +WiseCaptain  
  7.   console.log "Other team members:: "  
  8.   console.log member for member in others  
  9.   
  10. squad = [  
  11.    "Mahendra Singh Dhoni"  
  12.    "Virat Kohli"  
  13.    "Shikhar Dhawan"  
  14.    "Rohit Sharma"     
  15.    "Gurkeerat Singh Mann"  
  16.    "Rishi Dhawan"  
  17.    "R Ashwin"  
  18.    "Ravindra Jadeja"  
  19.    "Axar Patel"  
  20.    "Jasprit Bumrah"  
  21.    "Umesh Yadav"  
  22.    "Harbhajan Singh"  
  23.    "Ashish Nehra"  
  24.    "Hardik Pandya"  
  25.    "Suresh Raina"  
  26.    "Yuvraj Singh"  
  27.    "Ajinkya Rahane"  
  28.  ]  
  29.   
  30. indian_team squad...  

31) Explain the various math functions which can be used in CoffeeScript.

We can use the javascript library in CoffeeScript code. Hence we can use all the methods of math object in javascript.
Following are some math function present in the math object:
\
  • abs(): This function returns the absolute value of a number.
  • acos(): This function returns arccosine (in radians) of a number.
  • asin(): This function returns arcsine (in radians) of a number.
  • atan(): This function returns arctangent (in radians) of a number.
  • atan2(): This function returns arctangent of the quotient of its arguments.
  • ceil(): This function returns the smallest integer greater than or equal to the given number.
  • cos(): This function returns the cosin of the given number.
  • exp(): This function returns EN, Where N is the argument, and E is the Euler's constant, the base of the natural logarithm.
  • floor(): This function returns the largest integer less than equal to the number.
  • log(): This function returns natural logarithm (base E) of a number.
  • max(): This function returns largest of zero or more numbers.
  • min(): This function returns the smallest of zero or more numbers.
  • pow(): This function returns the base exponent of the given number.
  • random(): This function returns the pseudo-random number between 0 and 1.
  • round(): This function returns the value of the number rounded to the nearest integer.
  • sin(): This function returns the sine of the given number.
  • sqrt(): This function returns the square root of the specified number.
  • tan(): This function returns the tangent of the specified number.

No comments:

Post a Comment