CSC 309/404 Section 901/910 Spring 2017 Midterm Exam Due: May 7 (Sun) 2015, 11:59 pm

Similar documents
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.

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

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

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

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

Review Questions for Final Exam

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

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)

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

CMSC 202 Midterm Exam 1 Fall 2015

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 2: Basic Elements of C++

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

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

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

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

Objectives. In this chapter, you will:

/************************************************ * CSC 309/404 Review for Final -- Solutions * ************************************************/

Fundamentals of Programming. Lecture 19 Hamed Rasifard

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

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

Introduction to Programming EC-105. Lecture 2

CSCE 206: Structured Programming in C++

C++_ MARKS 40 MIN

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

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

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)


CIS*2430 (Fall 2012) Miterm Exam (I) Solutions

Assignment #1 (50 points; due 11:59 P.M.)

EE 109 Lab 8a Conversion Experience

Study Guide for Test 2

Java Basics Programming Fundamentals

Assignment 3: Distance COP3330 Fall 2017

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

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

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

REPETITION CONTROL STRUCTURE LOGO

Fundamentals of Programming CS-110. Lecture 2

Introduction to Computer Science Midterm 3 Fall, Points

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Scientific Computing

2 nd Week Lecture Notes

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

Increment and the While. Class 15

Exam I Review Questions Fall 2010

PIC 10A. Final Review: Part I

Programming Language. Functions. Eng. Anis Nazer First Semester

Exam 3 Chapters 7 & 9

Introduction to Programming

Getting started with C++ (Part 2)

EECS 183, Week 5. General. Variables I/O. 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library?

Computer Programming : C++

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

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

Review Questions for Final Exam KEY

Midterm Practice Exam

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

Programming Using C Homework 5

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

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

UNIT- 3 Introduction to C++

Lecture 23: Pointer Arithmetic

Data Structures (CS301) LAB

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

CSci 1113 Final. Name: Student ID:

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

Chapter 1 INTRODUCTION

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

CSCE 110 PROGRAMMING FUNDAMENTALS

Structured Data. CIS 15 : Spring 2007

C++ basics Getting started with, and Data Types.

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

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

Programming. C++ Basics

CSCI 111 First Midterm Exam Spring Solutions 09.05am 09.55am, Wednesday, March 14, 2018

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

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

int x = 5; double y = 3; // Integer division rounds the result down to the nearest whole number. cout << "1a: " << x / 3 << endl; //1

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

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

CS 201 (Intro. to Computing) Fall 2015 Sabancı University Sample Questions for Midterm 1

Object oriented programming

PIC 10A. Review for Midterm I

A First Program - Greeting.cpp

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

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

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

WARM UP LESSONS BARE BASICS

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

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

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

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

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

C and C++ I. Spring 2014 Carola Wenk

Transcription:

CSC 309/404 Section 901/910 Spring 2017 Midterm Exam Due: May 7 (Sun) 2015, 11:59 pm Directions: This is a take-home exam. Type your answers in an electronic file (all in ONE file), in a pdf/doc/txt format, and submit it on D2L. No late submissions or multiple files are accepted. You must also write your name and the course number (e.g. CSC 309 Online ) at the top of the file. Submissions without those information will be returned ungraded. ================================================= 1. [10 points] For each of the following pieces of code, what would be printed to the screen? (a) int x = 3, y = 9; while (x < y) cout << x << " " << y << endl; x = x + 3; (b) int x = 3, y = 9; do cout << x << " " << y << endl; x = x + 3; while (x < y); (c) int main() int x = 7, y = 2; fn1(x, y); cout << x << ", " << y << endl; fn2(x, y); cout << x << ", " << y << endl; return 0; void fn1(int& n1, int& n2) if (n1 > n2) n2 = n2 * 2; void fn2(int n1, int n2) if (n1 > n2) n2 = n2 * 2; 1

2. [5 points] What does this function Mystery do (in general)? Choose one that applies. int Mystery(int a, int n) int i = 1, x = 0; while (i < n) x += a; i++; return x; Hint: Try some example values such as a=2 and n=3 to develop your intuition. (a) It computes a + n and returns the value. (b) It computes a * n and returns the value. (c) It computes a + (n-1) and returns the value. (d) It computes a * (n-1) and returns the value. (e) It computes (a-1) + n and returns the value. (f) It computes (a-1) * n and returns the value. (g) It computes (a-1) + (n-1) and returns the value. (h) It computes (a-1) * (n-1) and returns the value. 3. [15 points] Write a function computebill which computes a service bill for a customer at an internet provider based on his/her package and usage hours. This provider has three different subscription packages for its customers: Package A: For $9.95 per month, 10 hours of access are provided. Additional hours are $2.00 per hour. Package B: For $14.95 per month, 20 hours of access are provided. Additional hours are $1.00 per hour. Package C: For $19.95 per month, unlimited access is provided. Here are some calculation examples for your reference. - Package A, 12.6 hours => $15.15; 8.6 hours => $9.95 - Package B, 9.2 hours => $14.95; 30.1 hours => $25.05 - Package C, 8.6 hours => $19.95; 40.6 hours => $19.95 The function should have two parameters: a char for the package (such as A, B ), and a double for the hours. The prototype is: double computebill(char pack, double hours); Note that you can make the package C as the default case. Also note the function should NOT PRINT anything at all. 2

4. [5 points] Using the function computebill from the previous question, complete the following program. #include <iostream> using namespace std; // prototypes double computebill(char pack, double hours); int main() char package; double usage; cout << "What is your package (A/B/C)? "; cin >> package; cout << "How many hours did you use? "; cin >> usage; // TO DO: Call the function to obtain the bill, and // display the bill amount in the form "$xx.yy" (i.e., // a dollar sign and two decimal digits). 5. [15 points] Write a function sumoddinrange. This function has two int parameters ( begin and end in the prototype below) which are intended to specify a range in the number line, and returns the summation of all odd integers between the range (both ends inclusive). For example, if the function is called with 3 (for begin ) and 20 (for end ), the function returns the result of 3 + 5 + 7 +... + 17 + 19. int sumoddinrange(int begin, int end); 6. [10 points] Write a function YesOrNo whose prototype is bool YesOrNo(const char question[]); This function receives a cstring ( question ) and does the following in order: Displays the parameter cstring, followed by (1 = Yes, 2 = No), to cout. Receives the user s input (an integer) from keyboard. Goes back to the first step when the entered integer was neither 1 nor 2. (after a correct value (1 or 2) is obtained) Returns true if the value was 1, or false if the value was 2. For example, for a function call: bool answer = YesOrNo("Do you like ice cream"); A sample session is: Do you like ice cream (1 = Yes, 2 = No) 3 Do you like ice cream (1 = Yes, 2 = No) -1 Do you like ice cream (1 = Yes, 2 = No) 1 3

And the function returns true in this case. 7. [10 points] Write a function indexof. This function receives a cstring and a character which is to be searched in the string. This function function returns the index of the search character if it exists in the array, or -1 if it does not. The returned index is 0-based. Also if the array contains more than one occurrence of the character, it returns the index of the left-most occurrence. For instance, if the cstring was abab and the search character was b, the function returns 1; whereas if the character was c, the function returns -1. Write the function WITHOUT using any C++ library functions. int indexof(const char str[], char ch); 8. [7 points] For each statement below, say true or false. You do not have to give a reason. (a) The member variables of a class (all of them) must be of the same type. (b) The member variables of a class (all of them) must be of C++ primitive type(s) (e.g. int, char, double). (c) A class can have more than one constructor. (d) A default constructor can have up to two parameters. (e) A member function of a class can change the value of a private member variable of the class. 9. [3 points] Suppose your program contains the following class definition (along with definitions of the member functions): class YourClass public: YourClass( int newinfo, char morenewinfo); YourClass( ); void dostuff( ); private: int information; char moreinformation; ; Which of the following are illegal or problematic (to be interpreted for unintended meaning)? YourClass anobject(42, A ); YourClass anotherobject; YourClass yetanotherobject(); // (A) // (B) // (C) (a) (B) only (b) (B) and (C) (c) (C) only (d) (A) only (e) None of the above. 4

10. [10 points] Following is a class CounterType which represents a counter (to keep track of the number of occurrences, for example). Write the definitions of the member functions (1), (2) and (3) indicated below. Use the correct/complete syntax such that the definitions are written OUTSIDE/below the class interface part. class CounterType private: int count; public: CounterType(); // sets the count to 0 CounterType(int init); // (1) sets the count to init ; you may assume // init is non-negative. int get(); // returns the current count value void set(int cnt); // sets the counter to a count given as an argument void increment(); // increases the count by 1 void increment(int inc); // (2) increases the count by inc ; you may assume // inc is non-negative. void decrement(); // decreases the count by 1 void decrement(int dec); // (3) decreases the count by dec only if the resulting // value (after subtracting all of dec ) does // NOT yield a negative number; if it does, // prints an error message and doesn t decrement; // you may assume dec is non-negative. void print(); // outputs the count to cout ; CounterType::CounterType() count = 0; // Asssume other member functions (besides yours) are written.. // YOUR CODE GOES HERE.. 11. [10 points] Using the CounterType class you wrote above, write an application (main()) which does the following. Note that you may assume that the class CounterType is completely and correctly written. Also note that each step below may be accomplished by more than one line of code. Declare/create a CounterType object c1 by using the default constructor. Increment c1 by 3 by calling the method increment() three times. Ask the user for a number to increment c1. Then increment c1 by calling the other increment method ONCE, with an appropriate parameter. Declare/create another CounterType object c2 with an initial value which is the same as the current counter value of c1. Call the constructor with one int parameter to do this. Decrement c2 by 2 by calling the decrement method with an int parameter. Repeat this procedure 5 times, by using a loop. Print the current counter value of c2 to the terminal. 5