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

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

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

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 Midterm 2 Exam Spring Solutions 09.00am 09.50am, Wednesday, May 04, 2016

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

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

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

CSci 1113 Midterm 1. Name: Student ID:

Review Questions II KEY

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

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

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

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

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.

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.

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Expressions and Casting

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

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

Computer Engineering Department CMPE110 Midterm Sample Questions, 2017/ Fall

POLYNOMIALS Graphing Polynomial Functions Common Core Standard

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

NOTE: This lesson is related to Polynomials, Lesson 6, Graphing Polynomial Functions LEARNING OBJECTIVES. Overview of Lesson

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

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

REPETITION CONTROL STRUCTURE LOGO

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

Review Questions I Spring 2010

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

Fundamentals of Programming CS-110. Lecture 2

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

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

Add Subtract Multiply Divide

CSCE 2004 Midterm Exam Spring 2017

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

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Test Bank for Problem Solving with C++: The Object of Programming, 8/e Chapter 2 C++ Basics

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

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

Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

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

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

CSCI 262 Data Structures. Recursive Function Analysis. Analyzing Power. Analyzing Power. Analyzing Power 3/31/2018

Chapter 3 Problem Solving and the Computer

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

Computer Programming

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

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

Practice test for midterm 2

#101 Page: 1 Name: CS32 Midterm Exam. E01, W15, Phill Conrad, UC Santa Barbara

Exam I Review Questions Fall 2010

Exam 3 Chapters 7 & 9

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

In this chapter you will learn:

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

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

CSCI 102 Fall 2010 Exam #1

Practice test for midterm 1

2. You are required to enter a password of up to 100 characters. The characters must be lower ASCII, printing characters.

CS 141, Introduction to Computer Science Fall Midterm Exam

LAB 4.1 Relational Operators and the if Statement

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

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Chapter 7 - Notes User-Defined Functions II

C/C++ Programming Lecture 7 Name:

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

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

C++ Structures Programming Workshop 2 (CSCI 1061U)

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

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

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)

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

Other Loop Options EXAMPLE

Exercise: Inventing Language

More Complex Versions of the if Statement. Class 13

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

Declaring a 2D Array

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

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

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

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

Chapter 2 - Control Structures

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

Homework 11 Program Setup (with some IMPORTANT NEW STEPS!)

CS 311 Data Structures and Algorithms, Spring 2009 Midterm Exam Solutions. The Midterm Exam was given in class on Wednesday, March 18, 2009.

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

Homework #3 CS2255 Fall 2012

Section we will not cover section 2.11 feel free to read it on your own

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

CSci 1113 Midterm 2. Name: Student ID:

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

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

DELHI PUBLIC SCHOOL TAPI

COMP 524 Spring 2018 Midterm Thursday, March 1

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

CS 115 Exam 3, Spring 2011

Transcription:

QUEENS COLLEGE Department of Computer Science CSCI 111 First Midterm Exam Spring 2018 03.14.18 Solutions 09.05am 09.55am, Wednesday, March 14, 2018 Problem 1 Write a complete C++ program that asks the user to type a big integer n. It should then ask 4 times for the user to type a smaller value of n and then terminate the program. However, if the user ever enters a value that is not smaller it should immediately say Goodbye and terminate the program. Partial credit will be given for programs that perform some of the required steps but excessively long or complicated programs will lose credit. Examples of two sample runs of the program: venus>./a.out Type a big integer n: 100 Type a smaller value of n: 20 Type a smaller value of n: 10 Type a smaller value of n: 2 Type a smaller value of n: 1 venus> int n, smaller; cout << "Type a big integer n: "; cin >> n; for (int i = 1; i <= 4; i++) { cout << "Type a smaller value of n: "; cin >> smaller; if (smaller >= n) { cout << "Goodbye" << endl; n = smaller; venus>./a.out Type a big integer n: 100 Type a smaller value of n: 0 Type a smaller value of n: 0 Goodbye venus>

Problem 2 Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of C++ code. Most answers need no more than two lines. No solution can use more than four lines. Assume that the following variables have been declared. int x, y, age; double z; string name, a, b; (a) Print a prompt and then read values from the screen for variables x and y (in this order). cout << "Enter integers x and y: "; cin >> x >> y; (b) If x is negative, assign name as Freddy otherwise as Jack if (x < 0) name = "Freddy"; else name = "Jack"; (c) Print name a total of y times on different output lines. for (int c = 1; c <= y; c++) cout << name << endl; (d) Until x and y are not equal, repeatedly make the user enter a new value for y while (x == y) { cout << "Enter a different value of y: "; cin >> y; (e) Print the last digit of the larger of x and y if (x > y) cout << x % 10; else cout << y % 10;

Problem 3 4 as input. Consider the following C++ program. Suppose that a user runs the program and enters 10 and then int a, b, c; cout << "Enter two\npositive integers\n"; cin >> a >> b; if ((a > b) && (b > 4)) cout << a - b << endl; else cout << b - a << endl; c = a + b; for (int n = (c / 2); n <= (c * 2); n += 3) cout << n; cout << endl; cout << c + b / a << endl; cout << (c + b) % a << endl; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the output at line (a)? Enter two positive integers (b) What is the output from the instruction beginning at line (b)? -6 (c) What is the output at line (c)? 710131619222528 (d) What is the output at line (d)? 14 (e) What is the output at line (e)? 8

Problem 4 Write a complete C++ program that asks the user for a number n and prints n upward diagonal stripes (each with height n and width n) in a horizontal sequence. For example, if the user specified 4 for n, the program would print as follows: * * * * * * * * * * * * * * * * (Each stripe 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 the number n: "; cin >> n; for (int r = n; r >= 1; r--) { for (int stripe = 1; stripe <= n; stripe++) { for (int c = 1; c <=n; c++) { if (r == c) cout << "*"; else cout << " "; cout << endl;

QUEENS COLLEGE Department of Computer Science CSCI 111 First Midterm Exam Spring 2018 03.14.18 Solutions 09.05am 09.55am, Wednesday, March 14, 2018 Problem 1 Write a complete C++ program that asks the user to type a big integer n. It should then ask 5 times for the user to type a bigger value of n and then thank the user. However, if the user ever enters a value that is not bigger it should immediately exit with no message. Partial credit will be given for programs that perform some of the required steps but excessively long or complicated programs will lose credit. Examples of two sample runs of the program: venus>./a.out Type a big integer n: 100 Type a bigger value of n: 200 Type a bigger value of n: 300 Type a bigger value of n: 400 Type a bigger value of n: 500 Type a bigger value of n: 600 Thank you venus> int n, bigger; cout << "Type a big integer n: "; cin >> n; for (int i = 1; i <= 5; i++) { cout << "Type a bigger value of n: "; cin >> bigger; if (bigger <= n) n = bigger; cout << "Thank you\n"; venus>./a.out Type a big integer n: 100 Type a bigger value of n: 200 Type a bigger value of n: 150 venus>

Problem 2 Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of C++ code. Most answers need no more than two lines. No solution can use more than four lines. Assume that the following variables have been declared. int x, y, age; double z; string name, a, b; (a) Print a prompt and read the value of name and of age from the screen (in this order). cout << "Enter name and age: "; cin >> name >> age; (b) Until age is between 10 and 110, repeatedly make the user enter a new value for age while ((age < 10) (age > 110)) { cout <<"Enter your real age: "; cin >> age; (c) Print the tens and units digits of age (on two lines, in this order): cout << (age / 10) % 10 << endl << age % 10 << endl; (d) if the user s name is Freddy set x to 4 otherwise set x to 5 if (name == "Freddy") x = 4; else x = 5; (e) Print the user s name a total of x times separated by spaces for (int c = 1; c <= x; c++) cout << name << " ";

Problem 3 5 as input. Consider the following C++ program. Suppose that a user runs the program and enters 6 and then int a, b, c; cout << "Enter two\npositive integers\n"; cin >> a >> b; if ((a > b) && (b > 4)) cout << a - b << endl; else cout << b - a << endl; c = a + b; for (int n = (c / 2); n <= (c * 2); n += 3) cout << n; cout << endl; cout << c + b / a << endl; cout << (c + b) % a << endl; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the output at line (a)? Enter two positive integers (b) What is the output from the instruction beginning at line (b)? 1 (c) What is the output at line (c)? 5811141720 (d) What is the output at line (d)? 11 (e) What is the output at line (e)? 4

Problem 4 Write a complete C++ program that asks the user for an odd number n and prints n large X patterns (each with height n and width n) in a horizontal sequence. For example, if the user specified 5 for n, the program would print as follows: * ** ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * ** ** ** ** * (Each X 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 an odd number n: "; cin >> n; for (int r = 1; r <= n; r++) { for (int cross = 1; cross <= n; cross++) { for (int c = 1; c <= n; c++) { if ((r == c) ((r + c) == (n + 1))) cout << "*"; else cout << " "; cout << endl;

QUEENS COLLEGE Department of Computer Science CSCI 111 First Midterm Exam Spring 2018 03.14.18 Solutions 02.45pm 03.35pm, Wednesday, March 14, 2018 Problem 1 Write a complete C++ program that asks the user 5 questions. The first question asks the user to think of a multiple of 1, the second question asks for a multiple of 2, and so on. However, if the user ever enters a value that is not a multiple as required the program should immediately say Wrong and terminate. Partial credit will be given for programs that perform some of the required steps but excessively long or complicated programs will lose credit. Examples of two sample runs of the program: venus>./a.out Think of a multiple of 1: 4 Think of a multiple of 2: 4 Think of a multiple of 3: 6 Think of a multiple of 4: 4 Think of a multiple of 5: 5 venus> int n; for (int i = 1; i <= 5; i++) { cout << "Think of a multiple of " << i << ": "; cin >> n; if ((n % i)!= 0) { cout << "Wrong" << endl; venus>./a.out Think of a multiple of 1: 4 Think of a multiple of 2: 4 Think of a multiple of 3: 4 Wrong venus>

Problem 2 Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of C++ code. Most answers need no more than two lines. No solution can use more than four lines. Assume that the following variables have been declared. int x, y, age; double z; string name, a, b; (a) Print a prompt and read values from the screen for y and x (in this order). cout << "Enter integers y and x: "; cin >> y >> x; (b) If y is an even number, assign name as Freddy otherwise as Jack if ((y % 2) == 0) name = "Freddy"; else name = "Jack"; (c) Print name a total of x times on one line, separted by spaces for (int c = 1; c <= x; c++) cout << name << " "; (d) Until x and y are not equal, repeatedly subtract 1 from x and divide y by 2 while (x == y) { x -= 1; y = y / 2; (e) Print the last digit of the larger of x and y. if (x > y) cout << x % 10; else cout << y % 10;

Problem 3 4 as input. Consider the following C++ program. Suppose that a user runs the program and enters 10 and then int a, b, c; cout << "Enter two\npositive integers\n"; cin >> a >> b; if ((a > b) && (b > 2)) cout << a - b << endl; else cout << b - a << endl; c = a + b; for (int n = (c / 2); n <= (c * 2); n *= 2) cout << n; cout << endl; cout << c + b / a << endl; cout << (c + b) % a << endl; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the output at line (a)? Enter two positive integers (b) What is the output from the instruction beginning at line (b)? 6 (c) What is the output at line (c)? 71428 (d) What is the output at line (d)? 14 (e) What is the output at line (e)? 8

Problem 4 Write a complete C++ program that asks the user for a number n and prints n squares made of symbols each with an upward diagonal stripe made of O symbols. Each square has height n and width n and the squares form a horizontal sequence. For example, if the user specified 4 for n, the program would print as follows: ***O ***O ***O ***O **O* **O* **O* **O* *O** *O** *O** *O** O*** O*** O*** O*** (Between each pair of squares leave a gap of one blank column.) int n; cout << "Enter the number n: "; cin >> n; for (int r = n; r >= 1; r--) { for (int square = 1; square <= n; square++) { for (int c = 1; c <=n; c++) { if (r == c) cout << "O"; else cout << "*"; cout << " "; cout << endl;

QUEENS COLLEGE Department of Computer Science CSCI 111 First Midterm Exam Spring 2018 03.14.18 Solutions 02.45pm 03.35pm, Wednesday, March 14, 2018 Problem 1 Write a complete C++ program that asks the user 4 questions. The first question asks the user to think of a number that ends in 1, the second question asks for a number ending in 2, and so on. If the user correctly answers all 4 questions the program should print a message Well done. However, if the user ever enters a value with the wrong last digit the program should immediately terminate. Partial credit will be given for programs that perform some of the required steps but excessively long or complicated programs will lose credit. Examples of two sample runs of the program: venus>./a.out Think of a number that ends in 1: 11 Think of a number that ends in 2: 12 Think of a number that ends in 3: 13 Think of a number that ends in 4: 14 Well done venus> venus>./a.out Think of a number that ends in 1: 11 Think of a number that ends in 2: 10 venus> int n; for (int i = 1; i <= 4; i++) { cout << "Think of a number that ends in " << i << ": "; cin >> n; if ((n % 10)!= i) cout << "Well done" << endl;

Problem 2 Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of C++ code. Most answers need no more than two lines. No solution can use more than four lines. Assume that the following variables have been declared. int x, y, age; double z; string name, a, b; (a) Print a prompt and read the value of age and name from the screen (in this order). cout << "Enter age and name: "; cin >> age >> name; (b) Until age is between 10 and 120, repeatedly make the user enter a new value for age while ((age < 10) (age > 120)) { cout <<"Enter your real age: "; cin >> age; (c) Set x as the tens digit and y as the units digit of age x = (age / 10) % 10; y = age % 10; (d) if the user s name is Freddy set x to y otherwise set y to x if (name == "Freddy") x = y; else y = x; (e) Print a 2 digit number whose first digit is x and second digit is y cout << x << y;

Problem 3 7 as input. Consider the following C++ program. Suppose that a user runs the program and enters 4 and then int a, b, c; cout << "Enter two\npositive integers\n"; cin >> a >> b; if ((a > b) && (b > 2)) cout << a - b << endl; else cout << b - a << endl; c = a + b; for (int n = (c / 2); n <= (c * 2); n *= 2) cout << n; cout << endl; cout << c + b / a << endl; cout << (c + b) % a << endl; // line (a) // line (b) // line (c) // line (d) // line (e) (a) What is the output at line (a)? Enter two positive integers (b) What is the output from the instruction beginning at line (b)? 3 (c) What is the output at line (c)? 51020 (d) What is the output at line (d)? 12 (e) What is the output at line (e)? 2

Problem 4 Write a complete C++ program that asks the user for a number n and prints n squares made of symbols each with an downward diagonal stripe made of Z symbols. Each square has height n and width n and the squares form a horizontal sequence. For example, if the user specified 4 for n, the program would print as follows: Z*** Z*** Z*** Z*** *Z** *Z** *Z** *Z** **Z* **Z* **Z* **Z* ***Z ***Z ***Z ***Z (Between each pair of squares leave a gap of one blank column.) int n; cout << "Enter the number n: "; cin >> n; for (int r = 1; r <= n; r++) { for (int square = 1; square <= n; square++) { for (int c = 1; c <=n; c++) { if (r == c) cout << "Z"; else cout << "*"; cout << " "; cout << endl;