Chapter 6 : Arrary Flashcards

1
Q

List out 2 categories that data types in C#

A
  1. Values Types ( x = “Hello” )
  2. Reference Types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What actually holds the data in a value type ?

A
  1. Value Type of Variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What will compiler allocates when I declare a value type variable?

A
  1. The compiller will allocate a chunk of memory that is big enough for the variable
  • When I’m working with a value type, I’m actually using a variable that holds a piece of data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

List out 2 things when I work with a reference type

A
  1. An object that is created in memory
  2. A variable that references the project
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does array stores?

A
  1. A group of the same data type together in memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What data isn’t ideal for array?

A
  1. Storing and Processing lists of data
  • Each variable can only store 1 data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What type of data type is array? ( Value Type / References Type )

A
  1. Reference Type Objects
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What do I need to do to create an array?

A
  1. Declare a reference type object
  2. Create the obkect and associate it with the reference variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the format for array?

A
  1. dataType[] arraryName;

int[] numbersArray

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

What is the format for adding new values for array?

A
  1. arrayName = new DataType[ArrarySize]

numbersArrary = new int[6]

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

What is the storage location known as in array?

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

What is noun for the unique number assigned at each element inside an array?

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

What is the subscript value for the first elements?

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

How to create a array with assigning the maximum number of elements in it?

A

const int size = 5;
int numbers = new int[size];

numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;

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

How to get the third element of array?

A
  1. number[2]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

List out 3 ways to initialize array

A
  1. int[] num = { 10 , 20 , 30 }
  2. int[] num = new int { 10, 20, 30 }
  3. const int size = 3
    int[] num = new int[size] {10,20,30};
17
Q

How to loop through arrary with adding value 99 into the array 3 times?

A

const int size = 3;
int[] myValues = new int[size];
for ( int i = 0; i < size; i++ )
{
myValues[i] = 99;
}

  • Please make sure that this code i < size musn’t be i <= size since the array value is only 0 , 1 , 2 . 3 will return an exception
18
Q

What is the length property does?

A
  1. Set the number of elements in the array
19
Q

How to set 25 elements in the array? How to output the length of an array?

A
  1. double[] temp = new double[25]
  2. MessageBox.Show(temperatures.Length.ToString());
20
Q

How to use the length property to loop through every single item in an array?

A

for ( int i = 0; i < tem.Length; i ++ )
{
MessageBox.Show(tem[i].ToString());
}

21
Q

What is the foreach loop used for?

A
  1. Special loop used to simplify array processing
  2. It is designed to work a temporary, read-only variable known as iteration variable
22
Q

What is the format for foreach loop?

A

foreach ( Type VariableName in ArraryName )
{
statement(s);
}

  • Type = data type of array
    VariableName = name of temporary iteration variable
23
Q

How to use foreach loop through the arrary
int[] numbers = { 3 , 6 , 9 }?

A

int[] numbers = { 3 , 6 , 9 }
foreach ( int val in numbers )
{
MessageBox.Show(val.ToString());
}

24
Q

How to use a method that passed in array as argument and show the content inside it?

A

string[] people = { “Bill” , “Jill”}
showArrary(people);

private void showArray(string[] strArray)
{
foreach ( string str in strArrary )
{
MessageBox.Show(str);
}
}

25
Q

How to create a program that searches value 30 inside the array and if the value is found, its positionis returned; otherwise -1 is returned

A

int[] sArray = new int[] { 10, 20, 30, 40, 50};
bool found = false;
int index = 0;
int position = -1;
while(!found && index < sArray.Length)
{
if ( sArray[index] == 30 )
{
found = true;
position = index;
MessageBox.Show(position.ToString());
}

 index++; }
26
Q

How to use for and foreach loop to create a second array and copy the individual element of the source array to the target array?

A

for ( int i = 0 ; i < firstArray.Length; i++ )
{
secondArray[i] = firstArrary[i];
}

int[] firstArray = { 3, 6, 9 };
int[] secondArray = new int[firstArray.Length];
int index = 0;
foreach ( var num in firstArray )
{
secondArray[index] = num
index ++;
}

27
Q

How to compare array with length and elements?

A

if (firstArray.Length != secondArray.Length) { return false; }

if (firstArray[index] != secondArray[index]) { return false ; }

28
Q

How to total up the value of an array?

A

for (int i = 0; index < units.length; index++ )
{
total += units[index];
}

29
Q

How to find out the highes value in an array?

A

int[] numbers = { 8 , 1 , 12 , 6 , 2 }
int highest = numbers[0];
for (int i = 1; i < numbers.Length; i ++ )
{
if (numbers[index] > highest)
{
highest = numbers[index];
}
}

30
Q

What is the difference between list and array?

A
  1. List can be resized dynamically but arrays can’t
  2. Proivides the methods to add, search, sort and manipulate lists
31
Q

What generic class is list under?

A
  1. System.Collections.Generic
31
Q

How to add 26, 15,12 into list numList?

A

numList.Add(26);
numList.Add(15);
numList.Add(12);

32
Q

How to insert number 53 into the second element?

A

numList.Insert(2,53);

32
Q

How to insert a list into another list?

A

int[] num = {11,22,33}
numList.InsertRange(4,num);

32
Q

How to remove an element 11 from a list?

A

numList.Remove(11);

32
Q

How to remove the element in a index of 4 in list?

A

numList.RemoveAt(4);

33
Q

List out some other methods in list ( 5 )

A
  1. Clear() - Remove all items in list
  2. Find() - Search item in list
  3. Sort() - Sort list in ascending order
  4. Reverse - Reverse items in list
  5. Count - Returns the size of list