Core Programming Flashcards
int
4 bytes / 32 bits
can store whole numbers only
from -2,147,483,648 to 2,147,483,647.
It is the preferred data type when we create variables with a numeric value.
long
8 bytes / 64 bits
can only store whole numbers
from
-9(Quintillion),223(Quadrillion),372(Trillion),036(Billion),854,775,808 to 9223372036854775807.
This is used when int is not large enough to store the value. Note that you should end the value with an “L”:
float
signed 4 bytes / 32 bits
Stores fractional numbers.
Sufficient for storing 6 to 7 decimal digits
The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an “F”:
double
signed 8 bytes / 64 bits
Stores fractional numbers.
Sufficient for storing 15 decimal digits
bool
1 bit Stores true or false values
char
2 bytes / 16 bits
Stores a single character/letter, surrounded by single quotes
string
2 bytes / 16 bits per character Stores a sequence of characters, surrounded by double quotes
decimal
signed 16 bytes / 128 bits
Stores a fractional number
up to 28-29 digits
Should end in M - decimal price = 1.50M;
To minimize the amount of storage used on the hard drive by an application that generates many small files, you should make the ______ as small as possible.
a - partition
b - file allocation table
c - block size
d - folder and file names
block size
byte
unsigned 8 BIT / 1 byte
0 to 255
Define Integer type (Integral)
Numbers that are whole numbers without decimal points. It can be negative or positive numbers.
Define Floating-point type
Floating-point type is numbers with one or more decimal points. It can be negative or positive numbers.
sbyte
signed 8 BIT / 1 byte
-128 to 127
short
It occupies 2 bytes / 16-bit memory.
The short data type is a signed integer that can store numbers from -32,768 to 32,767.
Signed vs unsigned values
signed means specified as positive or negative, unsigned means positive by default
Converting a value type to a reference type in an object is called BOXING.
Select the correct answer if the text in italics does not make the statement correct.
a - no change
b - unboxing
c - upcasting
d - downcasting
A - No change
changing a value type to a reference type = boxing
Value Type
A variable of a value type contains an instance of the type.
It is stored in the stack with both its name and value in the same entry.
Reference Type
contains a reference to an instance of the type (in heap memory)
An example is a string. The NAME of the variable is stored in the stack, which points to its memory address in the heap.
Upcasting
Upcasting is an operation that creates a base class reference from a subclass reference. (subclass -> superclass)
(i.e. Manager -> Employee)
Example:
Employee emp = (Employee)mgr; //mgr is Manager
Downcasting
Downcasting is an operation that creates a subclass reference from a base class reference. (superclass -> subclass) (i.e. Employee -> Manager)
Example:
if (employee is Manager)
{
Manager m = (Manager)employee;
}
GUID
Represents a globally unique identifier (GUID). A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.
for loop
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop.
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
Statement 1 sets a variable before the loop starts (int i = 0).
Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true, the loop will start over again, if it is false, the loop will end.
Statement 3 increases a value (i++) each time the code block in the loop has been executed.
foreach loop
used exclusively to loop through elements in an array:
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; foreach (string i in cars) { Console.WriteLine(i); }
while loop
The while loop loops through a block of code as long as a specified condition is True
int i = 0; while (i < 5) { Console.WriteLine(i); i++; }
The do/while loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
int i = 0; do { Console.WriteLine(i); i++; } while (i < 5);
Which statements can you use to exit a while, do, for or foreach loop?
- break
- continue
- goto
- throw
- reurn
- break
- goto
- throw
- reurn
Break statement:
At any point within the while, do, for or foreach statement blocks, you can break out of the loop by using the break statement.
Continue statement:
You can step directly to the evaluation of the while, do, for or foreach expression by using the continue statement.
If the expression evaluates to true, execution continues at the first statement in the loop. Otherwise, execution continues at the first statement after the loop.
The goto statement
The goto statement transfers the program control directly to a labeled statement.
A common use of goto is to transfer control to a specific switch-case label or the default label in a switch statement.
The goto statement is also useful to get out of deeply nested loops.
class SwitchTest { static void Main() { Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large"); Console.Write("Please enter your selection: "); string s = Console.ReadLine(); int n = int.Parse(s); int cost = 0; switch (n) { case 1: cost += 25; break; case 2: cost += 25; goto case 1; case 3: cost += 50; goto case 1; default: Console.WriteLine("Invalid selection."); break; } if (cost != 0) { Console.WriteLine($"Please insert {cost} cents."); } Console.WriteLine("Thank you for your business.");
// Keep the console open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } }
The return statement
The return statement terminates execution of the method in which it appears and returns control to the calling method. It can also return an optional value. If the method is a void type, the return statement can be omitted.
If the return statement is inside a try block, the finally block, if one exists, will be executed before control returns to the calling method.
class ReturnTest { static double CalculateArea(int r) { double area = r * r * Math.PI; return area; } ...
The Throw Statement
Signals the occurrence of an exception during program execution.
throw [e];
What is the purpose of a finally block in exception handling? Select all that apply.
- execute clean up code and release resources.
- execute code only when an exception is thrown
- Code in a finally block is executed even if an exception is not thrown
- to conclude the execution of an application
- to break out of the error handler
- execute clean up code and release resources.
- Code in a finally block is executed even if an exception is not thrown
What is the purpose of the throw keyword? Select all that apply:
- To move error handling to a separate thread
- To stop the processing of code
- To raise exceptions
- To re-throw exceptions as a different type
- to raise exceptions
- to re-throw exceptions as a different type