Test 2 Flashcards
Creating a table in SQL with restricted choice
CREATE TABLE user (
username VARCHAR(20) NOT NULL PRIMARY KEY,
password VARCHAR(20) NOT NULL,
gender VARCHAR NOT NULL CHECK(gender = “male” or gender = “female”),
nickname VARCHAR(20) NOT NULL,
FOREIGN KEY nickname REFERENCES score(nickname)
);
SQL query in php with a established connection
leftarrow ?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die(“Connection failed: “ . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = $conn->query($sql);
if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "br>"; } } else { echo "0 results"; } $conn->close();
Insert Query
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES (‘Cardinal’, ‘Tom B. Erichsen’, ‘Skagen 21’, ‘Stavanger’, ‘4006’, ‘Norway’);
Update Query
UPDATE Customers
SET ContactName = ‘Alfred Schmidt’, City= ‘Frankfurt’
WHERE CustomerID = 1;
Delete Query
DELETE FROM Customers WHERE CustomerName=’Alfreds Futterkiste’;
What does a binary search look like?
def BinarySearch1(searchlist1,goal): found = False startpos = 0 endpos = len(searchlist) -1
#print (“Endpos at beginning = “,endpos)
while (startpos <= endpos) and found == False:
middle = (startpos+endpos)//2 #Integer Division
if searchlist[middle] == goal:
found = True
elif searchlist[middle] smaller than goal:
startpos = middle + 1
else:
endpos = middle -1
if found == True:
print(“Match has been found at position”,middle)
else:
print(“Goal not found”)
What does a bubble sort look like?
def bubbleSort(beforeName, beforeScore):
for outer in range (len(beforeScore) -1,0,-1): for inner in range(outer): if beforeScore[inner]>beforeScore[inner+1]: temp = beforeScore[inner] tempName = beforeName[inner] beforeScore[inner] = beforeScore[inner+1] beforeName[inner] = beforeName[inner+1] beforeScore[inner+1] = temp beforeName[inner+1] = tempName
print(beforeScore, beforeName)
What does an insertion sort look like?
def insertionSort(beforeName, beforeScore):
for index in range(1,len(beforeScore)): currentval = beforeScore[index] currentName = beforeName[index] position = index while position > 0 and (beforeScore [position-1] > currentval): beforeScore[position] = beforeScore[position-1] beforeName[position] = beforeName[position-1] position = position - 1 beforeScore[position] = currentval beforeName[position] = currentName
print(beforeScore, beforeName)
How to create a 2D array
map = [[’.’ for col in range(0,30)] for row in range(0,10)]
What does instantiation mean
An object that has been created which contains the methods and attributes of a particular class e.g. Aiden is an instantiation of a human class
What is a class
A template for a particular object e.g. a car class has information about wheels, colour, engine, seats, speed and methods that can manipulate these values.
What does encapsulation mean
Attributes of an instantiation of a class are private and can only be changed using the setter and getter methods. E.g. a car’s speed can only be changed using car.accelerate() and car.decelerate() methods
What does polymorphism mean
When a subclass has a method of the same name as its superclass but the functionality is different. For example car superclass has an accelerate method which increases speed by 10 but a toycar subclass has an accelerate method which plays a sound effect of a car. This means that program code doesn’t have to know which class of car is being passed into it.
What does inheritance mean
Where a class can be set up as a subclass of another class (superclass) and this automatically inherits the attributes and methods of it’s parent. For example toycar has all the attributes of car - colour, speed, engine, seats, wheels
What does a constructor do
Creates an instantiation of a class with default attribute values