Football Tutorial

    Get Free Football Tutorial details and much more.
  • Home
  • About
  • Champion of Champions
  • Football - Tournaments
  • CONTACT
  • PRIVACY

Sunday, 29 September 2019

Best Javascript Interview Questions For Front End Developer

 No comments   

Javascript interview questions: Hi Guys, if you don’t know about Best Javascript interview questions, and you are searching for Best Javascript interview questions to learn. Than you are at the right place.

Latest Best Javascript interview questions of 2020
The Best JavaScript interview questions with answers for Front End Developer are given below.

Q1. What is function in java script?

A JavaScript function is a block of code. This function perform a specific task.
This function executes when "something" invokes it or calls it.
Ex:
function myFunc(a, b) {
  return a + b;   // The function returns the sum of a and b.
}

Q2. How code executes in a function in java script?

The code executes, in a function, is placed inside curly brackets: {}

function myFunc(param1, param2, ……) {
  // code to be executed
}
Note: Inside the function, parameters behave as local variables.

Q3. Why Functions in Java script?

Code reusability: Define the code once, and use it many times.

You can use the same code many times with different arguments, to produce different results.

Q4. When we invoke function in JavaScript?

We invoke function in JavaScript when:
An event occurs (button click, mouse up, mouse down, etc.…)
  • Called from a JavaScript code.
  • Automatically/ self-invoked
Q5. What is return statement in a function in java script?
When function reaches the return statement, the function will stop executing.
At this point functions compute a return value. The return value is returned back to the caller.
Example: Calculate the sum of two numbers, and return the result:

var myVal = sumFunction(4, 3);   // sumFunction is called, return value will end up in myVar.

function sumFunction (x, y) {
  return x +y;             // Function returns the sum of x and y
}
The result in x will be: 7

Q6. What are objects in a JavaScript?

Objects are collection of values. Each value can be written as name: value pairs.
Ex: var person = {firstName:"John", lastName:"Doe", age: 33};

Q7. What are Properties in java script?

The name: values pairs in JavaScript objects are called properties:

Property       Property Value
firstName     John
lastName     Doe
age              33

Q8. How to Accessing Object Properties in java script?
You can access object properties in two ways:

objectName.propertyName        or       objectName["propertyName"]

Example1: person.lastName;

Example2: person["lastName"];

Q9. What is Object Methods in Java script?
A method is a function stored as a property.

Example
var person = {
          firstName: "John",
          lastName : "Doe",
                   id   : 5566,
          fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};

Q10. What is this Keyword in a function in Java script?
In a function, this refers to the "owner" of the function.
In the above example, this is the person object that "owns" the fullName function.

Q11. How to access Object Methods in java script?
You can access an object method by following syntax: objectname.methodname()
Example: name = person.fullName();

Q12. What is events in Java Script?
An HTML event is an activity done by the browser, or done by a user.

Ex:
Events
Description
onclick
occurs when HTML element is clicked.
onfocus
occurs when an HTML element gets focus.
onblur
occurs when HTML form loses the focus from an element.
onsubmit
occurs when HTML form is submitted.
Onmouseover
occurs when mouse is moved over an HTML element.
onmouseout
 occurs when mouse is moved out from an HTML element.
onmousedown
occurs when mouse button is pressed over an html element.
onmouseup
occurs when mouse is released from an html element.
onload
occurs when document, frameset is loaded.
onunload
occurs when body or frameset is unloaded.
onscroll
occurs when document is scrolled.
onresized
occurs when document is resized.
onreset
occurs when form is reset.
onkeydown
occurs when key is being pressed.
onkeypress
occurs when user presses the key.
onkeyup
occurs when key is released.

Q13. How event handler attributes used in Java script?
We used it in two ways:
With single quotes: <element event='some JavaScript'>
Ex:
<button onclick=’displayDate()’>The date is?</button>

With double quotes:<element event="some JavaScript">
Ex:<button onclick="displayDate()">The date is?</button>

Q14. What is JavaScript string?
A JavaScript string is zero or more characters written inside single or double quotes.
In JavaScript strings are used for storing and manipulating text.
Ex:
var x = ‘John Doe’;

Ex:
var x = "John Doe";

Q15. How to retrieve a substring from a given string in Java Script?
The following 3 methods are used to retrieve a substring from a given string:
1.   substring()
2.   substr()
3.   slice()

substring() method : This method extracts the characters from a string. It always returns a new substring. This method has 2 parameters start and end. The character at the end position is not included in the substring.
Syntax: string.substring(start, end)
start argument is required.
end argument is optional.
Ex:
var str = "Hello India!";
Positions:
H(0 Position)
e(1st position) and so on.
var res = str.substring(6);//Start from 6th character.
Output: India

Ex: Extract the first 10 characters
var str = "JavaScript World";
var result = str.substring(0, 10);// 0 to 9th charachers
Output: JavaScript

Ex: If "start" >"end", it will swap the two arguments:
var str = "India!";
var res = str.substring(5,1);// Here 5 is greater than 1
Output: ndia

substr() method : This method has 2 parameters start and count. Start argument is required. Count argument is optional.

Extract the first 10 characters
var str = "JavaScript World";
var result = str.substr(0, 10);
Output : JavaScript

Ex:
var str = "JavaScript World";
var result = str.substr(11);
alert(result);
Output : World

slice() method : This method extracts the characters from a string. It always returns a new substring. This method has 2 parameters start and end. The character at the end position is not included in the substring.
Syntax: string.slice(start, end)
start argument is required.
end argument is optional.

Ex: Extract the first 10 characters
var str = "JavaScript World";
var result = str.slice(0, 10);
Output: JavaScript
Ex: If the end parameter is not specified.
var str = "JavaScript World";
var result = str.slice(11);
Output : World

Q16. What is the difference between substr and substring methods
Main difference:
1.   Second parameter of substring() method specifies the index position where as second parameter of substr() method specifies the number of characters to return.
2.   substr() method does not work in IE8 and earlier versions.

Ex: Extract the first 10 characters
var str = "JavaScript World";
var result = str.substring(0, 10);// 0 to 9th charachers
Output: JavaScript

Ex: Extract the first 10 characters
var str = "JavaScript World";
var result = str.substr(0, 10);
Output: JavaScript

Q17. What is the difference between slice and substring?
If start parameter is greater than stop parameter, then substring will swap those 2 parameters, whereas slice will not swap.
Q18. Where we can use indexOf() and substring() methods?
function getEmailandDomainParts()
{   var emailAddress = document.getElementById("txtEmailAddress").value;
    var emailPart = emailAddress.substring(0, emailAddress.indexOf("@"));
    var domainPart = emailAddress.substring(emailAddress.indexOf("@") + 1);
    document.getElementById("txtEmailPart").value = emailPart;
    document.getElementById("txtDomainPart").value = domainPart;
}
Input: anmolg4u@gmail.com
Output: anmolg4u (Email Part) & gmail.com (domain part).
lastIndexOf() method returns the position of the last occurrence of a specified value in a string. Since it's job is to return the last index of the specified value, this method searches the given string from the end to the beginning and returns the index of the first match it finds. This method returns -1 if the specified value is not available in the given string.
Example: Retrieve the last index position of dot (.) in the given string
var url = "http://www.mytutorials.com";
alert(url.lastIndexOf(".")); Output : 22

Q19. Give me real time example where lastIndexOf and substring methods can be used?
Ex: Find the domain name from the url.
<script type="text/javascript">
    function getDomainName()
    {  var url = document.getElementById("txtURL").value;
        var domainName = url.substr(url.lastIndexOf("."));
        document.getElementById("txtDomian").value = domainName;
    }
</script>

Final words
So Guys, This is my own Javascript interview questions For Front End Developer. I hope this article is helpful for you, please do share and comment you’re thought about this all Best Javascript interview questions. Thank You!

If you find something new to learn today from best Javascript interview questions, then do share it with others. And, follow us on our social media (Facebook/Twitter) accounts to see more of this.
Best,
PSCTech


Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Constructors in C#

 No comments   


Constructors in C# with Examples

In this article, I am going to discuss the different types of Constructors in C# with examples. Please read our previous article before proceeding to this article where we discussed how to create class and objects in C# with examples. As part of this article, we are going to discuss the following pointers in details with examples.

Hi Guys, Welcome in my online tutorials.
In this online tutorial, I am going to explain you the different types of Constructors in C# with multiple examples. 

Before proceeding to this article, please read our previous article where I have discussed how to create class and objects in C#.

  1. What is a Constructor in C#?
  2. Rules to follow while creating Constructors in C#.
  3. What a Constructor can have in C#?
  4. Can we define a method with the same class name in C#?
  5. How many types of constructors are there in C#.Net?
  6. What is Default Constructor in C#?
  7. When do we need to provide the constructor explicitly?
  8. What is User-Defined Default Constructor in C#?
  9. When should we define a parameterized constructor in a class?
  10. What is a Parameterized Constructor in C#?
  11. How many constructors can be defined in a class in C#?
  12. What is Copy Constructor in C#?
  13. Understanding Static Constructor in C#.
  14. Can we initialize non-static data members within a static constructor in C#?
  15. Is it possible to initialize static data fields within a non-static constructor in C#?
  16. Can we initialize static data fields in both static and non-static constructor?
  17. What is Private Constructor in C#?
  18. Understanding constructor overloading in C#?
 
What is a Constructor in C#?
A Constructor is a special types of method of a class which automatically executed whenever we create an instance (object) of that class.
Constructors are responsible for:
1.   object initialization
2.   Memory allocation in heap.
Remember: new keyword creates the object of that class.
Ex: Employee myEmployee= new Employee();

Remember:
  1. Constructor name must be same as the class name.
  2. It returns nothing, so we cannot define any return type even void in Constructors.
  3. Constructor doesnot contain modifiers.
  4. As part of the constructor body return statement with value is not allowed.
 
What is the work of Constructor in C#?
  1. Constructor allow all five accessibility modifier.
  2. Constructor may or may not parameters.
  3. We can throw an exception from the constructor if required.
 
Syntax:
Class Employee
{
Employee ()//Default Constructor
{
// All logical statements allow here except return statement with value.
}
}

Example: Program to show the use of the constructor in C#.
namespace ConstructorDemo
{
class Employee
{
Employee ()//Default Constructors
{
Console.WriteLine("This is Default Constructor");
    Console.ReadKey();

}
public static void Main(string[] args)
{
Employee p = new Employee ();
}
}
}
OUTPUT:
This is Default Constructor

Can we define a method with the same class name in C#?
No, it is not possible to define a method with the same class name in C#.
We receive a compile-time error if we do so.

How many types of constructors are there in C#.net?
Basically 5 types of constructor are used in C#:
  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
  4. Static Constructor
  5. Private Constructor

Let’s discuss one by with examples.
Default Constructor in C#:
This constructor has no any parameter. Again we classified it into two types:
  1. System-defined
  2. User-defined

What is System Defined Default Constructor in C#?
If we are not defined any constructor explicitly in your program, then the system will provide one by default constructor at the time of compilation. That constructor is called a default constructor. This constructor will assign default values to the data members (non-static variables). Because it is created by the system so it is called system-defined default constructor.

Ex:
namespace MyConstructorDemo
{
class Employee
{
int id{get; set;}
string name{get; set;}
int age{get; set;}
string address{get; set;}

public void GetDetails()
{
Console.WriteLine("Employee id is: " + id);
Console.WriteLine("Employee name is: " + this.name);
Console.WriteLine("Employee age is: " + this.age);
Console.WriteLine("Employee address is: " + address);
}
}
class TestData
{
static void Main(string[] args)
{
Employee myEmployee1 = new Employee();
Employee myEmployee2 = new Employee();
myEmployee1.GetDetails ();
myEmployee2.GetDetails ();
Console.ReadKey();
}
}
}
Output:
Employee id is: 0
Employee name is:
Employee age is: 0
Employee address is:
Employee id is: 0
Employee name is:
Employee age is: 0
Employee address is:

Remember: If you are not defined any constructor explicitly, system will provide you a default Constructor.

When do we need to provide the constructor explicitly?
At the time of object creation, if we want to execute some logic, then we must provide the constructor explicitly.

What is User-Defined Default Constructor in C#?
This constructor is defined by the user without any parameter. It does not accept any parameter.
Syntax:
Class Employee
{
Employee ()//User defined Default Constructor
{
// All logical statements allow here except return statement with value.
}
}






Ex:
namespace ConstructorDemo
{
class Employee
{
int id{get; set;}
string name{get; set;}
int age{get; set;}
string address{get; set;}

public Employee()
{
this.id = 1001;
age = 25;
this.name = " Allen Donald";
address = "Melbourne, Australia";
}
public void GetDetails()
{
Console.WriteLine("employee id is: " + id);
Console.WriteLine("employee name is: " + this.name);
Console.WriteLine("employee age is: " + this.age);
Console.WriteLine("employee address is: " + address);
}
}
class Test
{
public static void Main(string[] args)
{
Employee myEmployee1 = new Employee();
Employee myEmployee2 = new Employee();
myEmployee1. GetDetails();
myEmployee2. GetDetails();
Console.ReadKey();
}
}
}
OUTPUT:
employee id is: 1001
employee name is: Allen Donald
employee age is: 25
employee address is:  Melbourne, Australia

employee id is: 1001
employee name is: Allen Donald
employee age is: 25
employee address is:  Melbourne, Australia

Remember: Every object (myEmployee1 and myEmployee2) will be initialized with same values. It means it is not possible to initialize each object with different values.
 
When should we define a parameterized constructor in a class?
In previous example we have seen that it is not possible to initialize each object with different values.
If you want to initialize each object dynamically with unique values then we have to use the parameterized constructor.
Main advantage of parameterized constructor is: you can initialize each object with unique values.

What is Parameterized Constructor in C#?
Parametrized constructor allows us to provide parameters in constructor in C#.
Parametrized constructor allows us to provide unique values for each instance of the class.
Example of parameterized constructor in C#:
namespace ParameterizedConstructorDemo
{
class Employee
{
    int id { get; set; }
        string name { get; set; }
        int age { get; set; }
        string address { get; set; }

public Employee(int id, string name,  int age, string address)
{
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public void DisplayData()
{
Console.WriteLine("employee id is: " + id);
Console.WriteLine("employee name is: " + this.name);
Console.WriteLine("employee age is: " + this.age);
Console.WriteLine("employee address is: " + address);
}
}
class TestDemo
{
static void Main(string[] args)
{
Employee myEmployee1 = new Employee(1001, " Allen Donald ", 25," Melbourne, Australia");
Employee myEmployee2 = new Employee(1002, "Donald Trump",27, " Melbourne, Australia");
myEmployee1. DisplayData ();
myEmployee2. DisplayData ();
Console.ReadKey();
}
}
}
OUTPUT:

employee id is: 1001
employee name is:  Allen Donald
employee age is: 25
employee address is:  Melbourne, Australia

employee id is: 1002
employee name is: Donald Trump
employee age is: 27
employee address is:  Melbourne, Australia

How many constructors can be defined in a class in C#?
We can define multiple numbers of constructors with different signature in c#.  Different signature means the number of parameters, type of parameters and order of parameters should be different.
 
What is Copy Constructor in C#?
When we pass the parameter as class type in a constructor, then it is called copy constructor. Copy constructor is used to copy one object data into another object.
Purpose of the copy constructor: initialize a new object with values of an existing object.
Example of copy constructor in C#:
namespace CopyConstructorDemo
{
class Employee
    {
        int id { get; set; }
        string name { get; set; }
        int age { get; set; }
        string address { get; set; }
        public Employee()
        {
            Console.WriteLine("ENTER EMPLOYEE DETAILS");
            Console.WriteLine("Enter the employee id");
            this.id = int.Parse(Console.ReadLine());           
            Console.WriteLine("Enter the employee name");
            this.name = Console.ReadLine();
            Console.WriteLine("Enter the employee age");
            this.age = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the employee address:");
            this.address = Console.ReadLine();
        }
        public Employee(Employee myobj)
        {
            this.id = myobj.id;
            this.name = myobj.name;
            this.age = myobj.age;
            this.address = myobj.address;
        }
        public void DisplayData()
        {
            Console.WriteLine();
            Console.WriteLine("Employee id is: " + this.id);
            Console.WriteLine("Employee name is: " + this.name);
            Console.WriteLine("Employee age is: " + this.age);
            Console.WriteLine("Employee address is: " + this.address);
        }
    }
    class TestData
    {
        static void Main(string[] args)
        {
            Employee myEmployee1 = new Employee();
            Employee myEmployee2 = new Employee(myEmployee1);
            myEmployee1.DisplayData();
            myEmployee2.DisplayData();
            Console.ReadKey();
        }
    }
}
OUTPUT:
ENTER  DETAILS
Enter the  id
1001
Enter the  name
Donald Trump
Enter the  age
55
Enter the  address:
Melbourne, Australia

Id is: 1001
Name is: Donald Trump
Age is: 55
Address is: Melbourne, Australia

Id is: 1001
Name is: Donald Trump
Age is: 55
Address is: Melbourne, Australia
 
Understanding Static Constructor in C#:
Like other Constructor, we can also create Static Constructor. Static Constructor is invoked only once when creating first instance (object) of the class.
Static Constructor is used to initialize the static fields of the class. If we write some code inside the static constructor, it will execute only once, when creating first instance (object) of the class.
Remember:
  1. We can define only one static constructor in a class.
  2. Static constructor has no any parameter.
  3. Static constructor access static members of the class.
  4. No any access modifier define in static constructor definition.
  5. We cannot create the object for the static class.
  6. Static constructor invoked only once. It is called at the time of first object creation of the class. After that any object creation static constructor will not be called.

Ex: Static Constructor with an example:
namespace ConstructorDemo
{
class StaticconstructorDemo
    {
        int i;
        static int j;
        public StaticconstructorDemo()
        {
            i = 100;
        }
        static StaticconstructorDemo()
        {
            j = 100;
        }
        public void Display()
        {
            Console.WriteLine("value of i : " + i);
            i++;
            Console.WriteLine("value of j : " + j);
            j++;
        }
    }
    class TestDemo
    {
        static void Main(string[] args)
        {
            StaticconstructorDemo myDemo1 = new StaticconstructorDemo();
            e1.Display();
            e1.Display();
            StaticconstructorDemo myDemo2 = new StaticconstructorDemo();
            myDemo1.Display();
            myDemo2.Display();
            Console.ReadKey();
        }
    }
}
OUTPUT:
value of i : 100
value of j : 100
value of i : 101
value of j : 101
value of i : 100
value of j : 102
value of i : 101
value of j : 103

Is it possible to initialize non-static data members within a static constructor in C#?
No it is not possible. If we do show, we will get compile time error.
Ex:
namespace OOPSDEMO
{
    class StaticconstructorDemo
    {
        int i;
        static int j;

        static StaticconstructorDemo()
        {          
            i=101; //not allowed
            j=100;
        }
    }

    class TestDemo
    {
        static void Main(string[] args)
        {          
            Console.ReadKey();
        }
    }

}
Output: An object reference is required for the non-static field, method, or property.

Can we initialize static data fields within a non-static constructor in C#?
Yes it is possible. Consider the following example:
namespace OOPSDEMO
{
    class NonStaticconstructorDemo
    {
       public int i;
       public static int j;

       public NonStaticconstructorDemo()
        {          
            i=100;
            j=101;
        }
    }

    class TestDemo
    {
        static void Main(string[] args)
        {
            NonStaticconstructorDemo myDemo1 = new NonStaticconstructorDemo();
           
            Console.WriteLine("value of i : " + myDemo1.i);
            Console.WriteLine("value of j : " + NonStaticconstructorDemo.j);
            Console.ReadKey();
        }
    }
}

Output:
value of i : 100
value of j : 101

Can we initialize static data fields in both static and non-static constructor?
Yes, it is possible. 

What is Private Constructor in C#?

A constructor whose accessibility is private is called Private constructor. We cannot create an object for Private constructor outside of the class.  Private constructor is used to create an object within the same class. Generally, private constructors are used in Remoting concept.

Example of private constructor:
namespace PrivateConstructorDemo
{
class PCDemo
{
private PCDemo ()
{
Console.WriteLine("This is private constructor demo.");
}
static void Main(string[] args)
{
PCDemo myPCDemo = new PCDemo ();
Console.ReadKey();
}
}
}
Output: This is private constructor demo.

What is constructor overloading?

Constructor overloading means multiple constructors within a class with different parameter types, different number of parameters and different order of parameters.

Example of Constructor Overloading in C#:
namespace ConstructorOverloadingDemo
{
   public class MyConstructorOverloading
    {
        int myValue;
        public MyConstructorOverloading()
        {
            this.myValue = 10;
        }
        public MyConstructorOverloading(int x)
        {
            this.myValue = x;
        }
        public void DisplayData()
        {
            Console.WriteLine("the value of myValue:{0}", myValue);
        }
    }
    class TestDeta
    {
        static void Main(string[] args)
        {
            MyConstructorOverloading myObj1 = new MyConstructorOverloading();
            MyConstructorOverloading myObj2 = new MyConstructorOverloading(50);
            myObj1.DisplayData();
            myObj2.DisplayData();
            Console.ReadKey();
        }
    }
}

OUTPUT:
the value of myValue:10
the value of myValue:50

That’s it for today. In the next article, I am going to discuss the Destructors in C# with examples. Here, in this article, I try to explain the different types of Constructors in C# with examples. I hope now you understood the need and use of different types of constructors.


Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Posts Older Posts Home

Sample Text

Copyright © Football Tutorial | Powered by Blogger
Design by | Blogger