Chapter 22 Programming Constructs Flashcards

1
Q

What is a variable and provide an example?

A

A variable is a named location in computer memory used to hold data when a program is running: the value of a variable can change while the program is running.

When declaring a variable it must be given a name and a data type.

Example:

Python: number = 0

C#: int number

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

What is a constant and give an example?

A

A constant is the same as a variable except for 1 difference: the value of a constant remains the same when a program is running.

Example:

Python: there is no constant keyword

C#: Const float rateOfPay = 11.55

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

What is a Boolean Operator and give an example?

A

A Boolean Operator is an operator that allows conditions to be combined and then evaluated: the outcome is a Boolean variable.

Example:

Python: AND OR

C#: the AND operator is denoted by &&. the OR operator is denoted by ||

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

What is an Arithmetic Operator and give an example?

A

An arithmetic operator enables arithmetic operations to be carried out on variables: arithmetic operators include +, -,*,/.

Example:

b*c

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

What is an Input statement and give an example?

A

An input statement is a statement used to capture data which is to be used in the program.

Example:

Python: player1name=Input(“Please enter player 1’s name.”)

C#: player1name=Console.Readline()

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

What is an output statement and give an example?

A

An output statement is a statement used to output data and information from the program.

Example:

Python: print
C#: Console.Write

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

What is an assignment statement and give an example?

A

An assignment statement is a statement that assigns a value to a variable, constant, or other data structure: the value on the right-hand side of an assignment statement can contain a calculation.

Example:

a=b*c

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

What is an assignment statement and give an example?

A

An assignment statement is a statement that assigns a value to a variable, constant, or other data structure: the value on the right-hand side of an assignment statement can contain a calculation.

Example:

a=b*c

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

What is an array?

A

An array is a data structure that holds a set of data items of the same data type. The array is assigned a name, a size (representing the number of data items to be stored) and a data type by the programmer.

Key facts about arrays:

The computer reserves a set of memory locations, one for each element of the array. The memory locations are next to each other or ‘contiguous’.

In many programming languages (like python), arrays are zero-indexed. This means that the first element of an array is 0.

Examples:

** cars[0] cars[1] cars[2] cars[3] cars[4]
Pos in
array 0 1 2 3 4
(Index)

Value 35 250 69 22 204

Elements can be assigned values, for example cars[0]= 35

Creating an array in python:
cars= [35, 250, 69, 22, 204]

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

How do you code bubble sort?

A

Python (not indented correctly because of the limited space per line):

cars = [35, 250, 69, 22, 204]
for index in range (1,len(cars)):

currentvalue = cars[index]
position = index

while position>0 and cars[position-1]>current value:
cars[position] = cars[position-1]
position = position-1

cars[position]=current value
print (“After pass “ + str(index)+ “ “),
print (cars)

print(“The sorted list is: “),
print(cars)

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