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)

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

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

True or False (15 Points)

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

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

Definition Matching (10 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 (!

True or False (14 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?

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

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

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

REPETITION CONTROL STRUCTURE LOGO

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

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

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

CSCE 206: Structured Programming in C++

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

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

C++ Final Exam 2017/2018

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

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

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

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

Chapter 5: Loops and Files

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

Lab Instructor : Jean Lai

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

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

CS242 COMPUTER PROGRAMMING

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

True or False (15 Points)

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

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

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++

Scientific Computing

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

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

Programming. C++ Basics

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

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

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

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

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

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

Increment and the While. Class 15

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

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

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:

A SHORT COURSE ON C++

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

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

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

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

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

CMSC 202 Midterm Exam 1 Fall 2015

Objectives. In this chapter, you will:

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

CSCE Practice Midterm. Data Types

In this chapter you will learn:

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

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:

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

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

Chapter 3. More Flow of Control

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)

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

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

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

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

Introduction to Computer Science Midterm 3 Fall, Points

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

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

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


204111: Computer and Programming

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!

WARM UP LESSONS BARE BASICS

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

Chapter 4 - Notes Control Structures I (Selection)

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

A First Program - Greeting.cpp

CMPS 221 Sample Final

CS201- Introduction to Programming Current Quizzes

Lecture 3 The character, string data Types Files

C++ Programming for Non-C Programmers. Supplement

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Chapter 3 Problem Solving and the Computer

Computer Programming : C++

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 13

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 Page 2 of 13

13) With respect to a given block, a is any identifier declared outside that block. 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 1? 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-value parameter named x of data type float a pass-by-reference parameter named num of data type int. write a valid function prototype and function heading for the function GetNums? Function Prototype: Function Heading: 16) (4 pts) What is the output of the following code segment? int score = 85; if (score <= 90) score = score - 5; if (score <= 80) score = score - 10; if (score > 75) score = score - 15; cout << score << endl; Page 3 of 13

17) (6 pts) consider the following code segment where num is a declared int variable if (num <= 10) cout << less than 10 << endl; if (num > 15) cout << greater than 15\n ; else if (num < 5) cout << num is << num << endl; else cout << greater than 10\n ; 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. a) What is the output to the screen when num = 10? b) What is the output to the screen when num = 4? Page 4 of 13

18) (12 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 do-while 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 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) he default switch label is required in a switch statement. 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. Page 5 of 13

19) (6 pts) When the following segment of code is executed, the standard input stream contains the numbers 1 2 2 3 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 + 1; Rewrite the above code segment as a for loop such that the same output is obtained. Page 6 of 13

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)? 22) (8 pts) Consider the following segment of code int number; cout << Enter an integer between 0 and 10: ; cin >> number; switch(number) 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 13

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) (12 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 13

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 13

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 13

27) (16 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 11 of 13

28) (16 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 12 of 13

Extra Credit #2) (5 pts) For the following code segment, what is written to the terminal? Place a single character in each box, skip a box to indicate a space, and skip a row to indicate a blank line. int k; int j = 0; while ( j <= 3) for (k = j+1; k >= 0; k--) cout << k; cout << - << j << endl; j++; Bonus #2 (+5 pts) What is the output of the following code segment if Input.txt contains the following values: 10 10 4 15 5 3 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 13 of 13