ECS 34 Final Review

Please fill out this survey to let us know your thoughts on this course. Your thoughtful feedback allows us to improve future offerings of this course. Thanks!
This is not meant to be a comprehensive review of everything you need to know for the final. Please study everything (lectures, problem sets, projects, etc).
Exam info: The final exam will be on Tuesday, March 18, 2025, from 6pm-8pm. You may bring one 8.5x11" sheet of notes (front and back).
Completed 0 of X questions

Q1: Given the following code, what function must be implemented by class C1?

int main(){
    C1 Obj;
    double D = Obj(7);
    return 0;
}
    

Q2: Given the code below, at the specified line what memory sections are A[0], B[1], and B in? Justify your answer.

int main(){
    int A[] = {1, 2, 3};     
    int *B = new int[2]{9,2};   
    A[0] = B[1]; // At this line
    delete [] B;
    return 0;
}
    

Q3: What is the advantage of doing Test Driven Development (TDD)? Doesn’t it just slow development by requiring tests be written first?

Q4: Given the following weighted undirected graph, identify the edges that are part of the minimum spanning tree and calculate its total weight.

Graph with weights

Q5: The code below doesn’t compile when B is assigned 21 but compiles if 21.0 is used. What is the problem, and why does it compile one way but not the other?

Q7: A software developer likes Dijkstra’s algorithm but has a graph that has negative weights (no negative cycles). The developer decides that they can just add the negative of the smallest weight to all edges and run Dijkstra’s (e.g. if smallest was -3, 3 would be added to every edge), this way the graph won’t have any negative edges. Will this approach always work, why or why not?

Q8: The following code compiles for any type T except for bool. Why won’t it compile for bool?

Q9: Given the following snippet of a Makefile, draw a Directed Acyclic Graph that represents all the files specified and which depend upon one another. Directed edges should go out from the files that are depended upon.

PROG: ABC.o DEF.o
    g++ ABC.o DEF.o -o PROG
ABC.o: ABC.cpp ABC.h
    g++ ABC.cpp -c -o ABC.o
DEF.o: DEF.cpp ABC.h
    g++ DEF.cpp -c -o DEF.o
    

Q10: Given the code below, what will the value of y be and why?

int x = 3, y = 4;
auto foo = [x, &y](int a) {
    y += x + a;
};
foo(5);
    

Q12: If you need to build the interstate highway network as cheaply as possible, how would you build the network?

Q13: How do you overload the function call operator (operator()) in C++? Provide an example signature.

Q14: How do you allocate and deallocate a dynamic array of 10 integers in C++? Provide code snippets.

Q15: What is the difference between std::unique_ptr and std::shared_ptr in C++? Provide a brief example of each.

Q16: Write a simple function template that returns the maximum of two values.

Q17: How do lambda expressions in C++ capture variables by reference and by value? Provide a simple example.

Q18: What is a topological sort and in what type of graph is it applicable?

Q19: What is Breadth-First Search (BFS)? Give one example of application of BFS.

Q20: Describe Depth-First Search (DFS) and one application where it is particularly useful.

Q21: Briefly explain the concept of a Minimum Spanning Tree (MST) in a graph and name one algorithm used to find it.

Q22: What is a key limitation of Dijkstra’s algorithm and for what kind of graphs is it best suited?

Q23: How can you detect if a directed graph contains a cycle? Name one method or algorithm.

Q25: How would you overload the + operator for a class representing a 2D vector? Provide a brief code snippet.

Q26: How would you overload the << operator to output a custom class (e.g., a Point class) to an output stream?

Q28: How do you declare a pointer to a function that takes two ints and returns an int? Provide the syntax.

Q31: Why should a class with virtual functions also have a virtual destructor?

Q32: What is a pure virtual function in C++ and what effect does it have on a class?

Q33: Write a short C++ code snippet demonstrating exception handling using try, catch, and throw.

Q36: How would you overload the [] operator for a class that wraps an array? Provide an example signature.

Q40: Give an example of a real-world problem that can be modeled using a Directed Acyclic Graph (DAG) and solved with a topological sort.

Question 2: What is the difference between a relative path and an absolute path in a UNIX file system?

Question 3: Which UNIX command is used to change the permissions of a file?





Question 4: What does the UNIX command grep do?

Question 5: How do you redirect the output of a command to a file, and how do you pipe the output of one command into another command?

Question 6: In a UNIX shell, what happens when you press Ctrl+Z on a running process, and how can you resume it later?

Question 7: Consider the following shell script. What will it do when executed?

#!/bin/bash
for f in *.txt; do
    echo $f
done

Question 8: How can you make a shell script (for example, script.sh) directly executable from the command line?

Question 12: Which Git command is used to stage changes (add modified or new files) so that they will be included in the next commit?

Question 13: What is the purpose of unit testing in software development?

Question 14: Which of the following tools is specifically used to detect memory leaks and memory errors in C/C++ programs?





Question 16: How would you allocate an array of 10 integers on the heap in C?

Question 17: How do strings in C (character arrays) indicate the end of the string?

Question 18: Write a C struct definition for a Student that has two fields: a name (string) and a GPA (floating-point).

Question 19: In a C/C++ program, what's the difference between including a header with #include <...> and #include "..."?

Question 20: What is a memory leak in the context of C/C++ programming, and how can it be prevented?

Question 23: What is polymorphism in C++ and how do virtual functions enable it?

Question 26: What is encapsulation in object-oriented programming, and how does C++ support it?

Question 27: Briefly explain the difference between public inheritance and private inheritance in C++.

Question 28: Explain what happens when you compile and link a C++ program that consists of multiple source (.cpp) files and header files.

Question 30: What is a template in C++ and give an example of a simple function template.

Question 28: How do you access command line arguments in C++? Give example code.