Programming And Data Structures Flashcards
Difference between switch and if statements
- No condition, just value used
- input is compared to each of the values in each of the statements until a match is found
- if no match found, flow goes to default case
- cannot test fractional values
What is the ternary operator?
- ‘?’ Condenses whole if statement into 1 line
- “condition ? Expression1:expression2;
- first condition must be boolean
- if statement is true first condition will be met, if its false vise versa
1) what does break command do?
2) what does continue command do?
3) what is ‘uint’
1) command takes control to the first command after while loop
2) ignores everything in loop on current pass and goes to the top of the loop again
3) unsigned integer that has to be positive (takes up less memory as you know value is always +)
How do you generate Random numbers?
- using random class
- Random rnd = new random() then rnd.next(x,y)
- lower bound is inclusive, upper bound is exclusive
What is command line input?
- Uses the ability of programs to have data passed into them from outside the program
- all command line input is strings
What are the two types of string literals?
- quoted: normal string with speech marks
- @quoted: start with @ as well as speed marks
How do we read contents from a file?
- add using system.io
- create string to hole read-in text
- use .readalltext to read
- make sure filename is full path
- make sure file exists
What’s the difference between .ReadAllLines and .ReadLines?
- .ReadAllLines reads the whole file into memory
- .ReadLines reads each line as a text string which can be looped through with a foreach loop
How do i write text to a file?
- add using.system.IO
- 2 use File.WriteAllText(file name, text)
- if we want to add onto end of existing file use File.AppendAllText
- could also use the WriteAllLines variants of both of these to write line by line
1) what is a class?
2) what is an object?
1) defines a new type of object - defines its common characteristics
2) object is individual instance of that class
Difference between methods and properties?
- Methods are what an object can do
- Properties are the description of the object
What is the class identifier?
Name given to class
What two types of things can be contained in a class?
- Data members (variables and constants)
- Functions
What’s a void function?
Returns no values
What is the declaration of an object called?
Instantiation
What are constructors?
-Take in parameters and change the private values equal to these values
What is a static class member?
- when a class member is static it can invoked directly from the class level without creating an object
- Allows us to refer to a class from within itself
- makes members belong to the class itself instead of the individual objects
- static methods can access only static members
What are the two ways to pass data to a method?
- passing by value: passes copy, original not affected
- passing by reference: passes a reference to the memory location of the original variable. Original is affected
- default is pass by value, have to put ‘ref’ key word (in method and calling statement )to pass by reference
What is the ‘out’ keyword
-can be used in the method header specifically for returning values
What is method overloading?
- a class has 2 or more methods with the same name
- they may have different types, number or order of parameters in parameter list
- choice of method that is triggered is made on runtime and depends on the values being passed to the class
What is a methods signature?
- name, number, type and order of its parameters
- should be different for each overloading method
- changing return type will not affect the signature
What do properties allow? What does value mean? What are accessor methods?
- provide the opportunity to protect a field in a class by reading and writing to it through a property
- consists of get and set method wrapped inside the property (called property accessor methods)
- keyword ‘value’ refers to the value the user is trying to assign to a variable
What are constructors?
- used in initial set up or construction of an object
- can contain same type/amount of code as method
- can never return a value and has the same name as the class
- if no constructor is defined the default constructor is called which sets all value to 0 or false
- constructor with at least 1 parameter is called parameterised constructor. Can initialise each instance of a class with different values
- can be overloaded
What is a destructor?
- called automatically by the runtime after an object of a class is no longer in use (as program ends)
- defined by using the sqwiggle
- can only have one, cannot be overloaded, cannot manually call it