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

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

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

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

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

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)

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)

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

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

Introduction to C ++

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

Programming. C++ Basics

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

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

CSCE 206: Structured Programming in C++

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

A SHORT COURSE ON C++

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

Creating a C++ Program

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

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

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

C++ Input/Output: Streams

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

Summary of basic C++-commands

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

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.

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

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

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

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

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!

Computer Programming : C++

Lab Instructor : Jean Lai

CSCE 121 ENGR 112 List of Topics for Exam 1

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

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

Introduction to Programming EC-105. Lecture 2

Chapter 2: Introduction to C++

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

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

2 nd Week Lecture Notes

CSCE 110 PROGRAMMING FUNDAMENTALS

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Objectives. In this chapter, you will:

Chapter 2: Overview of C++

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

Do not turn to the next page until the start of the exam.

Lecture 3 The character, string data Types Files

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

What we will learn about this week:

Fundamentals of Programming CS-110. Lecture 2

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

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

LECTURE 02 INTRODUCTION TO C++

A First Program - Greeting.cpp

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

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

Chapter 1 INTRODUCTION

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Definition Matching (10 Points)

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

Chapter 2. C++ Basics

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

What will happen if the user presses <enter> after every input? Will all work like below? Enter the values of x, y, z: Result: The sum is 17.

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Grades. Notes (by question) Score Num Students Approx Grade A 90s 7 A 80s 20 B 70s 9 C 60s 9 C

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

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

CSCI 123 Introduction to Programming Concepts in C++

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

PROGRAMMING EXAMPLE: Checking Account Balance

Introduction to Programming

Introduction to Programming using C++

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

Introduction to C++ (Extensions to C)

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

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:

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

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

CMSC 202 Midterm Exam 1 Fall 2015

CS 1428 Review. CS 2308 :: Spring 2016 Molly O Neil

CSCI 102 Fall 2010 Exam #1

CS242 COMPUTER PROGRAMMING

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

Problem Solving: Storyboards for User Interaction

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

6.096 Introduction to C++ January (IAP) 2009

Introduction to C ++

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

Software Design & Programming I

BITG 1233: Introduction to C++

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

CSCE Practice Midterm. Data Types

Transcription:

Name: Print legibly! Section: COMPUTER SCIENCE 261 PROGRAMMING CONCEPTS EXAM 1 VERSION 1 Fall 2014 150 Points Absolutely no electronic devices may be used during this exam. 1. No cell phones, computers, calculators, music players, headphones, etc. 2. Set your cell phone to be completely silent, not vibrate. If it vibrates during the exam, it will be confiscated. 3. Place these and any other electronic devices in your backpack, out of immediate reach. Failure to follow these instructions will result in a zero for the exam. You are expected to complete this exam in its entirety before leaving the exam room. CLOSED BOOK CLOSED NOTES YOUR SCORE: POINTS 0

A little humor to relax you All of your answers must be written in clear, readable text. If we cannot read it, it is not correct. 1. 10 points. True or False. Write T or F to the left of each statement. T Pseudocode helps programmers develop their algorithm before implementing it with code. F Consider the statement (expr1) (expr2). Once expr1 evaluates to 1, the compiler will continue to evaluate expr2. T You cannot write an else statement without a corresponding if. F You can not use a conditional statement (such as if else or switch) inside the code block of another conditional statement. F If I know how many times I want a loop to run, the best choice is a while loop. T The concept of abstraction allows users of functions to view the functions as a black box. F Several math functions, e.g., pow() and abs(), are standard functions and do not require the user to include a header file. T A digital photo (or music file) is a stream of bits, nothing more. F fin and fout are the standard file input and output streams. F The continue statement will quit a loop, i.e., stop the loop from repeating further. 1

2. 9 points. Given the following variable declarations, write the output of each cout statement on the line to the right. There are no errors in this code, but Visual Studio may show warnings for some. Show the output that would be produced if warnings are ignored and the program is executed. int a = 17, b = 3; double x(5.0); int y(0); y = a / b; cout << y; x = a / b; cout << x; y = a % b; cout << y; y = b % a; cout << y; y = (double) a / b; cout << y; x = (double) a / b; cout << x; 5 5 2 3 5 5.6667 3. 3 points. Given the following variable declarations, write true or false on the line to the right to indicate the result of each logical statement. int a = -23, b = 7, c = 15, x = 0, y = 32; ( a + 1 b )!= ( y / 3 ) true ( a < b b < c ) && ( x < a ) false ( a < b && c < b) ( x < y && x < b ) true 2

4. 15 points. Fill in each of the 13 blanks. In order to create a different pseudorandom sequence we give a different integer called a seed as an argument to the srand function. What is the output of the following statements: a is 5 and x is 5 int a = 4; int x = ++a; cout << a is << a << and x is << x; What is the output of the following statements: a is 5 and x is 4 int a = 4; int x = a++; cout << a is << a << and x is << x; The most appropriate type of variable for y below is bool. y = (!(a > 5.0)) The break statement is used to end the evaluation of further choices in a switch statement. int i=0; do cout << i << endl; ++i; while (i < 10); An else statement does NOT [does does not] have a boolean expression associated with. Valid whitespace characters are spaces, tabs, and newlines. We create a class to define a new data type. The term scope describes which areas of a program can access specific variables. Including a function prototype above main() allows a programmer to place the function definition below main(). 3

5. 7 points. Multiple choice. A const keyword when used in a variable declaration: a) means that the variable can only be assigned data with digits. b) means the variable name should be written in camel case. c) means the data assigned to the variable cannot be modified. d) all of the above. e) none of the above. What is the result of this code snippet? for (int i=0; i <= 9; ++i) cout << i << ", "; cout << i; a) 0,1,2,3,4,5,6,7,8,9 b) 0,1,2,3,4,5,6,7,8,9,10 c) Error d) Nothing (no output) Circle the functions that are available to an input file stream: a) eof() b) put() c) open() d) unsuccessful() e) clear() 6. 6 points. What is written on standard output after the following C++ snippet of code is executed. int option = 3; switch (option) case 1: cout << "Red\n"; break; case 2: cout << "Green\n"; case 3: cout << "Blue\n"; case 4: case 5: cout << "Cyan\n"; default: cout << "Magenta\n"; PUT OUTPUT IN BOX: Blue Cyan Magenta 4

7. 10 points. Write a snippet of code that will read in user integer input until the inputted value is between 10 and 20 (inclusively), and will then output that number divided by 4 (with a decimal value, if not evenly divisible). If the user inputs a value outside the range, the user should be notified of the input error, and prompted to ask for input again. Notes: Don't worry about invalid inputs (i.e. putting a letter in instead of a number). Don't worry about #includes or defining a main function, but any variables you use that are not in a library (like cin/cout) must be declared. int input; do cout << Enter an integer between 10 and 20 << endl; cin >> input; while ((input < 10) (input > 20)); cout << The value is << input / 4.0 << endl; 8. 15 points. Write a snippet of code to check if a given integer is prime. First check that the user entered a number greater than one. If not, display an error message and exit. If yes, output whether the number provided is prime. Hint: To check if a number n is prime, it should not be divisible by any number between 2 and n/2. Don't worry about #includes or defining a main function, but any variables you use that are not in a library (like cin/cout) must be declared. int input; cout << "Enter a number to check whether it is prime" << endl; cin >> input; if (input <= 1) cerr << "error on input; exiting" << endl; return 1; bool prime = 1; for (int i = 2; i <= (input / 2); i++) if (( input % i) == 0) prime = 0; if (prime == 0) cout << "input is NOT prime" << endl; else cout << "input is prime" << endl; 5

9. 15 points. Ima Poorstudent has written the following program, which you can see has several mistakes. Fix these mistakes by updating the code so that it (1) will compile and (2) makes sense. All input/output should be from standard I/O. Ignore compiler warnings. include <fstream> using namespace std; è #include<iostream> int myfunc(int x); int main() char chara; cout << Enter a character, then press enter \n; è \n ; cin << char; è cin >> chara; for (j = 4; j < 8; j++); è for (int j=4; j<8; j++) cout << j is /t << j << endl; è \t cout << Function is << myfunc(7.4) << endl; return 0; void myfunc(int x); è int myfunc(int x) fout << I m in the function with x = << x << endl; è cout if ( x = 0 ) è if (x == 0) cout << x is equal to zero << endl; return x; 6

10. 10 points. What is the output of the following code segment? int countit = 2; PUT OUTPUT IN BOX: int total = 0; cout << "count total" << endl; while( countit <= 7 ) cout << countit << " " << total << endl; total += countit; ++countit; cout << "Final total is " << total << endl; count total 2 0 3 2 4 5 5 9 6 14 7 20 Final total is 27 11. 10 points. What is the output of the following code segment? void thefunc(int x, int y, int z); int main() double a(9.2), b(8.5), c(7.8); cout << a << " " << b << " " << c << endl; thefunc(a, b, c); cout << a << " " << b << " " << c << endl; return 0; void thefunc(int x, int y, int z) cout << x << " " << y << " " << z << endl; x = 3; y = 4; z = 5; cout << x << " " << y << " " << z << endl; PUT OUTPUT IN BOX: 9.2 8.5 7.8 9 8 7 3 4 5 9.2 8.5 7.8 7

12. 20 points. Complete the program below such that: a) a function prototype and definition is added for MPG, which has no return and no parameters b) a function prototype and definition is added for dompg, which returns a double and has two double parameters (miles and gas) c) function main calls function MPG d) function MPG asks the user to enter miles traveled and gas (in gallons) used e) function MPG calls dompg f) function dompg calculates fuel efficiency (miles per gallon) g) function dompg returns the value calculated h) function MPG displays the result returned #include<iostream> using namespace std; void MPG(); double dompg(double miles, double gas); int main() MPG(); return 0; void MPG() double miles, gas; cout << Miles traveled? ; cin >> miles; cout << Gas used (in gallons)? ; cin >> gas; cout << dompg(miles, gas) << miles per gallon << endl; double dompg(double miles, double gas) return (miles / gas); 8

13. 20 points. In this problem, you are to write short C++ snippets of code (no main function is required). Assume the proper libraries have been included. a) Write a single line C++ statement that declares and opens the file mydatain.txt for an input stream named myinput. b) Write a snippet of code that tests whether the input stream from (a) was successfully opened. If the file did not open properly, quit the program. c) Write a single line C++ statement that reads one line of data from the file mydatain.txt using myinput. The line of data consists of one integer accountnum and a floating point number currentbalance. (You may assume that the variables are declared properly.) d) Write a single line C++ statement that declares and opens the file mydataout.txt for an output stream named myoutput. e) Write a single line C++ statement that writes one line of data to the file mydataout.txt using myoutput. The line of data consists of the integer and floating point number from part (c). The only difference is you should subtract 50 from currentbalance. a) ifstream myinput( mydatain.txt ); b) if myinput.fail() cerr << File error; exiting << endl; return 1; c) myinput >> accountnum >> currentbalance; d) ofstream myoutput( mydataout.txt ); e) myoutput << accountnum << \t << (currentbalance 50) << endl; 9