Tuesday, January 31, 2017

Return a Value from function using thread

1. First Version

string res = null;
Thread newThread = new Thread(() => {res = sayHello("world!");}); 
newThread.Start(); 
newThread.Join(1000); 
Console.Writeline(res);

2. Second Version : Using Background Worker

In DoWork you can set e.Result =Value;

And in RunWorkerCompleted event you can get the result using e.Result.

DoWork event
private void BackgroundWorker1_DoWork(
    object sender,
    System.ComponentModel.DoWorkEventArgs e)
{
    AreaClass2 AreaObject2 = (AreaClass2)e.Argument;
    // Return the value through the Result property.
    e.Result = AreaObject2.CalcArea();
}

RunWorkerCompleted event
private void BackgroundWorker1_RunWorkerCompleted(
    object sender,
    System.ComponentModel.RunWorkerCompletedEventArgs e)
{
    // Access the result through the Result property.
    double Area = (double)e.Result;
    MessageBox.Show("The area is: " + Area.ToString());
}

3. Using Task class

.NET 4.0
Without specifying an input parameter:


Task task = new Task(() =>
{
int total = 0;
for (int i = 0; i < 500; i++)
{
total += i;
}
return total;
});

task.Start();
int result = Convert.ToInt32(task.Result);

.NET 4.5
The recommended way in .NET 4.5 is to use Task.FromResult, Task.Run or Task.Factory.StartNew:
FromResult:


public async Task DoWork()
{
int res = await Task.FromResult(GetSum(4, 5));
}

private int GetSum(int a, int b)
{
return a + b;
}

Infogain

1. Commands in WPF
Ans Commands provide a way to View to update the modal in MVVM architechture. ICommand interface use to bind buttons and other user control to Functions of Modal.

2. Difference between Generalization and Specialization
Ans : Click Her for Ans

3. Covariance and Contravariance
Ans : Click Here for Ans

4. Mutex() lock() and semaphore() in threading
Ans : Click Here For Semaphore 
          Click Here For Mutex 
          Click Here For Lock

5. Dead Lock
6. Custom Controls in WPF
Ans : Click Here For Detail


Shared by Arvind

Sapient

1) Value type vs Reference Type
2) Ref vs out
3) String vs StringBuilder
4) Throw vs Throw ex
Ans : Click Here For Detail
5) For vs foreach
6) Can we write try() without catch()?
7) What happens in finally() when catch() has return statement?
8) What are the ways to reverse a string without using reverse() function?
9) How can we execute a function which has some return statement in a thread?
Ans : Click Here For Detail

10) Singleton Pattern
Ans : Click Here For Detail

11) Observer Pattern
Ans : Click Here For Detail

12) MVVM

Shared by Arvind

Thursday, January 19, 2017

Difference Between List and Observable Collection

1. Observable collection has automatically update the UI in case of adding and removing items. While List does not update the UI while we use list as binding.

2. Observable Collection implements INotifyCollectionChanged and INotifyPropertyChanged interface. So in case of using Observable Collection no need to use INotifyCollectionChanged and INotifyPropertyChanged interface explicitly.

https://www.youtube.com/watch?v=YW3gq-zyzoE&feature=em-subs_digest

Wednesday, January 18, 2017

Devrient

1. Describe your project architecture of last project?
2. Which design pattern you have used there?
3. Why did you use factory pattern? Is any other alternate available?
4. How do you ensure your developer has written right code?
5. What type of testing you do before deployment of application after making changes?


Monday, January 16, 2017

Binary Tree

Binary Tree - A binary tree has a special condition that each node can have a maximum of two children.

Binary Search Tree - Binary Search tree exhibits a special behavior. A node's left child must have a value less than its parent's value and the node's right child must have a value greater than its parent value.

Full Binary Tree - A full binary tree (sometimes proper binary tree or 2-tree or strictly binary tree) is a tree in which every node other than the leaves has two children.

Complete Binary Tree - A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

B Tree -In B-Tree every node can store more than one value (key) and it can have more than two children. B-Tree was developed in the year of 1972 by Bayer and McCreight with the name Height Balanced m-way Search Tree. Later it was named as B-Tree.

B-Tree can be defined as follows...

B-Tree is a self-balanced search tree with multiple keys in every node and more than two children for every node.
Here, number of keys in a node and number of children for a node is depend on the order of the B-Tree. Every B-Tree has order.

B-Tree of Order m has the following properties...

Property #1 - All the leaf nodes must be at same level.
Property #2 - All nodes except root must have at least [m/2]-1 keys and maximum of m-1 keys.
Property #3 - All non leaf nodes except root (i.e. all internal nodes) must have at least m/2 children.
Property #4 - If the root node is a non leaf node, then it must have at least 2 children.
Property #5 - A non leaf node with n-1 keys must have n number of children.
Property #6 - All the key values within a node must be in Ascending Order.
For example, B-Tree of Order 4 contains maximum 3 key values in a node and maximum 4 children for a node.

Difference Between delegate (Multicast Delegate) and event

1. Events can be included in an interface declaration, whereas a field cannot.
2. Events can't directly be assigned. While delegate object can be assign directly means it is chance that you override  the previous assign method.
Example with Delegates (Action in this case that is a kind of delegate that doen't return value)

public class Animal
{
    public Action Run {get; set;}

    public void RaiseEvent()
    {
        if (Run != null)
        {
            Run();
        }
    }
}
to use the delegate you should do something like this

Animale animal= new Animal();
animal.Run += () => Console.WriteLine("I'm running");
animal.Run += () => Console.WriteLine("I'm still running") ;
animal.RaiseEvent();
this code works well but you could have some weak spots.

For example if I write this

animal.Run += () => Console.WriteLine("I'm running");
animal.Run += () => Console.WriteLine("I'm still running");
animal.Run = () => Console.WriteLine("I'm sleeping") ;
with the last line of code I had override the previous behaviors just with one missing + (I have used + instead of +=)

Another weak spot is that every class that use your Animal class can raise RaiseEvent just calling it animal.RaiseEvent().

To avoid this weak spots you can use events in c#.

3. You aren't using a public property but a public field (with events the compiler protect your fields from unwanted access)

4. No one outside of your class can raise the event.

Thursday, January 12, 2017

Accolite

1. Difference between multicast delegate and event
Ans : Click Here

2. Difference between static method call and instance method call using reflection
Ans :  In case of instance method call we to pass object while in case of staic method call we to pass null instead of object.

3. CompareTo method with Lowercase and uppercase string.

What  will be the value of i?

string  a = "F"
string b= "f";

int i = a.CompareTo(b);

Ans : It will return -1 if a is smaller than b
It will return 1 if a is greater than b
It will return 0 if a is equal to b.

In case of string it compare on ASCII values.

4. How can you create dynamic interfaces?
Ans : Using Reflection (System.Emit) class.

5. Difference between STA and MTA
Ans: Click Here

6. Why MTA option is not available in WPF?
Ans: Microsoft tried to go with MTA in WPF but could not due to incompatibility of old com component.

7. How delegate maintain function call? Someone is adding function and someone is removing function?
And : Delegate are immutable.

8. What else delegate store apart from function address?
And : object

9. If you have not source code of an interface and you want to add one function declaration than how you can?
Ans : You can do the same job by using dynamic keyword.

10. If two classes has same name method and you have to create an object which can call both class method than how will you do that?
Ans : Can achieve that using dynamic keyword.

11. How can we use multiple core of CPU while using thread?
Ans: While using task classes they used multiple core of CPU but can't do that explicitly.

12. Which Pattern support multiple factory?

13. What is thread affinity?
Ans : Click Here

14. How can you create thread safe singleton object without using lock statement
Ans. using Nested classes

15. How tables are stored in memory?
Ans : Database are stored in .mdf file (Measurement data file). .mdf file are binary file.


Wednesday, January 11, 2017

How to stop routed event?

We have following xaml
 x:Class = "WPFRoutedEvents.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Title = "MainWindow" Height = "450" Width = "604" ButtonBase.Click  = "Window_Click" >
 
    
       Margin = "20" ButtonBase.Click = "StackPanel_Click">             
          
   

If you want to stop the routed event at any particular level, then you will need to set the e.Handled = true;
Let’s change the StackPanel_Click event as shown below −
private void StackPanel_Click(object sender, RoutedEventArgs e) 
{
 txt2.Text = "Click event is bubbled to Stack Panel"; 
e.Handled = true; 
}


Before Stopping the output will be

Button is Clicked
Click event is bubbled to Stack Panel
Click event is bubbled to Window

After Stopping the output

Button is Clicked
Click event is bubbled to Stack Panel


Difference Static Class and Singleton Pattern



1. You can not create instance of static class while you can create instance of singleton class.

2. Static class loaded by compiler when the program or namespace containing the class is loaded.
While singleton can be lazy load when needed. Static class are always loaded.

3. Static class can not have instance constructor while singleton class can have instance constructor.

4. Static class not pass in a function while singleton object can be pass in a function.

5. Static class can not inherit another static class while singleton class can inherit another class.

6. Static class should have all method static while singleton class can have both methods.

7. Static class is fast in comparison of singleton.

8. Static class can not implement interface while singleton class can implement interfaces.

Devrient

1. Can abstract class have constructor?
Ans Yes

2. What is the difference between Static class and Singleton object class?
Ans Click Here

3. What is trigger in WPF?
Ans Click Here

4. What property trigger in WPF?
Ans Click Here

5  What is event routing?
Ans Click Here

6. How do you stop event routing at particular control?
Ans Click Here

7. Write a program to counts the occurrence of a particular number in an array.
8. How do you decide interface should be use of abstract class?
9. How do you ensure before write a function that it should be unit testable?
10. How many clustered index can be created in a table?
11. How many non clustered index can be created in a table.
12. How do you decide on which column index should be created?
13. Give me a real scenario where you have used dependency property.
Ans Click Here

14. Have you used TDD (Test Driven Development) approach?
15. What will be the output of following query

Select * from Emp where 1=0

And what will be the output of

Select * from Emp Null=Null

16. Write a query to give maximum salary of every department from Emp table.
17 Difference between Factory Pattern an abstract Factory pattern.

Difference between background thread and foreground thread


Foreground Thread


Foreground threads are threads which will continue to run until the last foreground thread is terminated. In another way, the application is closed when all the foreground threads are stopped.

So the application won't wait until the background threads are completed, but it will wait until all the foreground threads are terminated.

By default, the threads are foreground threads. So when we create a thread the default value of IsBackground property would be false.

Background Thread


Background threads are threads which will get terminated when all foreground threads are closed. The application won't wait for them to be completed.

We can create a background thread like following:
  1. Thread backgroundThread = new Thread(threadStart);  
  2. backgroundThread.IsBackground = true;  
  3. backgroundThread.Start();  

Tuesday, January 10, 2017

Competent Software

1. What is b Tree?
2. What is Binary Tree?
3. What is evil tree?
4. Design a database for name and multiple addresses
5. What is difference between function and procedure?
6. Write a function with Xml Documentation. Suppose you have to write a function for adding two numbers than what is the comment you will write above function.

7. Can we inheritance in structure?
Ans No

8. Can we have event in structure?
Ans Yes

9. Can we have delegates in structure?
Ans Yes

10. Can we have constructor  signature in interface?
Ans No

11. Write a LINQ query for an array.
12. What is Observer Pattern

13 Why can a .NET delegate not be declared static?
Ans

Try this:
public delegate void MoveDelegate(object o);
public static MoveDelegate MoveMethod;
So the method-variable can be defined static. The keyword static has no meaning for the delegate definition , just like enum or const definitions.

Friday, January 6, 2017

TDD

TDD (Test Driven Development) is an approach for software development where Test is written before starting the development.

Steps are

Suppose you are planning to write a function for adding two numbers. Than

1. Write the Test first to test that function that is in your mind but not exist yet.
2. Now write the function for adding to two number.
3. Now run the test that you have written already.

Repeat the these for every single function you are planing to write.




Followers

Link