Fundamentals Flashcards

0
Q

What is the difference between a statement, expression, and function?

A

A statement is a single instruction terminated with a semicolon.

An expression is a mathematical entity which evaluates to a value; often used within statements.

A function is a collection of statements designed to perform a specific job.

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

What are libraries?

A

Groups of functions packaged for reuse

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

What is an l-value?

A

A value that has an address in memory. Examples are variables. The ‘l’ refers to the fact that these are the only values which can be on the left side of an assignment statement

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

What is an r-value?

A

Any value that can be assigned to an l-value and always evaluates to produce a single value.

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

What happens to the value already in memory when that memory is allocated?

A

The preexisting value is NOT overwritten, which is part of why uninitialized variables yield seemingly random ‘undefined’ results.

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

What does the type specification tell the compiler when declaring a variable?

A

How much memory to allocate.

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

What is the difference between a literal and a variable?

A

A literal always evaluates to itself, while a variable evaluates to its held value.

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

What is the difference between a unary and binary operator?

A

A unary operator acts on one operand (i.e. the ‘-‘ symbol when specifying a negative number), while a binary operator acts on two operands (i.e. the ‘-‘ symbol when used to subtract one number from another)

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

What is a forward declaration?

A

Declaring a variable before it is used/initialized.

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

What is a function prototype, an what must it have

A

A function prototype declares the function but does not implement it. It contains the name of the function along with the parameter types and return types. Parameter names are optional but recommended. They should be declared near the top of a file.

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

What will happen if a function is defined but not implemented and then called?

A

The program will compile without error, but the linker will produce the error because it cannot find the function implementation and cannot resolve the function call.

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

How can a .cpp file become aware of functions in other .cpp files

A

A .cpp file can become aware of functions in other .cpp files by including a forward declaration of that function.

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

What is a header file?

A

Contains only function declarations and NOT implementations. For example, iostream.h declares ‘cout’ (so the compiler is happy) but the implementation is within the runtime support library, which is automatically linked to the program by the linker.

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

What does a library consist of?

A

A header file with declarations of public functions and a precompiled object that contains all of the implementation code in machine language.

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

Why is having a precompiled library beneficial?

A

They rarely change, and the constant need to recompile them is a waste of time. It also hides source code, which is important for intellectual properties.

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

T/F: header files should contain implementations.

A

F; header files should contain only declarations and prototypes, unless the implementation is extremely trivial.

16
Q

What are some rules about header files?

A

They should always include header guards, not declare variables unless they are constants, be independent and have a specific job relative to a corresponding .cpp file, and should include as few other headers as possible.

17
Q

Why do some standard library headers have the .h extension while some do not?

A

Those without extensions are newer, post standardization, and move their functions into the ‘std’ namespace. Those with .h extensions are older and allow for backwards compatibility.

18
Q

What does the #include directive do?

A

It inserts the contents of a file at the point of the directive.

19
Q

What is the #define directive?

A

define identifier replacement

It is a macro with the syntax:

The identifier should be in all caps and use snake_casing

20
Q

What is the #undef directive?

A

It undefines a macro

21
Q

What is the syntax of a conditional compilation?

A
#ifdef
#define
  //code here
#endif
22
Q

What is a header guard?

A

It prevents a file from being included more than once and wraps the entire header file.

It used #ifndef to see if a file has not been defined yet. It uses the following syntax:

#ifndef FILENAME_H
#define FILENAME_H
  //declarations here
#endif
23
Q

What is a good programming workflow?

A

1) define the problem
2) define the target (user, system, requirements, etc.)
3) break down the complex problem into simplest tasks using a top-down hierarchy
4) identify the sequence of events
5) figure out data inputs and outputs for each task by declaring prototypes
6) write the task details by implementing the functions
7) connect data inputs and outputs in a logical and readable way

24
Q

What are some other programming pointers?

A
  • KISS
  • add features over time
  • focus on one area at a time
  • test each piece of code as you go