Skip to content
LAM
Read Home Blog
Make Projects HTML Tools Games
Touch grass Notes Resume Links
Home Blog HTML Projects
Tools Games Notes Resume Links
Back CSE 240 Final Exam Guide Programming
Download Open
Show description 1,752 chars · Programming

CSE 240 Final Exam Guide

CSE 240 Final Exam Guide








CSE 240 Final Exam Mastery Guide

Your complete guide to C++, Scheme, and Prolog.






C++ (OOP)
Scheme (Functional)
Prolog (Logic)





C++: Object-Oriented Programming (~20%)


Class Relationships: `is-a` vs. `has-a`

This is the fundamental design choice in OOP. Getting this right is crucial.


"is-a" (Inheritance): Used when a class is a more specific version of another. A `Book` is-a `Publication`. This is for code reuse and polymorphism. Modeled with `public` inheritance.




class Publication { ... };
class Book : public Publication { ... }; // A Book "is-a" Publication



"has-a" (Containment/Composition): Used when a class is composed of, or contains, other objects. A `Car` has-a `Engine`. Modeled by including an object as a member variable.




class Engine { ... };
class Car {
private:
Engine myEngine; // A Car "has-a" Engine
};



Polymorphism & Virtual Functions

Polymorphism allows derived class objects to be treated as base class objects. The `virtual` keyword is the key to enabling this at runtime.


A function declared `virtual` in a base class can be **overridden** in a derived class. C++ will determine which version to call at runtime based on the object's actual type.




class Publication {
public:
virtual void display() { ... } // Can be overridden
};

class Book : public Publication {
public:
void display() override { ... } // Overrides the base version
};

// Usage
Publication* pub = new Book();
pub->display(); // Calls Book's display() at runtime



Critical Exam Fact: If you intend to `delete` a derived object through a base class pointer, you **MUST** declare the base class destructor as `virtual`.…

CSE 240 Final Exam Guide

18,612 bytes · HTML source
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSE 240 Final Exam Guide</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Inter', sans-serif;
            background-color: #111827;
            color: #D1D5DB;
        }
        .tab-btn {
            transition: all 0.3s ease;
            border-bottom: 2px solid transparent;
        }
        .tab-btn.active {
            color: #38BDF8;
            border-bottom-color: #38BDF8;
        }
        .content-section {
            display: none;
        }
        .content-section.active {
            display: block;
        }
        .code-block {
            background-color: #1F2937;
            border-radius: 0.5rem;
            padding: 1rem;
            margin-bottom: 1.5rem;
            overflow-x: auto;
            font-family: 'Courier New', Courier, monospace;
            font-size: 0.9em;
            border: 1px solid #374151;
        }
        .code-block .comment { color: #6B7280; }
        .code-block .keyword { color: #93C5FD; }
        .code-block .class-name { color: #A78BFA; }
        .code-block .function-name { color: #60A5FA; }
        .code-block .string { color: #FBBF24; }
        .code-block .number { color: #A3E635; }
        .code-block .operator { color: #F472B6; }
        h2 {
            font-size: 1.875rem;
            font-weight: 700;
            color: #F9FAFB;
            border-bottom: 1px solid #374151;
            padding-bottom: 0.5rem;
            margin-bottom: 1.5rem;
        }
        h3 {
            font-size: 1.5rem;
            font-weight: 600;
            color: #E5E7EB;
            margin-top: 2rem;
            margin-bottom: 1rem;
        }
        .fact-box {
            background-color: #1F2937;
            border-left: 4px solid #38BDF8;
            padding: 1rem;
            margin: 1.5rem 0;
            border-radius: 0.25rem;
        }
        .fact-box p {
            margin: 0;
        }
    </style>
</head>
<body class="antialiased">

    <div class="container mx-auto p-4 md:p-8">
        <header class="text-center mb-10">
            <h1 class="text-4xl md:text-5xl font-bold text-white">CSE 240 Final Exam Mastery Guide</h1>
            <p class="text-lg text-gray-400 mt-2">Your complete guide to C++, Scheme, and Prolog.</p>
        </header>

        <!-- Tabs Navigation -->
        <div class="flex justify-center border-b border-gray-700 mb-8">
            <button class="tab-btn active text-lg font-semibold px-6 py-3" onclick="showTab('cpp')">C++ (OOP)</button>
            <button class="tab-btn text-lg font-semibold px-6 py-3" onclick="showTab('scheme')">Scheme (Functional)</button>
            <button class="tab-btn text-lg font-semibold px-6 py-3" onclick="showTab('prolog')">Prolog (Logic)</button>
        </div>

        <!-- C++ Content -->
        <div id="cpp" class="content-section active">
            <h2>C++: Object-Oriented Programming (~20%)</h2>
            
            <h3>Class Relationships: `is-a` vs. `has-a`</h3>
            <p>This is the fundamental design choice in OOP. Getting this right is crucial.</p>
            <div class="fact-box">
                <p><strong>"is-a" (Inheritance):</strong> Used when a class is a more specific version of another. A `Book` is-a `Publication`. This is for code reuse and polymorphism. Modeled with `public` inheritance.</p>
            </div>
            <div class="code-block">
                <pre><span class="keyword">class</span> <span class="class-name">Publication</span> { ... };
<span class="keyword">class</span> <span class="class-name">Book</span> : <span class="keyword">public</span> <span class="class-name">Publication</span> { ... }; <span class="comment">// A Book "is-a" Publication</span></pre>
            </div>
            <div class="fact-box">
                <p><strong>"has-a" (Containment/Composition):</strong> Used when a class is composed of, or contains, other objects. A `Car` has-a `Engine`. Modeled by including an object as a member variable.</p>
            </div>
             <div class="code-block">
                <pre><span class="keyword">class</span> <span class="class-name">Engine</span> { ... };
<span class="keyword">class</span> <span class="class-name">Car</span> {
<span class="keyword">private</span>:
    <span class="class-name">Engine</span> myEngine; <span class="comment">// A Car "has-a" Engine</span>
};</pre>
            </div>

            <h3>Polymorphism & Virtual Functions</h3>
            <p>Polymorphism allows derived class objects to be treated as base class objects. The `virtual` keyword is the key to enabling this at runtime.</p>
             <div class="fact-box">
                <p>A function declared `virtual` in a base class can be **overridden** in a derived class. C++ will determine which version to call at runtime based on the object's actual type.</p>
            </div>
            <div class="code-block">
                <pre><span class="keyword">class</span> <span class="class-name">Publication</span> {
<span class="keyword">public</span>:
    <span class="keyword">virtual void</span> <span class="function-name">display</span>() { ... } <span class="comment">// Can be overridden</span>
};

<span class="keyword">class</span> <span class="class-name">Book</span> : <span class="keyword">public</span> <span class="class-name">Publication</span> {
<span class="keyword">public</span>:
    <span class="keyword">void</span> <span class="function-name">display</span>() <span class="keyword">override</span> { ... } <span class="comment">// Overrides the base version</span>
};

<span class="comment">// Usage</span>
<span class="class-name">Publication</span>* pub = <span class="keyword">new</span> <span class="class-name">Book</span>();
pub-><span class="function-name">display</span>(); <span class="comment">// Calls Book's display() at runtime</span></pre>
            </div>
             <div class="fact-box">
                <p><strong>Critical Exam Fact:</strong> If you intend to `delete` a derived object through a base class pointer, you **MUST** declare the base class destructor as `virtual`. This ensures the derived destructor is called, preventing memory leaks.</p>
            </div>
             <div class="code-block">
                <pre><span class="keyword">class</span> <span class="class-name">Publication</span> {
<span class="keyword">public</span>:
    <span class="keyword">virtual</span> <span class="function-name">~Publication</span>() {} <span class="comment">// Virtual destructor is essential!</span>
};</pre>
            </div>

            <h3>Exception Handling</h3>
            <p>The `try`/`catch` mechanism provides a safe way to handle runtime errors.</p>
            <div class="fact-box">
                <p>A `throw` statement is matched to a `catch` block based on the **data type** of the thrown value, not its name or content.</p>
            </div>
             <div class="code-block">
                <pre><span class="keyword">try</span> {
    <span class="keyword">if</span> (errorCondition) {
        <span class="keyword">throw</span> std::<span class="function-name">runtime_error</span>(<span class="string">"A specific error occurred!"</span>);
    }
}
<span class="keyword">catch</span> (<span class="keyword">const</span> std::<span class="class-name">runtime_error</span>& e) {
    <span class="comment">// This block executes because the data types match.</span>
    std::cerr &lt;&lt; e.<span class="function-name">what</span>() &lt;&lt; std::endl;
}</pre>
            </div>
        </div>

        <!-- Scheme Content -->
        <div id="scheme" class="content-section">
            <h2>Scheme: The Functional Paradigm (~40%)</h2>
            
            <h3>Core Philosophy: "What, Not How"</h3>
            <p>Scheme focuses on composing functions and evaluating expressions. The goal is to avoid side effects (modifying state outside a function's scope).</p>
            
            <h3>Syntax & Core Forms</h3>
            <div class="fact-box">
                <p><strong>Prefix Notation:</strong> The operator always comes first inside the parentheses. Example: `(+ 1 2)`.</p>
            </div>
            <div class="fact-box">
                <p><strong>`let` is Local, `define` is Global:</strong> Use `let` to create temporary, local names. Use `define` for top-level, global functions and constants. A `let` is just syntactic sugar for an immediately-invoked `lambda`.</p>
            </div>
            <div class="code-block">
                <pre><span class="comment">; let form for local scope</span>
(<span class="keyword">let</span> ((x <span class="number">5</span>)
      (y <span class="number">10</span>))
  (<span class="operator">+</span> x y)) <span class="comment">; => 15</span>

<span class="comment">; Equivalent lambda form</span>
((<span class="keyword">lambda</span> (x y) (<span class="operator">+</span> x y)) <span class="number">5</span> <span class="number">10</span>)</pre>
            </div>

            <h3>Data Structures: Pairs & Lists</h3>
            <p>The fundamental data structure is the pair (`cons` cell), which is used to build lists.</p>
            <div class="fact-box">
                <p>A list is a chain of pairs ending in the empty list `'()`. The empty list is the **only** list that is not a pair.</p>
            </div>
            <div class="code-block">
                <pre><span class="comment">; car: first element. cdr: rest of the list.</span>
(<span class="keyword">car</span> '(<span class="number">1</span> <span class="number">2</span> <span class="number">3</span>)) <span class="comment">; => 1</span>
(<span class="keyword">cdr</span> '(<span class="number">1</span> <span class="number">2</span> <span class="number">3</span>)) <span class="comment">; => '(2 3)</span>

<span class="comment">; Building lists</span>
(<span class="keyword">cons</span> <span class="number">1</span> '(<span class="number">2</span> <span class="number">3</span>))         <span class="comment">; => '(1 2 3) (adds one element)</span>
(<span class="keyword">list</span> <span class="number">1</span> <span class="number">2</span> <span class="number">3</span>)          <span class="comment">; => '(1 2 3) (creates from elements)</span>
(<span class="keyword">append</span> '(<span class="number">1</span> <span class="number">2</span>) '(<span class="number">3</span> <span class="number">4</span>)) <span class="comment">; => '(1 2 3 4) (joins lists)</span></pre>
            </div>

            <h3>Recursion & Higher-Order Functions</h3>
            <p>Recursion is the primary way to iterate in Scheme. Higher-order functions operate on other functions, enabling powerful abstractions.</p>
            <div class="fact-box">
                <p><strong>Tail Recursion</strong> is Scheme's version of an efficient loop. The recursive call is the final action, allowing the compiler to optimize it and prevent stack overflow. This often requires an "accumulator" parameter.</p>
            </div>
             <div class="code-block">
                <pre><span class="comment">; Tail-recursive factorial with an accumulator</span>
(<span class="keyword">define</span> (<span class="function-name">factorial-tail</span> n acc)
  (<span class="keyword">if</span> (<span class="operator">=</span> n <span class="number">0</span>)
      acc
      (<span class="function-name">factorial-tail</span> (<span class="operator">-</span> n <span class="number">1</span>) (<span class="operator">*</span> n acc))))</pre>
            </div>
             <div class="fact-box">
                <p><strong>`map` and `filter`</strong> are essential higher-order functions. `map` applies a function to each element. `filter` keeps elements that satisfy a predicate.</p>
            </div>
            <div class="code-block">
                <pre><span class="comment">; Double every element in a list</span>
(<span class="keyword">map</span> (<span class="keyword">lambda</span> (x) (<span class="operator">*</span> x <span class="number">2</span>)) '(<span class="number">1</span> <span class="number">2</span> <span class="number">3</span>)) <span class="comment">; => '(2 4 6)</span>

<span class="comment">; Keep only the even numbers</span>
(<span class="keyword">filter</span> <span class="keyword">even?</span> '(<span class="number">1</span> <span class="number">2</span> <span class="number">3</span> <span class="number">4</span>)) <span class="comment">; => '(2 4)</span></pre>
            </div>
        </div>

        <!-- Prolog Content -->
        <div id="prolog" class="content-section">
            <h2>Prolog: The Logic Paradigm (~40%)</h2>

            <h3>Core Philosophy: The Logic Database</h3>
            <p>You describe the world using **facts** and **rules**. You then ask questions (**queries**), and Prolog's search engine finds answers through unification and backtracking.</p>

            <h3>Facts, Rules, and Queries</h3>
            <div class="fact-box">
                <p><strong>Fact:</strong> A statement of truth. Must end with a period. `likes(bill, chocolate).`</p>
                <p><strong>Rule:</strong> An inference. `head :- body.` means "head is true if body is true". A comma `,` in the body means AND. A semicolon `;` means OR.</p>
                <p><strong>Query:</strong> A question asked to the database. Starts with `?-`.</p>
            </div>
            <div class="code-block">
                <pre><span class="comment">% Facts</span>
<span class="function-name">male</span>(luke).
<span class="function-name">parent</span>(luke, leia).
<span class="function-name">parent</span>(luke, han).

<span class="comment">% Rule</span>
<span class="function-name">father_of</span>(<span class="class-name">X</span>, <span class="class-name">Y</span>) :- <span class="function-name">male</span>(<span class="class-name">X</span>), <span class="function-name">parent</span>(<span class="class-name">X</span>, <span class="class-name">Y</span>).

<span class="comment">% Query</span>
<span class="operator">?-</span> <span class="function-name">father_of</span>(luke, <span class="class-name">Child</span>).
<span class="class-name">Child</span> <span class="operator">=</span> leia ;
<span class="class-name">Child</span> <span class="operator">=</span> han.</pre>
            </div>

            <h3>List Processing: `[Head|Tail]`</h3>
            <p>The `[H|T]` syntax is the primary tool for list manipulation and recursion. It deconstructs a list into its first element (`Head`) and the rest of the list (`Tail`).</p>
             <div class="fact-box">
                <p>This syntax is fundamental for writing recursive rules that process lists one element at a time.</p>
            </div>
            <div class="code-block">
                <pre><span class="comment">% Rule to check for list membership</span>
<span class="function-name">member</span>(<span class="class-name">X</span>, [<span class="class-name">X</span><span class="operator">|</span>_]).                 <span class="comment">% X is a member if it is the Head.</span>
<span class="function-name">member</span>(<span class="class-name">X</span>, [_<span class="operator">|</span><span class="class-name">T</span>]) :- <span class="function-name">member</span>(<span class="class-name">X</span>, <span class="class-name">T</span>).   <span class="comment">% X is a member if it's in the Tail.</span>

<span class="comment">% The underscore _ is the anonymous "don't care" variable.</span></pre>
            </div>
            
            <h3>Flow Control: Backtracking & The Cut</h3>
            <p>Prolog's execution is a search process. You can influence this search.</p>
             <div class="fact-box">
                <p><strong>Backtracking:</strong> When a goal fails, Prolog automatically goes back to the last choice it made and tries an alternative path.</p>
            </div>
            <div class="fact-box">
                <p><strong>The Cut (`!`):</strong> The `cut` is a special goal that always succeeds once but **commits** Prolog to all choices made so far in the current rule. It prunes the search tree, preventing backtracking past it. This can improve efficiency but may also eliminate valid solutions.</p>
            </div>
            <div class="code-block">
                <pre><span class="comment">% Without a cut, this would find both cat and dog</span>
<span class="function-name">mammal</span>(<span class="class-name">X</span>) :- <span class="function-name">warm_blooded</span>(<span class="class-name">X</span>), <span class="function-name">four_legs</span>(<span class="class-name">X</span>).

<span class="comment">% With a cut, it stops after finding the first solution.</span>
<span class="function-name">first_mammal</span>(<span class="class-name">X</span>) :- <span class="function-name">warm_blooded</span>(<span class="class-name">X</span>), <span class="function-name">four_legs</span>(<span class="class-name">X</span>), <span class="operator">!</span>.

<span class="operator">?-</span> <span class="function-name">first_mammal</span>(<span class="class-name">M</span>).
<span class="class-name">M</span> <span class="operator">=</span> cat. <span class="comment">% Prolog stops and does not backtrack to find dog.</span></pre>
            </div>
        </div>
    </div>

    <script>
        // Simple tab switching logic
        function showTab(tabId) {
            const contentSections = document.querySelectorAll('.content-section');
            contentSections.forEach(section => {
                section.classList.remove('active');
            });
            document.getElementById(tabId).classList.add('active');

            const tabButtons = document.querySelectorAll('.tab-btn');
            tabButtons.forEach(btn => {
                btn.classList.remove('active');
            });
            event.currentTarget.classList.add('active');
        }
    </script>

</body>
</html>