Midterm Flashcards

1
Q

Linux Command:

  • make a new directory
  • list files in current directory
  • change directory
  • display content of file to terminal
A
  • make a new directory: mkdir
  • list files in current directory: ls
  • change directory: cd
  • display content of file to terminal: cat
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do you need in the declaration of an array?

A
  1. Type of elements
  2. Name of array
  3. Size of array

type name[SIZE];

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

How do you initialise an array?

A

specify 1+ values inside { }

int nums[3] = {1, 2, 3};

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

How do you access a specific index of an array?

A

arr[index]

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

Conditional statements

A

manipulate control flow of program based on the results of BOOLEAN expressions

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

While loops

A

As long as (condition) is met, do (thing)

while(x > 0) {

{

always initialize conditions outside of loop

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

For loops

A

do (thing) for (number) times

for (int x = 0; x < 10; x += 1) {

}

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

Functions

What are some important features?

A

a chunk of code that does something

FEATURES: parameters, argument, declaration, definition, body, return type

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

What is the difference between a function declaration and a function definition?

A

DECLARATION: tells the compiler about the existence of a function and its parameters

DEFINITION: provides the implementation of the function

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

What do we need for file i/o?

A
  1. FILE: named collection of data stored in memory
  2. STREAM: way of reading from and writing to files in a program -> potentially infinite sequence of data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Steps to read from a file

What’s the numonic?

A

ORC

OPEN the file: connect a filestream to file
READ from file: using cin&raquo_space;
CLOSE: clean up

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

What operators can we use to read in information from a file?

A
  1. Read the next number in the file (until whitespace)
    int num;
    cin&raquo_space; num;
  2. Read a whole line (until newline character: \n), when you care about whitespace
    char next_char;
    infile.get(next_char);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a pointer?

A

a variable that stores the ADDRESS of another variable

Pointers enable pass-by reference: function can modify variables outside its scope via pointers to those variables

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

If we want to modify variables, should we pass them by value or reference?

A

pass by REFERENCE

any modifications happen to the actual variable (changed throughout the code) not the copy

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

What is pass by value?

A

pass by value: copy of the variable gets passed into function

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

What is pass by reference?

A

pass by reference: pass variable’s ADDRESS in memory

we have direct access to the original variable, not a copy

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

*

A
  1. DEREFERENCE: access variable itself
  2. DECLARE pointer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

&

A

ADDRESS of a variable

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

What is the heap?

A
  • a place to store dynamic memory
  • don’t need to know the size
  • Allocate using new and delete
  • Use VALGRIND to check freed all heap memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What must we do when using the HEAP?

A
  • use POINTERS (ie. pointer in the stack points to the array in the heap)
  • allocate memory using NEW
  • free memory using DELETE

Car *car_array = new Car[3];

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

How can we check that we have freed all of our heap memory?

A

use valgrind

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

What is a struct?

A

package different types of data

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

What are the components of a struct definition?

A
  1. struct keyword
  2. Name (capital)
  3. Member declarations inside { }
  4. Semicolon after { };
24
Q

Are structs passed by value or reference?

A

Structs are passed by value

25
Q

When do you use dot operator (.) when do you use arrow (–>)?

A

ARROW: when you have a POINTER to a struct and you want to access one of its members

Person p = new Person;
p–>name = (
p).name

DOT: access a member of a normal struct instance

Person p1 = {“Alice”, 25}
p1.age = 25

26
Q

int main (argc, char * argv [ ])

A

argc: # command line arguments

argv[ ]: array of strings containing command line arguments

27
Q

Binary –> decimal

A

1 0 1 0
12^3. 02^2. 12^1. 02^0

2^3 + 0 + 2^1 + 0 = 8 + 2 = 10

28
Q

Decimal –> binary

A

128 64 32 16. 8 4 2 1
0. 1 0. 0. 1 0 1 1

75 = 64 + 8 + 2 + 1
75 - 64 = 11 - 8 = 3 - 2 = 1 - 1 = 0

29
Q

Difference between an int & float

A

int: no decimal

float: has decimal component

30
Q

How would you write this if array was an array pointer?

arr[3] = 10

A

*(arr + 3) = 10

31
Q

How would the syntax differ between a car array on the stack vs heap?

A

Car car_array[10];

Car *car_heap_array = new Car[10];

32
Q

big O time complexity

A

worst behavior of an algorithm as input size grows

O(1) < O(logn) < O(n) < O(n logn) < O(n^2) < O(2^n)

33
Q

Time Complexity vs Space Complexity

A

Time Complexity: how long it takes to run a program

Space Complexity: how much space (memory) a program uses

34
Q

Scope of variables

A

Variables are only valid in the block in which they are declared

34
Q

Scope of variables

A

Variables are only valid in the block in which they are declared

Variable names can only be used once within its scope

35
Q

What is a global variables in C++?

A

can be accessed by any function

reduce flexibility (cannot re-use the name)

36
Q

Why do we need pointers?

A

PASS BY REFERENCE
* Instead of passing a copy of variable into a function, we pass adress in memory
* Any modifications will change the actual variable

PASS BY VALUE
* Copy variable gets passed

37
Q

What is recursion

A

Something is defined in terms of iteself (recursive function calls itself)

38
Q

How to design a recursive function?

A
  • Base case(s): case that we know the answer
  • Recursive case(s): one step closer to base case
39
Q

Class vs object

A

CLASS
* blueprint (describes the components of a thing)
* Outline how to interact with that ting

OBJECT
* Has specific values for each component

40
Q

Classes: getters & setters

A
  • getters: retrieve the value of a class’s private fields
  • setters: change the value of a class’s private fields
41
Q

Classes: deconstructor

~Name( );

A

Only need a deconstructor when we have DYNAMIC memory

Called automatically at the end of a progoroam

42
Q

Where are class methods defined?

A

In the .cpp file: FUNCTIONS

ie: getters & setters, functions

43
Q

Vectors

A

Array that can grow & shrink

under hood: when size=capacity, create a new array 2x as big, copy values, destroy origanal one

44
Q

Vector operations

  • size():
  • at(index):
  • push_back(value):
  • pop_back():
  • instert(index, value):
  • erase(index):
A
  • size(): size of the vector
  • at(index): find the value at a specified index
  • push_back(value): insert new value at end
  • pop_back(): delete last value in the vector
  • instert(index, value): insert value at specified index
  • erase(index): erase value at specified index

Call function: vendors.size()

45
Q

What do classes have?

A

Class declaration (.h file)
* public fields
* private fields (METHODS: getters & setters)

Constructor (automatically called when an object of a class is initialized)
Deconstructor (only with DYNAMIC memory)

46
Q

Card constructor example

A
47
Q

Card getter example

A
48
Q

Player setter example

A

player1.setPerstige(buy->getPrestige());

49
Q

Player destructor example

A
50
Q

What is time complexity?

A

O(1)

51
Q

What is time complexity?

A

O(n)

52
Q

What is time complexity?

A

O(2^n)

53
Q

What is time complexity?

A

O(nlogn)

54
Q

What is a binary search tree?

A

cut down complexity from O(n) to O(logn)

Every value on left < right