Finals Questions CS 4

6
1. Describe the difference between recursion and iteration. Give an example of a problem to which a recursive solution makes sense and explain why you would use a recursive function in that situation. 2. You have an associative array (hash) that looks like the following example (it may be any number of levels deep). It needs to be stored in a relational database. Describe the best table structure for storing it (include a create table statement if you can) and then write a function that accepts the associative array as input and stores it in the database. You may use pseudocode or any language you choose. EXAMPLE: $org_chart = array( 'john' => array( 'position' => 'CEO', 'salary' => 240000, 'reports'=>array( 'mary' => array( 'position' => 'CIO', 'salary' => 120000, 'reports'=>array( 'james'=>array('position' => 'Sales Rep', 'salary' => 50000, 'reports'=>array() ),'jason'=>array( 'position' => 'CS Rep', 'salary' => 24000, 'reports'=>array() ),'jules'=>array( 'position' => 'CS Rep', 'salary' => 24000, 'reports'=>array() ) ) ), 'charlie'=>array( 'position' => 'CTO', 'salary' => 120000, 'reports'=>array( 'david'=>array( 'position' => 'SysAdmin', 'salary' => 75000, 'reports'=>array( 'chris' => array( 'position' => 'Tech Support', 'salary' => 24000, 'reports'=>array() ) ) ),'sharon'=>array( 'position' => 'Developer', 'salary' => 100000, 'reports'=>array() ) ) ) ) ) ) _________________________________________________________________ _________________________________ 1.1 PHP 1. Using the function declaration below, explain whether or not the function has any errors in it, what its purpose is and any optimizations you think can be made. Be sure to include descriptions of the passed arguments and function output, if any. Be sure to state your assumptions.

description

Finals Questions CS 4

Transcript of Finals Questions CS 4

Page 1: Finals Questions CS 4

1. Describe the difference between recursion and iteration. Give an example of a problem to which a recursive solution makes sense and explain why you would use a recursive function in that situation.

2. You have an associative array (hash) that looks like the following example (it may be any number of levels deep). It needs to be stored in a relational database. Describe the best table structure for storing it (include a create table statement if you can) and then write a function that accepts the associative array as input and stores it in the database. You may use pseudocode or any language you choose.

EXAMPLE: $org_chart = array( 'john' => array( 'position' => 'CEO', 'salary' => 240000, 'reports'=>array( 'mary' => array( 'position' => 'CIO', 'salary' => 120000, 'reports'=>array( 'james'=>array('position' => 'Sales Rep', 'salary' => 50000, 'reports'=>array() ),'jason'=>array( 'position' => 'CS Rep', 'salary' => 24000, 'reports'=>array() ),'jules'=>array( 'position' => 'CS Rep', 'salary' => 24000, 'reports'=>array() ) ) ), 'charlie'=>array( 'position' => 'CTO', 'salary' => 120000, 'reports'=>array( 'david'=>array( 'position' => 'SysAdmin', 'salary' => 75000, 'reports'=>array( 'chris' => array( 'position' => 'Tech Support', 'salary' => 24000, 'reports'=>array() ) ) ),'sharon'=>array( 'position' => 'Developer', 'salary' => 100000, 'reports'=>array() ) ) ) ) ) )

__________________________________________________________________________________________________

1.1 PHP1. Using the function declaration below, explain whether or not the function has any errors in it, what its purpose is and any optimizations you think can be made. Be sure to include descriptions of the passed arguments and function output, if any. Be sure to state your assumptions.

1 function auto_query( $_table, $_vars ) {2 $_result = $this->query( "EXPLAIN $_table_name" );3 $_fields = array();4 while( $_row = $_result->fetchRow() ) {5 array_push( $_fields, $_row['Field'] );6 }78 if( $_vars[$_table . '_id'] ) {9 $_query = "UPDATE $_table SET ";10 }11 else {12 $_query = "INSERT INTO $_table SET " . $_table . '_date_created = NOW(), ';13 }1415 $_query_params = array();16 17 foreach( $_fields as $_field ) {18 if( isset( $_vars[$_field] ) ) {19 $_query_params[] = "$_field = " . $this->dbh->quoteSmart( $_vars[$_field] );20 }

Page 2: Finals Questions CS 4

21 }22 $_query .= implode( ',', $_query_params );23 if( $_vars[$_table . '_id'] ) {24 $_query .= " WHERE {$_table}_id = " . $this->dbh->quoteSmart($_vars[$_table . '_id']);25 }26 return $_query;27 }

Follow Up Questions:2.In line 18, why is isset() used instead of a simple conditional as was used in line 8?3.What assumptions can you make about the database structure? Hint: Lines 8 – 13

4.Using the function declaration below, explain whether or not the function has any errors in it, what its purpose is and any optimizations you think can be made. Be sure to include descriptions of the passed arguments and function output, if any. Be sure to state your assumptions.1 function get_employees_by_hierarchy( $_employee_id = 0,$_depth = 0,$_org_array = array() ) {2 if ( $this->org_depth< $_depth ) {3 $this->org_depth = $_depth;4 }5 $_depth++;6 $_query = "SELECT * FROM employees WHERE ";7 if ( !$_employee_id ) {8 $_query .= "employee_manager_id IS NULL OR employee_manager_id = 0";9 }10 else {11 $_query .= "employee_manager_id = " . $this->dbh->quoteSmart( $_employee_id );12 }13 $_result = $this->query( $_query );1415 while ( $_row = $_result->fetchRow() ) {16 $_row['depth'] = $_depth;17 array_push( $_org_array, $_row );18 $_org_array = $this->get_employees_by_hierarchy(19 $_row['employee_manager_id'],20 $_depth,21 $_org_array22 );23 }24 return $_org_array;25 }

Follow Up Questions:5.Speculate why lines 2 – 4 are necessary.6.Speculate why line 16 is necessary.

1.2 MySQL

Given the table structure and row data below, answer the follow up questions.mysql> explain user_skill;+--------------------------+------------------+------+-----+| Field | Type | Null | Key |+--------------------------+------------------+------+-----+| user_skill_id | int(11) | | PRI || user_skill_last_modified | timestamp(14) | YES | |

Page 3: Finals Questions CS 4

| user_skill_date_created | datetime | YES | || user_id | int(11) | YES | || skill_name | char(255) | YES | || skill_level | char(255) | YES | || skill_usage | char(255) | YES | || skill_last_used | char(255) | YES | || user_skill_endorsed | tinyint(1) | YES | |+--------------------------+------------------+------+-----+

mysql> *************+--------------------+-------------------+--------------------+| user_firstname | user_lastname | skill_name |+--------------------+-------------------+--------------------+| Kim | Simpson | PHP || Kim | Simpson | Perl || Kim | Simpson | Microsoft Word || Kim | Simpson | Microsoft Access || Kim | Simpson | Accounting/Billing || Kim | Simpson | Java || Kim | Simpson | SQL || Kim | Simpson | CSS || Kim | Simpson | OO Programming || Kim | Simpson | Microsoft Excel |+--------------------+-------------------+--------------------+10 rows in set (0.00 sec)

Follow Up Questions:1.Assuming that the data stored in skill_name in the user_skill table might be repeated for different users, what changes would you make to the database to normalize the skill_name and reduce repeated storage? Show the structure of the new table(s).

2.Recreate the query that returned the 10 rows of data supplied. Speculate on tables that would be needed that are not shown here.

3. Given the following query, how could it be optimized? List all assumptions:

select c.* FROM companies AS c JOIN users AS u USING(companyid) JOIN jobs AS j USING(userid) JOIN useraccounts AS ua USING(userid) WHERE j.jobid = 123;

Answer the follow up questions based on the below:

explain SELECT * FROM job JOIN job_postings ON (job.jobid = job_postings.jobid) JOIN companies ON companies.companyid = job.companyid WHERE job.jobid IN (16189,16188);+--------------+-------+-------------------+---------+---------+-------+------+-------------+| table | type | possible_keys | key | key_len | ref | rows | Extra |+--------------+-------+-------------------+---------+---------+-------+------+-------------+| job | const | PRIMARY,companyid | PRIMARY | 4 | const | 2 | || companies | const | PRIMARY | PRIMARY | 4 | const | 2 | || job_postings | ref | PRIMARY | PRIMARY | 4 | const | 8 | Using where |+--------------+-------+-------------------+---------+---------+-------+------+-------------+3 rows in set (0.00 sec)

Follow Up Questions4. Explain any optimizations that can be made to the above query.5. How many rows will be analyzed by this query?

Page 4: Finals Questions CS 4

1.3 JavaScript

Given the JavaScript below, answer the follow up questions.

spell_img = new Image();spell_img.src = '/images/standard/spellcheck.gif';spell_img.setAttribute('title',_lang_spellcheck );

functionfind_text_boxes(){myforms = document.forms;for( i=0;i <myforms.length; i++ ) {textareas = myforms[i].getElementsById('textarea');for( y=0; y <textareas.length; y++ ) {spelllink = document.createElement('a');spelllink.setAttribute('href',"javascript:spellCheck(" + i + ", '" + textareas[y].name + "')");spelllink.appendChild( spell_img.cloneNode(true) );textareaParent = textareas[y].parentNode;textareaParent.insertBefore( spelllink, textareas[y].nextSibling ); } }}

Follow Up Questions:1.Do any errors exist? If so, how would you fix them?2.How many images will this create and where will it place them?

Page 5: Finals Questions CS 4

2 Design & PlanningYou have been tasked with creating a simple contact manager. The client has provided

you with the following description of the need: "Users of the system will be able to login and create/manage/delete their personal list of contacts. Users may include other users as contacts, or may add non-user contacts. For each contact, the user should be able to store their name, personal data, an unlimited length note, an unlimited number of phone numbers of any type (home,cell,fax,etc.), an unlimited number of email addresses, and an unlimited number of postal addresses of any type (business,home, etc.). Users must also be able to see a list of all other users who have added them as a contact and be able to block all users or any number of individual users from adding them as a contact."

1. Using your best understanding of the requirements as given, create an Entity Relationship Diagram (ERD) for the database to be used by the application. Where data is insufficient, use your best judgment to fill in the gaps. (You may submit a scanned image of your hand-drawn diagram, a DBDesigner XML file, or any open source format ERD file to answer this question)

2. Describe the MVC design pattern and explain what type of problem it is intended to solve.

3. Talk about project planning... What phases does a software development project typically go through? How do you manage it from phase to phase? Do you have a preferred software development model?

4. Describe the difference between agile and waterfall approaches to application development. What are strengths and weaknesses of each?