CSCE Practice Midterm. Data Types

Similar documents
CSCE Practice Midterm. Data Types

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

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

CSCE 2004 Midterm Exam Spring 2017

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

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.

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

CSCE 206: Structured Programming in C++

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

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

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

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

4. C++ functions. 1. Library Function 2. User-defined Function

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

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

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

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

LAB 4.1 Relational Operators and the if Statement

Name Section: M/W or T/TH. True or False (14 Points)

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

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

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

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

A SHORT COURSE ON C++

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

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

CMSC 202 Midterm Exam 1 Fall 2015

Lab Instructor : Jean Lai

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

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

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

True or False (12 Points)

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

do { statements } while (condition);

Getting started with C++ (Part 2)

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Problem Solving: Storyboards for User Interaction

The following expression causes a divide by zero error:

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

CMPS 221 Sample Final

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

EECS402 Lecture 02. Functions. Function Prototype

1) You want to determine whether time has run out. The following code correctly implements this:

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

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

A First Program - Greeting.cpp

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

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

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

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

FORM 2 (Please put your name and form # on the scantron!!!!)

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

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

Practice test for midterm 1

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

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

CS2255 HOMEWORK #1 Fall 2012

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

System Design and Programming II

CSci 1113 Midterm 1. Name: Student ID:

ASSIGNMENT CLASS-11 COMPUTER SCIENCE [C++]

Local and Global Variables

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

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

Tutorial Letter 103/1/2017 Introduction to Programming I

Functions, Arrays & Structs

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

Introduction to Programming EC-105. Lecture 2

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

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

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

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

CPE Summer 2015 Exam I (150 pts) June 18, 2015

C++ For Science and Engineering Lecture 12

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

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

Exceptions, Case Study-Exception handling in C++.

PIC 10A. Review for Midterm I

Exam 3 Chapters 7 & 9

Outline. 1 About the course

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

1. HotDog 판매량출력프로그램. numsold = newnumsold; ID = newid; HotDogStand(); HotDogStand(int newid, int newnnumsold); return ID; void SetID(int newid);

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. A single variable can only hold one value. Declared using [] operator:

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

Programming. C++ Basics

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

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

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

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

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Transcription:

CSCE 2004 - Practice Midterm This midterm exam was given in class several years ago. Work each of the following questions on your own. Once you are done, check your answers. For any questions whose answers you don't understand, see the TA for an explanation. This practice midterm is slightly longer than the one you can expect to see next week to provide extra questions for your practice. Data Types [15 points] Examine the following C++ assignment statements variable = expression. For each statement indicate the order in which the sub-pieces are evaluated, and the values along the way. Give the value and data type (float, int, char) of the final expression as well as the value and type stored in the variable. float Taxes; Taxes = (20 * 4 + 30 * 3)/50; Expression: Value: 3 Data Type: int Variable: Value: 3.0 Data Type: float float Result; Result = 5 / 2-5 % 2; Expression: Value: 1 Data Type: int Variable: Value: 1.0 Data Type: float int Distance; Distance = 3.5 + 3 / (2 + 3); Expression: Value: 3.5 Data Type: float Variable: Value: 3 Data Type: int int Number; Number = 3 * 2 / 10.0; Expression: Value: 0.6 Data Type: float Variable: Value: 0 Data Type: int

Loops [5 points] Rewrite the following while loop as a for loop that performs the same task. int Max = 1900; int Sum = 0; while ((Max < 1950)) Sum = Sum + (Max - 1900); cout << "Sum: " << Sum << "\n"; Max = Max + 5; int Sum = 0; for(int Max = 1900 ; Max < 1950 ; Max += 5) Sum = Sum + (Max - 1900); cout << "Sum: " << Sum << "\n"; [5 points] Rewrite the following do-while loop as a while loop that performs the same task. int Hours = 0; int Total = 0; do cout << "Enter a number between 0 and 20: "; cin >> Hours; if ((Hours > = 0) && (Hours < = 20)) Total = Total + Hours; while (Total < 200); int Hours = 0; int Total = 0; cout << "Enter a number between 0 and 20: "; cin >> Hours; if ((Hours > = 0) && (Hours < = 20)) Total = Total + Hours; while(total < 200) cout << "Enter a number between 0 and 20: "; cin >> Hours; if ((Hours > = 0) && (Hours < = 20))

Total = Total + Hours; [5 points] Rewrite the following nested for loops as a single for loop that produces the same output. int i, j; for (j = 1; j < 4; j++) for (i = j; i < j+1; i++) cout << "The value is: " << i << "\n"; for(int i=1 ; i<4 ; i++) cout << "The value is: " << i << "\n"; Switch Statement [5 points] Write a C++ switch statement that stores 4.0 in NumberGrade if the grade is an A or a, 3.0 for B or b, 2.0 for C or c, 1.0 for D or d, and 0.0 for an F or f. It should output "invalid grade" and set value to -1.0 for any other character. char LetterGrade; float NumberGrade; cout << "Enter your grade: "; cin >> LetterGrade; switch(lettergrade) case 'a': case 'A': NumberGrade = 4.0; break; case 'b': case 'B': NumberGrade = 3.0; break; case 'c': case 'C': NumberGrade = 2.0;

break; case 'd': case 'D': NumberGrade = 1.0; break; case 'f': case 'F': NumberGrade = 0.0; break; default: cout <<"You have entered an invalid grade."; NumberGrade = -1.0; break; Nested Loops [5 points] Consider the following C++ program. What does it print out? Show your work where necessary. #include <iostream> using namespace std; int main() int X, Y; // Outer loop for (Y=1; Y < 10; Y = Y + X) X=1; // Inner loop while(x < 3) cout << "X: " << X << endl; X=X+1; cout << "Y: " << Y << "\n"; // Code after loop cout << "Final value of X: " << X << "\n"; cout << "Final value of Y: " << Y << "\n"; return(0);

output: X: 1 X: 2 Y: 1 X: 1 X: 2 Y: 4 X: 1 X: 2 Y: 7 Final Value of X: 3 Final Value of Y: 10 If Statements [10 points] Fill in a C++ program which asks the user several Y/N questions and selects a car for them based on their answers. The questions are 1) do you have children; 2) do you have lots of money; and 3) do you like trucks. The car selection should be based on the following: Porsche: Yukon: Civic: Villager: Explorer: Sentra: have no children, have lots of money, don't like trucks have no children, have lots of money, like trucks have no children, have little money have children, have lots of money, don't like trucks have children, have lots of money, like trucks have children, have little money #include <iostream> using namespace std; int main() // Declare variables char Children; char Money; char LikeTrucks; // Get user inputs (no error checking necessary) cout <<"\ndo you have children?\n"; cin >> Children; cout << "\ndo you have lots of money?\n"; cin >> Money; cout <<"\ndo you like trucks?\n"; cin >> LikeTrucks;

// Output the car choice based on info above if (Children == 'N' && Money == 'Y' && LikeTrucks == 'N') cout << "\nporshe\n"; if (Children == 'N' && Money == 'Y' && LikeTrucks == 'Y') cout <<"\nyukon\n"; if (Children == 'N' && Money == 'N') cout <<"\ncivic\n"; if (Children == 'Y' && Money == 'Y' && LikeTrucks == 'N') cout <<"\nvillager\n": if (Children == 'Y' && Money == 'Y' && LikeTrucks == 'Y') cout << "\nexplorer\n"; if (Children == 'Y' && Money == 'N') cout << "\nsentra\n"; return 0;

Calling Functions [10 points] For each of the following function prototypes, which of the following calls are syntactically correct? Note: function calls that would compile with warnings or compile without warnings but cause implicit type conversions should be considered incorrect. // Variable declarations int A, B, C; float X,Y; char Char; // Function prototypes int Maximum (const int Num1, const int Num2); void Total (const float A, const float B, float &C); char GetChar(); Maximum (A,B); A = Maximum (7,3); A = Maximum (Num1, Num2); Total (A, B, C); Total (3.0, X, Y); Total (3.0, 5.0, 8.0); Total (Y, X, Y); GetChar (Char); Char = GetChar ();

Defining Functions [10 points] For this question, your task is to write function prototypes (the first line of the function that gives the return type and the list of parameters). You do NOT have to fill in the function bodies. Use const where appropriate. // This function takes in two numbers and returns their average. float Average ( const float A, const float B ); OR void Average ( const float A, const float B, float &Average ); // This function asks the user how many sisters they have and returns their answer. int GetSisters ( ); OR void GetSisters(int &Answer); // This function takes in one number and returns the square root of this number. float SquareRoot (const float N); OR void SquareRoot (const float N, float &sqrtn); // This function take in a letter grade and returns // the corresponding grade point value (eg A = 4.0). float GetGPA (const char Letter); OR void GetGPA (float &GPA, const char Letter);

Function Tracing #include <iostream> using namespace std; const int X = 3; void Proc1 (int &Y) Y = 6; cout << "Inside Proc1, X: " << X << " Y: " << Y << "\n"; int Proc2 (int &Y, const int Z) int X = 7; Proc1 (X); Proc1 (Y); Y = 3; cout << "Inside Proc2, X: " << X << " Y: " << Y << " Z: " << Z << "\n"; return (Y + Z); int main() int A = 1; int B = 3; int C = 5; C = Proc2 (A,B); cout << "Finally, A: " << A << " B: " << B << " C: " << C << "\n"; return(0);

[5 points] Using the box method, trace the execution of the program above to show the sequence of function calls, the values of variables, and return values. [5 points] Using this information, what does the program output? Inside Proc1, X: 3 Y: 6 Inside Proc1, X: 3 Y: 6 Inside Proc2, X: 6 Y: 3 Z: 3 Finally, A: 3 B: 3 C: 6