C++ Core Flashcards
What are the 3 different categories of data types present in C++?
Fundamental data types, User defined data types, Derived data types
Name the C++ fundamental data types
- Boolean type (bool)
- Character types (char, wchar_t)
- Integer types (int, long long …)
- Floating-point types (double, long double ….)
- Void
What is the void type?
- Type with an empty set of values (no value applies to it).
- Incomplete type that cannot be completed.
What is an incomplete type?
A type that is not defined or void type.
What is nullptr_t?
Answer:
- nullptr_t solves previous ambiguity problems with NULL
- nullptr_t is the type of the nullptr literal.
- It is a distinct type that it’s not itself a pointer type.
Read more:
https://dzone.com/articles/what-exactly-nullptr-is-in-c
What RAII abbreviation means?
Resource Acquisition In Initialization
What is RAII Intent?
Answers:
- To guarantee the release of a resource at the end of scope.
- To guarantee no memory leaks
Read more:
http://www.vishalchovatiya.com/7-advance-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver
What is example RAII Implementation?
Answer:
- Wrap resource into a class
- Resource acquired in the constructor immediately after it’s allocation
- Destructor automatically releases resource
- Resource used via interface
Read more:
http://www.vishalchovatiya.com/7-advance-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver
Why RAII is imporant?
Answer:
- To not forget to release resource.
- If a function returns early (throws, returns, etc…) resource may be leaked.
Read more:
http://www.vishalchovatiya.com/7-advance-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver
What are RAII usecases?
Answer:
In acquisition/release of resource with:
1)new/delete (memory)
2)lock/unlock (mutex)
3)open/close (file)
4)and others
Read more:
http://www.vishalchovatiya.com/7-advance-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver
What is the intent of return type resolver idiom?
Answer:
To deduce the type of the object being initialized or assigned to.
Read more:
http://www.vishalchovatiya.com/7-advance-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver
What is NULL?
NULL is a MACRO that is evaluated to integer with value 0
What is the intent of Type Erasure?
Answer:
To create generic container that can handle variety of concrete types.
What we refer to when we say the implementation’s data model?
The sizes of the fundamental data types that are made by each implementation is referred to as the data model.
What are the four data models that found wide acceptance?
int/long/pointer notation (size in bytes)
32 bit systems:
LP32 (2/4/4 Win16) or ILP32 (4/4/4 Win32, Unix)
64 bit systems
LP64 (4/4/8 Win64) or ILP64 (4/8/8 Unix)
What are the integer types modifiers?
Signedness: signed (default) and unsigned.
Size: short (at least 16 bits) and long (at least 32 bits) and long long (C++11 at least 64 bits).
When integer types overflow is undefined?
When you overflow a signed integer.
At least how many bits is short?
16
At least how many bits is int?
16
At least how many bits is long?
32
At least how many bits is long long?
64
What is STL array?
STL array is fixed size array implementation from the standard library
What are the advantages of STL array over C array?
STL array doesn’t degenerate to pointer when passed to function and it knows it’s size
Where is STL array stored on the stack or on the heap?
On the stack
What does the const member function do?
1) Function declared const can be called for any object const or non const object
2) It doesn’t allow modification on the object which calls them
What is an lambda function?
Short snippets of code that
1) Not worth naming
2) Not reused
Write to list lambda function syntax?
[capture list] (parameters) -> return-type
{
method definition
}
What are lambda functions benefits?
1) They have application with generic functions like std::for_each, because it’s not appropriate to create a new class for a specific algorithm.
2) Zero cost abstraction
3) Code becomes compact, structured, expressive.
When is the notion of invariant applied?
It is central to the design of classes.
What type of language is C++ (compiled or not compiled).
Compiled.
What are the steps for generating a C++ program?
1) Source text is processed by a compiler producing object files.
2) Object files are linked together producing executable file.
Is a C++ program compiled on one computer portable on another?
No, an executable C++ programs is created for specific hardware/system combination.
When we talk about portability of C++ program what do we mean?
Portability of source code. That is the source code could be successfully compiled and run on a variety of systems.
What is statically type language?
The type of every entity (object, value, name, expression) must be known to the compiler at its point of use.
What does the type of object determine?
The set of operations applicable to it and set of possible values.
What does {} indicate in C++?
Grouping
What does // indicate in C++?
Comment
What is main function?
Every program must have exactly one main function, it’s where the program starts its execution.
What does non-zero value returned from main mean?
That the program has failed in some way.
What is a string literal?
A sequence of characters surrounded by double quotes.
What is a declaration?
A declaration is a statement that introduces a name to the program.
A declaration specifies the type of the name to inform the compiler what kind of entity the name refers to
What is an object?
An object is some memory that holds a value of some type.
What is a value?
A value is a set of bits interpreted according to a type.
What is a variable?
A variable is a named object.
What does sizeof operator do?
Determines the size of a type.
What does const mean?
Specify immutability in interfaces - “I promise not to change this value”
What does constexpr mean?
- To enable and ensure compile-time evaluation - “Evaluated at compile time”
What does switch do?
Tests a value against a set of constants.
In declarations what does [] mean?
Array of
In declarations what does * mean?
“pointer to”
In e declaration what does & mean?
“reference to”
What are &, * and [] called in a declaration?
Declarator operators.
Which types we call user defined?
Those that can be built from built-in types, const modifier, and declarator operators.
Why do we want to keep representation inaccessible to users?
1) To ease use
2) Guarantee consistent use of the data
3) Allows us later improve representation
With what is the interface of a class defined?
With public members.
How are private members accessible?
Only through a public interface.
What is the basic technique for handling varying amounts of information in C++?
A fixed-size handle referring to a variable amount of data “elsewhere” (e.g., on the free store allocated by new)
What do we call a constructor?
A function with the same name as the class.
How is a constructor different from than ordinary function for the initialization of an object?
A constructor is guaranteed to be used to initialize objects of its class. Thus, eliminates the problem of uninitialized variables for a class.
What is the purpose of enumerators?
To represent small sets of integer values. They make code more readable and less error-prone.
With what C++ represent interfaces?
With declarations.
What does declaration do?
It specifies all that’s needed to use a function or a type.
What is the link between declaration and definition?
A declaration must also have a definition somewhere but it can be in another place (In a cpp file, in library, etc.).
Why is separate compilation used in C++?
To minimize compilation time and to strictly enforce separation of logically distinct parts of a program.
What is the advantage of defining function inside a class?
It inline by default.
What does inline function mean?
It replaces function call with the function body.
What does inline function mean?
It replaces function call with the function body.
What does a!=b translate to?
operator!=(a,b).
What is a container?
An object holding a collection of elements.
How is a destructor named?
Name of the class + prefix ~
What is the purpose of destructor?
To ensure that memory allocated by the constructor is deallocated.
What is handle-to-data model?
A method used for managing data that can vary in size during the lifetime of an object.
What is the defining characteristic of concrete types?
It’s representation is part of its definition.
What is the defining characteristic of abstract class?
Implementation details are insulated from the user.
What does the word virtual mean?
“may be redefined later in a class derived from this one”
What does pure virtual function entail?
- That the class is abstract
- The function must be defined in some derived class
What is virtual function table or vtbl?
During polymorphism an object must contain extra information to allow it to select the right function to call at runtime. Each class has it’s own virtual function table. When a virtual function is called the compiler converts it into an index into a table of pointers to functions (vtbl).
What is the space and performance overhead of virtual functions?
1) Performance within 25% to normal function calls
2) Overhead one pointer per object & one vtbl for each class
When does copy elision apply?
The compiler can omit copy constructor, resulting in zero-copy pass-by-value semantics.
What is a resource?
A resource is something that must be acquired and later (explicitly or implicitly) released
What do we call a task?
A computation that can be potentially executed concurrently
When talking about the C++ standard what we mean by implementation defined?
Many things are deemed implementation-defined by the standard. Meaning each implementation must provide a specific, well-defined behavior for a constructor:
unsigned char c1 = 64 // well defined: a char has at least 8 bits
unsigned char c2 = 1256 // implementation-defined: trunctuation if a char has only 8 bits
When talking about the C++ standard what do we mean by unspecified behavior?
A range of possible behavior is acceptable, but the implementor is not obliged to specify which actually occur. Deciding for something to be unspecified is that the exact behavior is unpredictable for some reason.
What is an undefined behavior?
No reasonable behavior is required by an implementation. Ex. Accessing allocated memory out of bounds.
What is the difference between hosted and freestanding implementation?
A hosted implementation includes all the standard library facilities as described in the standard. A freestanding implementation may provide a fewer std facilities and a few headers must be provided…
What is ASCII abbreviation?
American standard code for information interchange
What do we need for x = y + f(2) to make sense?
The names x, y and f must be suitably declared. The programmer must specify that x, y, and f exist and that they are of types for which =, +, and () are meaningful.
What do we mean by name?
Every name(identifier) in a C++ program has a type associated with it. This type determines the operations that can be applied to the name and how such operations are interpreted.
Which types are called integral types?
The boolean, character and integer types are called integral types.
Which types are called arithmetic types?
The integral and floating-point types are collectively called arithmetic types.
Which types are called user-defined?
Enumerations and classes (because they must be defined by users)
Which types are called built-in types?
Fundamental types, pointers, and references.
What values can a boolean have?
True and False.
Why is boolean used?
To express the result of logical operations
Do integers implicitly convert to bool values and if so how?
Yes, nonzero integers convert to true and 0 converts to false
Do pointers implicitly convert to bools if so how?
Yes, a non-null pointer converts to true. Pointers with the value nullptr convert to false.
What does _t denote?
An alias type
What is character literal?
A character literal is a single character enclosed in single quotes
In what forms does integer type comes in?
“plain” int, signed int and unsigned int
In what sizes does integers come?
short, “plain” int, long int, long long int
What are plain ints always?
Signed
Name the integer literals?
Decimal, Octal, Hexadecimal
How many floating point types are there?
float (single precision), double (double precision), and long double (extended precision)
What is floating point literal by default?
Double
When is the void type used?
When we want to specify that a function doesn’t return a value. As a base type for pointers to objects of unknown type.
How many declarations or definitions can there be?
There can be only one definition and many declarations.
What are the five parts of a declaration?
- Optional prefix specifiers (e.g., static or virtual)
- A base type (e.g., vector or const int)
- A declarator optionally including a name (e.g., p[7], n)
- Optional suffix function specifiers (e.g., const or noexcept)
- An optional initializer or a function body (e.g., ={7,5,3} or {return x;})
For which constructs is the definitions’s value permanent?
types, aliases, templates, functions, and constants
Except for which constructs, a declaration is terminated by a semicolon?
Except for function and namespace definition.
What are the rules for C++ naming?
- A name consists of sequence of letters and digits.
- The first character must be a letter
- _ underscore is considered a letter
- Keywords cannot be used as names (e.g., class, struct…)
- Non local names starting with a an underscore are reserved
- Names starting with double underscore or underscore followed by uppercase are reserved
What do we mean by scope?
A declaration introduces a name into a scope; that is, a name can only be used in a specific part of the program text.
What is a block?
A block is a section of code delimited by {} pair.
What is a local name?
A name declared in a function or lambda.
What is class scope?
A name is called a member name ( or class member name) if it is defined in a class outside any function, class, enum class, or other namespace.
What is namespace scope?
A name is called a namespace member name if it is defined in a namespace outside any function, lambda, class, enum class, or other namespace
What is global scope?
A name is called a global name if it is defined outside any function, class, enum class, or namespace.
The scope of a global name extends from the point of declaration to the end of the file in which its declaration occurs.
What is statement scope?
A name is in statement scope if it is defined within the () part of for-, while-, if-, or switch- statement. All names in statement scope are local names.
What is function scope?
A label is in scope from its point of declaration until the end of the function.
What is shadowing?
The rule is that a declaration of a name in a block can hide a declaration in an enclosing block or a global name.
How does a hidden global name can be accessed?
With :: (ex. ::x = 2)
What is an initializer?
An initializer determines the initial value of an object.
Name the four syntactic styles of initialization with a variable of type ‘X’ with a value ‘v’.
- X a1 {v};
- X a2 = {v};
- X a3 = v;
- X a4(v);
When does the curly-brackets (“{}”) initialization appear in C++?
It appears in C++11