C++ Reinforcement Practice

Here are a set of problems designed to help you reinforce and retain some useful JavaScript knowledge. If you are an Anki or Quizlet fan, consider adding some of these questions into a deck. 😀


Who created C++ and in what year?Bjarne Stroustrup, 1983
C++ was intended to be a proper superset of what language?C
What does it mean that C++ is a systems language?It includes constructs that directly map to operating system and hardware elements, such as memory addresses (via pointers).
What does it mean that C++ is an unmanaged language?The programmer has to allocate and deallocate memory themself.
Unlike almost every other language with sophisticated modules that are imported, C++ wires together multifile programs with the weak #include directive. What exactly does #include do?It pulls in the source code of another file directly in the context of the file it is included into.
How do you write a string to standard output?
std::cout << s
What does using namespace do and why is it so common in C++?It allows you to omit the namespace prefix when using entities from the namespace. It is common since everything in the standard libary comes from the std namespace, so using std saves a lot of code clutter.
Show how to compile and run hello.cpp from the command line, using the g++ compiler, using the 2017 standard version of the language.
g++ -std=c++17 hello.cpp && ./a.out
C++17 has six different built-in character types. What are they? There is one new character type added in C++20. What is it?In C++ 17: char signed char unsigned char char16_t char32_t wchar_t. Added in C++ 20: char8_t.
Integers can be signed or ________________. They can be short, regular-sized, or ________________.unsigned, long
In addition to float and double, C++ has one other floating-point type. What is it?long double
You might see int& used where a type should appear (say, in a variable or parameter declaration). One can argue this is not a type at all! What does this expression mean, and why do we say it is not a real type?It simply means the declared name aliases an existing entity. The existing entity has type int. Nothing has type int&, there are only ints.
Is void a type? If not, what is it?Well, no entities have type void. I suppose you could call it “the type with no elements” or “the return type of functions that don’t return anything.” Or you can call it a “pseudo-type.” Reasonable minds can probably disagree here.
Give three different syntaxes to declare an immutable integer variable named dozen initialized to the value 12.
int dozen = 12;
int dozen(12);
int dozen {12};
What are the types of the expressions '$', u'$', and U'$'?char, char16_t, char32_t.
Do the types int, float, and double have fixed bit sizes? If so, what are they? If not, why not?No, their size depends on the hardware.
Given struct Point {int x; int y}; how do you declare a variable p of type Point whose x and y fields are initialized to 5 and 8, respectively?Point p {5, 8};
Do structs have value semantics or reference semantics? What about classes?Both have value semantics.
Suppose S is a struct type whose instances are very large. How do you define a parameter of type S for a function in which the S object will not be changed?const S& s
Since structs are copied when passed as parameters, how can you write a function that mutates a struct?Declare the parameter as a reference, or a pointer.
C++ references, defined using & are really just aliases and nothing more. True or false?True!
If we define both void f(int& x) {} and void f(int&& x) {} when would the first function be called and when would the second?
Can the function int f() {int x=2; int& y = x; return y;} cause a dangling pointer when called? Why or why not?
Can the function int* f() {int x=2; int* y = &x; return y;} cause a dangling pointer when called? Why or why not?
Given struct Point {int x; int y;} is it legal to define and initialize a variable like this: Point p = new Point {1,2}? Why or why not?
What things go in static storage? Stack storage? Heap storage?
Every new must be matched with a what?
What is the keyword for the expression meaning a “pointer that doesn’t point to anything”?
What do you have when you allocate a pointer with new but forget to deallocate it?
What do you have when a pointer has been deallocated but you try to dereference it?
If we define void inc(int* x) {(*x)++;}, and we wanted to use this function to increment a global variable y, how would we call the function?
It’s possible, but unusual, to write (*p).x. What do we write instead?
What is the expression for “a pointer to x”?
Explain what type x is given in the declarator char ***x[13][21].
Explain what type c is given in the declarator int (*c)(long, long).
What is the difference between an instance method and a class method?
Python uses the @classmethod decorator to mark a class method. How do you mark a class method in C++?
Why are constructors not considered to be methods?
If you don’t write a constructor for a struct, can you still create an instance of the struct type and supply initial values to its fields? If so, how does this work?
If you write a constructor for a struct type S that takes in a single argument, and no other constructor, can you define a variable like so: S s;. Why or why not? Can you define S* p? Why or why not?
Why would you never see the expression this.x in C++?
If you had an instance x of a struct type S, and no constructors were explicity defined for S, what does S y(x); do? Use the terms “construct,” “copy,” and “shallow” in your answer.
If you had two instances x and y of a struct type S, and no methods were explicity defined for S, what does x == y; do? What, technically speaking, is invoked here?
Suppose you were writing your very own Set class, for mathematical sets. Show how to declare (just declare, do not define) operators for intersection (use &), union (use |) and complement (use ~).
We know that cout is an instance of the class ostream. How did the creator of the ostream class get expressions like cout << x to work?
What are the three explicit access control modifiers for structs (and classes)?
What is the only difference between structs and classes in C++?
Although the difference between structs and classes is incredibly trivial, many C++ programmers use a particular rule of thumb to choose between them. What is this rule?
What are three nice things about statically typed languages?
What does C++ call its equivalent of Java generic types?
C++ has a module called that defines a template called list. So to use lists, you have to #include . But list is actually in a namespace as well. Which one?
Is list a class in C++? If not, what is it?
One of the goals of C++ was to make user-defined classes work just like the built-in types, in particular, that they have value semantics (a.k.a copy semantics). Why then are structs, or classes, with pointer members potentially worrisome?
C++ tries to do a good job of hiding the details of the members of classes: generally speaking, users of a class don’t know what’s inside objects of the class, they only know about the methods—how the objects behave. In particular, clients don’t (or shouldn’t) know if there are any pointer members! So users cannot directly free up encapsulated pointers! This fact led to C++ having what kind of special class member?
Which five members comprise the Rule of Five?
What is a copy constructor supposed to do? What happens to the source of the copy?
What is a move constructor supposed to do? What happens to the source of the move?
What is the first thing you should do when overloading an assigment operator (either a copy assignment or a move assignment)?
What should an assignment operator return? Why?
What does = default do?
What does = delete do?
When does the compiler choose a move constructor over a copy constructor?
When are unique_ptr objects deleted?
When are shared_ptr objects deleted?
Why can’t unique_ptr obejcts be copied?
Even though C++ is all about helping you define your own classes with copy semantics, reference semantics is available if you want it. What language feature provides this?
How are raw C-Style arrays different from arrays in almost every other language (including Java, JavaScript, Swift)?
If a is a raw C-Style array, why is it okay to use the expression 3[a]?
When writing a function to process a C-style array, it is generally not sufficient to pass only the array. What else must be passed and why?
When writing a function to process a C-style array, the array contents can be modified simply by passing the array; there is no need to pass the array as &a or for the parameter to be a reference parameter! Why do C-style arrays behave so differently from everything else in the language?
How is a C-style array allocated on the stack?
How is a C-style array allocated on the heap? How is it deallocated in this case?
If a and b are C-style arrays, what is the meaning of a = b?
How are std::array objects different from std::vector objects?
Define a 100-element std::array of doubles.
Give expressions to get, for a vector v, (1) the first element, (2) the last element, (3) the 5th element, and (4) the number of elements.
How do you add an element to the end of a vector?
How do you insert an element into a vector between its 7th and 8th elements?
How do you empty out a vector (remove all its elements and set its size to 0)? (Hint: the .empty() method does not do it!
What is a valarray and why is it so cool?
C++, because it is a superset of C, inherits C’s weird “strings.” What is the actual type of these weird things?
Do C-strings “know” their own length? Can the length be computed? If so, how?
If s and t are C-strings, assignment s = t does not copy the contents of t into s. How do you, then, copy each character?
If s and t are std::strings, does assignment s = t copy each character?
What are the types u16string and u32string for?
C++ programs have to have a main function accepting the command line arguments. Are the arguments represented as a std::vector of std:string objects? If not, how are the arguments passed? Why?
How do you get the address of an lvalue?
How do make an object (instance of a struct or class) be callable (essentially, a function object)?
Give a C++ lambda for a function accepting a string and a number and returning whether the string has a length greater than the number.
What is the meaning of a lambda whose capture is [=]? What is the meaning of a lambda whose capture is [&]. What about []?
How do you fill the first 100 elements of the C-style array a of integers with 0s? (Use std::fill.)
How do you fill the first 100 elements of the std::array a of integers with 0s? (Use std::fill.)
Write an expression that computes whether every element of the integer vector v is less than 10. (Use std::all_of.)
Write an expression that gives the first even number in the vector of longs v. (Use std::find_if, and state what happens if there are no even numbers in the vector.)
Given a vector of integers, v, write an expression to produce a new vector containing the squares of each of the elements in v. This operation would be called map in JavaScript, Python, Haskell, and most other languages, but it’s called something else in C++. Hint: the function you must use starts with t-r-a.
What Swift and Python call a dictionary, C++ calls a ________________.map
Unlike Java, which marks classes abstract, C++ simply says a class is abstract iff it has a pure virtual function. How are pure virtual functions declared?
C++ does not use the terms superclass and subclass. What does it use instead?Base type and derived type
Suppose class Dog was a derived class of class Animal. Can you assign a variable of list<Dog> to a variable of list<Animal>? If not, why not?
Suppose class Dog was a derived class of class Animal. Suppose both Dog and Animal define a speak method. If we defined Animal a = Dog(); a.speak(), which speak would be called, the animal one or the dog one?
Suppose class Dog was a derived class of class Animal. Suppose both Dog and Animal define a speak method. If we defined Animal* a = new Dog(); a->speak(), which speak would be called, the animal one or the dog one?
What is the difference between a declaration and a definition?