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

Similar documents
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 13) 26 Points Select all correct answers (multiple correct answers are possible)

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

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

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

True or False (12 Points)

True or False (15 Points)

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

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

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

Definition Matching (10 Points)

True or False (14 Points)

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

True or False (15 Points)

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.

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Why Is Repetition Needed?

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 CONTROL STRUCTURE LOGO

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

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

CSCE 206: Structured Programming in C++

CS2141 Software Development using C/C++ C++ Basics

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

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

C++ Final Exam 2017/2018

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Lab Instructor : Jean Lai

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

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

CS242 COMPUTER PROGRAMMING

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

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

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

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

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

Name. CPTR246 Spring '17 (100 total points) Exam 2

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

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

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

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

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

Chapter 5: Loops and Files

Chapter 2: Basic Elements of C++

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

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

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

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

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

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

In this chapter you will learn:

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

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

CSCE 110 PROGRAMMING FUNDAMENTALS

WARM UP LESSONS BARE BASICS

Objectives. In this chapter, you will:

Scientific Computing

Programming. C++ Basics

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

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

Fundamentals of Programming CS-110. Lecture 2

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)

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

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

CMSC 202 Midterm Exam 1 Fall 2015


Dept. of CSE, IIT KGP

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:

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

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

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

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

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

Increment and the While. Class 15

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

A SHORT COURSE ON C++

Chapter 6 Topics. While Statement Syntax Count-Controlled Loops Event-Controlled Loops Using the End-of-File Condition to Control Input Data

Chapter 1 INTRODUCTION

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

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

Input And Output of C++

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

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

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

Chapter 15 - C++ As A "Better C"

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

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

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

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

Introduction to Programming I COS1511 School of Computing Revision Notes

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

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:

CMPS 221 Sample Final

A First Program - Greeting.cpp

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

CSCE Practice Midterm. Data Types

Transcription:

Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program what you have been writing in lab. Write clearly and make sure the case of a letter is clear since C++ is case sensitive. Unless otherwise noted, assume a single space between all words. For this test the two-character sequence \n is to be taken to mean the newline character. here are no INENIONAL syntax errors. Assume that all code in this exam will compile. here may be logic errors in some of the code. Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) 1) A(n) is a function that returns a function value to its caller and is invoked in an expression. A) Value-returning function B) Main Function C) Subprogram D) Void Function E) None of these 2) How many function values does a void function have? A) 0 B) 1 C) As many as necessary D) 2 E) 3 F) None of these 3) Circle all of the following that are examples of event-controlled loops: A) Count-Controlled B) Sentinel-Controlled C) Flag-Controlled D) Previous-Value E) End-Of-File Controlled F) All of these 4) An individual pass through, or repetition of, the body of a loop is called a(n). A) Loop test B) Iteration C) ermination condition D) Priming read E) None of the above Page 1 of 14

5) Value parameters (passing by value) are used if a parameters data flow is: A) one-way, into the function B) one-way, out of the function C) two-way, into and out of the function D) None of these 6) Reference parameters (passing by reference) are used if a parameters data flow is: A) one-way, into the function B) one-way, out of the function C) two-way, into and out of the function D) None of these 7) A loop is a loop that executes a specified number of times. A) While B) Count-Controlled C) Looping D) Event-Controlled E) None of hese 8) A(n) loop is a loop that terminates when something happens inside the loop body to signal that the loop should be exited. A) Sequence B) Selection C) Event-Controlled D) Count-Controlled E) None of hese 9) A(n) is a variable used in a function call. A) Function Call B) Reference C) Parameter D) Argument E) None of hese 10) A(n) parameter is a parameter that receives a copy of the value of the corresponding argument. A) Function B) Value C) Variable D) Reference E) None of these 11) A(n) parameter is a parameter that receives the location (memory address) of the caller s argument. A) Function B) Value C) Variable D) Reference E) None of these 12) is the precedence that a local identifier in a function has over a global identifier with the same name. A) Scope B) Non-Local Identifier C) Name Precedence D) Local Identifier E) None of these 13) With respect to a given block, a is any identifier declared outside that block. Page 2 of 14

A) Scope B) Non-Local Identifier C) Name Precedence D) Local Identifier E) None of these 14) What is the output of the following code segment if num has a value of 3? Assume all variables are integers. switch(num) case 1: cout << a ; case 2: cout << b ; case 3: cout << c ; break; default: cout << end ; A) abcend B) abc C) ab D) a E) b F) c G) cend H) None of these 15) (6 pts)he void function named GetNums has two parameters: a pass-by-reference parameter named x of type float a pass-by-value parameter named num of type int. write a valid function prototype and function heading for the function GetNums? Function Prototype: Function Heading: 16) (4 pts) Write a structure declaration for a structure named Building containing the following members: - an integer variable representing the number of floors - a string variable representing the city of the buildings location - a string variable representing the name of the business contained in the building Page 3 of 14

17) (4 pts) Consider the following structure declarations when answering the questions below. struct Date int day; int month; int year; ; struct Account string name; Date duedate; ; a) Write a statement that declares the identifier today as a variable of Dataype Date.. b) Write a statement that declares the identifier kohls as a variable of Dataype Account. c) Write a statement that assigns a value of John to the name member of kohls.. d) Write an output statement that will output the value of day of the duedate member of kohls. Page 4 of 14

18) (14 pts) rue/false questions. Select for true and F for false.. F a) he statement for(;;); is a valid C++ statement. F b) he body of a for loop executes one or more times. F c) Local identifiers have name precedence over global identifiers. F d) he use of the statement: return; is not valid in a void function. F e) An argument corresponding to a reference parameter can be a constant or arbitrary expression? F f) Static variables in a function maintain their value from function call to function call. F g) Value parameters receive a copy of the arguments value. F h) Members of a structure must have unique names F i) A function call can contain more arguments than the number of parameters in the corresponding function heading. F j) A break statement is required in a switch statement F k) In sentinel-controlled loops, the sentinel is a value expected as normal input? F l) A compile error results when the Dataype defining a functions value type is omitted. F m) he default switch label is required in a switch statement. F n) he expression name.first could be used to access the name member of the structure variable first. Page 5 of 14

19) (6 pts) When the following segment of code is executed, the standard input stream contains the numbers 1 2 4 5 6 7 8 9. What is the output when the code executes int sum = 0; int number; do cin >> number; cout << sum << - ; // note no line termination sum = sum + number; while (number < 6); 20) (6 pts) Consider the following segment of code int loop = 0; while (loop < 7) cout << Hello << endl; loop = loop + 2; Rewrite the above code segment as a for loop such that the same output is obtained. Page 6 of 14

21) (6 pts) here are three functions shown in the code segment below. Assume all variables and function prototypes have been correctly declared before this segment of code. status = Average(sum); WriteInfo(outFile, Calc(num)); A) Which function(s) is(are) most likely value-returning function(s)? B) Which function(s) is(are) most likely void function(s)? C) What are the arguments that are used in the three function calls 22) (6 pts) Consider the following segment of code int number; cout << Enter an integer between 0 and 10: ; cin >> number; switch(number*2) case 2: cout << A ; case 5: cout << B ; case 6: cout << C ; case 12: cout << X ; case 15: cout << Y ; case 16: cout << Z ; break; default: cout << Default << endl; a) What is the output if 4 is entered? b) What is the output if 6 is entered? Page 7 of 14

23) (6 pts) What is the output for the code segment below: int count = 5; bool finished = false; do cout << count << - ; count--; finished = count < 0; while (!finished); // note no line termination 24) (10 pts) Finish the code segment below that reads any character from the standard input stream (cin) until an A is read. he segment is to print out how many characters were entered before the A (do not count the A). A new line(shown below as \n) can be entered and it counts as a single character. Example of a typical input line: Hello\nWorld\nA Example of output for the line: 12 characters were entered char ch; // holds the character read from the input stream int numchar = 0; // count of number of characters entered Page 8 of 14

25) (10 pts) What is the output of the following program: 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. #include <iostream> using namespace std; void est(); int main() est(); // First Call est(); // Second Call est(); // hird Call est(); // Fourth Call return 0; void est() static int i = 5; int j = 5; i--; j++; // increment i and decrement j cout << i << << j << endl; Page 9 of 14

26) (8 pts) When the program shown below is executed, what is the output to the screen? his problem deals with the scope of a variable in a program, and the order of execution of statements. here will be a total of 4 lines written to the screen from this program. Analysis of this program requires some thought. #include <iostream> using namespace std; void function_a(int); void function_b(int&); int number = 4; int main() int number = 3; function_b(number); cout << number in main is: << number << endl; function_a(number); return 0; void function_a(int num) int number = 2; num = num +2; cout << number in function A is: << number << endl; void function_b(int& sum) cout << number in function B is: << number << endl; sum = sum + 1; cout << sum in function B is: << sum << endl; he output for this program is as indicated below. In the blank to the left of the lines, place 1,2,3 or 4 to indicate the order the statements are printed (1 for first, 4 for last). In he blank at the end of the line, put in the output value. number in main is: number in function A is: number in function B is: sum in function B is: Page 10 of 14

27) (12 pts) Finish the program below by adding a void function as specified below. Add only a function prototype, function call statement and function definition to the following program. he name of the void function is InitStruct. he function has one parameter of the struct Dataype Myime. he function is to initialize the structure parameter with a time of 1:00:00 AM. he information stored in the parameter must be available in main() after the function call. #include <iostream> using namespace std; struct Myime int hour; int minute; int second; string daytime; // holds AM or PM ; // Place the function prototype below this line int main() Myime time; // Place the function call statement below this line return 0; // Place the function definition below this line Page 11 of 14

28) (12 pts) Finish the segment of code below so that it counts the number of Empty lines in an input file. Hint: An empty line when read is equal to a null string which is represented as. Use the getline function to read each line of the file. Output to the terminal the number of empty lines contained in the file. int numlines=0; string line; ifstream in; in.open( In.txt ); // put rest of code below this line. // do not declare any more variables Page 12 of 14

29) (12 pts) Write a void function definition that opens an input file in the following manner: 1) Prompts the user for a file name, reads the name entered and echo prints the file name, 2) Opens the file name provided and associates it with an input file stream 3) If the file did not open: a) Print out an appropriate error message b) Clear the input file stream (which will reset it) c) Repeat 1, 2 and 3 until a file name is successfully entered his function requires a single parameter an input file stream he above steps outline what is necessary for a while loop. However, a do-while loop is more compact if written properly. Page 13 of 14

Bonus #1 (+5 pts) he following program is executed. he user enters the integer 3 when prompted for a number. What is the output to the screen? Just fill in the blanks indicated in the output line shown Be careful on this problem. hink about what is being performed with which variables #include <iostream> using namespace std; void Summation( int& num, int& result ); int main () int number = 0; int result = 10; cout << Enter an integer between 1 and 10: ; cin >> number; Summation(number, result); // first function call Summation(number, result); // second function call // show the output for this cout statement only cout << Summation for << number<< is: <<result <<endl; return 0; void Summation( int& num, int& result) static int loop = 0; for (loop = num; num >=1; num--) result = result + loop; num = num 1; return; Answer: Summation for is Bonus #2 (+5 pts) What is the output of the following code segment if Input.txt contains the following values: 1 10 4 15 5 ifstream infile; int value; infile.open( Input.txt ); while(infile) // no priming read, so be careful of end result infile >> value; if (value >= 10) continue; else cout << value; Page 14 of 14