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

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

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

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

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

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

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

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

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

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

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.

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.

CS 141, Introduction to Computer Science Fall Midterm Exam

Perfect square. #include<iostream> using namespace std; int main(){ int a=1; int square; while(true){ } cout<<square<<endl; }

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

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

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

CSci 1113 Midterm 1. Name: Student ID:

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

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.

Declaring a 2D Array

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

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

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

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

CMSC 202 Midterm Exam 1 Fall 2015

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

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

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

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

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

MAT 142 College Mathematics. Module ST. Statistics. Terri Miller revised July 14, 2015

Add Subtract Multiply Divide

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

Introduction to Computer Science Midterm 3 Fall, Points

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

Exam I Review Questions Fall 2010

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

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

CSCE Practice Midterm. Data Types

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

More Complex Versions of the if Statement. Class 13

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

Arrays. Arrays (8.1) Arrays. One variable that can store a group of values of the same type. Storing a number of related values.

Review Questions I Spring 2010

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

From Pseudcode Algorithms directly to C++ programs

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

Arrays. What if you have a 1000 line file? Arrays

CSci 1113 Midterm 2. Name: Student ID:

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

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Lab 2.1: Fixing a C++ program

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

Simplifying Square Root Expressions[In Class Version][Algebra 1 Honors].notebook August 26, Homework Assignment. Example 5 Example 6.

Homework #3 CS2255 Fall 2012

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

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

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

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

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

1st Midterm Exam: Solution COEN 243: Programming Methodology I

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

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

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

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

CS 106B Practice Midterm Exam #1 (by Marty Stepp)

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

Practice test for midterm 1

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

Review Questions II KEY

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:

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

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.

Write a program that displays all the even integers between 1 and 100, inclusive

do { statements } while (condition);

Chapter 3 Problem Solving and the Computer

ENJOY! Problem 2 What does the following code do, in a brief English sentence? int mystery(int k) { int i = 0; if (k < 0) return -1;

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

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

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

Why Is Repetition Needed?

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

PIC 10A. Review for Midterm I

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

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 3

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Computer Engineering Department CMPE110 Midterm Sample Questions, 2017/ Fall

Chapter Four: Loops II

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

! 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)

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

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

Transcription:

QUEENS COLLEGE Department of Computer Science CSCI 111 First Midterm Exam Fall 2017 10.18.17 Solutions 09.00am 09.50am, Wednesday, October 18, 2017 Problem 1 (10 points) The following C++ program has errors at the lines marked a,b,c,d, and e. For each answer write a single line of C++ that fixes all errors in the corresponding line. Do not change anything that is correct. #Include <iostream> int main{ // line a // line b int x = 1, y = 2; for (int x == 1; x < 10; x++); // line c cout << y % y endl; // line d return; // line e (a) Correct line (a): (b) Correct line (b): (c) Correct line (c): for (int x = 1; x < 10; x++); (d) Correct line (d): cout << y % y << endl; (e) Correct line (e):

Problem 2 (10 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 that x and y have been declared. int x; double y; (a) Read a value of x typed on the screen by the user cin >> x; (b) If x is negative, replace x by the 4th power of its value if (x < 0) x = x * x * x * x; (c) Print on the screen the last 2 digits of x. cout << x % 100 << endl; (d) Print on the screen a random number between 1 and x (inclusive). cout << 1 + rand() % x << endl; (e) Set y to the exact value of the quotient of x by 100. y = x / 100.0;

Problem 3-10 as input. (10 points) Consider the following C++ program. Suppose that a user runs the program and enters int a; cout << "Enter a\npositive integer\n"; cin >> a; cout << a << - a << a - a << endl; for (int n = a; n <= 0; n += 4) cout << n; cout << a + 20 / 3 << endl; cout << (a + 20) % 3 << endl; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the output at line (a)? Enter a positive integer (b) What is the output at line (b)? -10100 (c) What is the output at line (c)? -10-6-2 (d) What is the output at line (d)? -4 (e) What is the output at line (e)? 1

Problem 4 (10 points) Write a complete C++ program that asks the user to enter a positive integer n. If n is not positive the program should print a message Not positive. Enter another: and continue to do this until the user enters a positive integer. Then the program should print n squares of *s with decreasing size that begin with an n n square and end with a 1 1 square. The right hand edges of the squares should line up. (Partial credit will be given for the parts of the program that you complete successfully.) Excessively long solutions lines might lose some credit. For example, here is a sample run of the program: Enter a positive integer n: -3 Not positive. Enter another: 4 **** **** **** **** *** *** *** ** ** * int n; cout << "Enter a positive integer n: "; cin >> n; while (n <= 0) { cout << "Not positive. Enter another: "; cin >> n; for (int s = n; s>= 1; s--) { for (int r = 1; r <= s; r++) { for (int c = 1; c <= n; c++) if (c > n - s) cout << "*"; else cout << " ";

QUEENS COLLEGE Department of Computer Science CSCI 111 First Midterm Exam Fall 2017 10.18.17 Solutions 09.00am 09.50am, Wednesday, October 18, 2017 Problem 1 (10 points) The following C++ program has errors at the lines marked a,b,c,d, and e. For each answer write a single line of C++ that fixes all errors in the corresponding line. Do not change anything that is correct. #include <IOstream> Int main() // line a // line b int z = 5; double y = 12.0; for (z = 1; z < 10; z--); cout << y % z << endl; return z // line c // line d // line e (a) Correct line (a): (b) Correct line (b): (c) Correct line (c): for (z = 1; z < 10; z++); (d) Correct line (d): cout << ((int) y) % z << endl; (e) Correct line (e): return z;

Problem 2 (10 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 that x and y have been declared. int y; double x; (a) Read a value of x typed on the screen by the user. cin >> x; (b) If x is negative, replace x by the 2nd power of its value. if (x < 0) x = x * x; (c) Make y be the closest integer to x (round up half). y = (int) (x + 0.5); (d) Print to the screen a random number between 0 and y (inclusive). cout << rand() % (y + 1) << endl; (e) Set x to the exact value of the quotient of y by 13. x = y / 13.0;

Problem 3 6 as input. (10 points) Consider the following C++ program. Suppose that a user runs the program and enters int a; cout << "Enter a\npositive integer\n"; cin >> a; cout << a << - a << a - a << endl; for (int n = a; n <= 0; n += 4) cout << n; cout << a + 20 / 3 << endl; cout << (a + 20) % 3 << endl; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the output at line (a)? Enter a positive integer (b) What is the output at line (b)? 6-60 (c) What is the output at line (c)? (d) What is the output at line (d)? 12 (e) What is the output at line (e)? 2

Problem 4 (10 points) Write a complete C++ program that asks the user to enter an odd positive integer n. If n is illegal the program must terminate at once. Otherwise the program should print squares of *s. that begin with an n n square and end with a 1 1 square and such that the sizes of squares decrease by 2 as they go down the page. The right hand edges of the squares should line up. (Partial credit will be given for the parts of the program that you complete successfully.) Excessively long solutions lines might lose some credit. For example, here is a sample run of the program: Enter an odd positive integer n: 5 ***** ***** ***** ***** ***** *** *** *** * int n; cout << "Enter an odd positive integer n: "; cin >> n; if (n <= 0 n % 2 == 0) for (int s = n; s>= 1; s-=2) { for (int r = 1; r <= s; r++) { for (int c = 1; c <= n; c++) if (c > n - s) cout << "*"; else cout << " ";

QUEENS COLLEGE Department of Computer Science CSCI 111 First Midterm Exam Fall 2017 10.18.17 Solutions 02.45pm 03.35pm, Wednesday, October 18, 2017 Problem 1 (10 points) The following C++ program has errors at the lines marked a,b,c,d, and e. For each answer write a single line of C++ that fixes all errors in the corresponding line. Do not change anything that is correct. #INCLUDE <iostream>. int main{({ int x = 1; y = 2; for (int x == 1; x == 1; x++); // line a // line b // line c // line d cout << y % y << endl; Return 10; // line e (a) Correct line (a):. (b) Correct line (b): (c) Correct line (c): int x = 1, y = 2; (d) Correct line (d): for (int x = 1; x == 1; x++); (e) Correct line (e): return 10;

Problem 2 (10 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 that name and age have been declared. int age; string name; (a) Read values of name and age typed on the screen by the user cin >> name >> age; (b) If the name is Freddy, divide the age by 10 and use that as age. if (name == "Freddy") age = age / 10; (c) Print on the screen the last 2 digits of the age. cout << age % 100 << endl; (d) Print on the screen a random number between 1 and age (inclusive). cout << 1 + rand() % age << endl; (e) Print the users name 5 times on 5 lines of the screen. for (int i = 1; i <= 5; i++) cout << name << endl;

Problem 3-20 as input. (10 points) Consider the following C++ program. Suppose that a user runs the program and enters int a; cout << "Enter a\npositive integer\n"; cin >> a; cout << a << - a << a - a << endl; for (int n = a; n <= 0; n += 4) cout << n; cout << a + 20 / 3 << endl; cout << (a + 20) % 3 << endl; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the output at line (a)? Enter a positive integer (b) What is the output at line (b)? -20200 (c) What is the output at line (c)? -20-16-12-8-40 (d) What is the output at line (d)? -14 (e) What is the output at line (e)? 0

Problem 4 (10 points) Write a complete C++ program that asks the user to enter a positive integer n. If n is not positive the program should print a message Not positive. Enter another: and continue to do this until the user enters a positive integer. Then the program should print a square of side n that is made up from a large letter L made of # s in the left column and bottom row, then a slightly smaller L made made of o s inside it, then a smaller L made of # s and so on. (Partial credit will be given for the parts of the program that you complete successfully.) Excessively long solutions lines might lose some credit. For example, here is a sample run of the program: Enter a positive integer n: -5 Not positive. Enter another: 7 #o#o#o# #o#o#oo #o#o### #o#oooo #o##### #oooooo ####### int n; cout << "Enter a positive integer n: "; cin >> n; while (n <= 0) { cout << "Not positive. Enter another: "; cin >> n; for (int r = n; r >= 1; r--) { for (int c = 1; c <= n; c++) { int x = r; if (c <= r) x = c; if (x % 2 == 0) cout << "o"; else cout << "#";

QUEENS COLLEGE Department of Computer Science CSCI 111 First Midterm Exam Fall 2017 10.18.17 Solutions 02.45pm 03.35pm, Wednesday, October 18, 2017 Problem 1 (10 points) The following C++ program has errors at the lines marked a,b,c,d, and e. For each answer write a single line of C++ that fixes all errors in the corresponding line. Do not change anything that is correct. include <iostream> // line a int z = 5, double y = 12.0; for (z = 1; z!> 10; z++); // line b // line c cout (int) y % z endl; // line d Return z. // line e (a) Correct line (a): (b) Correct line (b): int z = 5; double y = 12.0; (c) Correct line (c): for (z = 1; z <= 10; z++); (d) Correct line (d): cout << (int) y % z << endl; (e) Correct line (e): return z;

Problem 2 (10 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 that score and name have been declared. string name; double score; (a) Read the score and name typed on the screen by the user. cin >> score >> name; (b) If the name is not Freddy add a bonus of 10 to the score. if (name!= "Freddy") score += 10; (c) Print the closest integer to the score (round up half). cout << (int) (score + 0.5); (d) Print to the screen a random integer between 1 and score. cout << 1 + rand() % ((int) score) << endl; (e) Print the user s name 20 times on one line of output. for (int i = 1; i <= 20; i++) cout << name << " ";

Problem 3 20 as input. (10 points) Consider the following C++ program. Suppose that a user runs the program and enters int a; cout << "Enter a\npositive integer\n"; cin >> a; cout << a << - a << a - a << endl; for (int n = a; n <= 0; n += 4) cout << n; cout << a + 20 / 3 << endl; cout << (a + 20) % 3 << endl; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the output at line (a)? Enter a positive integer (b) What is the output at line (b)? 20-200 (c) What is the output at line (c)? (d) What is the output at line (d)? 26 (e) What is the output at line (e)? 1

Problem 4 (10 points) Write a complete C++ program that asks the user to enter a positive integer n. If n is not positive the program must terminate at once. Then the program should print a square of side n that is made up from a large backwards letter L made of # s in the right column and bottom row, then a slightly smaller reversed L made made of o s inside it, then a smaller reversed L made of # s and so on. (Partial credit will be given for the parts of the program that you complete successfully.) Excessively long solutions lines might lose some credit. For example, here is a sample run of the program: Enter a positive integer n: 7 #o#o#o# oo#o#o# ###o#o# oooo#o# #####o# oooooo# ####### int n; cout << "Enter a positive integer n: "; cin >> n; if (n <= 0) for (int r = 1; r <= n; r++) { for (int c = 1; c <= n; c++) { int x = r; if (c >= r) x = c; if (x % 2 == n % 2) cout << "#"; else cout << "o";