CS 103: Introduction to Programming Fall Written Final Exam 12/11/16, 4:30PM 6:30PM

Similar documents
Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Exam I KEY (corrected)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice Questions (20 questions * 6 points per question = 120 points)

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CSci 1113 Final. Name: Student ID:

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

COL 100. Minor 2 Exam - Practice

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Review Questions II KEY

CS 455 Final Exam Fall 2016 [Bono] December 13, 2016

Scientific Computing

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

Name Section: M/W T/TH Number Definition Matching (6 Points)

CSE 333 Midterm Exam 7/25/16. Name UW ID#

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CMSC 202 Midterm Exam 1 Fall 2015

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

CSci 1113 Final. Name: Student ID:

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Part I: Short Answer (12 questions, 65 points total)

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

CS 455 Final Exam Spring 2015 [Bono] May 13, 2015

Data Structures (CS301) LAB

CS 240 Data Structure Spring 2018 Exam I 03/01/2018

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties. Do not start the test until instructed to do so!

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

2. It is possible for a structure variable to be a member of another structure variable.

Dynamic Data Structures

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

Midterm Exam. Sample Solutions

CSci 1113 Final. Name: Student ID:

Unit 14. Passing Arrays & C++ Strings

Spring 2008 Data Structures (CS301) LAB

Final Exam. Name: Student ID: Section: Signature:

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

A SHORT COURSE ON C++

Solution to CSE 250 Final Exam

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Name Section: M/W T/TH Number Definition Matching (8 Points)

Midterm Practice Exam

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Largest Online Community of VU Students

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

CS 103 Unit 14 - Streams

CS 103 Unit 12 Slides

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

C++_ MARKS 40 MIN

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Introduction to Computer Science Midterm 3 Fall, Points

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

CSCI 104 Memory Allocation. Mark Redekopp David Kempe

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

CS 216 Exam 1 Fall SOLUTION

1. Which of the following best describes the situation after Line 1 has been executed?

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

ECE 461 Fall 2011, Final Exam

CS 103 Unit 14 - Streams

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 3

Review Questions for Final Exam

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

READ THIS NOW! Do not start the test until instructed to do so!

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Definition Matching (10 Points)

COMSW Introduction to Computer Programming in C

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

CS 211 Winter 2004 Sample Final Exam (Solutions)

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

Final Exam Solutions PIC 10B, Spring 2016

Name Section: M/W or T/TH. True or False (14 Points)

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

STL. Prof Tejada Week 14

COS 126 General Computer Science Spring Written Exam 1

Multiple Choice Questions (20 questions * 5 points per question = 100 points)

This test is OPEN Textbook and CLOSED notes. The use of computing and/or communicating devices is NOT permitted.

CSCE 206: Structured Programming in C++

CS 103 Unit 13 Slides

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Ch 6. Functions. Example: function calls function

Introduction to C++ Systems Programming

Transcription:

CS 103: Introduction to Programming Fall 2017 - Written Final Exam 12/11/16, 4:30PM 6:30PM Name: USC Email Address: Lecture (Circle One): Redekopp 2:00 TTh Goodney: 2 MW 9:30 TTh 11:00 TTh Complete the Information Above for 1 point of credit. Page Your score Max score 1 1 2 7 3 8 4 4 5 7 6 5 7 7 8 6 9 5 Total 50 Note 1: You need NOT worry about #include or 'using namespace' statements. Note 2: The last page is blank and for scratch work. You MUST turn it in with your exam. 1

1. (3 pts) Consider the following recursive function: int d(int n, int m) cout << n << " " << m << endl; if( n < m ) return 0; else return d(n-m, m) + 1; a. What is the return value for the call: d(10, 3)? b. What is printed to cout for the call: d(7, 2)? 2. (3 pts). Consider the following recursive function. bool fn(char *s, int len) cout << "len =" << len << endl; if (len <=1 ) return true; else return( (s[0] == s[len-1]) && fn(s+1, len-2) ); // fn() a. What is the return value for the call: fn("abc", 3)? b. What is printed to cout for the call: fn("noon",4)? 3. (1 pt) The following code fragment doesn t compile. Explain why in a short sentence: struct D int x; double y;; int main() D* my_d = new D; my_d.x = 10; my_d.y = 3.14; 2

4. (2 pts) For each operation below indicate if it will take constant time (i.e. O(1)) or linear time (O(n)) where n is the number of items in the array/list/vector. a) Inserting to the back of a vector constant or linear b) Remove from the front of a deque constant or linear c) Inserting to the back of a deque constant or linear d) Remove from the front of a linked list constant or linear 5. (3 pts) The following shows a doubly linked list with three nodes. Assuming this configuration with 3 nodes, suppose we want to delete the node pointed to by 'curr' (i.e. node w/ value 9) and leave the linked list in a correct state afterwards. Circle which code operations are correct. More than 1 may be correct, circle all that apply. head 0x148 curr 0x1c0 0x148 0x1c0 0x190 NULL 3 0x1c0 0x148 9 0x190 0x1c0 12 NULL prev val next prev val next prev val next Option A Works: Yes / No Option B Works: Yes / No Option C Works: Yes / No curr->prev->next = curr->next; curr->next->prev = curr->prev; delete curr; Node* temp = curr->next; temp->prev->prev->next = temp; temp->prev = curr->prev; delete curr; Node* temp = curr->prev; curr->prev = curr->next; curr->next = temp; delete curr; 6. (1 pt) Using the original linked list shown in the previous problem and the 'curr' pointer as shown, write a single cout statement that would print the value 12 to the screen. (Do NOT just write 'cout << 12;' You should access the linked list node and get 12 to be printed). 7. (2 pts) Given an array with n elements, what is the best run-time complexity that can be achieved for an algorithm (with no prior knowledge) to check if a value does NOT appear in the array: a. If the array is NOT sorted? O( ) b. If the array IS sorted? O( ) 3

8. (1 pt.) What is the big-o runtime of the following code: O( ) int i=1; //assume some large N while(i < N) i *= 2; 9. (2 pts) Billy Bruin finishes his CS degree and gets a job as a C++ programmer. As part of a project he writes the following code fragment: vector<object> v; for(i=0;i<n;i++) Object o = objectfactory.getnextobject(); v.insert(v.begin(), o); Billy was proud of this code, however during testing when N is large his code is very slow. You proposed the following rewrite: vector<object> v; for(i=0;i<n;i++) Object o = objectfactory.getnextobject(); v.push_back(o); reverse(v); // Reverses the vector contents Explain in one short sentence using your knowledge of vectors why your solution is much faster: 10. (1 pt.) Given a text file ("dat.txt") whose contents are 4 5, (i.e. spaces after 4 and 5 but no newline at the end just the EOF (End-of-File). What will the following code print? ifstream ifile("dat.txt"); int temp, cnt = 0; while(! ifile.fail()) ifile >> temp; cnt++; cout << cnt << endl; Output: 4

11. (4 pts.) Consider the following graph. We are going to do a breath-first search on the graph starting at node 3. Assume the neighbors of a particular node are stored sorted by node ID. Nodes are only discovered once (assume there is some sort of visited flag or list). You can pretend this is a friends graph like PA5/6. On the line below, fill-in the order in which the nodes are discovered and put into the queue (we started it for you): queue: 3, 12. (3 pts) The following program shows a function, f, and its invocation (call), using pointer notation for passing the parameter. Re-write the program on the right side to instead use C++ reference-based syntax instead. Pointer-based Implementation #include <iostream> using namespace std; void f(int *x) *x += 5; int main() int k=15; f(&k); cout << k << endl; // outputs 20 return 0; Equivalent C++ Reference (i.e. &) argumentpassing implementation 5

13. (5 pts) Assume you are given a file whose name is "file.txt" and whose contents are shown below. Show what the program below will print when executed. File contents of file.txt : 5 3 2 6 5 4 3 2 1 7 8 #include <iostream> #include <fstream> #include <sstream> #include <string> using namespace std; int main() ifstream f("file.txt"); int t, x, y; string z; f >> x >> y; cout << "L1: " << y << endl; getline(f, z); getline(f, z); cout << "L2: " << z << endl; stringstream ss(z); while(y > 0) ss >> t; x += t; y--; cout << x << endl; Output: 6

14. (7 pts) Examine the code below. For the lines A1-A4 indicate what will be printed. For lines B1-B3 indicate whether they will compile or not. #include <iostream> using namespace std; class ABC public: ABC() a = 7; b = 3.0; ABC(int mya, double myb) a = mya; b = myb; int a; double get_b() return b; private: double b; ; class XYZ public: XYZ(ABC mya) y = mya; x = 5; int get_val() x++; return dowork(); private: int dowork() return x + y.a; ; int x; ABC y; ; int main() ABC a1; ABC a2(8, 12.5); XYZ x1(a1); cout << x1.get_val() << endl; // Line A1 --a1.a; cout << a1.a << endl; // Line A2 cout << x1.get_val() << endl; // Line A3 cout << a2.get_b() << endl; // Line A4 x1.y = a2; cout << a1.b << endl; cout << x1.dowork() << endl; return 0; // Line B1 // Line B2 // Line B3 Enter your answers here: Line A1: Output: Line A2: Output: Line A3: Output: Line A4: Output: Line B1: Compiles (Yes / No) Line B2: Compiles (Yes / No) Line B3: Compiles (Yes / No) 7

15. (6 pts) Consider the following program that should read in 1024 words (strings) from a file into an array, and returns the array to main which then outputs 512 strings. The 512 strings should be the result of concatenating the first and last, second with second to last, third with the third to last, etc. Fill in the indicated blanks below (return type, array allocation, words declaration, cout code, and cleanup). #include <iostream> #include <fstream> using namespace std; // This function reads 1024 words from the given file // fill in the return type below readdict(char* f) ifstream infile(f); // allocate an array 's' of 1024 strings string ; for(int i=0; i<1024; i++) infile >> s[i]; return s; // readdict() int main() // Indicate the type of words below words = readdict("dict.txt"); for(int k = 0; k < 512; k++) cout << words[k] << endl; // now we're done working with the words // Show any cleanup code you need here (if any) return 0; 8

16. (5 pts) Consider the following program utilizing recursion and show what will be printed to the screen. #include <iostream> #include <vector> #include <string> #include <cmath> using namespace std; void myst(vector<string>& sol, int n, string pre) if(n > 0) for(unsigned k=0; k < pre.size()+1; k++) char c = 'a' + k; myst(sol, n-1, pre + c); else sol.push_back( pre ); int main() vector<string> items; myst(items, 3, ""); for(unsigned j = 0; j < items.size(); j++) cout << items[j] << endl; return 0; Output: 9

This page is for scratch work. You MUST turn it in with your exam 10