Fundamentals of programming Flashcards

1
Q

What is the && operator?

A

Called Logical AND operator. If both the operands are non zero then condition becomes true.

e.g. (A && B) is false.

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

What is the || operator?

A

Called Logical OR Operator. If any of the two operands is non zero then condition becomes true.

e.g. (A || B) is true.

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

What is the ! operator?

A

Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

!(A && B) is true.

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

What is the difference between the For, While and Do While loop? When should you use each?

A

While Loop:
- Used when you know you need to loop
- But don’t know how many times
- Remember checks condition first
- So may never run if condition not met at the start
- Need additional variables to control or count

For Loop:
- Use when you know how many times to run
- Or you can work out how many times
- Good for traversing a table or structure
- The variable declared can be used in the code

Do While
- Used when you don’t know how many times
- But you do know it is at least once
- Need additional variables to control or count

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

How do you change between Upper and Lower case?

A

string word = “crystal”;
Console.WriteLine(word.ToUpper());
Console.WriteLine(word.ToLower());
output=
CRYSTAL
crystal

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

How do you create a substring from a string?

A

string word = “crystal”;

Console.WriteLine(word.Substring(3, 3));
Console.WriteLine(word.Substring(1, 4));
//first number is index, second is length

output=
STA
RYST

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

How do you find the index of something within a string? How do you check if a string is contained within another?

A

string word = “crystal”;

Console.WriteLine(word.IndexOf(“sta”));
Console.WriteLine(word.Contains(“cry”));

output=
3
TRUE

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

How do you insert one string into another?

A

word.Insert(index, “”)

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

What are the Math operations?

A

Math.Abs - returns the absolute value
Math.Truncate - cut off any fractional part e.g. 14.56 -> 14
Math.Round - round to nearest whole number
Math.Floor - round down
Math.Pow - raise a number to the power of
Math.Sqrt - returns the squre root

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

What is a function?

A

A subroutine that returns a value

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

How does passing parameters into a function work?

A
  • Subroutines can accept variables
    1. Data types must be specified within brackets, 2. The same order of specifying variables must be used when calling the function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does reference (ref) do for a value in a subroutine?

A

Any changes made to the value within the subroutine are retained, without this, they are not.

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

What is one benefit of using reference?

A

It removes the need for global variables

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

What is an exception?

A

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions

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

How does try, catch work?

A
  • If an exception occurs, instead of erroring, the code at risk of causing an error can be wrapped in a ‘try{}’, if it does cause an exception the ‘catch{}’ block will be run.
  • If no exception occurs, the catch block is ignored
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a base case?

A
  • Any recursive subroutine must meet have a stopping
    condition (called a base case) which must be met at some
    point in the execution of the program.
17
Q

What is a Local Variable?

A
  • A local variable is a variable that can only be accessed from the subroutine within which it is declared.
  • They only exist in the computer’s memory when their parent subroutine is
    executing.
  • More memory efficient way of storing data than using global variables,
18
Q

What is a Global Variable?

A
  • Can be accessed from any part of a program
  • Exists in memory for the entire duration of the program’s execution.
  • Local variables can be given the same identifier name as global variables -
    generally considered bad practice.
  • When the local variable’s value is changed, the global
    variable’s value remains the same.
19
Q

What is Object Oriented Programming?

A

A programming paradigm based on objects:
Object: An instance of a class
Class: Blueprint/template for an object

20
Q

What does an object contain?

A
  • Attributes (properties) e.g. size, shape, colour
  • Methods (actions) e.g. changeSize(), changeShape(), changeColour()
21
Q

What is an object defined as?

A

An instance of a class, contains both attributes and methods

22
Q

What is a class defined as?

A

A template for creation of objects, an object is built from a class and you can have many instances of the same class

23
Q

What is a Method defined as?

A
  • A program routine within an object
  • Carries out a particular task on an object
  • Can be inherited from a superclass (Parent)
24
Q

What is procedural programming?

A

Where programs are formed from sequences of instructions which are executed in the order in which they appear.

25
Q

What are some problems with procedural programming?

list 2

A

Data and procedures that operate on said data are kept separate, therefore:

  • Relationships between data item and code operating on it may not always seem obvious
  • Errors would be difficult to find
26
Q

What is inheritance defined as?

A

Inheritance is where a child class takes on attributes and methods from a parent class, it is efficient as code can be re used and organised

27
Q

What is encapsulation defined as?

A

The process of packaging an objects data together with its new methods. It is a hidden technical implementation within the object. They cannot be directly other objects
Uses public (+), private(-) and protected(#) access modifiers

28
Q

Benefits of encapsulation?

A
  • Prevents users from gaining access to certain data
  • Any modifications to the internal code will not cause problems elsewhere, object can be maintained independently
29
Q

What is Polymorphism? What can it be used for?

A

Polymorphism allows many similar versions of a method (with the same name)
It can be used to:

  • Provide several versions of a method in once class, with different parameters
  • Override a method inherited from a parent class
30
Q

How would you define Composition?

A
  • Creating an object containing other objects, that will cease to exist if the containing object is destroyed
  • e.g. Workforce contains manager, employee – manager and employee will cease to exist if workforce is destroyed
  • filled diamond defines composition
31
Q

How would you define Aggregation?

A
  • Creating an object that would contain other objects, but the objects will continue to exist even if the containing object is destroyed
  • e.g. House has parent and children contained within, but if House is destroyed, parent and children continue to exist
  • empty diamond defines aggregation
32
Q

What is a ‘Static’ method?

A
  • Static means the method can be used without creating an instance, as it does not change
33
Q

What is a ‘Virtual’ method?

A
  • Virtual means the method is defined in the base class, but can be overriden by a method in the subclass
34
Q

What is an ‘Abstract’ method?

A
  • The actual method is not supplied by the base class, and must be provided by the sub class. The object is used as an interface between the method and the data.