Types in C++ Flashcards

1
Q

Is C++ statically typed?

A

Yes

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

What does it mean for a lanuage to be statically typed?

A

Every variable, function argument and function return must be labeled with a type so the compiler knows how much memory to allocate, what type of values to store, what operations can be performed and how to interpret the bit patterns associated with the value.

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

What are the two catagories for types in C++?

A

Fundamental (built-in) types and derived (compound) types

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

List C++ built in types

A

Booleans (bool), Characters (char), Integers (int), Floating Point (float), Double Precision Floating Point (double), Valueless (void), Wide Char (wchar_t), std::nullptr_t

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

List C++ built-in type specifiers

A

short, long, signed, unsigned.

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

Where are type specifiers placed?

A

Before the type (unsigned int, for example)

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

What typical sizes are available for the int built-in type?

A

short int (16-bit), int (32-bit) long int (64-bit) - except windows which keeps it at 32-bit

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

Name the built-in integral types

A

int and char

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

What sizes are available to a char?

A

One byte (typically 8 bits, but not on all systems)

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

At Google, what can we assume about the sizes of ints and chars?

A

The char is 8-bits and the int is at least 32-bits. Within google3, we can assume a 32-bit int size with sizeof(int) = 4.

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

What is the default signed-ness of an int?

A

Signed, it is not common to write signed int

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

How does a signed int represent its numbers when it has N bits available?

A

Two’s compliment (-2^(N-1), 2^(N-1) - 1)

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

How does an unsigned int represent its numbers when it has N bits avaible?

A

(0, 2^N - 1)

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

Describe overflow behavior for signed and unsigned ints

A

For signed, it is unpredictable. For unsigned, it is predictable and acts similar to the modulo operator.

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

Describe conversion behavior from signed to unsigned ints

A

It is well defined,

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

What is the typical size of a bool?

A

One byte

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

What is the size of a char?

A

One byte

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

Are chars signed or unsigned by default?

A

Neither, it is up to the implementation

19
Q

How large is a wchart_t

A

Typically 2 to 4 bytes

20
Q

What is the typical encoding of a char?

A

It is typically UTF8

21
Q

What is a void* pointer?

A

Universal pointers that can be point to any variable not declared as const or volatile.

22
Q

Are void pointers use in C++?

A

Yes but it is uncommon

23
Q

How do you dereference void *s?

A

Cast it to another type

24
Q

What is std:nullptr_t?

A

Is a built-in type with one value which is the null point literal nullptr

25
Q

Write a simple function in C++ to showcase its syntax

A

int MyFunc(int v) {return 1;}

26
Q

What is a member function? What is the difference between a static and non-static?

A

A member function is defined and associated with a class. A static function is associated with the class at large, while a non-static function is associated with an instance or object of the class

27
Q

Provide an example of passing a function pointer as an argument for a function

A

void TakesFunc(int (*)(int));

// The & here is optional. There is an implicit conversion (“decay”) from a
// function type to its corresponding function pointer type.
TakesFunc(&MyFunc);

28
Q

What is the type of a non-static member function?

A

It is the return type, the implicit this parameter from its class and the argument parameters -> int (Class::*) (int int)

29
Q

What is the type of static member functions and free member functions?

A

It is the return type and the argument parameters -> int (int, int)

30
Q

Show an example of how you create an array in C++

A

int array[5] = {1,2,3,4,5};
int array = {1,2,3,4,5};
int array[5] = {}

31
Q

How do you create a char array in C++?

A

char array[3] = {‘a’,’b’,’c’};
char array[3] = “abc”;

32
Q

Show how you can create a pointer.

A

Use the * after the type in the declaration. Use the & to reference the value (provide the address):
int* ptr = &value;

33
Q

When is the memory location of a variable set?

A

At runtime

34
Q

What is the size of a pointer?

A

It is architecture dependent. However, within an architecture, all pointers are typically the same size. 8 bytes for 64 bit architectures and 4 bytes for 32 bit.

35
Q

What is dereferencing and how do you do it?

A

Dereferencing is retrieving the value stored at an address associated with a pointer. Use the * operator (*ptr)

36
Q

What are references?

A

They act like aliases and allow one to refer to existing objects without explicitly dereferencing them (unlike pointers). They cannot be null and must be initialized.

37
Q

What are lvalue and rvalue references?

A

References that are assigned from the left or right side of an assignment operator respectively. & is used for lvalue (int& val = 1) and && used for rvalue (int&& val

38
Q

How can a user define their own types?

A

Through enums, classes, structs and unions.

39
Q

Compare and contrast structs and classes

A

Both contain member functions and data members. Members of structs are default public while members of classes are default private. Typically structs are used for simple code blocks without invariants classes for code with invariants (the state stored in the object does not change).

40
Q

Why should you use enum class over plain enum?

A

enum class is strongly-typed and scoped and prohibits direct comparison via integers making it safer then the upscoped plain enum.

41
Q

Show and example of how you could create an enum in c++?

A

enum class Color (red, blue, green}
Color color = Color::red;

42
Q

What are type aliases?

A

Allow you to refer to another type using an alias

using Bananas = int;
Bananas b = 5;

typedef int Bananas;
Bananas b = 5;

43
Q
A