Definition Matching (10 Points)

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

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)

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

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

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

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

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

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

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.

Chapter 3 - Notes Input/Output

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

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

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

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

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

C++ Input/Output: Streams

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

C++ Input/Output Chapter 4 Topics

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


Expressions, Input, Output and Data Type Conversions

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

CHAPTER 3 Expressions, Functions, Output

Software Design & Programming I

Introduction to C++ (Extensions to C)

The C++ Language. Arizona State University 1

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: 12:45 2:20. True or False (12 Points)

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

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

Name Section: M/W or T/TH. True or False (14 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 (!

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

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

Objectives. In this chapter, you will:

Lecture 3 The character, string data Types Files

Chapter 3. Numeric Types, Expressions, and Output

3.1. Chapter 3: The cin Object. Expressions and Interactivity

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

Chapter 3: Expressions and Interactivity

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

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

True or False (12 Points)

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

Getting started with C++ (Part 2)

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

WARM UP LESSONS BARE BASICS

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)

Chapter 3: Input/Output

C++ Final Exam 2017/2018

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

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

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

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

BITG 1233: Introduction to C++

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

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

Engineering Problem Solving with C++, Etter/Ingber

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

PIC 10A. Review for Midterm I

CS242 COMPUTER PROGRAMMING

TEST 1 CS 1410 Intro. To Computer Science Name KEY. October 13, 2010 Fall 2010

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

COMP322 - Introduction to C++

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

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Chapter 2 Basic Elements of C++

CS2141 Software Development using C/C++ Stream I/O

Fundamentals of Programming CS-110. Lecture 2

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

Window s Visual Studio Output

Introduction to Programming EC-105. Lecture 2

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

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Programming with C++ as a Second Language

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

CMPS 221 Sample Final

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

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

COMP322 - Introduction to C++

UNIT- 3 Introduction to C++

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

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

Chapter 2. Outline. Simple C++ Programs

LECTURE 02 INTRODUCTION TO C++

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

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

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

Programming. C++ Basics

Name. Classes and Objects 1. We re going to develop a class to help out Café Below is the definition for a class called

Lab # 02. Basic Elements of C++ _ Part1

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

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Transcription:

Name SOLUTION 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. There are no syntax errors on this exam. If you find one let me know. 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. Definition Matching (10 Points) 1. (10 pts) Match the words with their definitions. Choose the best definition for each word. Identifier G Syntax _M Literal Value _K Data Type F Named Constant _H Declaration O Algorithm L Semantics _J Type Cast _B Reading Marker N_ A) Definition is not listed below (This answer can be used more than once if necessary) B) The explicit conversion of a value from one data type to another. C) The mechanism that transfers control to a function D) A program that translates a high-level language into machine code. E) The implicit conversion of a value from one data type to another F) A specific set of values along with a set of operations on those values G) A Name associated with a function or data object and used to refer to that function or data object. H) A location in memory, referenced by an identifier, which contains a data value that cannot be changed. I) A location in memory, referenced by an identifier, which contains a data value that can be changed J) The set of rules that determines the meaning of instructions written in a programming language. K) Any constant value written in a program. L) A step-by-step procedure for solving a problem in a finite amount of time. M) The formal rules governing how valid instructions are written in a programming language. N) Indicates the next character waiting to read from the input stream O) A statement that associates an identifier with a data object, a function or a data type Page 1 of 11

True or False (12 Points) 2. (12 pts) Circle T for true and F for false: T a) The C++ compiler finds syntax errors in a program? T b) The function main() is required for every C++ program. T c) Type coercion is the implicit conversion of one data type to another. T d) A C++ program is written in code during the implementation phase of a programs life. F e) A void function can be invoked (called) within an expression. T f) Algorithms are a step-by-step procedure for solving a problem in a finite amount of time. T g) The size() function returns the number of characters contained in a string variable F h) The find() function returns string::npos if it successfully finds its argument. F i) The get() function obtains the next non white space character from the input stream. F j) The extraction operator (>>) terminates on trailing whitespace characters only. F k) The statement cout << num++; gives the same output as cout << ++num; T l) The ignore function is used to skip characters on the input stream. Multiple choice (24 points) Questions 3 26 For these problems circle all correct answers For example if answers A, C and E are all valid then circle A, C and E. 3. In this class, which of the following are invalid identifiers in C++? A) A--B B) %456 C) twobits D) House_ E) Yes 4. In this class, which of the following are valid identifiers in C++? A) 2z3 B) m_2*c_a_r C) four-teen D) _Name E) No Page 2 of 11

5. Which output manipulator is used to terminate output on the current line? A) setw B) showdecimal C) showpoint D) endl 6. What is the name of the header file required for using setw and setprecision? A) iostream B) manip C) iomanip D) string E) cmath 7. Which output manipulator ensures a decimal point is output for all floating-point numbers? A) setw B) showdecimal C) showpoint D) endl 8. Which output manipulator can be used to specify the output justification used with setw? A) setw B) center C) showpoint D) endl 9. Which output manipulator specifies how many character positions the next data item should occupy when it is output? A) setw B) center C) showpoint D) endl 10. After the following code segment executes, what value is stored in the variable result?: float result; int num = 4; result = int(5/num + 1.5); A) 2 B) 1 C) 2.75 D) 2.5 E) a runtime error F) None of These 11. The following C++ statements are to be included in a program. What is the best correct data type needed for the variable mystery? (only one possible answer for this question)????? mystery; getline(cin, str1, mystery); A) float B) string C) int D) char E) None of these Page 3 of 11

For questions 12-14, consider the following C++ declarations. In the code, a indicates a space string str1 = This Class is CPE211 ; string str2 = CPE211 is easy ; string str3 = Length is ; string::size_type num, Position; For the above declarations, answer the questions based on the program fragment shown. 12. What is the output of the following program fragment listed below? (a indicates a space) num = str2.size(); cout << num; A) 12 B) 13 C) 14 D) 15 E) string::npos 13. What is the output of the following program fragment listed below? (a indicates a space) Position = str1.find( C ); cout << Position; A) 0 B) 1 C) 5 D) 6 E) string::npos 14. What is the output of the following program fragment listed below? (a indicates a space) str3 = str2.substr(6,5); cout << str3; A) 1 is B) is e C) 11 is D) 1 is e 15. C++ is an example of a(n) A) Meta Language B) High Level Language C) Assembly Language D) Low Level Language E) Machine language F) None of These 16. A(n) is a function that returns a function value to its caller and is invoked within an expression. A) Main Function B) Value-returning function C) Void Function D) Subprogram E) None of These Page 4 of 11

17. The following C++ statements are to be included in a program. What is the best possible data type needed for the variable len? (only one possible answer for this question) string firstname= Reginald ;????? len; len = firstname.find( Hello ); A) string::npos B) string::size_type C) string D) char E) None of these 18. Given the constant declaration and integer variable declaration const int FACTOR = 95; int num = 5; which of the following is a valid use of FACTOR? A) cin >> FACTOR; B) cout << FACTOR * 3; C) FACTOR = 24; D) num = num*factor; are valid 19. Given the following series of C++ statements, what value is stored in the variable ch3 assuming that the user inputs the text highlighted in gray. Reading marker is on A. ( indicates a space, \n represents a newline) A B \n CD\n char ch1; char ch2; char ch3; cin >> ch1; cin >> ch2; cin >> ch3; A) A B) C) \n D) C 20. Given the following series of C++ statements, what value is stored in the variable ch3 assuming that the user inputs the text highlighted in gray?( indicates a space) Reading marker is on A A \n C BD\n char ch1; char ch2; char ch3; cin.get(ch1); cin.get(ch2); cin.get(ch3); A) A B) C) \n D) C Page 5 of 11

21. Given the following series of C++ statements, what value is stored in the variable ch assuming that the user inputs the text highlighted in gray? Reading marker is on the first A. ABCDEF\n FEDCBA\n char ch; cin.ignore(10, \n ); cin.get(ch); A) E B) D C) C D) F 22. Given the following series of C++ statements, what value is stored in the variable string2 assuming that the user inputs the text highlighted in gray? ( indicates a space). The reading marker is on F February 29, 1996\n A) February 29, 1996 string string1; B) 29, 1996 string string2; cin >> string1; C) February getline(cin,string2, \n ); D) 29, 1996\n 23. Given the following series of C++ statements, what value is stored in the variable string1 assuming that the user inputs the text highlighted in gray? ( indicates a space). The reading marker is on F February 29, 1996\n string string1; cin.ignore(3, \n ); getline(cin,string1, \n ); A) ruary 29, 1996\n B) ruary 29, 1996 C) February D) 29, 1996 24. In the following C++ statement, what is Beta? Alpha = 4 * Beta(gamma, delta) + 3; A) int variable B) float variable C) void function D) value-returning function E) Not enough information provided to know (acceptable as well) Page 6 of 11

25. If alpha and beta are int variables, which of the following is a valid C++ statement? A) alpha + beta = alpha*beta; B) 6 = beta; C) alpha%beta = alpha + alpha*beta + beta; D) alpha*2 = beta*3; are valid 26. In the following C++ statement, what is Display? Display(gamma, delta); A) int variable B) float variable C) void function D) value-returning function E) Not enough information provided to know (acceptable as well) Unix Commands ( 5 points) Questions 27 31 27. What Unix command is used to obtain the current working directory path for a terminal (shows you which directory the terminal is in)? pwd 28. Give the Unix command to create the directory Programs. mkdir Programs 29. Give the Unix command to delete the file MyProgram rm MyProgram 30. Give the Unix command that is used to copy the file in.txt to in.txt.bk cp in.txt in.txt.bk 31. Give the Unix command used in the labs to compile the C++ program Project_04.cpp and create an executable named Project_04? g++ Project_04.cpp o Project_04 32. (4 pts) Declaration statements Short Answer (49 points) Questions 32 42 a) Provide a constant declaration for a string identifier of NAME with a value of Test. const string NAME = Test ; b) Provide a float variable declaration for the identifier value. float value; Page 7 of 11

33. (4 pts) Assignment statements a) Write a statement that assigns H to the char variable init. init = H ; b) Write a statement that assigns the value 25 to the int variable num. num = 25; 34. (3pts) Identifiers can consist of what types of characters (three distinct answers)? Letters, Underscore, Digits 35. (3pts) An expression is an arrangement of literals, _operators and identifiers that can be evaluated to compute a value of a given type. 36. (2 pts) What are two methods used to indicate comments in a C++ program? // single line comment /* multi-line comment */ 37. (3 pts) The input stream buffer contains the following characters with the reading marker on the A (\n represents the new line character): A B C\n1 2 3\nD E F\n. What is the output to the terminal when the code segment below is executed? Place one character in each box. Hint: Some variables have their value changed as the input is read. A indicates a space. int num1; char ch1, ch2; cin.get(ch2); cin.ignore(200, \n ); cin >> num1; cin.ignore(200, 3 ); cin >> ch2 >> ch1; cout << ch1 << - << num1 << - << ch2; E - 1 - D Page 8 of 11

38. (4 pts) Show the output of each statement below.(output starts at the left side) Place a single character in each box, Skip a box to indicate a space. a) cout << setw(6) << right << Hello << setw(3) << left << World ; H e l l o W o r l d b) cout << left << setw(8) << Number << right << setw(8) << please ; N u m b e r p l e a s e 39. (4 pts) Finish the code segment below so that the an input file entered by the user is opened. The following actions are performed: a) Prompt the user for the name of an input file and read it into the variable filename. b) Open the file entered by the user. // variables to use for the code segment ifstream infile; string filename; cout << Enter the input file name: ; cin >> filename; infile.open(filename.c_str()); Page 9 of 11

40. (6 pts) Show precisely the displayed output of the following cout statement. Write one character per box. A indicates a space. Skip a box to indicate the presence of a blank space in the output. Skip a row to indicate the presence of a blank line in the output. Example: cout << left << Line one << endl; cout << the next\n << endl; cout <<setw(6) << line << is not << endl; cout << setw(6) << right << Line << setw(6) << left << three << endl; L i n e o n e t h e n e x t S p o t r u n. l i n e i s n o t L i n e t h r e e 41. (6 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 quantity; float price; float total; // number of items purchased // cost for one item // total cost of the order cout << Price of item: ; cin >> price; cout << Quantity: ; cin >> quantity; total = price*quantity; cout << Cost of the items is: << total << endl; Page 10 of 11

42. (10 pts) Write a complete program (turn an empty file into a program that compiles, runs and performs the task mentioned.) that performs the following: Prompts the user to type in a sentence and uses the getline function to read the sentence (ending with the new line character) entered The sentence has two parts separated by a colon Output the original sentence and the location of the colon Assume that the sentence entered has a single colon in it Do not forget the necessary header files Sample output:. if the sentence is This sentence is:split at the colon, then the output is: This sentence is:split at the colon colon position is: 16 #include <iostream> #include <string> using namespace std; int main() { string sentence; string::size_type pos; cout << Enter in a sentence:\n ; getline(cin,sentence, \n ); cout << sentence << endl; pos = sentence.find( : ); cout << colon position is: << pos << endl; } return 0; Page 11 of 11