Chapter 1 Flashcards Preview

C# > Chapter 1 > Flashcards

Flashcards in Chapter 1 Deck (65)
Loading flashcards...
0
Q

What is an algorithm?

A

Algorithm is a set of ordered and finite steps to resolve a given problem.

1
Q

What is a program?

A

A program is a set of precise instructions to complete a task.

2
Q

What is a flowchart?

A

A flowchart is a graphical representation of an algorithm.

3
Q

What is a decision table?

A

Decision tables are used when an algorithm involves a large number of conditions. They are a more compact and readable format for presenting algorithms.

4
Q

What is C#?

A

C# is a high level language that allows you to write computer programs in a human readable format. It is part of the .NET framework and benefits from the runtime support and class libraries provided by the framework.

5
Q

What are the 3 main components of the .NET framework?

A

1) Runtime execution environment
2) Set of class libraries ­ reusable
3) Language compilers for C#, Visual Basic and C++

6
Q

What do modern high level languages e.g. C# do?

A

They allow us to write precise human readable instructions that a language compiler can translate into lower level language that can be understood by the runtime execution system.

7
Q

What happens when a C# program starts?

A

Visual studio calls the .NET compiler, which translates it to lower level language, Common Intermediate Language. This is stored in an .exe file also known as an assembly.

Before this code can be executed it must be translated by the .NET framework runtime execution system in a process called just in time compilation.

8
Q

What is CIL?

A

Common Intermediate Language is the lowest level human readable programming language. It is an object-orientated assembly language.

Before this code can be executed it must be translated by the .NET framework runtime execution system in a process called just in time compilation.

9
Q

What is a class?

A

A class is a set of data and methods, which is defined using the class keyword and class name.

10
Q

What is a namespace?

A

Namespaces are used to organise classes and uniquely identify them.

Namespace and class name combine to create a fully qualified name e.g. namespace.class.

.NET framework provides a large number of useful classes such as the System namespace which contains Console class.

11
Q

What is a fully qualified name?

A

A fully qualified name is the complete path of an identifier e.g. System.console.

12
Q

What is the main method?

A

The main method serves as the entry point to a program.

Always starts when a program starts.

Can have only 1 main method in a program.

The main method must always declared as static.

13
Q

What is a static method?

A

A static method allows you to call the method without having to create an instance of it.

14
Q

What is instantiation?

A

Instantiation is the creation of an object in object-oriented programming. An instantiated object is given a name and created in memory or on disk using the structure described within a class declaration. Referred to as creating an instance of an object.

15
Q

What is a variable?

A

Variables provide temporary storage during the execution of a program. They must have a name and a data type.

Must be unique within the scope that they are declared in.

Name must begin with a letter or underscore and can only contain letters, numbers or underscores.

16
Q

What is a constant?

A

Constants are fields or local variables whose value cannot be changed.

Declared using the const keyword.

17
Q

What is a data type?

A

A data type defines the size in memory needed to store the data and the kind of operation that can be performed on the data.

Can create own data types using class or a struct.

18
Q

What data types are 1 byte in size?

A

Byte

19
Q

What data types are 2 bytes in size?

A

Char
Short
Bool

20
Q

What data types are 4 bytes in size?

A

Int

Float

21
Q

What data types are 8 bytes in size?

A

Long

Double

22
Q

What are primitive data types?

A

Built-in data types provided by C# that can be used in programs e.g. int, float, bool, char etc.

23
Q

What is the string data type?

A

The string data type is a reference type as opposed to value types such as int, bool etc.

24
Q

What is an array?

A

An array is a collection of items, which each have a unique index.

It is zero based i.e. index 0 refers to the first item in the array.

25
Q

What is an operator?

A

Operators specify which operation to perform on operands before returning a result.

Can be either:
Unary - work with 1 operand each e.g. ++i
Binary - takes 2 operands e.g. x + y
Ternary - takes 3 operands e.g. ?:

26
Q

What are the values of x and y in example below?

int x = 12;
int y = x++;

A
x = 13
y = 12

The value of y is assigned before the increment takes place.

27
Q

What are the values of x and y in example below?

int x = 12;
int y = ++x;

A
x = 13
y = 13

The value of y is assigned after the increment takes place.

28
Q

What does the && operator represent?

A

And

29
Q

What does the || operator represent?

A

Or

30
Q

What does the ! operator represent?

A

Not

31
Q

What is a method?

A

A method is a block of code containing a series of statements.

Can receive input via arguments and can return a value to the caller.

32
Q

What is a method that uses the void keyword?

A

A method that uses the void keyword doesn’t return a value e.g. main method.

33
Q

What are decision structures?

A

Decision structures provide decision making structure to a program. This allows us to branch off into different sections of code depending on a boolean value e.g. If statement, if else statement and switch statement.

34
Q

What is an if statement?

A

It is a decision structure that only executes if the boolean expression evaluates to true.

If (booleanExpression)
{
statement;
}

35
Q

What is an if-else statement?

A

The if-else statement is a decision structure that allows a program to perform one action if the boolean expression evaluates to true and another if false.

If (booleanExpression)
{
Statement1;
}
Else
{
Statement2;
}
36
Q

What is a switch statement?

A

The switch statement is a decision structure that allows multi-way branching. Can be useful to use to simplify a complex if-else statement.

Always need a default code block if controlling case doesn’t match any of the cases.

Limited in that case labels must be constants e.g. + or 42, cannot calculate at runtime. Useful when labels are constant e.g. day of week.

Switch(opr)
{
case '+':
result = op1 + op2;
break;

case ‘-‘:
result = op1 - op2;
break;

case ‘*’:
result = op1 * op2;
break;

default:
console.WriteLine(“Unknown operator”);
break;

}

37
Q

What is a repetition structure?

A

Allow programs to perform repetitive tasks.

While loop
Do-while loop
For loop
Foreach loop

38
Q

How do you terminate a loop?

A

A loop can be terminated and control transferred elsewhere by using break, goto, return or throw.

39
Q

What is a while loop?

A

Repeatedly executes a block of code until a boolean expression evaluates to false.

While (boolean test)
{
Statement
}

40
Q

What is a Do-While loop?

A

Executes a block of statements until a Boolean expression equals false, similar to while loop except evaluates at bottom of loop meaning statement is executed at least once.

do
{
Statement;
}
while (boolean test);
41
Q

What is a for loop?

A

Useful for creating iterations that must execute a specified number of times.

Similar to while loop in that it allows statement to be executed repeatedly until it evaluates to false.

for (i=1, i + i);
}

42
Q

What is a foreach loop?

A

This is used to iterate through the elements of a collection. It can be seen as an enhanced for loop as it can iterate through arrays and lists.

Foreach(ElementType element in Collection) e.g. (int i in numbers)
{
Statement;
}

43
Q

What is recursion?

A

Recursion causes a method to call itself in order to compute a result. Recursive algorithm calls itself over and over in order to solve a problem.

Easy to identify as method name will appear in the method body. Has a recursive case and base case.

Base case is the condition that will cause the recursion to terminate, without this can cause infinite loop and stackoverflowexception error.

Recursive case moves the algorithm toward the base case.

44
Q

What is an exception?

A

An exception is an error condition that occurs during the execution of a C# program.

Runtime will create an object for exception and ‘throw’ it, you can ‘catch’ this by writing exception handling code.

45
Q

What is used in C# to handle exceptions?

A

Try-Catch-Finally

Try block is used to surround statement that could cause error.

Catch block is used to handle any error (specific or generic) that is generated.

Finally block is always run regardless if an error has been thrown, as long as it enters try block.

46
Q

The ________ statement selects for execution a statement list having an associated label that corresponds to the value of an expression.

A

Switch

47
Q

The________ loop tests the condition at the bottom of the loop instead of at the top.

A

Do-while

48
Q

On a 32-bit computer, a variable of int data type takes _____ bytes of memory.

A

2 bytes

49
Q

________ is a programming technique that causes a method to call itself in order to compute a result.

A

Recursion

50
Q

________ are data fields or local variables whose value cannot be modified.

A

Constants

51
Q

When an algorithm involves a large number of conditions, a(n) __________ is a compact and readable format for presenting the algorithm.

A

Decision table

52
Q

A(n) ________ is a graphical representation of an algorithm.

A

Flowchart

53
Q

Review the following code snippet:
int n = 20;
int d = n++ + 5;

What will be the value of d after this code snippet is executed?

a. 25
b. 26
c. 27
d. 28

A

a. 25

Value of d assigned before the increment takes place.

54
Q
Review the following code snippet:
private static void WhileTest()
{
    int i = 1;
    while (i
A

c. 4

55
Q

Review the following code snippet:

int number1 = 10;
int number2 = 20;

if (number2 > number1)
Console.WriteLine(“number1”);
Console.WriteLine(“number2”);

What output will be displayed after this code snippet is executed?
a.	
number1
b.	
number2
c.	
number1
number2
d.	
number2
number1
A

c.
number1
number2

56
Q

In a switch statement, if none of the case statements match the switch expression, then control is transferred to which statement?

a. break
b. continue
c. default
d. return

A

c. default

57
Q

You need to write code that closes a connection to a database and you need to make sure this code is always executed regardless of whether an exception is thrown. Where should you write this code?

a. Within a try block
b. Within a catch block
c. Within a finally block
d. Within the Main method

A

c. Within a finally block

58
Q

You need to store values ranging from 0 to 255. You also need to make sure that your program minimizes memory use. Which data type should you use to store these values?

a. byte
b. char
c. short
d. int

A

a. byte

59
Q

If you don’t have a base case in your recursive algorithm, you create an infinite recursion. An infinite recursion will cause your program to throw an exception. Which exception will your program throw in such a case?

a. OutOfMemoryException
b. StackOverflowException
c. DivideByZeroException
d. InvalidOperationException

A

b. StackOverflowException

60
Q

You are learning how to develop repetitive algorithms in C#. You write the following method:

private static void ForTest()
{
for(int i = 1; i

A

d. Infinite repetitions

There is no increment so i will always = 1 and therefore always

61
Q

Which of the following C# features should you use to organize code and create globally unique types?

a. Assembly
b. Namespace
c. Class
d. Data type

A

b. Namespace

62
Q

You write the following code snippet:
int[] numbers = {1, 2, 3, 4};
int val = numbers[1];

You also create a variable of the RectangleHandler type like this:

RectangleHandler handler;

What is the value of the variable val after this code snippet is executed?

a. 1
b. 2
c. 3
d. 4

A

b. 2

Arrays are zero based when referencing arrays item index will be in square brackets.

63
Q

What size are each of the data types (32 bit)?

A

byte 0 to 255
char U+0000 to U+ffff (unicode characters)
short -32,768 to 32,767 (5 digits)
int -2,147,483,648 to 2,147,483,647 (10 digits)
long (19 digits + or -)
float decimal figure with 7 decimal digits
double decimal figure with double the decimal digits of float
Bool true or false

64
Q

When is the finally block executed?

A

The finally block is executed when code leaves any part of the try statement, except when a stackoverflowexception is thrown.