.111 10-15 Flashcards

1
Q

What is the pre-processor useful for?

A

defining constants (#define)
defining macros (#define)
including files (#include)
conditional compilation (#if/#ifndef)

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

How does the macro #define MIN(X,Y) (X) < (Y) ? (X) : (Y) work?

A

means where you see MIN(int,int) you substitute it with the comparison which will return whichever integer is smaller

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

how is conditional compilation helpful?

A

used to compile for different types of processor
can use #ifndef to protect header files from multiple inclusion errors

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

How do you do a binary shift in C?

A

&laquo_space;= left shift
» = right shift

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

How does compilation work for a C file?
e.g. gcc -o main main.c

A

preprocessor (takes the source - main.c) -> C compiler (main.o) -> linker (main aka exectuable)

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

What are the 4 language types?

A
  • imperative/procedural
  • object oriented
  • declarative
  • (pure) functional
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are functional languages?

A

declarative languages w/ no explicit state
e.g. Haskell + Erlang

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

What are declarative languages?

A

describe the logic of the program w/ minimal explicit control of the flow of execution
e.g. SQL, HTML, Regex

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

What are object oriented languages?

A

imperative w/ implicit encapsulation of data + functions
e.g. C++, Java, C#, JavaScript + Python

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

How does procedural programming treat code + data?

A

treat as separate concepts
data modularity only occurs in structs

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

What is modularity?

A

it allows us to divide code to inc. reliability (reducing interaction between code) and make code more scalable!

it relies on standards (an API)

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

How does OOP treat code + data?

A

combines them into objects (methods + attributes)

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

What does it mean that objects provide encapsulation?

A
  • data + code cannot be separated
  • data is defined through attributes
  • behaviour is defined through methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a class?

A

specification that defines objects

  • add attributes by defining variables inside the class
  • add methods by declaring functions with ClassName::function

NEED ; after closing } at end of class

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

How do you create an object?

A

simply use the class name as a variable type
object = instance of a class

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

What does modularity rely on?

A

encapsulation
- module must contain its inner workings, protect them + only expose functionality through its interfaces

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

What ways can attributes + methods be declared as?

A

public or private
ideally private attributes + public methods

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

Why is access control important?

A

private attributes help keep other programmers directly reading/writing to your attributes

public methods helps promote modularity w/o exposing more functionality than desired

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

What are methods vital to classes?

A

accessors (getters)
mutators (setters)
constructors
destructors

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

What is an accessor?

A

public method that gets the value of a private attribute
no params but returns value

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

What is a mutator?

A

public methods that change the value of a private attribute
take 1 param to set attribute to but void (no return)

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

What does a constructor do?

A

public method that is automatically called when you create an object
helps initialise an object
may have multiple but each needs to have unique parameters for identification

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

What does a destructor do?

A

method that is invoked automatically whenever an object is to be destroyed
occurs if…
- var relating to that object goes out of scope
- object is explicitly deleted

24
Q

Benefits of access control + encapsulation:

A
  • programmers can’t create invalid/inconsistent variable values (accidentally or maliciously)
  • promotes simpler, safer code
  • programmer writing a class can now define + enforce its API
25
How do you cast a type to a variable in C++?
casting changes the variable type - like parsing (type) var
26
How is failure a debugging strategy?
compile often + test frequently to debug features one at a time
27
Describe code inspection as a debugging strategy?
read through the code + be thorough dry run before executing if no obvious issues move onto a more systematic approach
28
What are the pros and cons of code inspection?
cheapest approach however it relies on experience
29
How do runtime debuggers aid debugging?
allows you to visualise what the program is doing can see each line executed in real time + watch values change as it runs
30
What are some helpful features of runtime debuggers?
breakpoints, single stepping, variable inspection + stack frames
31
How does reproducibility help debugging?
can create a reproducible test case + document everything you do NOTE: some bugs are transient but they WILL come back
32
Describe rubber duck debugging:
explain the problem + code to yourself out loud also can help avoid unconscious bias by talking to another developer
33
What does writing scalable code rely on?
- integrating high quality 3rd party classes into code - writing reusable code
34
What is a library?
a collection of prewritten, precompiled code e.g. set of functions in procedural or set of classes in OO
35
How do you create a library?
- separate function + class declaration into header files - separate function/method implementation into .cpp files - do NOT inc. main() - compile code using -c to get object files - then group files into static library using ar rcs -o lib.a object.o
36
How do you compile with a library?
add library names between gcc and -o when compiling (may inc. multiple libraries at once)
37
What are some C++ standard libraries?
, +
38
What does inc.?
cin - inputs cout - outputs cerr - print error clog - logging
39
What is a namespace?
provides space where we can define/declare identifiers (e.g. variables, methods + classes)
40
What do namespaces have to do w/ scope?
namespace defines a scope allows you to differentiate functions, classes + variables w/ the same name available in different libraries by writing namespace:: thing
41
What namespace does the C++ standard library use?
std
42
What does inc.?
string variable type (hides functionality of C char []) allows concatenation using + rfind() substr() append() length()
43
What does passing by value do?
function takes value of the passed variable and copies it into a new independent variable
44
What does passing by reference do?
creates an alias for the same variable so compiler treats it and the passed variable as equivalent
45
What are the rules regarding references?
references NEVER permit reassignment - they're immutable references MUST be intialised (not null)
46
How do you indicated a variable being passed by reference?
& symbol e.g. void func (string &str) and call with str passed as normal
47
What types can be null in C++?
ONLY pointers
48
What is a virtual machine?
software emulation of a physical computer than runs programs in an isolated environment
49
What is the main benefit of VMs and how does this work?
enables platform independence instead of running directly on hardware, VM-based languages run on a software-based virtual environment
50
What are some VM-based languages?
JAVA, C#, Python, Kotlin, JS
51
What is the design goal of Java?
simplicity + reusability before being a programming language, used for embedded systems + as a web language
52
Benefits of Java:
reliable, good performance + platform independent
53
What is bytecode?
java bytecode = intermediate language bytecode has very simple instruc. (think like assembly)
54
How is bytecode related to Java?
java is compiled into bytecode which the JVM then interprets into whatever the hardware understands
55
How does Java compare to C++?
Java's syntax is based on C++/C but slower given it's an intermediate language
56
How do you compile a Java program?
compile to bytecode - javac Program.java interpret w/ JVM - java Program
57
What are the pros + cons of C++?
+ scalabale + type safe (strong type checking) + encapsulation + efficient (uses less RAM + storage) - !platform independent so difficult to share libraries - doesn't check pointers - security risks w/ input of pointers - though some things are simple, some are very complex