Data/Control Structures
Arrays
Strings & Lists
Objects
Exception Handling
100

What is a FOR control structure?

This control structure loops through code for a set number of iterations

100

What is a major difference between arrays and lists?

Arrays all have to be the same data type.

(other answers can be accepted if I determine so)

100

Where does indexing start?

0

100

What is a class?

This is where we specify an object. It is the template for that object type and specifies what the object is (state) and what the object does (behaviour)

100

Who is almost always at fault for errors?

the programmer 

200

What is an IF control structure?

This control structure proceeds with code so long as a condition is true.

200

How would you create an array in Python?

import numpy as np

array = np.array([])

200

What would list[3] equal?

list = ['a','b','c','d','e']

'd'
200

List 3 benefits of classes/objects

  • protects data from being used incorrectly
  • increases code reuse
  • makes code easier to read and maintain
  • objects know how to respond to requests
  • relates to how objects function in the real word
200

What are the three types of errors?

- syntax error

- runtime error

- logical error

300

What is a WHILE loop?

This control structure loops until the condition is no longer met.


300

if the array is [1,2,3,4,5,6,7,8,9] what would the value be at array[5]

6

300

How do you add an element to a list?

list.append()

300

What is the difference of a class variable and an instance variable?

A class variable will apply to all objects of that class whereas the instance variable is local to each object.

300

What are the three exception types?

- TypeError

- ValueError

- NotImplementedError

400

What are two (2) differences between Panda tables and numpy arrays and what is one (1) similarity?

  • Numpy arrays can be multi-dimensional whereas DataFrame can only be two-dimensional.
  • Arrays contain similar types of objects or elements whereas DataFrame can have objects or multiple or similar data types.
  • Both arrays and DataFrames are mutable
400

How would you create a multidimensional array?

  • from lists: listarray = np.array([[1,2],[3,4]])
  • initialised values: np.zeros((10,10)), np.ones((10,10))
  • range of numbers: np.mgrid[0:x,0:y]
400

How would you make a string uppercase and duplicated?

Use mystring = "abcd" as an example

mystring.upper()*2
400

What are the 5 categories of methods in a class?

Receive half points if you can name at least 3.

  • the constructors
  • the accessor methods (getters)
  • the mutator methods (setters)
  • doing methods
  • [private] methods
400

Give one example of each error type

Any one of the following (for each error)

Syntax errors

• leaving out a keyword
• putting a keyword in the wrong place
• leaving out a symbol, such as a colon, comma or brackets
• misspelling a keyword
• incorrect indentation
• empty block

Runtime errors

    • division by zero

  • performing an operation on incompatible types

  • using an identifier which has not been defined

  • accessing a list element, dictionary value or object attribute which doesn’t exist

  • trying to access a file which doesn’t exist

Logical errors

• using the wrong variable name
• indenting a block to the wrong level
• using integer division instead of floating-point division
• getting operator precedence wrong
• making a mistake in a boolean expression
• off-by-one, and other numerical errors


500

What is a dictionary, set and tuple?

A dictionary is an unordered set of value-key pairs that can be added to, deleted from and overwritten, so long as the keys remain untouched (immutable).

A set is an unordered, unique collection of data.

A tuple is an immutable list that are usually created by comma separated values 

500

How would you add an element to an array?

let a be the original array and b be the new element(s);

a = np.append(a, b)

500

What would be the resultant string?

name = "John Cleese"

name[0:-1:2]

"Jh le"

500

What is an example of a class relationship and what does it entail/what is its definition?

Any one of the below

Aggregation: one class is declared as a class field with another class ("has-a" but not lifecycle dependant usually)

Composition: one class is included as part of the other class. the included class does not exist without the host class ("has-a"/"whole-part", strong lifecycle dependency)

Inheritance: one class is a descendant of another class ("is-a" relationship, one class is a subclass of another)

Association/Dependency: used to show that one class invokes methods in another but there is no other relationship beyond this.

500

What is the structure of coding exception handling in a python program

try:

    ...

    raise ValueError(x)

except ValueError as err

    print(err)

M
e
n
u