C# Basics / File I/O / Console App Flashcards

1
Q

How would you display a line of text in the console?

A

Console.Write() - to continue on same line Console.WriteLine() - to start new line after displayed

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

What is string interpolation and how is it used?

A

String interpolation enables you to insert values in string literals to create formatted strings.

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

How would you read a sting literally or verbatim?

A

@”C:Windows\System32”

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

What is composite formatting of parameters?

A

{Index [, alignment][:formatString]

Console.WriteLine(“{0,-20} {1,5}\n”, “Name”, “Hours”);

// The example displays the following output:

// Name Hours

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

What is a list?

A

A list is similar to an array but provides additional functionality, such as dynamic resizing.

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

Define: Index

A

Index is a number starting from 0 that identifies a corresponding item in the list of objects.

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

Define: Alignment

A

Alignmet is a signed integer indicating the preferred formatted field width

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

Define: Format string

A

Format String is a format string that is appropriate for the type of object being formatted

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

What datatypes does C# read from the keyboard?

A

ONLY STRINGS, unline java, so the need for parsing is often used

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

How would you parse a readline to an integer?

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

What is the other way to convert a sting into an integer?

A

int myAge = Convert.ToInt32(input)

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

What is the naming convention for methods inside of a class in C#?

A

Pascal not camel case!

eg. SetName

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

How would you manually create a property to get and set the instance variable?

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

What would a Class diagram look like with a property?

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

How would you manually add validation to a property set method?

A

Private decimal balance: // instance variabl

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

How would you create an auto-implemented property?

A

public string Name { get; set; }

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

What is the format specifier for currency and how is it used?

A

Format Specifier :C

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

What are the main format specifiers?

A
19
Q

What are the default values for the simple data types in C#?

A

Types char, byte, sbyte, short, ushort, int, uint, long, ulong, float, double, and decimal are all given the value 0 by default.

Type bool are given a value of false by default.

Reference-type instance variables are of type null by default.

20
Q

What datatype do you use for monetary amounts?

A

decimal

21
Q

What are the conversion types of datatypes?

A
22
Q

What is an optional paramater and how is it used?

A

Methods can have optional parameters that allow the calling method to vary the number of arguments to pass. An optional parameter specifies a default value that’s assigned to the parameter if the optional argument is omitted. You can create methods with one or more optional parameters. All optional parameters must be placed to the right of the method’s non-optional parameters.

eg. static int Power(int baseValue, int exponentValue = 2);

23
Q

What are names parameters and how are they used?

A

C# provides a feature called named parameters, which enable you to call methods that receive optional parameters by providing only the optional arguments you wish to specify.

Explicitly specify the parameter’s name and value—separated by a colon (:)—in the argument list of the method call.

For example:

// sets the time to 12:00:22

t.SetTime( hour: 12, second: 22 );

The compiler assigns parameter hour the argument 12 and parameter second the argument 22. The parameter minute is not specified, so the compiler assigns it the default value 0.

It’s also possible to specify the arguments out of order when using named parameters. The arguments for the required parameters must always be supplied.

24
Q

What is the new syntax for methods that just return a value?

A

static int Cube(int x) => x * x * x;

(lambda expressions)

used to be:

static int Cube(int x)

{

return x * x * x;

}

25
Q

How would you use a lambda expression (bodied method) to return true or false?

A

public bool IsNoFaultState => State == “MA” || State == “NJ” || State == “NY” || State == “PA”;

26
Q

What are the out and ref keywords used for?

A

Normally functions are passed and return “Values” Meaning that the value passed on if changed does not effect the original variable/number. OUT and REF help pass variables by reference, so if they are passed into another method and changed, the original is changed too becuase its referencing back to the original object.

27
Q

How are the ref and out keywords used?

A

The out can only send reference out of a method, not pull it in. Ref does both.

eg. out: send objVal 20 in, add objVal 10, send out objVal = 10;
ref: send objVal 20 in, add objVal 10, send out objVal = 20;

28
Q

what does the code for ref keyword look like?

A
29
Q

what does the code for out keyword look like?

A
30
Q

How do you create a named constant?

A

const int ArrayLength = 5;

31
Q

What is a foreach stament and how does it work?

A

The foreach statement iterates through the elements of an entire array or collection.

32
Q

How would you create a two dimentional rectangular array?

A

A two-by-two rectangular array b can be declared and initialized with nested array initializers as follows:

int [,] b = {{1, 2}, {3, 4}};

The initializer values are grouped by row in braces. The compiler will generate an error if the number of initializers in each row is not the same, because every row of a rectangular array must have the same number of column

33
Q

How would you create a two dimentional jagged array?

A

A jagged array with three rows of different lengths could be declared and initialized as follows:

int [][] jagged = {

new int [] {1, 2},

new int [] {3},

new int[] {4, 5, 6}

};

34
Q

How would you create a variable-length argument list?

A

Variable-length argument lists allow you to create methods that receive an arbitrary number of arguments. The necessary params modifier can occur only in the parameter list’s last parameter.

35
Q

What library is used for the main I/O funcionality?

A

using System.IO;

36
Q

How do you create a new filestream?

A

FileStream file = new FileStream(“path.txt”, FileMode , FileAccess );

FileMode specifies what the FileStream should do with the file to begin with

The FileMode.option can be set to one of: Append, Create, CreateNew, Open, OpenOrCreate, or Truncate

FileAccess specifies the file privileges. The FileAccess.option can be set to one of: Read, ReadWrite, or Write

37
Q

How do you write to a file?

A

StreamWriter data = new StreamWriter(file);

Two options for writing to a file:

Write, or WriteLine data.Write(“Hello file system world!”);

Remember to close the stream when done data.Close();

38
Q

How do you read from a file?

A

StreamReader data = new StreamReader(file);

Four options for reading from a file:

Read, ReadBlock, ReadLine, or ReadToEnd

string line = data.ReadToEnd();

Remember to close the stream data.Close();

When done with the file make sure to close that as well file.Close();

39
Q

How do you check for the end of a file?

A
40
Q

How do you view file information?

A
41
Q

How do you list Disk Drives?

A
42
Q

How do you list Subfolders?

A
43
Q

How do you list files?

A