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

Similar documents
Definition Matching (10 Points)

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

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

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

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

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

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)

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

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

C++ Input/Output Chapter 4 Topics

Chapter 3 - Notes Input/Output

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

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

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

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

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

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

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

Chapter 2: Basic Elements of C++

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

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

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

The C++ Language. Arizona State University 1

Lecture 3 The character, string data Types Files

True or False (12 Points)

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

Objectives. In this chapter, you will:

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

C++ Input/Output: Streams

Software Design & Programming I

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

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

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

CS242 COMPUTER PROGRAMMING

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


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

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.

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

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

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

CS 1044 Programming in C++ Test 1 Spring 2011 Form B Page 1 of 12 READ THIS NOW!

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

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

WARM UP LESSONS BARE BASICS

Expressions, Input, Output and Data Type Conversions

Chap 0: Overview. Overview of basic C++ syntax Refresh programming basics C++ Vs. Java differences Coding conventions used. EECS 268 Programming II 1

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

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

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

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Chapter 1 INTRODUCTION

COMP322 - Introduction to C++

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

Chapter 3: Input/Output

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ 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!

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CS 1044 Programming in C++ Test 1 READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties.

ENGI 1020 Introduction to Computer Programming R E Z A S H A H I D I J U L Y 2 6,

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

Chapter 5: Loops and Files

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

String Variables and Output/Input. Adding Strings and Literals to Your Programming Skills and output/input formatting

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

Streams in C++ Stream concept. Reference information. Stream type declarations

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

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

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

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

UNIT- 3 Introduction to C++

True or False (15 Points)

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

CSc Introduction to Computing

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

CSI33 Data Structures

C++ Quick Guide. Advertisements

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

CHAPTER 3 Expressions, Functions, Output

C++ Programming: From Problem Analysis to Program. Design, Fifth Edition. Chapter 1: An Overview of Computers and Programming Languages

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

C++_ MARKS 40 MIN

Chapter 2 Basic Elements of C++

Engineering Problem Solving with C++, Etter

Getting started with C++ (Part 2)

We will exclusively use streams for input and output of data. Intro Programming in C++

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

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

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

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Loops and Files. of do-while loop

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

Structured Types. 8. Arrays. collection of components whose organization is characterized by method used to access individual components.

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

Input And Output of C++

What we will learn about this week:

Chapter 14 Sequential Access Files

Transcription:

Name Closed notes and book. If you have any questions ask them. Write clearly and make sure the case of a letter is clear (where applicable) since C++ is case sensitive. You can assume that there is one space between words unless otherwise noted. If necessary, the symbol indicates a space, and it is used when two or more spaces are together. For this test the two-character sequence \n is to be taken to mean the newline character. 1) (12 pts) True/False questions. Circle T or F T F a) The function main() is required for every C++ program. T F b) The extraction operator skips whitespace characters before reading a single characte T F c) A value returning function can be invoked (called) within an expression.. T F d) The getline function reads in an entire line skipping any white spaces. T F e) The C++ compiler finds syntax errors? T F f) Block statements are not terminated with a semi-colon. T F g) The bool data type has a domain consisting of just two values. T F h) Type coercion is the implicit conversion of one data type to another. T F i) Algorithms are a step-by-step procedure for solving a problem in a finite amount of time.. T F j) average = (float)(average) + sum; is a valid assignment statement. T F k) Relational expressions consist of expressions and logical operators T F l) A relational expression can be a logical expression. 2) (3pts) Identifiers can consist of what characters (three distinct answers)? 3) (3pts) An expression is an arrangement of, and that can be evaluated to compute a value of a given type. Page 1 of 11

4) (2 pts) What are two methods used to indicate comments in a C++ program? 5) (3 pts) What are the three logical operators for C++?(show the symbols used) 6) (6 pts) For the operators shown below, CLEARLY indicate if the operator is: Relational (use an R), or Other (use an O). NOTE: that there are no spaces between characters even though it may appear that there is a blank in some of the two character sequences. a) = b) c) > d) >! d) >= e)! f)!= g) =! h) <= i) && j) < k) == Multiple choice Circle all correct answers 7) (2 pts) Which of the following are control structures that can be used to structure statements in a program? A) Subprograms B) Indexing C) Looping D) Compiling E) Execution F) Forwarding G) Coupling H) Selection 8) (2 pts) C++ is an example of A) An assembler B) A Machine language C) Assembly Language D) A Compiler E) A Low Level Language F) None of the above 9) (2 pts) When used with an input file stream variable, which statements below are true about the open function? (circle all that are true) A) If the file does not exist, it creates a new, empty file. B) It sets the reading marker at the first character in the file. C) If the file does not exist, it puts the stream into the fail state. D) It associates the name of a stream variable with the name of a physical disk file. E) None of the above are true Page 2 of 11

10) (2 pts) What happens when a C++ input stream enters the fail state? A) The system does not display an error message, and program execution is terminated. B) The system displays 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 11) (2 pts) Assume that the Boolean variable X has a logical value true and that the Boolean variable Y has the logical value false. What is the logical value of the following logical expression? (Y && (X!X)) A) true B) false C) true D) false E) None of the above 12) (2 pts) Which of the following are required in order to use files in a C++ program A) Use a preprocessor directive to include the header file fstream B) Perform a priming read on each input file C) Erase the contents of each output file D) Declare a file stream variable for each file stream using a variable declaration statement E) Specify the name of the file stream in each input or output statement F) All of the above G) None of the above 13) (10 pts) For each identifier below, indicate which are valid and which are not. For those identifiers that are not valid, give the reason why. a) 5_speed Valid Invalid b) MyWay Valid Invalid c) ab&c Valid Invalid d) clock2 Valid Invalid e) Money$ Valid Invalid Page 3 of 11

14) (2 pts) In the following code segment, what is the best possible data type of the variable named mystery? (be careful on this one)???? mystery; A) int int x ; B) float mystery >> x; x = x*5; C) ofstream D) ifstream E) Can t determine from given info 15) (2 pts) In the following code segment, what is the best possilbe data type of the variable named mystery????? mystery; A) int int x ; string line; B) string getline(cin, line, mystery); C) char 16) (6 pts) Assignment Statements D) bool E) Can t determine from given info a) Write a statement to assign the word Hello to the string variable str1. b) Write a statement to assign the value 75 to the int variable num. c) Write a statement to assign the letter A to the char variable grade. 17) (4 pts) Write a code segment that uses one or more output statements that writes (to the standard output stream cout) the value in the variable money in eight spaces (left justified). Page 4 of 11

18) (8 pts) Declaration Statements: Declare the following identifiers as constants of the data type specified with the values provided a) Provide an integer constant declaration for the identifier SIZE with a value of 5. b) Provide a char constant declaration for the identifier GRADE with a value of A. c) Provide an int variable declaration for the identifier number. d) Provide a double variable declaration for the identifier average. 19) (3 pts) What is the output for the following segment of code? int num = 10; cout << num << endl; --num; cout << num*2 << endl; num++; cout << num + num << endl; 20) (8 pts) Given the following values for the Boolean variables w, x, y and z: w = false, x = true, y = false, z = true 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 Page 5 of 11

21) (10 pts) Match the words with their definitions. Choose the best definition for each word. a) Semantics b) Syntax c) Variable d) Identifier e) Expression f) Type Coercion g) Literal Value h) Data Type i) Declaration j) void Function A) Definition is not listed below (This answer can be used more than once if necessary) B) A specific set of values along with a set of operations on those values. C) The set of rules that determines the meaning of instructions written in a programming language. D) Arrangement of identifiers, literals and operators that can be evaluated to compute a value of a given type. E) A statement that associates an identifier with a data object, a function or a data type. F) A Name associated with a function or data object and used to refer to that function or data object. G) A location in memory, reference by an identifier, which contains a data value that cannot be changed H) The formal rules governing how valid instructions are written in a programming language. I) The implicit conversion of a value from one data type to another. J) A function that returns a single function value to its caller K) A function that does not return a function value to its caller 22) (6 pts) Finish the segment of code using an if-then-else-if structure such that the following phrases are printed out for the speeds indicated: Too Slow if speed is less than 30 Just Right if speed is less than 60 and greater than or equal to 30 Too Fast if speed is greater than or equal to 60 int speed; cout << Enter in a speed (integers only): ; cin >> speed; // Place if-then-else-if testing structure below this statement Page 6 of 11

23) (6 pts) Show the output for the segment of code below. Remember \n indicates the new line character, and the character indicates a space The input file (Input.txt) for the code below is shown on the right: string phrase; Input.txt int num1; 100\n char ch; 50ABC6 DEF Hi There\n ifstream InFile; Hello World\n InFile.open( Input.txt ); InFile >> num1; // read in an integer InFile >> num1; // read in another integer InFile.get(ch); InFile.ignore(7, \n ); InFile >> phrase; InFile.get(ch); getline(infile,phrase, \n ); cout << num1 << endl; cout << - << ch << - << endl; cout << phrase << endl; 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

24) (8 pts) Given the following declarations, show the output for each separate code segment. In the code, a indicates a space string str1 = First string to analyze ; string str2 = Second STRING ; string str3; string::size_type length, position; a) length = str2.size(); cout << length; b) position = str1.find( Str ); cout << position; c) position = str2.find( T ); cout << position; d) str3 = str1.substr(str1.find( r ),6); cout << str3; 25) (8 pts) For the two code segments shown, add code to open, in the manner specified, an input file named input.txt. a) For this code segment use the literal value of the file name. ifstream infile; // place open statement using a literal value below this line b) For this code segment the file name is stored in a string variable, and the string variable is used for opening the input file. ifstream infile; string filename = input.txt ; // place open statement using a string variable below this line Page 8 of 11

26) (6 pts) Consider the standard input stream cin containing the values shown. The reading marker starts on the 0 (zero), the character - - indicates a space and \n represents the new-line character. Contents of Input Stream for cin: 0 1 2 3\n4 5 6\n7 8 9\n Given the input stream above, what values are output by the cout statements when the code segment is executed? Complete the partially supplied output. int num1; char ch1, ch2; cin.ignore(5, \n ); cin >> ch1; cin.ignore(200, 5 ); cin >> num1; cin.ignore(200, \n ); cin >> ch2 >> ch2; cout << num1 = << num1 << endl; cout << ch1 = << ch1 << endl; cout << ch2 = << ch2 << endl; num1 = ch1 = ch2 = 27) (6pts) Show PRECISELY the output of the following statements. Place a single character in each box, skip a box to indicate a space, and skip a row to indicate a blank line. In the code, a indicates a space. cout << \nblank Line First << endl; cout << \nblank line? << endl; cout << the next line << endl; cout << setw(2) << is last << endl; Page 9 of 11

28) (8 pts) Show the output of each statement below. Place a single character in each box, Skip a box to indicate a space. a) cout << setw(3) << right << Hello << setw(7) << World ; b) cout << setw(8) << left << Number: << right << setw(6) << Five ; 29) (8 pts) Write a segment of code that prompts the user for the price of an item and the quantity of that item purchased. These values are read in and the total cost of the purchase(quantity times item price) is calculated and output. Assume that all header files have been declared, and use the following variable declarations. int number; float cost; float total; // number of items purchased // cost for one item // total cost of the order Page 10 of 11

Extra Credit #1) (3 pts) After execution of the following code segment what logical value (true or false) is contained in the Boolean variable result. Explain your answer. If the segment will not compile, state that fact and explain why it won t compile. bool result; int num1, num2; num1 = 5; num2 = 4; result = num1 = num2; Extra Credit #2) (2 pts) The following C++ statements are to be included in a program. What is the correct data type needed for the variable len? (only one possible answer for this question) string firstname= Reginald ;????? len; len = firstname.substr(5,5); A) float B) string::size_type C) string D) bool E) None of these Page 11 of 11