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

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

Name Section: M/W T/TH Number 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 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 14) 28 Points Select all correct answers (multiple correct answers are possible)

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

Definition Matching (10 Points)

Why Is Repetition Needed?

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

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

Chapter 5: Loops and Files

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

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

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

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

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


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

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

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

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

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

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

REPETITION CONTROL STRUCTURE LOGO

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. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

C++ Final Exam 2017/2018

The C++ Language. Arizona State University 1

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

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

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

Repetition Structures

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

Chapter 5: Control Structures II (Repetition)

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

Software Design & Programming I

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

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

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

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

CS242 COMPUTER PROGRAMMING

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

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

True or False (15 Points)

Loops and Files. of do-while loop

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

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

CSCE 206: Structured Programming in C++

True or False (12 Points)

Chapter Goals. Contents LOOPS

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

ECE 122. Engineering Problem Solving with Java

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

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

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

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

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

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

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Control Structures of C++ Programming (2)

Quiz Determine the output of the following program:

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

Lecture 3 The character, string data Types Files

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

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

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Chapter 2: Basic Elements of C++

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

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 3 - Notes Input/Output

A SHORT COURSE ON C++

Increment and the While. Class 15

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

CSCI 1061U Programming Workshop 2. C++ Basics

C/C++ Programming Lecture 7 Name:

Simple File I/O.

C++ Input/Output: Streams

Chapter 4 - Notes Control Structures I (Selection)

Looping. Arizona State University 1

C++ Input/Output Chapter 4 Topics

CSCE 121 ENGR 112 List of Topics for Exam 1

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

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

Principles of Computer Science I

Computer Programming I - Unit 5 Lecture page 1 of 14

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Engineering Problem Solving with C++, Etter/Ingber

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Repetition, Looping CS101

The following expression causes a divide by zero error:

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

More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

Class 14. Input File Streams. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

1. In C++, reserved words are the same as predefined identifiers. a. True

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Transcription:

Name Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Relational Expression Iteration Counter Count-controlled loop Loop Flow of Control Event Counter Event-controlled loop Logical Expression A) Definition is not listed below (can be used more than once if necessary) B) A variable that is incremented with each iteration of the loop C) A loop that executes a specified number of times D) A variable that is incremented each time a particular event occurs E) An expression followed by a relational operator followed by an expression that evaluates to a logical value of true or false F) A step for which the implementation details are fully specified G) A statement that transfers control to a function. H) The order in which the computer executes statements in a program I) A statement used to alter the normally sequential flow of control J) A control structure that causes a statement or block to be executed repeatedly K) A step for which some implementation details remain unspecified L) A loop that terminates when something happens inside the loop body to signal that the loop should be exited Multiple choice (12 points) Questions 2 13 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 < 6) sum = sum + count; count = count + 1; A) 3 B) 4 C) 5 D) 6 E) 7 F) None of these Page 1 of 11

3. When used with an input file stream variable, which statements below (A through D) are false about the open function? A) It creates a new, empty file if the file does not exist. B) It sets the reading marker at the first character in the file. C) It puts the stream into the fail state if the file does not exist. D) It associates the name of a stream variable with the name of a physical disk file. E) All of the above are true statements 4. 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; the program continues running, and further input operations with that stream are ignored. C) The system does not display an error message, and program execution is terminated. D) The system displays an error message; the program continues running, and further input operations with that stream are ignored. E) None of the above are valid explanations 5. What is the output for the following segment of code(be careful on this problem)? int sum = 0; for (int j = 1 ; j > 7; j++) sum = sum +j; cout << sum; A) 0 B) 2 C) 6 D) 12 E) 20 F) 30 G) None of These 6. What is the output of the following code segment? int score = 85; if (score <= 90) score = score - 5; if (score <= 80) score = score - 5; if (score <= 75) score = score - 5; cout << score << endl; A) 85 B) 80 C) 75 D) 70 E) None of These 7. In the following code segment, what is the best data type for the variable named mystery????? mystery; mystery.open( file.txt ); int x = 25; mystery << X is << x << endl; A) ofstream B) string C) ifstream D) bool E) Can t determine from given info Page 2 of 11

8. The What is the output for the following code if the user enters: 325 PAST A indicates a space int number; char letter; cin >> letter >> number >> letter; cout << letter; A) 2 B) 5 C) P D) A E) S F) T G) (A space) H) Answer is not shown 9. What is the output for the following code if the user enters: 325 PAST A indicates a space int number; char letter; cin >> letter >> number; cin.get(letter); cout << letter; A) 2 B) 5 C) P D) A E) S F) T G) (A space) H) Answer is not shown 10. Assume that the Boolean variable X has the value false and that the Boolean variable Y has the value true. What is the value of the following expression?!( (Y!Y)&& (X!X) ) A) true B) false C) maybe D) I don t know 11. What is the output for the following segment of code? int sum = 0; int j; for (j = 0; j < 5; j++) sum += j; cout << j; // what is being output? A) 0 B) 5 C) 10 D) 15 E) Nothing, it is an infinite loop 12. What are the two main categories of loops discussed in class: A) Sentinel-Controlled B) Count-Controlled C) Flag-Controlled D) Summing-Controlled E) End-Of-File Controlled F) Event-Controlled Page 3 of 11

13. What is the output for the following code if the user enters: 55 B6A 33.\n ABC A indicates a space string line; char letter; getline(cin, line, \n ); cin >> letter; cin.get(letter); cout << Start- << letter; A) Start-A B)Start-B C)Start-C D)Start- E)Answer is not shown True or False (12 Points) 14. Circle T for true and F for false: T F a) Logical operators can take relational expressions as operands? T F b) A logical expression can consist of a single Boolean variable? T F c) Relational expressions are made up of logical values and logical operators T F d) The state of an input stream can be tested as a true or false value? T F e) Placing an if statement inside a block associated with another if statement results in a syntax error? T F f) The statement cin.get(ch); reads the first non-whitespace character from cin into ch. T F g) The number of times an event-controlled loop executes is known ahead of time. T F h) While loops can be nested (one loop inside of another loop)? T F i) The body of a while loop is always executed at least one time. T F j) The extraction operator can be used to read a newline character. T F k) In sentinel-controlled loops, the best sentinel is a value that is not expected as normal input. T F l) The body of a for loop executes zero or more times. Page 4 of 11

Short Answer (68 points) Questions 15 31 15. (3 pts) What are the three logical operators for C++ (show the symbols)? 16. (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) == h)>= i) <= j) =! k) = 17. (4pts) Given the following values for the Boolean variables w, x, y and z: w = false, x = false, y = true, z = true Clearly indicate whether each logical expression has a Boolean value of true or false. a) (!w!y &&!z) && x b) w && x y &&!x c) (w && x && y && z) y d) z x && y 18. (5 pts) Place the following statements in the proper order for correctly using file stream variables in a program (there are 4 required and one optional step). Place the numbers 1 through 5 on the line provided with 1 being the first step. 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. Declare an identifier to be an input file stream or output file stream variable Close the file stream variable Include the header file fstream Page 5 of 11

19. (4 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 loop_b; int loop_a = 0; while(loop_a < 4) for (loop_b = 0; loop_b < 4; loop_b++) cout << loop_b; cout << - << loop_a << endl; // line terminate the output here loop_a++; 20. (4 pts) Consider the following segment of code int count = 10; // declare and initialize the variables int sum = 0; while (count > 0) sum += count; cout << sum << endl; count--; Rewrite the entire code segment above as a for loop such that the same output is obtained. Page 6 of 11

21. (4 pts) Show the output for the following segment of code. Remember \n indicates the new line character. The contents of the standard input stream (cin) are shown at the right. Reading marker is on the A. string str; int num1; char ch; cin.get(ch); cin >> num1; getline(cin, str, : ); cin.get(ch); cin.ignore(3, A ); cin >> num1; Contents of cin A1234BCD:E12345 cout << num1 << - << ch << - << str; Place a single character in each box, skip a box to indicate a space, skip a row to indicate a blank line. 22. (4 pts) For the following code segment, what is the output printed to the terminal? int num = 15; if (num <= 10) cout << less than 10 << endl; if (num == 10) cout << equal to 10\n ; if (num > 5) cout << greater than 5 << endl; else cout << greater than 10\n ; cout << num is << num << endl; Spaces above are represented as. Place a single character in each box, skip a box to indicate a space, skip a row to indicate a blank line. Page 7 of 11

23. (8 pts) Write a segment of code that performs the following: - prompts for the name of an input file to open, reads the file name and opens the file, - Prints out a message stating a successful or unsuccessful open of the file - For this segment of code, use the following declared variables only. string file; ifstream infile; 24. (6 pts) Finish the segment of code using an if-then-else-if statement such that the following phrases are printed out for the temperatures indicated: Ice if temperature entered is less than 32 Water if temperature entered is less than 212 and greater than or equal to 32 Steam if temperature entered is greater than or equal to 212 int temp; cout << Enter in a temperature (integers only): ; cin >> temp; // Place if-then-else-if testing structure below this statement Page 8 of 11

25. (8 pts) Using a priming read, write a while loop segment of code that keeps reading integers until the user enters in an integer that is greater than or equal to 1000. The loop will sum the integers entered, and the program prints out this sum when the loop exits. Declare all variables required by your code segment. Assume user enters integers only Page 9 of 11

26. (12 pts) Write a complete program that uses a loop to read an unknown number of words separated by spaces from a single line of data entered by the user. The final word on the input line is the sentinel word EOL. The program is to print out how many words were present on the input line excluding the sentinel character word. Do not forget any header files that may be necessary and remember to declare all variables used in the program. Input is from the standard input stream (cin), and output is to the standard output stream (cout) Sample input line #1: Hello World Today is Thursday EOL Sample output #1: There were 5 words on the line Sample input line #2: Hello World EOL Sample output #2: There were 2 words on the line Page 10 of 11

Extra Credit #1(3 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 loop_b; int loop_a = 4; while ( loop_a > 0) for (loop_b = 0; loop_b < loop_a; loop_b++) cout << loop_b; cout << - << loop_a << endl; loop_a--; Extra Credit #2 (3 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 loop = 1; int remain; // remainder of integer division while( loop <= 12) remain = loop%4; if (remain!= 0) cout << remain; // note: no endl else cout << - << loop << endl; loop++; Page 11 of 11