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

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

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

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

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

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

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

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

Chapter 5: Loops and Files

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

Definition Matching (10 Points)

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

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

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

Why Is Repetition Needed?

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

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

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

C++ Final Exam 2017/2018

Week 3: File I/O and Formatting 3.7 Formatting Output

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

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.

Control Structures of C++ Programming (2)

I/O Streams and Standard I/O Devices (cont d.)

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

True or False (15 Points)

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Review Summer CSC1322 Adv Programming in C++ Professor: Zhang

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

True or False (12 Points)


Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

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

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

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

REPETITION CONTROL STRUCTURE LOGO

C++ Programming: From Problem Analysis to Program Design, Third Edition

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

ECE 122. Engineering Problem Solving with Java

Loops and Files. of do-while loop

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

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

C++ Input/Output: Streams

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

C++ Input/Output Chapter 4 Topics

Week 5: Files and Streams

Chapter 3 Problem Solving and the Computer

CSCI 1061U Programming Workshop 2. C++ Basics

The C++ Language. Arizona State University 1

Software Design & Programming I

Chapter 5: Control Structures II (Repetition)

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Increment and the While. Class 15

Programming. C++ Basics

FORM 2 (Please put your name and form # on the scantron!!!!)

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

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

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Convenient way to deal large quantities of data. Store data permanently (until file is deleted).

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

A SHORT COURSE ON C++

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++


CSCE 206: Structured Programming in C++

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Looping. Arizona State University 1

Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

CSCE 121 ENGR 112 List of Topics for Exam 1

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

Score score < score < score < 65 Score < 50

Chapter 2: Overview of C++

int x = 5; double y = 3; // Integer division rounds the result down to the nearest whole number. cout << "1a: " << x / 3 << endl; //1

EP578 Computing for Physicists

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

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Lecture 7 Tao Wang 1

The following expression causes a divide by zero error:

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CSI33 Data Structures

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

Computer Programming : C++

Study recommendations for Quiz 5 - Chapter 5

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

SFU CMPT Topic: Control Statements

Creating a C++ Program

204111: Computer and Programming

CS242 COMPUTER PROGRAMMING

Chapter void Test( int, int, int ); // Function prototype int main() // Function heading { int h; // Local variable

Transcription:

Name Section: M/W T/TH Number Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Iteration Counter Event Counter Loop Abstract Step Concrete Step Flow of Control Event-controlled loop Count Controlled Loop A) Definition is not listed below (can be used more than once if necessary) B) A loop that executes a specified number of times C) A variable that is incremented with each iteration of the loop D) A control structure that causes a statement or block to be executed repeatedly E) A step for which some implementation details remain unspecified F) The order in which the computer executes statements in a program G) A variable that is incremented each time a particular event occurs H) A statement used to alter the normally sequential flow of control I) A step for which the implementation details are fully specified Multiple choice (14 points) Questions 2 15 For these problems circle all correct answers For example if answers A, C and E are all valid then circle A, C and E. 2. How many times does the body of the following while loop execute? int sum = 0; int count = 1; while (count <= 9) sum = sum + count; count = count + 2; A) 3 B) 4 C) 5 D) 6 E) 7 F) None of these Page 1 of 11

3. What happens when a C++ input stream enters the fail state? A) The system displays an error message, and program execution is terminated. B) The system does not display an error message, and program execution is terminated. C) The system does not display an error message; the program continues running, and further input operations with that stream are ignored. D) None of the above 4. What is the output for the following segment of code? Note: the comment is correct int sum = 0; int j = 1; do if ( j%2 == 1 ) // true if j is not divisible by 2 sum = sum +j; j++; while(j < 10); cout << sum; A) 0 B) 1 C) 4 D) 9 E) 16 F) 21 G) 25 H) None of These 5. What is the output for the following code if the user enters: 55B6A int number; char letter; cin >> number >> letter >> letter; cout << letter; A) 5 B) 6 C) A D) A space E) Answer is not shown 6. What is the output for the following code if the user enters: 55B6A int number; char letter; cin >> number; cin.get(letter); cin >> letter; cout << letter; A) 5 B) B C) A D) A space E) Answer is not shown 7. Assume that the Boolean variable X has a logical value false and that the Boolean variable Y has the logical value true. What is the logical value of the following logical expression? (!Y &&Y) (X &&!X) A) true B) false C) maybe D) not sure E) None of these Page 2 of 11

8. What is the output for the following code if the user enters: 55 B6A 33.\n (in this problem a indicates a single space) string line; char letter; getline(cin, line, A ); cin.get(letter); cout << Start << letter << End ; A) StartEnd B)Start.End C)Start D)Start End E)Answer is End not shown 9. What is the output of the following if-then-else-if code segment? int score = 70; if (score < 70) score = score + 5; else if (score < 80) score = score + 10; else if (score < 90) score = score + 15; cout << score << endl; A) 85 B) 80 C) 75 D) 70 E) None of These 10. In the following code segment, what is the data type of the variable named mystery????? mystery; A) int char ch = A ; B) float cin.ignore(20, mystery); C) char D) string E) Can t determine from given info 11. In the following code segment, what is the data type of the variable named mystery????? mystery; // variable declaration of mystery string start; mystery.open( somefile.txt ); mystery >> start; A) ofstream B) string C) ifstream D) bool E) Can t determine from given info Page 3 of 11

12. What is the output for the following segment of code? (Hint: be careful on this problemlook at all parts of the for statement) int sum = 0; for (int j = 5; j < 0; j--) sum += j; cout << sum; A) 0 B) 5 C) 10 D) 15 E) Nothing, it is an infinite loop 13. What are the two main types of loops mentioned in class and in the text? A) Event-Controlled B) End-Of-File Controlled C) Count-Controlled D) Sentinel-Controlled E) Flag-Controlled F) None of These 14. After the following code is executed, what value is output by the cout statement? int number = 2; int count = 0; bool done = false; while (!done) number = number+2; count++; done = (number == 12); // assigns true or false to done cout << count << endl; A) 0 B) 1 C) 2 D) 3 E) 4 F) Nothing, it is an infinite loop G) None of These 15. What is the output for the following segment of code? for (int i = 1; i < 5; i++) switch(i) case 1: cout << 1 ; case 2: cout << 2 ; break; case 3: cout << 3 ; case 4: cout << 4 ; break; default: cout << 5 ; A) 1234 B) 12234545 C) 122344 D) 123452345 Page 4 of 11

True or False (15 Points) 16. Circle T for true and F for false: T F a) The body of a do-while loop is executed one or more times. T F b) The body of a for loop executes zero or more times. T F c) In count-controlled loops, the loop terminates when a specific event occurs. T F d) The statement cin.get(ch); reads the first non-white space character from cin? T F e) The getline function reads in an entire line up to a line termination character without skipping any white spaces. T F f) The statement cin.ignore(10, A ); always skips 10 characters on the input stream cin? T F g) A logical expression can consist of a single Boolean variable? T F h) Logical operators can take relational expressions as operands? T F i) Placing an if statement inside the block associated with another if statement is an example of nested if statements? T F j) The state of an input stream can be tested as if it were a Boolean value? T F k) When a continue statement is executed, the loop in which it appears is exited. T F l) When a break statement is executed, the loop in which it appears is exited. T F m) In a switch statement, a variable can be used as a case label T F n) Switch statements are required to have a default label T F o) In a switch statement the switch expression can be a floating point variable Page 5 of 11

Short Answer (63 points) Questions 17 27 17. (3 pts) What are the three logical operators (show the symbols) for C++? 18. (6 pts) For the operators shown below, CLEARLY indicate if the operator is relational (use an R) or other (use an O). NOTE: there are no spaces between characters a)=> b) > c) == d) = d)! e) >= f)!= g) >> 19. (4pts) Given the following values for the Boolean variables w, x, y and z: w = false, x = true, y = true, z = false Clearly indicate whether each logical expression is true or false. a)!w &&!y z && x b)!w x && y c)(!w y) && (z && x) d) y w z x 20. (4 pts) Place the following statements in the proper order for correctly using file stream variables in a program. Place the numbers 1 through 4 on the line provided with 1 being the first step. Include the header file fstream Declare an identifier to be an input file stream or output file stream variable Use the file stream variable in a statement to write to a file or read from a file. Associate a file on the hard drive with a file stream variable using the open function. 21. (2 pts) For the code segment shown, add a single line of code to open the output file output.txt using the literal value of the file name. ofstream out; // place open statement using a literal value below this line Page 6 of 11

22. (8 pts) For the following code segment, write out what is printed to the screen. Place a single character in each box, skip a box to indicate a space, and skip a row to indicate a blank line. int m; int n; for (n = 0; n < 4; n++) for (m = n; m >= 0; m--) cout << m; cout << - << n << endl; // m starts at value of n Page 7 of 11

23. (10 pts) Finish the code segment below so that the following actions are performed: a) Prompt the user for the name of an input file and read it. b) Open the file entered by the user. c) While the file does not open successfully, print out a message stating an error in opening the file, reset the input stream variable (code to do this is infile.clear();) Prompt the user for the name of an input file and read it. Open the file entered by the user. // variables to use for the code segment ifstream infile; string filename; Page 8 of 11

24. (8 pts) Write a segment of code that uses an if-then-else-if statement to a) Print out Penny if the value of coinvalue is 1 b) Print out Nickel if the value of coinvalue is 5 c) Print out Dime if the value of coinvalue is 10. d) Print out Unknown Coin Value for all other values int coinvalue; // other statements that result in an integer value being assigned to coinvalue 25. (6 pts) Consider the following segment of code for(int count=0; count < 10;count++) cout<< count is: << count << endl; Rewrite the above code segment using a do-while loop such that the same output is obtained. Page 9 of 11

26. (8 pts) Using a while loop, write segment of code that a) Prompts the user for an integer, and reads the integer entered b) While the integer entered is not 0, i) Increment the counter variable count by one ii) Add the integer read to the variable sum ii) Prompt for and read another integer c) Repeat step b until 0 is entered. // Variable declarations int count = 0; // counter of how many words were read int value; // integer value entered by the user int sum = 0; // sum of all numbers entered Page 10 of 11

27. (4 pts) What is the output of the following code segment, all variables are integers. int maxheight = 40; int maxweight = 40; int height = 30, weight = 50; // declare and initialize variables if ( height >= maxheight) cout << Message #A\n ; if (weight >= maxweight) cout << Message #1\n ; else cout << Message #2\n ; else cout << Message #B\n ; if (weight >= maxweight) cout << Message #3\n ; else cout << Message #4\n ; Extra Credit #1) (2 pts) What is the code from class on Wednesday 10/8 or Tuesday 10/14? Had to be present in class to receive credit A) FallBreak B) SpringBreak C) 123 D) 456 E) 789 Extra Credit #2) (3 pts) What is the output of the following code segment? int count = 6; int sum = 0; while (count > 0) sum = (++sum) + (--count); // be careful on this line count--; cout << sum << - << count << endl; Page 11 of 11