In this post we will review a bunch of small and simple LINQ interview questions to get you started. Once you understand these standard query expressions, it should be a breeze for you to answer LINQ questions.
Question: Given an array of numbers, find if ALL numbers are a multiple of a provided number. For example, all of the following numbers - 30, 27, 15, 90, 99, 42, 75 are multiples of 3.
The trick here is to use the Enumerable.All<TSource> method which determines whether all elements of a sequence satisfy a condition.
static void Main(string[] args)
{
int[] numbers = { 30, 27, 15, 90, 99, 42, 75 };
bool isMultiple = MultipleTester(numbers, 3);
}
private static bool MultipleTester(int[] numbers, int divisor)
{
bool isMultiple =
numbers.All(number => number % divisor == 0);
return isMultiple;
}
Question: Given an array of numbers, find if ANY of the number is divisible by 5. For example, one of the following numbers - 30, 27, 18, 92, 99, 42, 72 is divisible by 5.
Again, the key fact here is to use Enumerable.Any<TSource> method which determines whether any element of a sequence satisfies a condition. The code is very similar to the ALL case.
static void Main(string[] args)
{
int[] numbers = { 30, 27, 18, 92, 99, 42, 72 };
bool isDivisible = IsDivisible(numbers, 3);
}
private static bool IsDivisible(int[] numbers, int divisor)
{
bool isDivisible =
numbers.Any(number => number % divisor == 0);
return isDivisible;
}
Another way to present a similar question that utilizes Enumerable.Any<TSource> method is shown below.
Question: Given a GPSManufacturer and GPSDevice data structure as defined below, find all the manufacturers that have at least 1 or more active GPS devices.
class GpsDevice
{
public string Name;
public bool IsActive;
}
class GpsManufacturer
{
public string Name;
public GpsDevice[] Devices;
}
static List<GpsManufacturer> gpsManufacturers =
new List<GpsManufacturer>
{
new GpsManufacturer
{Name = "manf1",
Devices = new GpsDevice[]
{
new GpsDevice {Name = "device1", IsActive = true},
new GpsDevice {Name = "device2", IsActive = false}
}
},
new GpsManufacturer
{Name = "manf2",
Devices = new GpsDevice[]
{
new GpsDevice {Name = "device1", IsActive = false},
new GpsDevice {Name = "device2", IsActive = false}
}
},
new GpsManufacturer
{Name = "manf3"}
};
The following function finds out the active GPS manufacturers.
public static void ActiveGpsManufacturers()
{
// Determine which manufacturers have a active device.
IEnumerable<string> activeManf =
from manf in gpsManufacturers // for all manufacturers
where manf.Devices != null && // they have a list of devices
// and at least one of the device is active
manf.Devices.Any(m => m.IsActive == true)
select manf.Name; // select them
// just for debugging
foreach (string name in activeManf)
Console.WriteLine(name);
}
As an extension question and using Lambda expressions, consider the following question:
Question: Find the count of all the manufacturers that have at least 1 or more active GPS devices.
public static void ActiveGpsManufacturersCount()
{
// to count the number of active manufacturers
int activeManfCount =
gpsManufacturers.Count
(manf => (manf.Devices != null &&
manf.Devices.Any(m => m.IsActive == true)));
// debugging
Console.WriteLine("Active Manf count: {0}", activeManfCount);
}
nice piece of work
ReplyDeleteHi Nikhil,
DeleteHip Hip Hooray! I was always told that slightly slow in the head, a slow learner. Not anymore! It’s like you have my back. I can’t tell you how much I’ve learnt here and how easily! Thank you for blessing me with this effortlessly ingestible digestible content.
Do you know something about that? is it coming? is going to progress? A friend of mine told me but I didn't understand very well.
There can be many reasons due to which application can be taking too long, however one reason can be wrong configuration of Xms. For instance, if Xms value is too high, close to physical memory (RAM) than pagination of memory will start happening, resulting in slowness of application.
if you know something about this, can you share with me?
THANK YOU!! This saved my butt today, I’m immensely grateful.
Obrigado,
Kevin
thanks for the nice questions. http://prasadaknair.blogspot.com
ReplyDeleteNice work dude....
ReplyDeleteHi There,
DeleteAmaze! I have been looking bing for hours because of this and i also in the end think it is in this article! Maybe I recommend you something helps me all the time?
have created a Cache connection manager to store 20GB of data. My Physical memory on the server is 1.5TB and whey I run the package, it is failed at Cache connection with the following error.
Deactivate rollback segment R0 and activate the newly created rollback segments.
Error: The system reports 12 percent memory load. There are 1649153396736 bytes of physical memory with 1446557532160 bytes free. There are 4294836224 bytes of virtual memory with 39915520 bytes free. The paging file has 1711430443008 bytes with 1493163847680 bytes free.
Error: A buffer failed while allocating 14679520 bytes.
When it caches data, where is it stored? Physical Memory or Virtual Memory??
How to change it to store in Physical Memory?
I am so grateful for your blog. Really looking forward to read more.
Cheers,
Morgan
Thanks, looking more linq interview questions.
ReplyDeleteGorgeous man that did the coding
ReplyDeletethanks a lot .....
ReplyDeleteRealy great effort Thanks
ReplyDeleteNice post Nikhil. Keep at it. You are doing a great favour to plenty of programmers around the world.
ReplyDeletethanks for the nice questions..
ReplyDeletehttp://www.sqlaspnet.com/interview/LINQ/
good
ReplyDeleteGood Article..
ReplyDeleteOnline LINQ
Training
LINQ Training
Online LINQ Training from India
LINQ Training in Chennai
Dot Net Training in Chennai
.Net Online Training
ASP.NET Training
Awesome..Thanks
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteSVR Technologies provide Mulesoft Training with Mulesoft Video Tutorials, Live Project, Practicals - Realtime scenarios, CV, Interview and Certification Guidance.
ReplyDeleteSVR Technologies MuleSoft training is designed according to the latest features of Mule 4.It will enable you to gain in-depth knowledge on concepts of Anypoint Studio Integration techniques, testing and debugging of Mule applications, deploying and managing the Mule applications on the cloud hub, dataweave transformations, etc. You will also get an opportunity to work on two real-time projects under the guidance of skilled trainers during this training.
Enquire Now: +91 9885022027
Enroll Now: https://bit.ly/2OCYVgv
https://svrtechnologies.com/contact-us/
Angular Training,
AWS Training Online,
Best Online Training,
Devops Training,
Machine Learning Training,
Mulesoft Training,
Online Training Institute,
Python Training,
Salesforce Training,
SAP Training,
Tableau Training,
Tibco Training
Descriptive Questions are always helpful to clear a job Interview. I am a programmer and I know how hard is to clear a job interview. That why I created Online Interview questions
ReplyDeleteUseful information,don't stop sharing and Please keep updating us..... Thanks
ReplyDeletewordpress interview questions
Nice post , thanks
ReplyDeleteInterview Queries
Thanks for sharing such a great information. Its really nice and informative kanban training.
ReplyDeleteHi ,thanks for this awesome post . check Sql query questions asked in interviews
ReplyDeleteNice blog to read, Thanks for sharing this valuable article.
ReplyDeleteBest Apache Spark Online Course
Apache Spark and Scala Online Training
nice Post thanks for the information, good information & very helpful for others.
ReplyDeleteSailpoint Interview Questions
Oracle Fusion HCM Training
Workday Training
Nice post and good information on LINQ interview questions. Thanks for sharing.
ReplyDeletepsychometric test for employees
swift interview questions
Good Post, Thank You for shearing the post MCQ Point
ReplyDeleteMua vé tại đại lý vé máy bay Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ bao nhiêu
giá vé máy bay từ california về việt nam
chuyến bay từ canada về việt nam
chuyến bay nhân đạo từ nhật về việt nam
đặt vé máy bay từ hàn quốc về việt nam
Vé máy bay từ Đài Loan về Việt Nam
danh sách khách sạn cách ly tại hà nội
ve may bay chuyen gia nuoc ngoai sang Viet Nam
nice Post thanks for the information, good information & very helpful .
ReplyDeleteBest MicroNutrients Company in India
It’s amazing in support of me to truly have a web site that is valuable meant for my knowledge.
ReplyDeletelogo design business