2 - Programming Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What are data types? Give some examples of some:

A

-data types describe the different types of data being used, and defines the range of values a variable may take

-int, real, bool, char, string

in python, real numbers are called floats

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

Why are data types needed?

A

-so the computer knows what operations it can perform on it
-it allocates the necessary amount of memory to optimise the program

in python, real numbers are called floats

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

What is a variable?

A

named identifier that holds a value in memory that can change

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

What is a constant, and what is it used for?

A

-named identifier that is given a value when initialised and holds it in memory, and doesn’t change throughout the program

-used to reference known values like pi

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

Define declaration/initialisation:

A

-naming a new identifier for the first time

-then assigning it a value

In Python, both are usually done at the same time, but some languages (like VB) do it separately

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

What is an identifier?

A

a name for a variable, constant, or subroutine

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

Why should you give meaningful identifier names?

A

-meaningful identifier names describe the purpose of the variable
-makes the program easier to interpret/debug

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

What are the 3 programming constructs?

A

-sequence
-selection
-iteration

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

What is sequence in programming?

A

executing 1 instruction after another (imperative programming)

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

What is selection in programming?

A

statements that change the flow of a program based on conditions that are met (branching)

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

What is iteration in programming? Describe the 3 types:

A

repeated execution of a section of code

for - count controlled

while - condition checked at start, may never run
repeat until - condition checked at end, always runs once

The bottom 2 are condition controlled

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

What is nesting?

A

putting a statement inside another with selection/iteration

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

Name 3 types of arithmetic division and give their Python symbols:

A

-real division, just normal ÷

/

-integer division, DIV, division that returns the integer part of the result

//

-remainder division, MOD, returns remainder

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

Name the boolean operators:

A

NOT
AND
OR

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

What are data structures?

A

an abstract idea of how to store and access data in a computer system

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

Why are data structures described as abstract?

A

-the computer memory doesn’t actually know it is a list, it just stores it in memory as it usually would

-it just makes it easier for the programmer to read/access

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

What is an array?

A

a data structure (1 or 2 dimensional) comprised of a set number of cells containing data all of the same type

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

How do you access data from a 2D array?

A

-the first square bracket indicates the list you want to access from the 2D array
-the second square bracket indicates the item from the previously specified list

remember indexing starts from 0

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

What is a record?

A

a data structure containing a set of information about certain items

20
Q

How would you make a record in python without importing anything?

A

make a dictionary where the key is the name of the item, and the value is all the information about the item in the form of another dictionary

record = {
 "bob":{"score":102,"name":"bob name},
 "john":{"score":321,"name":"john name"},
 "fred":{"score":7,"name":"fred name"}
}
21
Q

Using an import statement, how could you make a record in python?

A

if you are making more than one dataclass, always put @dataclass before it

22
Q

How do you add a record to a dataclass?

A

make sure you give a value to every one of the arguments when adding a record

23
Q

How do you access a particular value from a record in a dataclass?

A
24
Q

Describe a dictionary:

A
dict = {key1: value1, key2: value2}

-values can be accessed by doing dict[key]

25
Q

What is the code for making substrings?

A
myString[a:b]

a, start
b, end (not included)

26
Q

How do you make a string in all capitals or lower case?

A
myString.upper()
myString.lower()
27
Q

How do you find a particular character or word within a string?

A

myString.find("a bc")

-returns integer that shows the position of the first character in the find brackets
-if it doesn’t find it, then it will return -1

28
Q

How do you reverse a string?

A

myString[::-1]

29
Q

What is another way to concatenate some variables in a print statement?

A

always put the f before so that it recognises the formatting needed

30
Q

Functions for converting to character code and back:

A

ASCii to text:

chr(myAsc)

Text to ASCii:

ord(myChar)
31
Q

How do you generate a random number in python?

A
import random

num = random.randint(startRange,endRange)
32
Q

What is the difference between randint and randrange?

A

-randint takes a random number between the start and end numbers inclusive
-randrange does not include the end number

However both are used in the same way - (random.randint(), random.randrange())

33
Q

How do you use shuffle?

A
import random

list = [1,2,3,4,5,6,7,8,9]
random.shuffle(list)

DO NOT put the last line in a print statement, it doesn’t work for some reason

34
Q

What is a subroutine, and how is it used?

A

-indented block of code used in order to execute a commonly used task
-can be called by writing its identifier name as a statement and passing the necessary parameters

35
Q

What is the difference between a global and a local variable?

A

global is can be accessed/changed throughout the whole program, while local only exists within a certain subroutine

36
Q

What are the advantages of local variables over global ones?

A

-easier to debug program as variable can only be accessed within its own subroutine

-more memory efficient as once the subroutine finished, all of the local variables used in it are removed from memory

37
Q

Difference between a procedure and a function:

A

functions return a value

Functions can also return a value as a string, eg:
return "num1"
This won’t print the string but assign it to the variable where the function was called

38
Q

Why was the structured approach to programming developed?

A

to give programmers a logical structure that uses modules/subroutines to aid code interpretation/debugging

39
Q

What are the advantages to using modularised programming in the structured approach of programming?

A

-can be reused to reduce development time
-easier to find errors in code
-can be changed without affecting the rest of the program
-subroutines can be developed separately by different people and combined later on

Make sure to put the first point as a whole, don’t just put “reduces development time” by itself

40
Q

A programmer uses the code below in order to make a program. Explain how they have used the structured approach to programming:

A

-decomposed the problem into sub-problems
-solved sub-problems by using subroutines and passing the necessary parameters

41
Q

What is robust and secure programming?

A

writing sections of code to catch errors from invalid user inputs that wouldn’t usually be expected

42
Q

Types of testing data:

A

-valid data, within accepted bounds
-invalid/erroneous data, rejected by program
-boundary/extreme data, either side of bounds

If asked to give an example of boundary data, just give one value rather than giving both sides of the boundary

43
Q

Name the different types of validation checks:

A

range check - for numbers
length check - for strings
presence check - eg checking if a password has been entered a second time
format check - eg checking if your date of birth is in the form DDMMYYYY
type check - eg checking if a number is an integer

44
Q

Name 2 types of errors:

A

-syntax (misspelt code, eg missing punctuation/letters)
-logic (program can run but not as intended, eg bool value is true rather than false)

45
Q

A program has the statement:
if distance <= 5:
How many possible values could the result of this comparison have?

A

2 (either distance is less than/equal to 5, or it is greater than 5)

boolean