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

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

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

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

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

CSci 1113 Midterm 1. Name: Student ID:

CS101 PLEDGED SPRING 2001

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

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

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

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

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

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

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

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

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

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

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

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Honors Computer Science C++ Mr. Clausen Program 6A, 6B, 6C, & 6G

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

Number: Name-Surname :... Group:...

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

WARM UP LESSONS BARE BASICS

CSI33 Data Structures

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

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.

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.

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

CSCE 2004 Midterm Exam Spring 2017

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

Chapter-9 INPUT AND OUTPUT OPERATORS

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

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

CPS 216, Problem Solving and Programming Techniques in C++ Saleh M. Alnaeli, Ph.D. Spring, Lab_Week2

clicking on the on the New

Array Elements as Function Parameters

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

CSCE Practice Midterm. Data Types

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

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

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

CMSC 202 Midterm Exam 1 Fall 2015

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 111 Second Midterm Exam Spring Solutions 09.00am 09.50am, Wednesday, May 10, 2017

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Computer Engineering Department CMPE110 Midterm Sample Questions, 2017/ Fall

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Chapter 7 - Notes User-Defined Functions II

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

AP Computer Science Unit 1. Programs

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

Midterm Exam 5 April 20, 2015

Exam 3 Chapters 7 & 9

Question: How can we compare two objects of a given class to see if they are the same? Is it legal to do: Rectangle r(0,0,3,3);,, Rectangle s(0,0,4,4)

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

Control Structures of C++ Programming (2)

Lab 1. largest = num1; // assume first number is largest

1. In C++, reserved words are the same as predefined identifiers. a. True

Arrays in C++ Instructor: Andy Abreu

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

York University Faculty Science and Engineering Fall 2008

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Lab 2.1: Fixing a C++ program

1. Word Analysis: (Which nouns suggest a need for memory (a variable) and which verbs suggest a need for action (a function maybe).

Note: The buy help from the TA for points will apply on this exam as well, so please read that carefully.

CSci 1113 Final. Name: Student ID:

Practice Midterm 1 Answer Key

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

What Is a Function? Illustration of Program Flow

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

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

IT 1033: Fundamentals of Programming Data types & variables

Programming in C++ The manager of a company. Lecture Notes 6. Functions (Procedures) 4/24/2018. he he he. Does Does Does

1 Short Answer (15 Points Each)

Area and Perimeter Name: Date:

Exam I Review Questions Fall 2010

Module Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1

Data Types & Variables

CSCE Practice Midterm. Data Types

CSC102 INTRO TO PROGRAMMING WITH PYTHON LECTURE 27 CRASH COURSE IN C++ MICHAEL GROSSBERG

Programming Language. Functions. Eng. Anis Nazer First Semester

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Assignment 4: Object creation

Chapter 3 Problem Solving and the Computer

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

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

Control Structures. Repetition (Loop) Structure. Repetition (Loop) Structure. Repetition (Loop) Structure. CS225: Slide Set 8: C++ Loop Structure

Scope and Parameter Passing

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

Problem 3: Theoretical Questions: Complete the midterm exam taken from a previous year attached at the end of the assignment.

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

The figures below are all prisms. The bases of these prisms are shaded, and the height (altitude) of each prism marked by a dashed line:

Declaring a 2D Array

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

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

LAB 4.1 Relational Operators and the if Statement

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

Polymorphism CSCI 201 Principles of Software Development

Sample Code: OUTPUT Daily Highs & Lows

Transcription:

QUEENS COLLEGE Department of Computer Science CSCI 111 Midterm 1, version A Exam Fall 2015 10.13.15 Solutions 09.00am 09.50am, Tuesday, October 13, 2015 Problem 1 Write a complete C++ program that does the following. 1. It asks the user to enter the base and height of a triangle. 2. If either the base or height is less than or equal to 0, the program should immediately terminate. 3. The program calculates and prints the area of the triangle using the formula: Here is an example of how the program should work: Enter the base and height of a triangle: 8 10 Area: 40.0 double base, height; double area; Area = 1 Base Height 2 cout << "Enter the base and height of a triangle:"; cin >> base >> height; if ((base <= 0) (height <= 0)) area = base * height / 2.0; cout << "Area: " << area << 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. No answer can use more than two lines. Assume the following variables have been declared and have legal values int x, y; string name, message; (a) Ask for, and read the value for name from the user. cout << "Enter your name: "; cin >> name; (b) Print the cube of x. cout << x * x * x << endl;

(c) Print the last digit of y. cout << y % 10 << endl; (d) If x is greater than y print the value of x, otherwise print the value of message. if (x > y) cout << x << endl; else cout << message << endl; (e) Print x copies of the value of name followed by y copies of the value of message. for (int a = 1; a <= x; a++) cout << name; for (int a = 1; a <= y; a++) cout << message; Problem 3 Consider the following C++ program. int p = 11, q = 13, r = 15; string a = "x"; string x = "xx"; if (a == x) cout << p; else cout << q; if ((p <= q) && (p <= r)) cout << "a" << endl; if ((a == x) ("a" == "x")) r++; cout << r << endl; if (!((p % 2) < (q % 2))) cout << "no"; while (p < r) p++; cout << p << "\n"; (a) What is the output at line (a)? 13 (b) What is the output at line (b)? a (c) What is the output at line (c)? 15 (d) What is the output at line (d)? // line (a) // line (b) // line (c) else cout << "yes"; // line (d) // line (e)

no (e) What is the output at line (e)? 15 Problem 4 Write a complete C++ program that asks the user for a number n and prints n triangles (each with height n) in a horizontal sequence. For example, if the user specified 4 for n, the program would print as follows: ** ** ** ** *** *** *** *** **************** (Each triangle 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 = 1; r <= n; r++) { for (int triangle = 1; triangle <= n; triangle++) { for (int c = 1; c <=n; c++) { if (r >= c) cout << "*"; else cout << " ";

QUEENS COLLEGE Department of Computer Science CSCI 111 Midterm 1, version B Exam Fall 2015 10.13.15 Solutions 11.10am 12.00noon, Tuesday, October 13, 2015 Problem 1 Write a complete C++ program that does the following. 1. It asks the user to enter a temperature in degrees fahrenheit. 2. If the temperature f does not satisfy 0 f 100 the program should print the message Out of range and terminate. 3. The program calculates and prints the celsius value of temperature using the formula: Here is an example of how the program should work: Celsius = 5 (F ahrenheit 32) 9 Enter the temperature in degrees fahrenheit: 68 Celsius: 20.0 double celsius, fahrenheit; cout << "Enter the temperature in degrees fahrenheit: "; cin >> fahrenheit; if (!((fahrenheit >= 0) && (fahrenheit <= 100))) { cout << "Out of range." << endl; celsius = 5.0 / 9 * (fahrenheit - 32); cout << "Celsius: " << celsius << 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. No answer can use more than two lines. Assume the following variables have been declared and have legal values int x, y; string name, message; (a) Ask for, and read the value for name from the user. cout << "Enter your name: "; cin >> name; (b) Print the square of y.

cout << y * y << endl; (c) Print the last digit of x + y. cout << (x + y) % 10 << endl; (d) If x is not greater than y print the value of name, otherwise print the value of message. if (!(x > y)) cout << name<< endl; else cout << message << endl; (e) Print x copies of the value of x followed by y copies of the value of message. for (int a = 1; a <= x; a++) cout << x; for (int a = 1; a <= y; a++) cout << message; Problem 3 Consider the following C++ program. int p = 31, q = 23, r = 15; string a = "abc"; string x = "abc"; if (a == x) cout << p; else cout << q; if ((p <= q) && (p <= r)) cout << "a" << endl; if ((a == x) ("a" == "x")) r++; cout << r << endl; if (!((p % 2) < (q % 2))) cout << "no"; while (p < r) p++; cout << p << "\n"; (a) What is the output at line (a)? 31 (b) What is the output at line (b)? // line (a) // line (b) // line (c) else cout << "yes"; // line (d) // line (e) (c) What is the output at line (c)?

16 (d) What is the output at line (d)? no (e) What is the output at line (e)? 31 Problem 4 Write a complete C++ program that asks the user for a number n and prints n 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 = 1; r <= n; r++) { for (int stripe = 1; stripe <= n; stripe++) { for (int c = 1; c <=n; c++) { if (r == c) cout << "*"; else cout << " ";

QUEENS COLLEGE Department of Computer Science CSCI 111 Midterm 1, version B Exam Fall 2015 10.13.15 Solutions 11.10am 12.00noon, Tuesday, October 13, 2015 Problem 1 Write a complete C++ program that does the following. 1. It asks the user to enter a temperature in degrees celsius. 2. If the temperature is greater than 40, the program should once ask the user to enter a different value. 3. The program calculates and prints the fahrenheit value of temperature using the formula: Here is an example of how the program should work: Enter the temperature in degrees celsius: 60 Enter a different value: 60 Fahrenheit: 140.0 double celsius, fahrenheit; F ahrenheit = 9 Celsius + 32 5 cout << "Enter the temperature in degrees celsius: "; cin >> celsius; if (celsius > 40) { cout << "Enter a different value: "; cin >> celsius; fahrenheit = 9.0 / 5 * celsius + 32; cout << "Fahrenheit: " << fahrenheit << 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. No answer can use more than two lines. Assume the following variables have been declared and have legal values int a, b; string word, second; (a) Ask for, and read the value for second from the user. cout << "Enter second: "; cin >> second; (b) Print the cube of b.

cout << b * b * b << endl; (c) Print the number a without its last digit. cout << a / 10 << endl; (d) If word and second are equal print the value of a, otherwise print the value of b. if (word == second) cout << a << endl; else cout << b << endl; (e) Print a copies of the value of word on one line separated by single spaces. for (int x = 1; x <= a; x++) cout << word << " "; Problem 3 Consider the following C++ program. int p = 36, q = 49, r = 25; string a = "yes"; string x = "no"; if (a == x) cout << p; else cout << q; // line (a) if ((p <= q) && (r <= q)) cout << "ok" << endl; // line (b) if ((a == x) ("x" == "x")) r+=5; cout << r << endl; // line (c) if (!((p % 2) < (q % 2))) cout << "gg"; else cout << "ll"; // line (d) while (p > r) p--; cout << p << "\n"; // line (e) (a) What is the output at line (a)? 49 (b) What is the output at line (b)? ok (c) What is the output at line (c)?

30 (d) What is the output at line (d)? ll (e) What is the output at line (e)? 30 Problem 4 Write a complete C++ program that asks the user for a number n and prints n 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 = 1; r <= n; r++) { for (int stripe = 1; stripe <= n; stripe++) { for (int c = 1; c <=n; c++) { if (r == c) cout << "*"; else cout << " ";