Interview Questions

C++ Interview Questions

From CS247 midterm prep:

Short Questions

  1. Describe the conditions under which a method call uses the static type vs when it uses the dynamic type.
    • If the method call is on an object, it will always use the static type
    • If the method call is on a reference or a pointer, it will:
      • use the dynamic type if the method is virtual
      • use the static type if the method is non-virtual
  2. Explain why we prefer to put implementations in a .cc file vs in a .h file.
    • Because every time we modify the .h file, the linker needs to recompile every .cc file that includes that .h file, whereas if the implementation in is a .cc file, the compiler only needs to recompile that particular .cc file. See Compilation (C++)
  3. Describe at a high level how a templated class is compiled.
    • At compile time, for each use of class<T>, the compiler makes a copy of the class with type T substituted as necessary. Then, it compiles as usual.
  4. Define in your own words what an invariant is.
    • It’s something about a class / ADT that we expect to always be true
  5. Describe the steps of the Object Construction sequence. How do they relate to those of the Object Destruction sequence?
    1. Space is allocated
    2. Parent Constructor is called
    3. MIL called to initialize fields
    4. Constructor Body runs
    • For destructor, everything runs in reverse order. The fields are destructed in reverse order that they were declared in.
  6. What is the difference between Overloading and Overriding?
    • When we are overloading a function, we are providing multiple implementations of the same function for a different number and types of arguments.
    • When we are overriding a function, we are replacing the function of the superclass with a new implementation
  7. NOT SURE Give two differences between overloading operators as a method vs. as a standalone function.
    1. When you overload operator as a method, you don’t need to worry about declaring as a friend, as the overloaded method has access to the private fields
    2. The signature of the function changes (differing number of arguments if inside the class, you only need the RHS, since *this is the LHS)
  8. Give one example where an operator overload has to be defined as a standalone function, and one where it has to be defined as a method.
    1. Standalone function: istream& operator>>(istream&, int x) {}
    2. Method: operator=
  9. Explain the different situations where we would prefer pass-by-value, pass-by-reference, and pass-by constant-reference.
    1. Pass-by-value when you wish to make a copy of the value passed
    2. Pass-by-reference when you wish to mutate the original variable
    3. Pas-by-const-reference when wish to pass a reference to the original variable, but not allow modification
  10. Give the definition of an lvalue, give the definition of an rvalue.
    1. lvalue: variable that has a memory address associated with it
    2. rvalue: variable that does not have a memory address
  11. Describe the mechanism behind the copy-swap idiom, and why one one may wish to use it.
    1. copy constructor and copy assignment operator may be very similar. Therefore, one should probably use the copy and swap idiom:
Node(const Node& n): data{n.data}, next{n.next == nullptr ? nullptr : new Node{n.next}} { }

Node& operator=(const Node& n) {
	Node tmp{n};
	swap(data, n.data);
	swap(next, n.next);
	return *this;
}
  1. For a container class C, list what methods must be defined to implement an iterator for C.
    • begin() and end() methods
    • For the iterator: operator++ and operator!=
  2. List two advantages to using a Makefile for build automation
    1. Automatic linking
  3. Explain differences between the waterfall and agile methodology of software development.
    1. waterfall: the client gives specifications, and the engineers build out the specifications
    2. agile: It is an interactive process comprised of sprints, where the work is done in 1-2 weeks
  4. Describe the conditions that indicate an owns-a relationship, and a has-a relationship
    • owns a is indicated a solid diamond, called Composition. If A dies, B dies. Usually implemented via member fields
    • has-a is indicated with an empty diamond, also called Aggregation. If A dies, B lives on. Usually implemented via references or non-owning pointers
    • (3rd) is-a is indicated with an empty arrow, also just called Specialization. If B is-a A, then anywhere where we expect A, we can replace with B. Usually implemented via inheritance.

Coding Questions

2 Coding Questions

  1. Implement the Big 5 for a doubly linked list. Do so using a Node with fields Node* next and Node* prev.
  2. Transform this implementation to be well encapsulated via a DoublyLinkedList class that only exposes public methods.
  3. Having implemented the big 5 for the doubly linked list, implement a bi-directional iterator. A bidirectional iterator allows both ++it to take you to the next element, as well as –-it to take you to the previous element.
  4. Implement a reverse iterator for your linked list. Reverse iterators can be created using rbegin() and rend(). They start at the end of the data structure, and ++it takes them to the previous element.
  5. Consider a class RepeatedString, which contains a string s and a positive integer k. For a RepeatedString rs, implement overloads so that the following code performs the desired behaviour:
    1. A single constructor that allows both default construction (with s set to the empty string and k set to 0), as well as construction given the string s and int k
    2. cout << rs which prints the string rs.s k times.
    3. cin >> rs which should read in a string s
    4. For integer j, rs * j and j * rs which creates a RepeatedString with the same string, and associated integer k ∗ j.
  6. Consider creating a chess game. Consider how you may represent different pieces using an inheritance hierarchy. Each piece should have a column and row field that range from 0 to 7, and also a 2D board that allows them to determine whether a piece is occupied at a particular location. Implement a virtual method getMoves that returns a vector of possible coordinates a given piece could move to. Do so for each possible piece on the chessboard, keeping in mind that shared logic should be abstracted into the base class where possible.

Question Part 2 (from finals of CS247)

1 Short Answer Questions

  1. Describe the difference between virtual and multiple inheritance
    1. Virtual inheritance is about ensuring that only one copy of the base class is made by the derived grandchild classes
    2. Multiple Inheritance is the idea that a derived class can inherit from multiple base classes
  2. Describe how the compiler provided big 5 operate when using inheritance
    • Suffers from partial assignment problem (do to no virtual keyword), and mixed assignment problem (due to virtual keyword, and overloaded in child). Fixed by using Abstract Base Class, and protected operator=
  3. Describe the conditions necessary to overload a method
    • Different number of arguments or different type of arguments
  4. Explain some ways of performing error handling apart from using exceptions, and give their drawbacks
    1. Using sentinel values: Hard to determine a valid sentinel value for other types
    2. Using global variables: Limited # of errors, and might be overwritten
  5. Define stack-unwinding. How does it relate to the concept of RAII?
    1. Stack unwinding is used for exception handling, to find the appropriate handler. Stack unwinding ensures that the destructors are called for all objects constructed within the scope of the try block, which ensures proper resource cleanup. This aligns with RAII by managing resources and calling the necessary cleanup code automatically.
    2. RAII means that an object’s lifecycle is tied to its resource management. When object goes out of scope, the destructor is automatically called
  6. Why do we re-throw exceptions with “throw” rather than “throw e”?
    1. Because we lose the dynamic type when we copy construct.
  7. What type of error occurs when copying a unique_ptr? Run-time or compile-time? Why?
    1. Compile-time, because the copy constructor is disabled using the = delete keyword
  8. What is the purpose of employing design patterns?
    1. To have reusable solutions to common problems
  9. Draw the UML for each of the design patterns discussed in the course: observer, decorator, strategy, visitor, adapter, command.
    • DO on paper
  10. List the four types of casts in C++, and explain the purpose and limitations of each. Why do we prefer C++ casts over C-style casts?
    • static_cast: conversion between well-defined types
    • dynamic_cast: conversion between polymorphic types in an inheritance hierarchy
    • const_cast: remove constness
    • reinterpret_cast: Allows for poorly defined implementation-dependent casts
  11. Explain why dynamic_cast only works for classes with at least one virtual method defined.
    • For dynamic dispatching to work, it needs to look for the vtable
  12. Give an example of a program design that is highly cohesive but also highly coupled. Give an example of a program design that is lowly coupled but also lowly cohesive. 1.
  13. List the 5 SOLID design principles. Give a brief description of each principle, as well as the benefits of applying the principle to your code.
    1. Single Responsibility Principle: Each class should only implement one core functionality
    2. Open-Closed Principle: Each class should be open to extension, but closed to modification
    3. Liskov Substitution Principle: Substituting a superclass object with a derived subclass object should still keep the program in a valid state
    4. Interface Segregation Principle: Classes should only depend on methods that they use, else if creates needless Coupling
    5. Dependency Inversion Principle: High-level modules should not depend on low-level modules - instead, they should depend on an abstraction
  14. Give an example of a design that does not follow the Open/Closed principle. How could the design be adjusted to follow the principle?
  15. Explain the purpose of the template method pattern. Give an example of how you could rewrite a program to follow the template method pattern.
  16. Explain how private/protected inheritance work
    1. Private: all public and protected members become private
    2. protected: all public and protected become protected
  17. Assume there is a class A with some fields, and no virtual methods. B inherits from A, with some fields of it’s own. Draw a memory diagram for a B object. How does it change when you introduce a virtual method to A? How does it change when you introduce multiple inheritance? Multiple virtual inheritance?
  18. Give two uses of the visitor pattern.
    1. Method that depends on 2 polymorphic types
    2. Add additional functionality, without modifying the classes themselves
  19. Give two uses of the CRTP pattern. What special considerations are needed when using it in a pre-established inheritance hierarchy?
    1. Reduce boilerplate code while maintaining compile time polymorphism
    2. Enable polymorphic cloning
  20. Define the 4 levels of exception safety guarantee
    1. No exception guarantee: No guarantees if exception is thrown, program might or might not crash
    2. Basic Guarantee: If exception is thrown, program is valid, but unspecified state
    3. Strong Guarantee: If exception is thrown, program is valid, and program reverts to state prior to method being run
    4. Nothrow guarantee: Exceptions are never propagated outside of the function call
  21. What is the benefit to labelling methods noexcept? If a method is noexcept, does that mean it necessarily provides the nothrow guarantee?

2 Coding Questions

  1. Write a full implementation of shared_ptr. It should have a default constructor, a constructor taking in a ptr of type T, and support all big 5 operations, as well as an overloaded dereference operator.
  2. A class that defines operator< can define the other methods in terms of the < operator. For example: (a > b) is equivalent to (b < a). (a == b) is equivalent to !(a < b) && !(b < a). Write a CRTP class Compareable. Compareable should add the various comparison operators to a class that supports operator<. Give an example of a class that implements operator< and show how Compareable can be used with it. Explain why one might prefer this over an abstract class “AbstractCompareable” that defines the various comparison operators in terms of <, and defines operator< as a virtual method to be overridden in the subclass.
  3. Consider making the game of Chess. Mock up an inheritance hierarchy in UML where a computer class uses the strategy pattern to make moves depending on the difficulty level of the AI. Implement this in C++.
  4. Consider a hierarchy representing a file system. We could have a superclass Entry, then subclasses for things like Files, Directories, links, network devices, etc. Write a hierarchy including at least Entry, File, and Directory, with an accept method taking in a visitor. Write visitors to count the number of bytes in a directory/file, and one to print out all the file names in a directory recursively.
  5. Consider performing manipulations to a string via the command pattern. You start with some initial string s, then have different commands such as Slice (which returns a substring of s given some starting and ending index), Insert (which inserts a string t into s at some index), and Repeat (which causes s to repeat n times). Implement these commands via the Command pattern, along with an Invoker and the ability to undo each command. Try implementing it in the simple way (the invoker simply keeps a history of the state of the string in-between each command) and in the memory efficient way (the commands have code and extra fields to undo their action).
My own questions
  1. What is Forward Declaration?
    • This is when you only declare classes and functions, without the definition, so that the compiler only needs about the declaration. During the linking stage, the linker will figure out where the implementation of the functions.
  2. Give the 4 steps of construction
  3. Give the 4 steps of destruction

Other Questions

From TRON25 list

  1. Explain how Smart Pointers work in C++ and what their use/attributes are (unique pointers, shared pointers)
  2. What are access specifiers in C++?
  3. Difference between static and shared libraries in C
  4. Name all the C++ containers you know and how to use them
  5. Demonstrate (live on a code editor) how inheritance works in C++
  6. Difference between pass-by-reference and pass-by-value in C++ and when would you use either
  7. What is the purpose of the static keyword in C
  8. Can static variables be accessed by another file?
  9. How would you dynamically create a new object?
  10. What is a pure virtual function and how is it related to polymorphism?
  11. Why should implementations be restricted to source files? Are there potential pitfalls if not?
  12. What is memory corruption and leaks. What debugging tools do you use to find such problems?
  13. Create a virtual class  function and pure virtual class function, explain the difference and usages of both

From Reddit

  1. Understand the differences between stl container (std::vector) vs stl aggregates (std::array)
  2. The cost of dynamic memory allocation
  3. The cost of dynamic polymorphism
  4. Moving vs copying vs passing reference

IndiaBix Test

https://www.indiabix.com/online-test/cpp-programming-test/251