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!

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

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

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

Spring 2002 Page 1 of 8 READ THIS NOW!

Do not start the test until instructed to do so!

Fall 2002 Page 1 of 9 READ THIS NOW!

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

Do not start the test until instructed to do so!

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.

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

Setting Justification

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

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

CS 1704 Homework 2 C++ Pointer Basics Fall 2002

CS 1704 Intro Data Structures and Software Engineering Test 2 Fall 2001 READ THIS NOW!

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

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

1. Which of the following best describes the situation after Line 1 has been executed?

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

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

CS 1044 Project 1 Fall 2011

C++ Input/Output: Streams

Definition Matching (10 Points)

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

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

Introduction to Programming EC-105. Lecture 2

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

Fundamentals of Programming CS-110. Lecture 2

Multiple Choice Questions (20 questions * 5 points per question = 100 points)

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

What we will learn about this week:

2 nd Week Lecture Notes

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

Chapter 4 - Notes Control Structures I (Selection)

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

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

EECS 183 Fall 2015 Exam 2 Free Response (134 points)

Getting started with C++ (Part 2)

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

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

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

Multiple Choice Questions (20 questions * 6 points per question = 120 points)

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

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.

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

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

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

Chapter 3 - Notes Input/Output

CS101 PLEDGED SPRING 2001

WARM UP LESSONS BARE BASICS

C++ for Python Programmers


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

PROGRAMMING EXAMPLE: Checking Account Balance

CSCE 206: Structured Programming in C++

CS 1044 Program 2 Spring 2002

Chapter 2 C++ Fundamentals

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Introduction to C++ (Extensions to C)

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

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

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

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

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

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

Summary of basic C++-commands

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

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

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

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

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

Creating a C++ Program

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

REVIEW. The C++ Programming Language. CS 151 Review #2

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

Chapter 2. C++ Syntax and Semantics, and the Program Development Process. Dale/Weems 1

Programming. C++ Basics

CSCI 1061U Programming Workshop 2. C++ Basics

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

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

Computer Programming : C++

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)

File I/O Christian Schumacher, Info1 D-MAVT 2013

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

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

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

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

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

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

Introduction to Programming using C++

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

CS242 COMPUTER PROGRAMMING

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

LECTURE 02 INTRODUCTION TO C++

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

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

Computer Science II Lecture 1 Introduction and Background

Transcription:

READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties. Print your name in the space provided below. Print your name and ID number on the Opscan form; be sure to code your ID number correctly on the Opscan form. Code Form B on the Opscan. Choose the single best answer for each question some answers may be partially correct. If you mark more than one answer to a question, you will receive no credit for any of them. Unless a question involves determining whether given C++ code is syntactically correct, assume that it is. Unless a question specifically deals with compiler #include directives, you should assume the necessary header files have been included. Be careful to distinguish integer values from floating point values (containing a decimal point). In questions/answers that require a distinction between integer and real values, integers will be represented without a decimal point, whereas real values will have a decimal point, [1044 (integer), 1044.0 (floating point)]. This is a closed-book, closed-notes examination. No calculators or other electronic devices may be used during this examination. You may not discuss (in any form: written, verbal or electronic) the content of this examination with any student who has not taken it. You must return this test form when you complete the examination. Failure to adhere to any of these restrictions is an Honor Code violation. There are 40 equal-valued multiple-choice questions. The answers you mark on the Opscan form will be considered your official answers. When you have finished, sign the pledge at the bottom of this page and turn in the test and your Opscan. Do not start the test until instructed to do so! Name (Last, First) printed Pledge: On my honor, I have neither given nor received unauthorized aid on this examination. signature Summer I 2001 Page 1 of 10

For the next 2 questions, assume the input file stream ifile is connected to an input file whose contents are: 47 A321 (There's a single tab separating the '7' from the 'A'.) Consider execution of the following code fragment immediately after the file stream has been opened: int i1; char ch1 = 'x', ch2 = 'y', ch3 = 'z'; ifile >> ch1 >> i1 >> ch2 >> ch3; 1) The resulting value of the variable i1 would be: 1) 4 2) 47 3) 7 4) 3 5) 32 6) 2 7) 321 8) none of these 2) The resulting value of the variable ch3 would be: 1) '4' 2) '7' 3) ' ' (a space) 4) 'A' 5) '3' 6) '2' 7) '1' 8) '\n' 9) none of these For the next 3 questions, assume the following variable declarations and initializations: bool Burke; int a = 5, b = 0, c = 3; Determine the value assigned by each of the following statements to the relevant Boolean variable, or if there's something (syntactically) wrong with the expression; choose from the following answers: 3) Burke = 2 + b < c ; 1 true 2 false 3 syntax error 4) Burke = ( a + b < c a + b == c ); 5) Burke = (a!= 0) (a <= b) && (c < b); Summer I 2001 Page 2 of 10

For the next 2 questions, select the value assigned to the relevant variable, given the declarations: int IntVar; double DecVar; 1 2 3 4 5 6) IntVar = 19.0 / 5; 3 3.0 3.8 4 none of these 7) DecVar = 5 / 2; 2 2.0 2.5 0.4 none of these For the next 4 questions, suppose the (file) input stream In contains the following 5 lines of data (there's one tab character between columns and a newline character immediately after the last character on each line): 55 23 72 40 Gomer 17 30 95 28 Goober 6 34 82 66 Opie 19 62 36 21 Floyd 8 49 45 33 Bea What is the value of each of the indicated variables after the execution of the following program segment? int Zero = 0, One = 1, Two = 2, Three = 3, Four = 4; string First = "Andy", Second = "Barney", Third = "Otis"; In >> Zero >> One >> Two >> Two; In.ignore(100, '\n'); In >> Three >> Four >> Zero; In.ignore(100, '\n'); In.ignore(100, '\n'); In.ignore(100, '\t'); In >> First; In.ignore(100, '\n'); In >> Second; 1 2 3 4 5 8) Zero 0 30 55 95 none of these 9) Four 4 23 30 34 none of these 10) First "Andy" "Goober" "62" "6" none of these 11) Second "Floyd" " 8" "8" "\n" none of these For the following 3 questions, select the value of the given C++ arithmetic expression. Note that the presence of a decimal indicates a double value, rather than an int. 1 2 3 4 5 12) 7 / 5 + 4 / 5 1 2 2.2 not allowed none of these 13) 16 / 4 * 2 + 1 1 2 3 9 none of these 14) 1.0 + 7 / 2 4 4.0 4.5 not allowed none of these Summer I 2001 Page 3 of 10

15) What is the second step in Polya's four-step process? 1) Design a solution. 2) Understand the problem. 3) Test the solution. 4) Implement the solution. 5) Find an excuse. 6) none of these For the next 4 questions, assume the input file stream In is connected to an input file whose contents are: 3.14 2.76 828 0.43 0 27 (There's a single space separating each adjacent pair of values.) Consider execution of the following code fragment immediately after the file stream has been opened: int anint1; float afloat1, afloat2, afloat3; cout << fixed << showpoint; In >> anint1 >> afloat1; cout << anint1 << endl; cout << setprecision(3) << afloat1 << endl; In >> afloat2 >> afloat3; cout << setprecision(1) << afloat2 << endl; cout << setprecision(3) << afloat3 << endl; // A // B // C // D 16) In the statement labeled A, the value printed for the variable anint1 would be: 1) 3 2) 1 3) 4 4) 14 5) 2 6) 76 7) 828 8) 314 9) none of these 17) In the statement labeled B, the value printed for the variable afloat1 would be: 1) 0.1 2) 0.14 3) 0.140 4) 2.76 5) 2.760 6) 3.14 7) 3.140 8) 0.100 9) none of these 18) In the statement labeled C, the value printed for the variable afloat2 would be: 1) 3.1 2) 3.2 3) 2.7 4) 2.8 5) 2.76 6) 14.0 7) 8.2 8) 8.3 9) none of these 19) In the statement labeled D, the value printed for the variable afloat3 would be: 1) 0.828 2) 8.280 3) 828.0 4) 828.00 5) 828.000 6) 0.43 7) 0.430 8) 2.760 9) none of these Summer I 2001 Page 4 of 10

For the next 4 questions, consider writing a program that must read lines of data formatted in the following way. Each line will contain the name of a team, followed by a tab, followed by a score, followed by a tab, followed by another team name, followed by a tab, followed by another score, followed by a newline: <team name><tab><score><tab><team name><tab><score><newline> The team names are character strings and the scores are nonnegative integers. Assume that an input stream variable, In, has been opened on such a file, the current stream position is at the beginning of the file, and that the following variables have been declared: string Team1, Team2; int Score1, Score2; 20) Which of the following statements will correctly read the first team name into the variable Team1? 1) In >> Team1; 2) getline(in, Team1); 3) getline(in, Team1, '\t'); 4) 1, 2 and 3 5) 1 and 2 only 6) 1 and 3 only 7) 2 and 3 only 8) none of these 21) Assuming that the first team name, and the tab following it, have been read, which of the following statements will correctly read the first score into the variable Score1? 1) In >> Score1; 2) getline(in, Score1); 3) getline(in, Score1, '\t'); 4) 1, 2 and 3 5) 1 and 2 only 6) 1 and 3 only 7) 2 and 3 only 8) none of these For the next 2 questions suppose the input stream contains the following data: Florida State<tab>61<tab>Virginia<tab>7<newline> 22) Which of the following would be true after the following statements were executed? In >> Team1; // line 1 In >> Score1; // line 2 1) Team1 == "Florida State" 2) Team1 == "Florida" 3) Score1 == 61 4) An input failure occurred when line 1 was executed. 5) An input failure occurred when line 2 was executed. 6) 1 and 3 only 7) 2 and 3 only 8) 1 and 4 only 9) 2 and 5 only 10) none of these 23) Which of the following would be true after the following statements were executed, starting over at the beginning of the input stream? getline(in, Team1, '\t'); // line 1 In >> Score1; // line 2 getline(in, Team2, '\t'); // line 3 1) Team2 == "Virginia" 2) Team2 == "\tvirginia" 3) An input failure occurred when line 1 was executed. 4) An input failure occurred when line 2 was executed. 5) An input failure occurred when line 3 was executed. 6) 1 and 3 only 7) 2 and 3 only 8) 1 and 4 only 9) 2 and 5 only 10) none of these Summer I 2001 Page 5 of 10

For the next 2 questions, consider execution of the following switch statement: int Enter = 10; cin >> Enter; switch (Enter) { case 1: Enter = -4; break; case 2: Enter = -6; case 4: break; case 6: Enter = -8; break; default: Enter = -1; What would the value of Enter be after execution of this code if the given value were read for Enter? 1 2 3 4 5 6 24) 4-1 -4-6 -8 10 none of these 25) 3-1 -4-6 -8 10 none of these For the next 2 questions, assume the declarations: string FName = "Foghorn", LName = "Leghorn"; int Age = 74; 26) What would be printed by the statement: cout << FName + LName; 1) Foghorn Leghorn 2) Leghorn, Foghorn 3) FoghornLeghorn 4) The statement is not allowed. 5) None of these 27) What would be printed by the statement: cout << Fname.length(); 1) Foghorn 2) 7 3) 8 4) The statement is not allowed. 5) None of these 28) Which of the following statements would print the name Leghorn, followed by the value of Age, so that the value of Age would be right-justified to column 20? 1) cout << LName << setw(20) << Age; 2) cout << LName << setw(20 - LName.length()) << Age; 3) cout << LName << setw(20 + LName.length()) << Age; 4) cout << LName << setw(lname.length() - 20) << Age; 5) All of these Summer I 2001 Page 6 of 10

29) What output will the following program produce? (Be careful this may be tricky.) #include <iostream> using namespace std; int main( ) { int Score = 87, Rank = 2; if (Score >= 95) if (Rank <= 5) cout << "Nice job!"; else cout << "Good job!"; return 0; 1) Nice job! 2) Good job! 3) "Nice job!" 4) "Good job!" 5) both 1 and 2 6) both 3 and 4 7) No output is produced. 8) none of these 30) What is the value of the variable Z after the following code is executed? int W = 5, X = 9, Y = 5, Z = 1; if (X + Y >= 2 * W) { Z++; if (Y 3*W >= -X) Z--; else Z++; else { Z = -1; 1) 1 2) 0 3) 1 4) 2 5) 3 6) the code contains a syntax error 7) none of the above 31) What is the value printed for the variable Delta if the following code is executed? int Delta = 0, X = 4; if ( X % 2 == 1 ) Delta = Delta + X; X++; if ( X % 2 == 0 ) Delta = Delta + X; X++; if ( X % 2 == 0 ) Delta = Delta + X; cout << "Delta = " << Delta << endl; 1) 0 2) 1 3) 2 4) 3 5) 4 6) 5 7) 6 8) None of these Summer I 2001 Page 7 of 10

For the next 2 questions, consider the loop below is intended to print the output: 2 4 6 8 10 int N; for ( ; N <= 10; ) { // Line 1 cout << N << ' '; 32) How should the first blank in Line 1 be filled? 1) N = 0 2) N > 0 3) N = 2 4) It should be left blank. 5) It doesn't matter. 33) How should the second blank in Line 1 be filled? 1) N++ 2) N = N + 2 3) N = 2 * N 4) It should be left blank. 5) It doesn't matter. For the next 2 questions, consider the following short program #include <iostream> using namespace std; int main() { const int LIMIT = 10; int howmany; int Output = 5; while ( howmany < LIMIT ) { cout << Output << endl; howmany = howmany + 1; return 0; 34) What is the loop control variable in the code above? 1) LIMIT 2) howmany 3) Output 4) while 5) cout 6) There isn't one. 7) None of these 35) What is missing in the design of the loop? 1) the initialization of the loop control variable 2) the testing of the loop control variable 3) the updating of the loop control variable 4) the loop control variable 5) Nothing is missing. Summer I 2001 Page 8 of 10

36) The loop below is intended to print the output: 3 6 9 12 15 18 21 int N = 0; while ( ) { // Line 1 N = N + 3; cout << N << ' '; How should the blank in Line 1 be filled in order to accomplish that? 1) N <= 21 2) N < 21 3) N <= 18 4) Any of them will do 5) 1 and 2 only 6) 1 and 3 only 7) 2 and 3 only 8) None of these 37) Which of the following represents the correct logic for a read-until-input-failure loop? 1) while ( input succeeded ) { // read next data item // process last data read 2) // read first data item while ( input succeeded ) { // process last data read 3) while ( input succeeded ) { // process last data read // read next data item 4) // read first data item while ( input succeeded ) { // process last data read // read next data item 5) 1 and 3 only 6) 2 and 4 only 7) None of these Summer I 2001 Page 9 of 10

For the next 3 questions, consider the following program and input file: #include <iostream> #include <fstream> using namespace std; int main() { // Line 1 const int stopvalue = -1; // Line 2 int nextvalue = 0; // Line 3 ifstream In("Data.txt"); // Line 4 In >> nextvalue; // Line 5 Data.txt 3 10 7 25-1 31-1 while ( nextvalue!= stopvalue ) { // Line 6 cout << nextvalue << ' '; // Line 7 In >> nextvalue; // Line 8 In.close(); // Line 9 return 0; // Line 10 38) What output would the program produce with the input file given above? 1) 3 10 7 25-1 2) 3 10 7 25 3) 3 10 7 25 1 31-1 4) 3 10 7 25 1 31 5) 3 10 7 25 1 31 1 1 1... (forever) 39) What output would the program produce if the input file were changed to: Data.txt 1) 3 10 7 25-1 2) 3 10 7 25 3) 3 10 7 25 31 4) 3 10 7 25 31 1 1 1... (forever) 5) 3 10 7 25 31 31 31 31... (forever) 3 10 7 25 31 40) What output would the program produce if the program were changed by deleting Line 5 and reversing the order of Line 7 and Line 8 and the first input file were used? 1) 3 10 7 25-1 2) 3 10 7 25 3) 3 10 7 25 1 31-1 4) 3 10 7 25 1 31 5) 3 10 7 25 1 31 1 1 1... (forever) Summer I 2001 Page 10 of 10