Fundamentals of programming Flashcards
What is the && operator?
Called Logical AND operator. If both the operands are non zero then condition becomes true.
e.g. (A && B) is false.
What is the || operator?
Called Logical OR Operator. If any of the two operands is non zero then condition becomes true.
e.g. (A || B) is true.
What is the ! operator?
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.
What is the difference between the For, While and Do While loop? When should you use each?
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 do you change between Upper and Lower case?
string word = “crystal”;
Console.WriteLine(word.ToUpper());
Console.WriteLine(word.ToLower());
output=
CRYSTAL
crystal
How do you create a substring from a string?
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 do you find the index of something within a string? How do you check if a string is contained within another?
string word = “crystal”;
Console.WriteLine(word.IndexOf(“sta”));
Console.WriteLine(word.Contains(“cry”));
output=
3
TRUE
How do you insert one string into another?
word.Insert(index, “”)
What are the Math operations?
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
What is a function?
A subroutine that returns a value
How does passing parameters into a function work?
- 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
What does reference (ref) do for a value in a subroutine?
Any changes made to the value within the subroutine are retained, without this, they are not.
What is one benefit of using reference?
It removes the need for global variables
What is an exception?
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions
How does try, catch work?
- 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
What is a base case?
- 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.
What is a Local Variable?
- 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,
What is a Global Variable?
- 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.
What is Object Oriented Programming?
A programming paradigm based on objects:
Object: An instance of a class
Class: Blueprint/template for an object
What does an object contain?
- Attributes (properties) e.g. size, shape, colour
- Methods (actions) e.g. changeSize(), changeShape(), changeColour()
What is an object defined as?
An instance of a class, contains both attributes and methods
What is a class defined as?
A template for creation of objects, an object is built from a class and you can have many instances of the same class
What is a Method defined as?
- A program routine within an object
- Carries out a particular task on an object
- Can be inherited from a superclass (Parent)
What is procedural programming?
Where programs are formed from sequences of instructions which are executed in the order in which they appear.
What are some problems with procedural programming?
list 2
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
What is inheritance defined as?
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
What is encapsulation defined as?
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
Benefits of encapsulation?
- Prevents users from gaining access to certain data
- Any modifications to the internal code will not cause problems elsewhere, object can be maintained independently
What is Polymorphism? What can it be used for?
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
How would you define Composition?
- 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
How would you define Aggregation?
- 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
What is a ‘Static’ method?
- Static means the method can be used without creating an instance, as it does not change
What is a ‘Virtual’ method?
- Virtual means the method is defined in the base class, but can be overriden by a method in the subclass
What is an ‘Abstract’ method?
- 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.