MENU

CS225 Exam0 Walkthrough

August 28, 2022 • Read: 279 • Practice Exam,CS 225

exam0_1.png

A:

Neighbor Definition: Parent or Children of the node

Based on the definition above, Node "14" has a parent of node "23", and has children of node "3" and node "18". Only answer choice c matches one of the neighbors.
 


exam0_2.png

A:

exam0_runtime.jpeg

Along with the above picture, "$O(nlog(n^2))$" is faster than "$O(n^2)$" ($log(n^2) < n$). Having constant power is way faster than n's power, so "$O(n^2) < O(2^n)$". Also, "$O(2^n)$" can be broken down to "$2*2*2*2...$", and "$n!$" can break down to "$n * (n-1) * (n-2) * ... * 2 * 1$", since "$(n-2)$" is greater than 2, so "$O(n!)$" is the fastest growing terms. So the answer choice is d is correct.
 


exam0_3.png

A:

Leaf Definition: The node that does not have any children

Based on the above definition, we can see that only the answer choice a has a node that does not have any children.

 


exam0_4.png

A:

Internal Node Definition: the node that has one or more children, aka not a leaf node

Based on the above definition, all the answer choice matches the leaf node, but the answer choice b.
 


exam0_5.png

A:

Cut Edge Definition: the graph would become disconnected if that edge were removed

Based on the above definition, the edge that will make the graph be 2 connected components when removed is the answer choice c.
 


exam0_6.png

A:

a set of vertices in a graph that is linked to each other by paths

Based on the above definition, we have 3 "clusters" of nodes. There are no paths connected between the clusters, so the answer is 3.
 


exam0_7.png

A:

public Student {

};

// These objects will be hosted on the stack
int number;
Student student;
Student* student_ptr = &student;

// This object will be hosted on heap/free store
Student* student = new Student();

The object will be hosted on Heap, aka free store, only if the user used the word "new", so the choice a is the correct answer.
 


exam0_8.png

A:

a) Correct! The class Foo is declared under the namespace "exam", and since we did not "using namespace exam" before using the Foo class, the compiler is going to be mad.
 
b) Incorrect! Given we have already "using namespace std", we can use "cout" and "endl" directly, without adding "std::" before them.
 
c) Incorrect! There are some compelling reasons that might cause the error to occur.
 
d) False, but fair enough. Some compilers will give you errors or warnings on unused variables, and will not compile the code. However, this is not the most compelling reason that caused the error to occur.
 


exam0_9.png

A:

// r now has a value of 1
int r = 1;

// q is a ptr, so it stores an address. In this case, q stores r's address: 0xcafebabe
int* q = &r; 

// the address 0xcafebabe, the one q is holding, now stores the value "6".
*q = 6;

return 0;

Remember q is a pointer, and the value that a pointer holds is an address. The address that q holds did not change for the whole time, although the value that the address "0xcafebabe" changed from "1" to "6", but the pointer q still holds the address "0xcafebabe" which makes choice e the correct answer.
 


exam0_10.png

A:

a) Incorrect! The "thickFrosting\_" is a private member of the Cake class, so the dot notation cannot access its value.
 
b) Incorrect! The "bakeCake()" method is not one of the member functions of the Cake class, so it does not have access to the Cake class's private variables.
 
c) Correct! "setNumLayers()" is within the Cake class, so it can modify the private member "thickFrosting\_".
 
d) False, but fair enough. You can modify the private member "thickFrosting\_" in the constructor, but the word use of "only in the constructor" makes this answer choice incorrect. Because you can modify the private variable in any other Cake class member function without any issues.
 
e) Incorrect! There is an answer choice available.
 


exam0_11.png

A:

#include <iostream>
using namespace std;
int growl(int x,int y) {x = x + y; return x;}
int grow2(int *x, int *y) {*x = *x + *y; return *x;}
int grow3(int &x,int &y) {x = x + y; return x;}

int main() {
  int x = 0; int y = l;
  y = y + growl(x,y);
  // after excute line 9, x = 0 and y = 2
 
  y = y +grow2(&x,&y);
  // after excute line 12, x = 2 and y = 4

  y = y +grow3(x,y);
  // after excute line 15, x = 6 and y = 10

  cout << (x + y) << endl;
  return O;
}

Note that pass-by-reference and pass-by-pointer can both reflect the modified value to the passed-in variable. In this case, the variable x got modified from methods "grow2()" and "grow3()". After adding "x" and "y" together, the sum is "16" which makes choice c the correct answer.

 

Last Modified: October 4, 2023
Archives QR Code
QR Code for this page
Tipping QR Code