Variables Flashcards

1
Q

What is a block?

A

Also known as a compound statement, it is a group of statements that are surrounded by {}, and are treated by the compiler as a single statement
.

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

What is the recommended maximum depth for nesting blocks, and what should be done when it is reached?

A

3 - 4 blocks deep, anymore and it’s time for multiple functions.

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

What is a local variable?

A

A variable inside a code block that has local (also known as block) scope.

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

What happens to local variables when the block ends?

A

They are destroyed.

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

T/F: Nested blocks are considered part of the outer block in which they are defined

A

T; variables declared in the outer block can be seen inside the nested block

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

What is the best practice when deciding the scope of a variable?

A

Give it the smallest possible use depending on where it is used. This avoids confusion and can help with efficiency.

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

What is the scope of a global variable?

A

Program scope; can be accessed anywhere.

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

T/F: global variables can be accessed across multiple files.

A

T; they have program scope.

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

What has to be done in order to share a global variable across files?

A

The ‘extern’ keyword, which tells the compiler you are not declaring a new variable but rather using a variable defined in an external source.

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

How can a global variable be declared in a header file?

A

Declare the variable in a .h file with the ‘extern’ keyword, then define it in the corresponding .cppfile, WITHOUT the extern keyword

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

In the case that you are working in a block where the a local variable with the same name as a global variable replaces it, what is the operator to access the global version of a variable?

A

The unary scope operator, although the need to do this indicates poor organization/naming.

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

What are two reasons global variables are dangerous?

A

They increase complexity because it means a bug can be literally anywhere in the program, since the global variable is in effect everywhere, and they can be changed by any function anywhere, which can lead to unexpected results.

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

What does file scope mean, and how is it different from global scope?

A

A variable with file scope can be accessed by any function or block within a single file. File scoped variables act exactly like global variables, except their use is restricted to the file in which they are declared (which means you can not extern them to other files)

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

How is a file scoped variable declared?

A

with the ‘static’ keyword before the typename

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

What are the different meanings of the static keyword, and what determines its function?

A

Its location in the program; outside of a block it gives a variable file scope, within a block it gives a variable fixed duration, meaning it is NOT destroyed at the end of the block. This is in contrast to the usual automatic duration of a local variable, which is destroyed at the end of the block.

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

include

What does the following program print?

void IncrementAndPrint()
{
    using namespace std;
    static int s_nValue = 1; // fixed duration
    \++s_nValue;
    cout
A

2
3
4
b/c a static variable in a block has fixed scope, meaning it is only initialized ONCE and persists throughout the program

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

What is one common use for fixed duration (static local) variables?

A

For generating unique id’s across a file:

int GenerateID()
{
    static int nNextID = 0;
    return nNextID++;
}

Because nNextID is a local variable, it can not be “tampered with” by other functions.

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

Describe implicit type conversion.

A

Implicit type conversion is done automatically by the compiler whenever data from different types is intermixed. When a value from one type is assigned to another type, the compiler implicitly converts the value into a value of the new type. For example:

double dValue = 3; // implicit conversion to double value 3.0

int nValue = 3.14156; // implicit conversion to integer value 3

19
Q

What is widening?

A

In any operation involving two or more operands with at least two different types, the operand with the smallest data type is converted to the larger data type to minimize data loss.

20
Q

What is wrong with the following conversion:

float fValue = 10 / 4;

A

10 and/or 4 need to first be cast as floats, otherwise the expression will first evaluate to 2 (without the fractional component), and the float cast will merely convert 2 to 2(.0)

21
Q

What is the syntactical difference between a standard cast and a C-style cast?

A

Both are explicit type conversions.

Standard cast:
(castType)varName

C-style cast (more function-like):
castType(varName)

22
Q

What is a static cast?

A

It is used to tell the compiler you want to convert a value of one type to a type with a smaller range, even though this can cause data loss:

int nValue = 48;
char ch = static_cast(nValue);

23
Q

What is an enumerated type?

A

A simple way to, in C++, create a new data type. An enumerated type is a data type where every possible value is defined as a symbolic constant (called an enumerator)

24
Q

T/F: defining an enumerated type allocates memory.

A

F; memory is only allocated when a variable of the enumerated type is declared

25
Q

Enum variables have the same size as what other type?

A

int

26
Q

What are the default values for enum types, and how can values be defined explicitly?

A

By default, the first value is 0 and increases by 1 for each enumerator. If a value is explicitly defined but the next is not, then the next value will be implicitly defined as one greater than the previous value:

enum Animal
{
    ANIMAL_CAT = -3,
    ANIMAL_DOG, // assigned -2
    ANIMAL_PIG, // assigned -1
    ANIMAL_HORSE = 5,
    ANIMAL_GIRAFFE = 5,
    ANIMAL_CHICKEN // assigned 6
};
27
Q

T/F: You can cast an enum type as an int, but not the other way around.

A

T

28
Q

Assuming COLOR_BLUE is from an enumerated value but not from the ‘animal’ enum, will the following code compile?

A

No; each enumerated type is a distinct type

29
Q

What are some good uses for enum types?

A

Array indices, error code return values

30
Q

What is a typedef and what is its syntax?

A

Typedefs allow the programmer to create an alias for a data type, and use the aliased name instead of the actual type name:

typedef long miles; // define miles as an alias for long

// The following two statements are equivalent:
long nDistance;
miles nDistance;
31
Q

What is a smart use of typedefs?

A

To make code more legible:

typedef long miles;
typedef long speed;

miles nDistance = 5;
speed nMhz = 3200;

// The following is okay, because nDistance and nMhz are both type long
nDistance = nMhz;

Note that, because the underlying types are identical, the last statement in the above example is acceptable.

32
Q

Why are typedefs extremely useful for maintaining code?

A

It allows you to change the underlying type of an object without having to change lots of code. For example, if you were using a short to hold a student’s ID number, but then decided you needed a long instead, you’d have to comb through lots of code and replace short with long. However, with a typedef, all you have to do is change typedef short studentID to typedef long studentID and you’re done.

33
Q

Explain the following example:

#ifdef INT_2_BYTES
typedef char int8;
typedef int int16;
typedef long int32;
#else
typedef char int8;
typedef short int16;
typedef int int32;
#endif
A

It allows for platform independent coding by ensuring that, if the size of an int is 2 bytes instead of the usual 4, an int will not be used to store more than 2 bytes of information, as data loss will occur otherwise.

34
Q

What is an aggregate data type?

A

A data type that groups multiple individual variables together.

35
Q

What is a struct?

A

An aggregate data type which allows us to group variables of mixed data types together into a single unit

36
Q

What are the variables inside a struct known as?

A

Members

37
Q

When is memory allocated to a struct type?

A

When a variable of the struct’s type is declared, NOT when the struct is defined.

38
Q

How can the values of struct members be set?

A

With the member selection operator:

structVar.memberName = value

39
Q

T/F: An entire struct can be passed as a parameter

A

T; the function can then access the members of the struct

40
Q

In the case that a struct contains another struct, how can the members of the inner struct be accessed?

A

With two ‘.’s:

struct Company
{
    Employee sCEO; // Employee is a struct within the Company struct
    int nNumberOfEmployees;
};

Company sMyCompany;

41
Q

What is an initializer list, and what is its syntax?

A

It is a way to quickly set all the members of a struct when declaring a new variable of the struct’s type:

struct Employee
{
    int nID;
    int nAge;
    float fWage;
};

Employee sJoe = {1, 42, 60000.0f};

42
Q

T/F: You cannot initialize a struct within a struct with an inner initializer list.

A

F; the following is valid:

struct Employee
{
    int nID;
    int nAge;
    float fWage;
};
struct Company
{
    Employee sCEO; // Employee is a struct within the Company struct
    int nNumberOfEmployees;
};

Company sCo1 = {{1, 42, 60000.0f}, 5};

43
Q

Where is the best place to declare structs?

A

A header file