MT1 Flashcards

1
Q

Purpose of top-level statements?

A

write smaller programs without the use of the main method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to write top-level statements

A
  1. “using” directives (optional)
    ex. using System; using System.Collections.Generic;
    (compiler automatically adds set of “using” directives” based on project type)
  2. Statements (what we would write in main method)
    ex. Console.Writeline…
  3. More type / namespace declarations (optional)
    - used when we have user-defined classes etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What happens to a top-level statement?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What makes software high quality?

A
  1. correct (safe from bugs)
  2. comprehensible (readable, understandable)
  3. changeable (ready to change)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a type

A

set of values (data) and the operations permitted on the values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Two naming conventions

A

PascalCasing –> class + method
camelCasing –> local variables + fields

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a constructor?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does “private” mean

A

protects types from being manipulated outside of the class it is in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Value vs Reference type

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain the concept of Stack and Heap

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a class?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a Class: Field?

A

stores data
a variable (of any type) that is a member of a class

PascalCasing for static public + protected fields
otherwise camelCasing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a Class: property?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a method?

A

Block of code that can be executed / invoked
( do not use void to indicate that method doesn’t take parameters; simply use () )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are local variables

A

holds temporary data within scope of a method (doesn’t exist outside of a method)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Difference between Private and Public Access Modifiers

A

Private members –> accessible within class in which they are declared (default access level)

Public members –> accessible to other objects in program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What does the following code do?

Console.WriteLine(message + “ “ + name)

A

Appends the data for message, appends a space, and appends the data for name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What happens to the string if a code below is compiled:

Console.WriteLine($”{message1.ToUpper()}”);

A

prints in ALL CAPS

BUT
b/c strings are immutable, a new string data is created that has the same string but modified.

19
Q

What is String Builder

A

provides a mutable alternative to string
(uses namespace: System.Text)

ToString –> convert to string
Append –> adds value
Clear , Remove, Insert etc.

20
Q

What is encapsulation?

A

information hiding
ex. declaring fields to be private

advantage: hiding details allows them to be changed later without changing the entire system

21
Q

What is Inheritance?

A

types can be derived from other types (types can be subtypes)
ex. all types derived from System.Object type

22
Q

Polymorphism

A

ability of object to take on many forms
ex. (supertype can refer to subtype object)

23
Q

What is method overloading?

A

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
}

24
Q

What is an array?

A

set of uniform data elements represented by a single name
- size cannot be changed

int[] array = {1, 2, 3} ;
char [] array = “string”

25
Q

What are lists?

A

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>

26
Q

Methods of List <T> class</T>

A

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

27
Q

What are math methods

A

static methods used to compute mathematical calculations
ex.
Math.Cos()
Math.Sqrt()
Math.Abs()
+ , -, / , *,

uses System namespace

28
Q

What is overflow?

A

when data inputted / calculated is larger/ smaller than the range (out of bounds of type range)

29
Q

What is BIgInteger?

A

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)

30
Q

What happens when this code is compiled:

int x = 2_000_000;
BigInteger y = x * 2;

A

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

31
Q

What is static checking
ex.
int x = 13;
if (x == false) { … }

A

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

32
Q

What is dynamic checking
ex.
int x = 12, y = 0;
Console.WriteLine(x/y)

A

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

33
Q

What happens in “no checking”

A

language doesn’t help with finding error

34
Q

What is readonly and how is it used for defensive programming?

A

keyword used to indicated that a field can only be assigned at the time of declaration / in a constructor of that class.

35
Q

What does the const keyword do?

A

declares a constant local / field

  • can only be initialized at declaration (immutable afterwards)
36
Q

What happens when this code is compiled?

class Age
{
readonly int year;
public Age (int year)
{
this.year = year;
}
void ChangeYear()
{
year = 1967;
}
}

A

Compiler error
–> year has been initialized due to the keyword “readonly” which only lets the data to be assigned in a constructor

37
Q

Function of “this” keyword

A

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;
}

38
Q

What is a Queue and what are its operations?

A

FIFO structure

enqueue –> add item at end
dequeue –> remove item at front

39
Q

What do the paranthesis when instantiating a data abstraction do?

Queue<string> numbers = new Queue<string>();</string></string>

A

used for constructors
–> creates an empty data abstraction

40
Q

What are operations for Stack data abstraction?

A

push –> add elements to top of stack
pop –> remove elements at top of stack

41
Q

What is a Dictionary <Tkey, Tvalue>

A

data type that holds a colletion of “key-value” pairs (key, value)
ex. collection of all (country, capital city) pairs

42
Q

What is a set?

A

datatype in which we can add / remove elements + check if an element is in a set

–> unordered + do not contain duplicates

43
Q

what is .NET

A

takes source code file and produces intermediate output file (assembly)

  • code in assembly is an intermediate language