CSCI 111 Midterm 1 Exam Spring Solutions 09.00am 09.50am, Monday, March 21, 2016

Similar documents
CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

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

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

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

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

CSCI 111 Midterm 2 Exam Spring Solutions 09.00am 09.50am, Wednesday, May 04, 2016

CSCI 111 Second Midterm Exam Spring Solutions 09.05am 09.55am, Monday, May 07, 2018

Lab Instructor : Jean Lai

CSci 1113 Midterm 1. Name: Student ID:

CSCI 111 Second Midterm Exam Spring Solutions 09.00am 09.50am, Wednesday, May 10, 2017

Problem 1 Write the best title lines for the functions that are called by the following main program. Do not supply blocks for the functions.

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

Declaring a 2D Array

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, November 24, 2015

Problem 1 Write the best title lines for the functions that are called by the following main program. Do not supply blocks for the functions.

CSCI 111 Second Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, November 29, 2017

Programming Language. Functions. Eng. Anis Nazer First Semester

CSCE 2004 Midterm Exam Spring 2017

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Errata for C++ and Algorithmic Thinking for the Complete Beginner

PASS$MOCK$EXAM$ $FOR$PRACTICE$ONLY$

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Pre- Defined Func-ons in C++ Review for Midterm #1

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Al Imam Mohammad Ibn Saud Islamic University College of Computer and Information Sciences Computer Science Department

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

Problem 1 Write the best title lines for the functions that are called by the following main program. Do not supply the blocks for the functions.

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

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

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

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

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

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

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

A SHORT COURSE ON C++

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

CSCE Practice Midterm. Data Types

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

CS 007A Midterm 1 Practice Chapters 1 5

Al Imam Mohammad Ibn Saud Islamic University College of Computer and Information Sciences Computer Science Department

FOR loops! Objectives! 1E3! Topic 7! n To be able to write a FOR statement! n To be able to predict what a given FOR statement will do.!

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

Introduction to FORTRAN

C++ Programming Lecture 11 Functions Part I

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

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

Introduction to Programming I COS1511 School of Computing Revision Notes

CSci 1113 Midterm 2. Name: Student ID:

Question 2. [5 points] Given the following symbolic constant definition

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

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

Review Problems for Final Exam. 1. What is the output of the following program? #include <iostream> #include <string> using namespace std;

Using Free Functions

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

University of Dublin

2.1-First.cpp #include <iostream> // contains information to support input / output using namespace std;

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.6

Chapter 7 - Notes User-Defined Functions II

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:

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

PIC 10A. Review for Midterm I

Why Is Repetition Needed?

Practice test for midterm 1

do { statements } while (condition);

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Al Imam Mohammad Ibn Saud Islamic University College of Computer and Information Sciences Computer Science Department

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

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Chapter 3 - Functions

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

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

EP578 Computing for Physicists

Programming I Lecture 7

CSci 1113 Final. Name: Student ID:

Chapter 7 Functions. As an example, let us write some code to make a virtual cup of coffee.

ECOR Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

Lab01: C++ Expressions ES036a: Programming Fundamentals Fall 2007

Companion C++ Examples

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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

CSCE Practice Midterm. Data Types

(created by professor Marina Tanasyuk) FUNCTIONS

3.1. Chapter 3: The cin Object. Expressions and Interactivity

CMSC 202 Midterm Exam 1 Fall 2015

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

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:

Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names

CS Spring 05 - MidTerm

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

Your First C++ Program. September 1, 2010

CSI33 Data Structures

Transcription:

QUEENS COLLEGE Department of Computer Science CSCI 111 Midterm 1 Exam Spring 2016 03.21.16 Solutions 09.00am 09.50am, Monday, March 21, 2016 Problem 1 ( points) Write a complete C++ program that prints the numbers from 28 to 387 with 10 numbers (separated by spaces) on each line. The output from your program should begin 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 for (int n = 28; n < 388; n++) { cout << n << " "; if (n % 10 == 7) return 0;

Problem 2 ( points) Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of C++ code. No answer can use more than two lines. Assume the following variables have been declared and have legal values int x = 18; (a) Print to the user s screen the sentence:in C++ an endl makes a new line. cout << "In C++ an endl makes a new line." << endl; (b) Print the square of x. cout << x * x << endl; (c) Print a random number with 4 digits. cout << rand() % 9000 + 1000 << endl; (d) Print all numbers less that 1000 that are either divisible 7 or are even and greater than 400. for (int n = 1; n < 1000; n++) if ((n % 7 == 0) ((n % 2 == 0) && (n > 400))) cout << n << endl; (e) Print the square root of 3/8. cout << sqrt(3.0 / 8) << endl;

Problem 3 ( points) Consider the following C++ program. #include <cmath> double x = 9.0, y = 16.0, z = 25.0; string a = "b", b = "a"; cout << sqrt(z) << endl; cout << sqrt(sqrt(y)) << endl; if ((x + y)!= z) cout << b << endl; cout << a << "a" << "b" << b << endl; if (a == "b") cout << z; else cout << x; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the output at line (a)? 5 (b) What is the output at line (b)? 2 (c) What is the output at line (c)? (d) What is the output at line (d)? baba (e) What is the output at line (e)? 25

Problem 4 ( points) Write a complete C++ program that asks the user for a number n and prints 2 large copies of an X pattern (each with height n) in a horizontal sequence. For example, if the user specified 5 for n, the program would print as follows: * ** * * * * * * * * * * * * ** * (Each X pattern should begin in the column after the previous one ends. Do not try to check whether the user input is legal or sensible.) int n; cout << "Enter a number: "; cin >> n; for (int row = 1; row <= n; row++) { for (int pattern = 1; pattern <= 2; pattern++) { for (int c = 1; c <= n; c++) { if (row == c (row + c) == (n + 1)) cout << "*"; else cout << " "; return 0;

QUEENS COLLEGE Department of Computer Science CSCI 111 Midterm 1 Exam Spring 2016 03.21.16 Solutions 09.00am 09.50am, Monday, March 21, 2016 Problem 1 ( points) Write a complete C++ program that prints the numbers from 980 down to 666 with 6 numbers (separated by spaces) on each line. The output from your program should begin 980 979 978 977 976 975 974 973 972 971 970 969 for (int n = 980; n >= 666; n--) { cout << n << " "; if (n % 6 == 3) return 0;

Problem 2 ( points) Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of C++ code. No answer can use more than two lines. Assume the following variables have been declared and have legal values int y = 12; (a) Print to the user s screen the sentence:c++ output uses cout. cout << "C++ output uses cout." << endl; (b) Print the square root of y. cout << sqrt((double) y) << endl; (c) Print a random 3 digit even number. cout << 2 * (rand() % 450) + 100 << endl; (d) Print all numbers less that 1000 that end in a 7 and are divisible by 3. for (int n = 1; n < 1000; n++) if ((n % 10 == 7) && (n % 3 == 0)) cout << n << endl; (e) Print the square of 3/8. cout << (3.0 / 8) * (3.0 / 8) << endl;

Problem 3 ( points) Consider the following C++ program. #include <cmath> double x = 9.0, y = 16.0, z = 25.0; string a = "a", b = "a"; cout << sqrt(y) << endl; cout << sqrt(y) + sqrt(x) << endl; if ((x + y) == z) cout << b << endl; cout << a << "a" << "b" << b << endl; if (a == "b") cout << z; else cout << x; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the output at line (a)? 4 (b) What is the output at line (b)? 7 (c) What is the output at line (c)? a (d) What is the output at line (d)? aaba (e) What is the output at line (e)? 9

Problem 4 ( points) Write a complete C++ program that asks the user for a number n and prints 3 large copies of an L pattern (each with height n) in a horizontal sequence. For example, if the user specified 4 for n, the program would print as follows: * * * * * * * * * **** **** **** (Each L pattern should begin after a gap of one column after the previous one ends. Do not try to check whether the user input is legal or sensible.) int n; cout << "Enter a number: "; cin >> n; for (int row = 1; row <= n; row++) { for (int pattern = 1; pattern <= 3; pattern++) { for (int c = 1; c <= n; c++) { if (c == 1 row == n) cout << "*"; else cout << " "; cout << " "; return 0;

QUEENS COLLEGE Department of Computer Science CSCI 111 Midterm 1 Exam Spring 2016 03.21.16 Solutions 02.45pm 03.35pm, Monday, March 21, 2016 Problem 1 ( points) Write a complete C++ program that prints the numbers from 28 to 387 with 10 numbers (separated by commas) on each line. The output from your program should begin 28,29,30,31,32,33,34,35,36,37 38,39,40,41,42,43,44,45,46,47 for (int n = 28; n < 388; n++) { cout << n; if (n % 10 == 7) else cout << ","; return 0;

Problem 2 ( points) Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of C++ code. No answer can use more than two lines. Assume the following variables have been declared and have legal values int z = 5; (a) Print to the user s screen the words:endl makes a line and for makes a loop cout << "endl makes a line and for makes a loop" << endl; (b) Print the cube of z + 1. cout << (z + 1) * (z + 1) * (z + 1) << endl; (c) Print a random 2 digit number to the user s screen. cout << rand() % 90 + 10 << endl; (d) Print all three digit numbers that either end in a 7 or are even and divisible by 7. for (int n = 100; n < 1000; n++) if ((n % 10 == 7) ((n % 2 == 0) && (n % 7 == 0))) cout << n << endl; (e) Print the square root of 3/7. cout << sqrt(3.0/7) << endl;

Problem 3 ( points) Consider the following C++ program. #include <cmath> double x = 49.0, y = 81.0, z = 25.0; string a = "ab", b = "ba"; cout << sqrt(x) << endl; cout << sqrt(sqrt(y)) << endl; if ((x + y)!= z) cout << a << endl; cout << a << "a" << "b" << b << endl; if (a == "b") cout << x; else cout << y; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the output at line (a)? 7 (b) What is the output at line (b)? 3 (c) What is the output at line (c)? ab (d) What is the output at line (d)? ababba (e) What is the output at line (e)? 81

Problem 4 ( points) Write a complete C++ program that asks the user for a number n and prints 2 large copies of an E pattern (each with height n that is odd) in a horizontal sequence. For example, if the user specified 5 for n, the program would print as follows: ***** ***** * * ***** ***** * * ***** ***** (Each E pattern should begin after a gap of one column after the previous one ends. Do not try to check whether the user input is legal or sensible.) int n; cout << "Enter an odd number: "; cin >> n; for (int row = 1; row <= n; row++) { for (int pattern = 1; pattern <= 2; pattern++) { for (int c = 1; c <= n; c++) { if (c == 1 row == 1 row == n row == (n + 1) / 2) cout << "*"; else cout << " "; cout << " "; return 0;

QUEENS COLLEGE Department of Computer Science CSCI 111 Midterm 1 Exam Spring 2016 03.21.16 Solutions 02.45pm 03.35pm, Monday, March 21, 2016 Problem 1 ( points) Write a complete C++ program that prints the numbers from 980 down to 669 with 6 numbers (separated by periods) on each line. The output from your program should begin 980.979.978.977.976.975 974.973.972.971.970.969 for (int n = 980; n >= 669; n--) { cout << n; if (n % 6 == 3) else cout << "."; return 0;

Problem 2 ( points) Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of C++ code. No answer can use more than two lines. Assume the following variables have been declared and have legal values int y = 12; (a) Print to the user s screen the sentence:quote Hello but do not quote cout. cout << "Quote Hello but do not quote cout." << endl; (b) Print the square root of the square root of y. cout << sqrt(sqrt((double) y)) << endl; (c) Print a random 3 digit number that is divisible by 3. cout << 3 * (rand() % 300) + 102 << endl; (d) Print all numbers less that 1000 that end in a 7 and are divisible by 7. for (int n = 1; n < 1000; n++) if ((n % 10 == 7) && ((n % 7) == 0)) cout << n << endl; (e) Print the square of 5/8. cout << (5.0 / 8) * (5.0 / 8) << endl;

Problem 3 ( points) Consider the following C++ program. #include <cmath> double x = 81.0, y = 49.0, z = 36.0; string a = "az", b = "za"; cout << sqrt(y) << endl; cout << sqrt(y) + sqrt(x) << endl; if ((x + y) == z) cout << b << endl; cout << a << "a" << "b" << b << endl; if (a == "b") cout << z; else cout << x; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the output at line (a)? 7 (b) What is the output at line (b)? 16 (c) What is the output at line (c)? (d) What is the output at line (d)? azabza (e) What is the output at line (e)? 81

Problem 4 ( points) Write a complete C++ program that asks the user for a number n and prints 3 large copies of a T pattern (each with height n that is odd) in a horizontal sequence. For example, if the user specified 5 for n, the program would print as follows: ***** ***** ***** * * * * * * * * * * * * (Each T pattern should begin after a gap of one column after the previous one ends. Do not try to check whether the user input is legal or sensible.) int n; cout << "Enter an odd number: "; cin >> n; for (int row = 1; row <= n; row++) { for (int pattern = 1; pattern <= 3; pattern++) { for (int c = 1; c <= n; c++) { if (row == 1 c == (n + 1) / 2) cout << "*"; else cout << " "; cout << " "; return 0;