After staring at a blank IDE for a while, I decided what I reallywanted to do was create an app that would give me n number of prime numbers in a list.
Rules for optimization:
- Don't optimize.
- Optimize later.
I decided that the best way to go for this project would be to determine the next prime number from a given integer. That way, I could generate lists and other such nonsense. I settled on the following sequence:
public int nextPrime(int lastNo)
int currentNo = lastNo;
bool isNotPrime = true;
isNotPrime = false;
for (int i = 2; i < currentNo; i++)
if (currentNo % i == 0)
isNotPrime = true;
The premise of the function is that we want to find the next prime number, not whether the input number itself is prime.
- We create a copy of the input number (currentNo).
- We create a conditional check (is the number not prime?) and set to true. This is so we can pass into the first conditional.
- While the conditional is true (the number is not prime)
- Set the conditional to false. We are setting the assumption that the number is indeed a prime.
- increment the currentNovalue by 1.
- Create a for-loop to go through all of the numbers between 2 and the currentNominus 1.
- If the modulus of currentNodivided by the increment value (i) equals 0, then the number cannot be prime, so we set the test flag as true and break from the loop. Otherwise, we continue iterating through the set range of numbers until we exhaust it, or we manage to change the flag.
- If the test flag is true, re-run the sequence until it is not.
- Return the first value that results in the for-loop not changing the test flag to true.
- What are the four tenets of an OOP language?
Abstraction- refers to hiding the internal operations of a class from anything that consumes that class.
Encapsulation- refers to the bundling of data and methods into a single object or class
Inheritance- refers to the ability of one class to use properties of another class
Polymorphism- refers to the ability to use different object types through the same interface.
- What is boxing and unboxing?
Boxing is the ability to store a value type into an object.
Unboxing is the ability to take an object variable and convert it to a specific value type.
- What is a generic?
A genericis a placeholder type. It allows the developer to use an object without immediately determining a typeset. Used primarily in collections.
- What is LINQ?
LINQ(Language Integrated Query) is a methodology for querying data and objects.
- What is a Lambda Expression?
A Lambda Expression is the shorthand syntax for writing LINQ statements.
- What are delegates?
Delegates allow the developer to set variables as function calls. It can be thought of as to a pointer to a function.
- What is an Abstract Class?
An abstract class is a class that cannot be instantiated. Abstract classes can, however, be inherited.
- What is an Interface?
An interfaceis a collection of abstract methods.
- What is the difference between overloading and overriding?
Overloading - multiple functions of the same name, using different input parameters.
Overriding - use of a subclass to override(replace) the method functionality of a main class.
Overloading is a static binding, while overriding is a dynamic binding.
- What are the Access Modifiers?
- What is the difference between Public, Static and Void?
Public - the object or method is available to anything able to consume it.
Private - the object is available only to the class that contains it.
Static- the object is globally accessible without instantiating a class.
- What is a Constructor?
A constructor is a function that has the same name as its containing class.
- What is Serialization?
Serialization is the act of turning an object into a stream of bytes. This stream can then be stored into another platform (like a file.)
A class can be made serializable by declaring it as ISerialize.
- What is Deserialization?
Deserialization is the conversion of a byte array into a set object.
- What's the difference between Read-Only variables and Constant variables?
Constant variables are set once during compile time and cannot be changed. Read-Only variables are used only when setting a value at runtime.
- Can you override the methods in an interface?
No. All of the interface methods are virtual, so they can only be overridden in the class that inherits them.
- What is the difference between a Struct and a Class?
A Struct is a value-type variable, where a Class is a reference type. Structs cannot be inherited, and are stored on the stack for speed of access.
- What is the difference between Stack and Heap memory?
Stack memory is more structured and managed by the system. Heap memory is managed by the application.
Value types are allocated to the Stack. Reference types are allocated to the Heap.
- What is a sealed modifier?
A sealed modifier prevents the object from being inherited,
- What is Jagged Array?
A jagged array is an array of arrays.
- What is Managed code?
Managed Code is any code executed by the CLR. The code is 'managed' by the .NET Framework.
- What are the different types of classes in C#?
Keyword: Partial Class - A 'shared' class allows its members to be used by multiple .cs files.
Keyword: Sealed class - A class that cannot inherited. To access its members, you need to instantiate the class directly.
Keyword: Abstract class - A class that cannot be instantiated. This class can only be inherited, and will contain at least one method.
Keyword: Static class - A class that does not allow inheritance. Members of a static class are also static.
- What are the steps taken in C# code compilation?
1. Compiling the source code to Managed Code
2. Combining newly created code to Assemblies
3. Loading the Common Language Runtime (CLR)
4. Executing the assembly by the CLR
- What's the difference between a Virtual Method and an Abstract Method?
Abstract Method - always has a default implementation. It can be overridden in its derived class, though not necessarily mandatory. It can be overridden using the keyword: override.
Virtual Method - does not have an implementation, and resides in the abstract class. It is mandatory that the the derived class implements the abstract method.
- What are Events?
Events are actions that generate some type of notification.