Practiceexam1 Q&A
Practiceexam1 Q&A
This is a practice exam only. It has many (!) more questions than the actual
exam, and problems that occur here may or may not appear in the actual exam.
Source Code: A human-readable text file that resembles a mixture of English and math, written according to the C+
+ programming language rules
Machine (Object) Code: A file that is not human-readable but the computer can read it and execute it.
Compiling: The process of checking a source code file for errors and converting it into a machine code file.
Linking: The process of combining several object files into an executable file.
2. Describe the advantages and disadvantages associated with the basic data types int and double.
int variables have a limited range but are fast, exact, and use little memory
double variables have a large range but are slow, inexact, and use more memory
3. Please find out what is wrong with the if statement listed below, and rewrite it to fix that logical error.
(Hint: what if someone's GPA is 1.0). Your new if statement should contain nested if-else-if statements.
if ((gpa >= 2.3) && (gpa < 3.0))
cout << "Your GPA is not bad but you should improve it.";
if ((gpa >= 3) && (gpa <= 3.7))
cout << "You got a pretty good GPA";
else
cout << "Congratulations. You made the Dean's list";
If gpa was equal to 1, the first text returns false, so the first cout statement does not execute. The second test also
returns false, but it is an if-else statement so that the else part executes: “Congrats, you made deans list”. That’s
not what we had in mind.
To rectify the situation, use a nested if-else-if statement such as the following:
if (gpa < 0)
cout << “invalid input”;
else if (gpa < 2.3)
cout << “bad gpa”;
else if (gpa < 3.0)
cout << "Your GPA is not bad but you should improve it.";
else if (gpa <= 3.7)
cout << "You got a pretty good GPA";
else if (gpa <= 4.0)
cout << "Congratulations. You made the Dean's list";
else
cout << “Invalid input”;
The main function. Programs should be designed from the “top down”, i.e. first determine the main function’s
functionality. Based on that you will know the functions you need, and the function headers. Then you create the
functions.
b) What are the three pieces usually necessary when using a while loop in C++ programming ?
Anywhere you want, but before the variable is used. It’s best to put it close to where you first use it.
d) Can you declare a variable, and at the same time assign a value to it ?
Yes, as in double x; you should, however, assign an initial value even if you don’t have to.
g) When using C++, you create a loop that keeps multiplying a positive integer by other positive integers.
Can, for the computer, the integers ever become negative ? If so, explain.
Yes. Integers have a limited range. If you go beyond the largest integer, the value will “wrap” around and jump to
the smallest integer, which is negative.
5. Write some C++ program segments that solves the indicated tasks (you do not have to write a complete
program, nor be concerned about "good" output; a small code segment will be sufficient).
a) A program that gets a double number from the user, decides whether that number is positive, negative, or
zero and display its decision on the screen.
double x = 0.0;
cout << “Enter number: “;
cin >> x;
if (x > 0)
cout << “positive”;
else if (x == 0)
cout << “zero”;
else
cout << “negative”;
h) A function isPositive that takes as input a double number and returns the integer 1 if the number is
positive, and zero otherwise.
int isPositive(double x)
{
if (x > 0)
return 1;
else
return 0;
}
j) A program to reads a real number as input and adds it to a running total until the user enters the number -1.
At that time the program should print out the final sum (not including, of course, the number -1).
double input = 0.0;
double sum = 0.0;
while (input != -1)
{
cout << “enter number: “;
cin >> input;
if (input != -1)
sum += input;
}
cout << “Sum: “ << sum << endl;
6. What is wrong with the following while loops (and how does the correct ones look like):
In the first loop the variable counter is initialized to 1. Inside the loop, counter is decremented, so it will have values
1, 0, -1, -2, -3, … In particular, it will always be less than 100, so this is an infinite loop. To fix, use counter++
instead of counter—to fix, or change test from (counter < 100) to (counter >= 0)
In the second loop there are not brackets surrounding the code block of the while loop. Therefore, only the line
immediately following the while statement repeats. Since that line does not modify counter, its value will always be
1. Hence, it’s another infinite loop. The fact that the last line is indented and seems to be part of the loop code block
is purely cosmetic and of no consequence to the actual loop. To fix, add grouping bracket around the indented lines
after the while statement.
7. List the output for each “cout” statement in the following program:
#include <iostream.h>
int main(void)
{
int i = 10; // i is 10
i++; // i is incremented, i.e. it is 11
i--; // i is decremented, i.e. it is 10 again
cout << i << "\n"; // the output will be 10.
return 0;
}
#include <iostream.h>
int main(void)
{
double x = 20.0; // x is 20
double sum = 0.0; // sum = 0.0
#include <iostream.h>
int main(void)
{
double y = 0; // y is set to 0
while (y < 10)
y += 3; // y is set to 3, checked, to 6, checked, then
// to 9, checked, to 12, checked => loop over
cout << y << "\n"; // output will be 12
return 0;
}
#include <iostream.h>
int main(void)
{
for (int i = 0; i < 3; i++) // i=0,printout, i=1,printout,i=2,
// printout, i = 3 => loop over.
cout << "How often ?" << "\n";
return 0; // it will show exactly 3
printouts “How often?”
}
This would be a sample extra credit question (i.e. it is difficult and covers material we did not discuss in class)
#include <iostream.h>
int main(void)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j+=2)
{
cout << i*j << “\n”;
}
}
return 0;
}
8. Create a program that prints out the first 500 even integers.
#include <iostream.h>
int main(void)
{
for (int i = 2; i <= 500; i+=2)
cout << i << endl;
return 0;
}
9. Write a complete program that contains a main function and at least two additional functions to compute
the area and perimeter of a square. The program should ask the user to enter a double number corresponding to the
side length of the square, and display the area and perimeter of the square on the screen.
I choose to do 3 functions + the main function:
#include <iostream.h>
10. Write a function called RectangleProperties that computes the area and perimeter of the rectangle based
on the height and width of the rectangle. The function is not supposed to display anything on the screen. Instead, it
should return the computed area and perimeter via the parameters. Note: since the function needs to return two
answers, it can not use the standard return mechanism. Instead, perhaps you could use reference parameters in
addition to value parameters.
There should be two input variables (height and width) and two output variables (area and perim). Thus, the function
header should be:
i.e. the function returns the perim via a return statement and the area via a reference variable. However, that
variation is not good. The first idea is better, here is its completion:
y = 2, x = 1, j = 4, i = 3
-> <-> -> <->
void Mystery(double x/*=2*/,double& y/*=1*/,int i/*=4*/,int& j/*=3*/)
{
x = y; // y was 1, so now x is 1
y = x; // x is 1, so now y is 1
i = 2*i; // i is 4, so now i = 8
j = 2*j; // j is 3, so now j = 6
// the change in y goes back to x, change in j goes back to i
}
12. Please list the output of the program below. In other words, every time there is a cout statement, list the
values that will appear on the screen.
#include <iostream.h>
int main(void)
{
for (int i = 0; i <= 5; i+=2)
cout << "i = " << i << endl; // i = 0, i = 2, i = 4
// tricky
for (int i = 1; i < 3; i++)
for (int j = 1; j <= 5; j+=2)
cout << "i * j = " << i*j << endl;
double xx = 1;
while (xx < 32)
xx *= 2; // xx becomes 2, 4, 8, 16, 32, then test is false
cout << "xx = " << xx << endl; // output is 32
int i = 8;
int j = 3;
int k = i / j; // k = 8 / 3 = 2 (integer division)
double x = i / j; // x = 8 / 3 = 2 (still integer division)
double y = ( (double)i / (double)j ); // x = 8.0 / 3.0 = 2.6666666
cout << "k= " << k << ", x = " << x << ", y = " << y << endl;
return 0;
}
13. Suppose that a gas company charges the following rates, based on consumption:
Write a program that computes the charges for a given amount of gas usage. The program should continue to
work until the user enters a negative number.
We create one function that takes as input a double, representing the amount of gas used in cubic meters and delivers
as output the cost. There’s only one output value so we’ll use a return statement, not reference parameters. The
function will use a nested if-else-if statement to compute the cost based on the above table:
double computeCost(double x)
int main(void)
{
double usage = 0;
while (usage >= 0)
{
cout << “Enter cubic meters of gas used: “;
cin >> usage;
if (usage >= 0)
cout << “Cost for “ << usage << “ cubic meter of gas is “ <<
computeCost(usage) << endl;
}
return 0;
}
double A[10];
double A[10];
for (int i = 0; i < 10; i++)
A[i] = 1.0;
3. Assume A is an array of N integers. Find the sum of the first and last entries and assign it to the third
element.
4. Write some code segment that prints out an array of doubles of size N
5. Write some code segment that multiplies each element of an array by 2, storing it back in the array.
1. Write some code segment that prints out an array of doubles of size N.
2. Write some code segment that finds the sum of two arrays.
The remaining questions are again for possible extra credit, since we did not cover that in class
4. Write some code to find (a) the maximum and (b) the minimum of elements in an array.