MT1 Flashcards
Purpose of top-level statements?
write smaller programs without the use of the main method
How to write top-level statements
- “using” directives (optional)
ex. using System; using System.Collections.Generic;
(compiler automatically adds set of “using” directives” based on project type) - Statements (what we would write in main method)
ex. Console.Writeline… - More type / namespace declarations (optional)
- used when we have user-defined classes etc.
What happens to a top-level statement?
compiler will put statments (not written in main method) into an auto-generated method / class
if methods are used to write top-level statements, then they become local functions
at most one file in C# project can have top-level statements
What makes software high quality?
- correct (safe from bugs)
- comprehensible (readable, understandable)
- changeable (ready to change)
What is a type
set of values (data) and the operations permitted on the values
Two naming conventions
PascalCasing –> class + method
camelCasing –> local variables + fields
What is a constructor?
method used to initialize objects whenever an instance of the class (both have same name) is created
ex. when class Date is instantiated, constructor Date will set up an int type to create the numerical value of said Date
What does “private” mean
protects types from being manipulated outside of the class it is in.
Value vs Reference type
Value types –> need 1 allocated memory space to store data
ex. int x = 5;
Reference type –> stores reference to some other memory location where actual data is stored
ex string str = “hello” (str is immutable because it only references a memory locattion)
Explain the concept of Stack and Heap
Stack and Heap –> regions of memory to store data
Stack–> stores value types + reference type (only the reference), LIFO data structure
Heap –> stores actual data of reference type
What is a class?
type that stores logically related data + functions
a way to create + define new types and a set of values + operations associated with that type
contains:
data members –> variables
function members –> operations
What is a Class: Field?
stores data
a variable (of any type) that is a member of a class
PascalCasing for static public + protected fields
otherwise camelCasing
What is a Class: property?
member that represents data
(looks like a field but it does not necessarily allocate memory for data)
name set of two methods (accessors):
set + get
What is a method?
Block of code that can be executed / invoked
( do not use void to indicate that method doesn’t take parameters; simply use () )
What are local variables
holds temporary data within scope of a method (doesn’t exist outside of a method)
Difference between Private and Public Access Modifiers
Private members –> accessible within class in which they are declared (default access level)
Public members –> accessible to other objects in program
What does the following code do?
Console.WriteLine(message + “ “ + name)
Appends the data for message, appends a space, and appends the data for name
What happens to the string if a code below is compiled:
Console.WriteLine($”{message1.ToUpper()}”);
prints in ALL CAPS
BUT
b/c strings are immutable, a new string data is created that has the same string but modified.
What is String Builder
provides a mutable alternative to string
(uses namespace: System.Text)
ToString –> convert to string
Append –> adds value
Clear , Remove, Insert etc.
What is encapsulation?
information hiding
ex. declaring fields to be private
advantage: hiding details allows them to be changed later without changing the entire system
What is Inheritance?
types can be derived from other types (types can be subtypes)
ex. all types derived from System.Object type
Polymorphism
ability of object to take on many forms
ex. (supertype can refer to subtype object)
What is method overloading?
allows us to create multiple implementations of method with same name
ex.
public int Add(int a, int b, int c)
{
return a + b + c
}
public int Add(int a, int b)
{
return a + b
}
What is an array?
set of uniform data elements represented by a single name
- size cannot be changed
int[] array = {1, 2, 3} ;
char [] array = “string”
What are lists?
data type representing an ordered sequence of elements that can be accessed by index
- can add / remove elements
- initialize via using directive:
ex. using System. Collections. Generic;
List<T> (where T can be any type
List<int> numbers = new List <int>();
List <char> characters = new List <char> ();</char></char></int></int></T>
Methods of List <T> class</T>
list.Add –> adds elements to end of list
Remove –> removes first occurence of specified object from list
Insert –> inserts elements into list at specified index
Clear–> removes all elements
ToArray –> copies elements of list to a new array
list.Count –> gives length of list
What are math methods
static methods used to compute mathematical calculations
ex.
Math.Cos()
Math.Sqrt()
Math.Abs()
+ , -, / , *,
uses System namespace
What is overflow?
when data inputted / calculated is larger/ smaller than the range (out of bounds of type range)
What is BIgInteger?
struct used to represent an arbitrarily large signed integer
** creates immutable objects**
using System.Numerics
can be instantiated:
1. using constructors:
BigInteger num - new BigInteger (123)
2. as a numeric type (for integrals)
What happens when this code is compiled:
int x = 2_000_000;
BigInteger y = x * 2;
Overflow occurs because x and 2 are integers.
When multiplied they create a value of type int that exceeds the range
to fix:
BigInteger.Multiply(x, 2)
or
cast x and 2 to be Big Integers
What is static checking
ex.
int x = 13;
if (x == false) { … }
bug found automatically before program even runs
Can detect:
syntax errors (missing paranthesis)
wrong names (Math.sine(2)
wrong numer of arguments (Mat.Sin(2,3))
Wrong argument types
Wrong return types
What is dynamic checking
ex.
int x = 12, y = 0;
Console.WriteLine(x/y)
bug found automatically during runtime
- sometimes compiler must run in order for language to see what variables stand for etc.
Can detect:
- illegal argument values (x/y when y = 0)
- unrepresented return values (when the value of return is not related to its type)
- out-of-range indexes (run time index being too-large)
- calling a method on a null object reference
What happens in “no checking”
language doesn’t help with finding error
What is readonly and how is it used for defensive programming?
keyword used to indicated that a field can only be assigned at the time of declaration / in a constructor of that class.
What does the const keyword do?
declares a constant local / field
- can only be initialized at declaration (immutable afterwards)
What happens when this code is compiled?
class Age
{
readonly int year;
public Age (int year)
{
this.year = year;
}
void ChangeYear()
{
year = 1967;
}
}
Compiler error
–> year has been initialized due to the keyword “readonly” which only lets the data to be assigned in a constructor
Function of “this” keyword
used to quality class members that have the same name as the parameters
int Quadratic Equation(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
What is a Queue and what are its operations?
FIFO structure
enqueue –> add item at end
dequeue –> remove item at front
What do the paranthesis when instantiating a data abstraction do?
Queue<string> numbers = new Queue<string>();</string></string>
used for constructors
–> creates an empty data abstraction
What are operations for Stack data abstraction?
push –> add elements to top of stack
pop –> remove elements at top of stack
What is a Dictionary <Tkey, Tvalue>
data type that holds a colletion of “key-value” pairs (key, value)
ex. collection of all (country, capital city) pairs
What is a set?
datatype in which we can add / remove elements + check if an element is in a set
–> unordered + do not contain duplicates
what is .NET
takes source code file and produces intermediate output file (assembly)
- code in assembly is an intermediate language