Review Questions II KEY

Similar documents
Exam I Review Questions Fall 2010

Review Questions I Spring 2010

Exam I KEY (corrected)

Review Questions for Final Exam

Review Questions for Final Exam KEY

CSCI 111 First Midterm Exam Spring Solutions 09.05am 09.55am, Wednesday, March 14, 2018

//main.cpp. using namespace std;

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

Chapter 6 - Pointers

Programming for Engineers Pointers

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

! 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)

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

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

Homework 3. Due: Feb 25, 23:59pm. Section 1 (20 pts, 2 pts each) Multiple Choice Questions

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

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

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

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

OBJECT ORIENTED PROGRAMMING USING C++

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

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)

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

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

Practice test for midterm 2

Chapter 3. More Flow of Control

CS302 - Data Structures using C++

Pointers and Strings Prentice Hall, Inc. All rights reserved.

CS Spring 05 - MidTerm

The following is a typical execution run of this program:

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!

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

Classes Classes 2 / 35

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

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

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

Objects and streams and files CS427: Elements of Software Engineering

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

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

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

Midterm Practice Exam

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Chapter 10 - Notes Applications of Arrays

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions

Refresher: Interface Specifications. ADT Documentation. Set Represented as an Array. Representation Invariant, Abstraction Function

Representation Invariant, Abstraction Function

Review Problems for Final Exam. 1. What is the output of the following program? #include <iostream> #include <string> using namespace std;

C++ Final Exam 2017/2018

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Chapter 5 - Pointers and Strings

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

Chapter 5 - Pointers and Strings

Getting started with C++ (Part 2)

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

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Pointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

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

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

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

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.

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

Week 5: Files and Streams

COS 126 General Computer Science Spring Written Exam 1

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

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

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

PIC 10A. Final Review: Part I

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):

CS2255 HOMEWORK #1 Fall 2012

Introduction to Programming

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

Note: The buy help from the TA for points will apply on this exam as well, so please read that carefully.

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

How Many Sorts? if( list[j] < list[small]) small = j; double temp = list[i]; list[i] = list[small]; list[small] = temp; } // end of i loop

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

Exam 3 Chapters 7 & 9

Lecture 05 POINTERS 1

What will happen if the user presses <enter> after every input? Will all work like below? Enter the values of x, y, z: Result: The sum is 17.

CSE115 Introduction to Computer Science for Majors I Spring 2008 Final Exam. First/Given Name (PRINT) May 6, 2008

Structures. Today s Goals. Structure Operations. Structure Type Declaration. Struct Instance. typedef. CS246 Lec12. Structures

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 (!

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

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

Data Structures CSci 1200 Test 1 Questions

Name SECTION: 12:45 2:20. True or False (12 Points)

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

C Structures Self-referencing Card Shuffling Unions. C Structures CS Prof. Jonathan Ventura. Prof. Jonathan Ventura C Structures

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

Chapter 8 STRUCTURES IN C

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

CSCE 206: Structured Programming in C++

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Transcription:

CS 102 / ECE 206 Spring 2011 Review Questions II KEY The following review questions are similar to the kinds of questions you will be expected to answer on Exam II (April 7), which will focus on LCR, chs. 5 7 and TCS, chs. 5 13. Make sure you do your best to answer each question before you look at this Key; that will help you to learn. If you have any questions about the answers, please ask them in class or ask your Teaching Assistants. The actual exam will be about 15 questions. 1. (5 points) Write a C++ main() function that reads double numbers from a file called "in.dat" until it reaches the end-of-file, and prints the numbers on cout left-justified in a field of width 6 with three digits after the decimal point. Your program should not use vectors or arrays (which it does not need). Make sure to show any required #includes. Ans: [LCS pp. 123 6, 184 9] #include <iostream> #include <fstream> int main () { ifstream infile ("in.dat"); while (!infile.eof()) { 2. (5 points) double datum; infile >> datum; printf ("%-6.3f\n", datum); infile.close(); return 0; // not required, but good form Define an enumeration types Suit and Rank for suits of cards (CLUBS, DIAMONDS, HEARTS, SPADES) and their ranks (ACE, TWO,, KING). The Rank enumeration should be defined so that ACE has the numerical value 1 and the others follow consecutively (TWO = 2, etc.). Ans: enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES ; enum Rank { ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING ; page 1 of 8

3. (5 points) State in your own words the difference between reactive (or direct) control and behavior-based control. Ans: See LCR, p. 171. 4. (5 points) For the following questions, tell (1) what the function does with the argument, (2) what can be done to the argument within the function, and (3) how the original argument is affected by the action of the function. (a) When an argument is passed by value to a function [e.g., f(int v)], what happens? Ans: (1) The function copies the actual parameter; (2) the value can be read, but assignments to the formal parameter do not affect the actual parameter; (3) the actual parameter cannot be changed by the function. (b) When a reference variable is passed to a function [e.g., f(int& v)], what happens? Ans: (1) The function receives a reference to (address of) the actual parameter, which is usually more efficient than copying it; (2) assignments can be made to the formal parameter, which (3) change the corresponding actual parameter. (c) When a constant reference variable is passed to a function [e.g., f(const int& v)], what happens? Ans: (1) The function receives a reference to (address of) the actual parameter, which is usually more efficient than copying it; (2) assignments cannot be made to the formal, and therefore (3) the actual cannot be changed. [See TCS 8.6, 8.7, 9.5] 5. (5 points) Complete function read_vec(n) to read values into an integer vector of size N and return it. Stop when either the vector is full or you reach the end of the file. You may assume valid data. vector<int> read_vec (const int N) { //Ans [LCR pp. 123 6]: vector<int> v(n); //end of ans. for(int i = 0; i < N &&!cin.eof(); i++) cin >> v[i]; return v; page 2 of 8

6. (5 points) You have the following code fragment. What is printed with the indicated input values? int x; cin >> x; switch(x) { case 40: x %= 6; case 60: x--; x--; break; case 80: x += 586; default: if(x!= 0) x = -x; cout << x << endl; a) input: 40 Ans: 2 b) input: 80 Ans: -666 7. (5 points) Given the following structure definitions, write an output command to print (as an integer) the suit of the last card in deck. (Note that decks need not contain 52 cards.) Ans: struct Card { int suit, rank; ; struct Deck { ; vector<card> cards; Deck deck; // deck initialization would be here // your answer: cout << deck.cards[deck.cards.size()-1].suit << endl; // endl is optional page 3 of 8

8. (5 points) You have vectors U and V declared as: vector <int> U (10); // create vector with 10 elements, all 0 vector <int> V (10, -1); // create vector with 10 elem., all -1 Write code to copy V to U. Hint: One line of code is all that is needed. Ans: U = V; 9. (5 points) Suppose that Card and Deck are defined as in question #7, and suppose that you are given a bool function greater(c1, c2) that tests if card c1 is greater than card c2. Complete the following definition of a bool function ordered(d) that returns true if deck d is sorted in ascending order and false otherwise. (Note that d may have less than 52 cards.) //Ans: bool ordered (const Deck& d) { for (int i=0; i < d.cards.size()-1; i++) { //end of ans. 10. (5 points) if (greater (d.cards[i], d.cards[i+1])) return false; return true; State the Information Hiding Principles. Ans: The implementer should have all the information needed to implement a module correctly, and nothing more. The user of a module should have all the information needed to use a module correctly, and nothing more. (To get complete credit, you need to mention both the implementer and the user (or client) and nothing more [the hiding part].) [Discussed in class] 11. (5 points) Describe in words and show in a diagram (you must do both to get full credit) how you would wire up a Braitenberg vehicle so that it exhibits the following behavior: The vehicle accelerates towards light directly in front of it, but tends to veer away from light that is on one side or the other of it. Ans: The left sensor is connected to the left motor and the right sensor to the right motor. See diagram for Coward in LCR, p. 145. page 4 of 8

12. (5 points) Fill in the new array values after the code segment is executed. Leave no blank array elements. const int N = 8; int i, A[N] = {0, 11, 22, 33, 44, 55, 66, 77; for (i = 1; i < N-1; i++) A[i] = A[i+1]; // look carefully! original A: 00 11 22 33 44 55 66 77 --------------------------------------- 0 1 2 3 4 5 6 7 Ans: new A: 00 22 33 44 55 66 77 77 13. (5 points) --------------------------------------- 0 1 2 3 4 5 6 7 Complete the function sorted() that returns true if two double vectors are sorted in ascending order, and false otherwise. Declare any variables that you need. To get full credit, your function must have a single exit point. bool sorted (vector<int> A, vector<int> B) { bool ordered = true; // assume vectors are sorted for (int i = 0; i < A.size()-1 && ordered; i++) if (A[i] > A[i + 1]) ordered = false; for (int i = 0; i < B.size()-1 && ordered; i++) if (B[i] > B[i + 1]) ordered = false; return ordered; page 5 of 8

14. (5 points) Suppose you are doing a bisection search for the number 2000 in a 64-element vector that has been initialized to 0, 100, 200,..., 6300: vector<int> V (64); for (int i=0; i<64; i++) V[i] = i*100; Write down the low and high index for each stage of the bisection search until it finds the number 2000. To give you a start, I have written down the first few: 0 63 0 30 16 30 Ans: [TCS 12.9] 16 22 20 22 20 20 15. (5 points) Rewrite the following as a functionally equivalent while loop. // M is an integer variable for (i = 0; i < M; i++) { cout << i << " " << i*i << endl; Answer: i = 0; while (i < M) { cout << i << " " << i*i << endl; i++; page 6 of 8

16. (5 points) Write an int function firstnb (string S) that returns the index of the first nonblank character in S. If S does not have any nonblank characters, then your function should return the length of S. Ans: int firstnb (string S) { for (int loc = 0; loc < S.size(); loc++) if (S[loc]!= ' ') return loc; return loc; There are many other ways to do this with either for-loops or while-loops. 17. (5 points) Suppose the Time structure is declared: struct Time { ; int hour, minute; double second; void print12() const; Define the member function print12() to print the time in 12-hour format (with AM or PM appended as appropriate). For example, 07:15:00 should print as 7:15:00 AM and 13:30:05 should print as 1:30:15 PM. Note 00:45:00 should print as 12:45:00 AM. Ans: Time::print12() const { int hr = hour; string AM_PM = " AM"; if (hour >= 12) { hr -= 12; AM_PM = " PM"; else if (hour == 0) { hr = 12; cout << hr << ':' << minute << ':' << second << AM_PM << endl; page 7 of 8

18. (5 points) Given the declaration of Time in question #17, define a constructor for Time that creates a time structure from input specified the 12-hour format. The constructor is declared: Time (int h, int m, double s, string AM_PM); The AM_PM parameter is either "AM" or "PM". Write a definition (implementation) of this constructor. Ans [TCS 11.7 11.8]: Time::Time (int h, int m, double s, string AM_PM) { hour = h; minute = m; second = s; if (AM_PM == "PM") { hour += 12; else if ((AM_PM == "AM") && (h == 12)) { hour = 0; page 8 of 8