[go: up one dir, main page]

0% found this document useful (0 votes)
7 views9 pages

Practiceexam1 Q&A

The document is a practice exam for a C++ programming course, containing various questions related to programming concepts, data types, and coding tasks. It includes explanations of terms like source code and machine code, advantages of data types, and common coding errors, along with code snippets for specific tasks. Additionally, it covers function design, variable declaration, and loop behavior, providing examples and expected outputs.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Practiceexam1 Q&A

The document is a practice exam for a C++ programming course, containing various questions related to programming concepts, data types, and coding tasks. It includes explanations of terms like source code and machine code, advantages of data types, and common coding errors, along with code snippets for specific tasks. Additionally, it covers function design, variable declaration, and loop behavior, providing examples and expected outputs.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

CSAS 1111: Exam 1

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.

An ANSWER KEY will be posted Saturday evening. Email me any questions!


1. Explain each of the following terms: source code, machine code, compiling, linking:

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”;

4. Please answer the following questions:


a) When writing a C++ program, you think that you should design three functions to solve individual
subtasks, as well as the usual int main(void) function to put all the pieces together in the correct order to
solve the actual task completely. Which of these functions do you design first ?

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 ?

Initialization, Testing, and Modification


c) In C++ programming, where does the declaration of a new variable belong ?

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 = 10;

e) Can you declare a variable without assigning a value to it ?

Yes, as in double x; you should, however, assign an initial value even if you don’t have to.

f) In C++ programming, what is the difference between = and == ?

The first is assignment (=), while the second is equality (==).

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;
}

i) A program to print all even integers between 1 and 30 on the screen

for (int i = 2; i <= 30; i+=2)


cout << i << endl;

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

int counter = 1; int counter = 1;


while (counter < 100) while (counter < 100)
{ cout << counter << “\n”;
cout << counter << “\n”; counter++;
counter--;
}

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

sum += x; // sum = sum + x (0 + 20), i.e sum is 20


sum += x; // sum = sum + x (20 + 20), i.e. sum is 40
cout << sum << "\n"; // the output will be 40
cout << x << “\n”; // the output will be 20
return 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;
}

First, i = 0. Then the “inner” loop starts and sets j = 0: output is 0


i is still 0, the inner loop sets j to 1: output is 0
i is still 0, the inner loop sets j to 2: output is 0
i is still 0, the inner loop sets j to 3: output is 0
now the inner loop is over and the “outer” loop executes again:
i is incremented to 1, j starts back at 0: output is 0
i is still 1, j is 1: output is 1
i = 1, j =2: output 2;
i = 1, j = 3: output 3:
now inner loop is over and “outer” loop executes again:
i = 2; j = 0: output 0;
i = 2; j = 1; output 2;
i = 2; j = 2; output 4;
i = 2; j = 3; output 6;
now inner loop is over and “outer” loop executes again:
i = 3; j = 0: output 0;
i = 3; j = 1; output 3;
i = 3; j = 2; output 6;
i = 3; j = 3; output 9;

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>

double computeArea(double side)


{ return side * side ;
}
double computePerim(double side)
{ return 4*side;
}
double getNumber(void)
{ double number = 0.0;
cout << “Enter number: “;
cin >> number;
return number;
}
int main(void)
{
double input = getNumber();
cout << “Area: “ << computeArea(input) << endl;
cout << “Perim: “ << computePerim(input) << endl;
return 0;
}

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:

void RectangleProperties(double height, double width,


double& area, double& perim)

Another possibility would be to use:

double RectangleProperties(double height, double width, double& area)

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:

void RectangleProperties(double height, double width,


double& area, double& perim)
{ area = height * width;
perim = 2*height + 2*width;
}

Incidentally, another possible but bad variation would be:

void RectangleProperties(double& heightArea, double& widthPerim)


i.e. it uses the input values of the variables by interpreting them as height and width, while returning the output
values through the same variables, interpreting them as area and perim, respectively. That is also very confusing and
should be avoided (but it does save memory).

11. Consider the following function:


// by value param value param
void Mystery(double x, double& y, int i, int& j)
{
x = y;
y = x;
i = 2*i;
j = 2*j;
}
What is the output when the function Mystery is used as in the code segment below:
double x = 10.0;
double y = 20.0;
int i = 30;
int j = 40;
Mystery (x,y,i,j);
cout << x << y << i << j;
x = 1.0;
y = 2.0;
i = 3;
j = 4;
Mystery (y,x,j,i);
cout << x << y << i << j;

Variables are matched as follows in the first call:

x = 10, y = 20, i = 30, j = 40


-> <-> -> <->
void Mystery(double x/*=10*/,double& y/*=20*/,int i/*=30*/,int& j/*=40*/)
{
x = y; // y was 20, so now x is 20
y = x; // x is 20, so now y is 20
i = 2*i; // i is 30, so now i = 60
j = 2*j; // j is 40, so now j = 80
// only the changes in y and j go “back”
}

Output will be: x = 10, y = 20, i = 30, j = 80

Variables are matched as follows in the second call:

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
}

Output will be: x = 1, y = 2, i = 6, j = 4

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;

// output would be: (1*1=) 1, (1*3=) 3, (1*5=) 5,


// (2*1=) 2, (2*3=) 6, (2*5=) 10;

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;

// output would be: k = 2, x = 2.0, y = 2.666

return 0;
}

13. Suppose that a gas company charges the following rates, based on consumption:

Gas used Rate


First 70 cubic meters $5.00 fixed cost
Next 100 cubic meters 5.0 cents per cubic meter
Next 230 cubic meters 2.5 centers per cubic meter
Above 400 cubic meters 1.5 cent per cubic meter

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)

Assuming that function works, our main function looks as follows:

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;
}

14. Following are some sample questions dealing with arrays:


1. Define an array A of 10 doubles.

double A[10];

2. Define an array A of 10 doubles and set all of them to 1.0;

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.

A[2] = A[0] + A[N-1];

4. Write some code segment that prints out an array of doubles of size N

for (int i = 0; i < N; i++)


cout << A[i] << endl;

5. Write some code segment that multiplies each element of an array by 2, storing it back in the array.

for (int i = 0; i < N; i++)


A[i] = 2 * A[i];

1. Write some code segment that prints out an array of doubles of size N.

void printArray(double A[], int N)


{
for (int i = 0; i < N; i++)
cout << A[i] << endl;
}

2. Write some code segment that finds the sum of two arrays.

for (int i = 0; i < N; i++)


C[i] = A[i] + B[i];

3. Write some code segment that adds up all elements in an array.

double sum = 0.0;


for (int i = 0; i < N; i++)
sum += A[i];
// sum now knows the answer

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.

// to find the minimum:


double min = A[0]
for (int i = 0; i < N; i++)
if (A[i] < min)
min = A[i];

// to find the maximum


double max = A[0];
for (int i = 0; i < N; i++)
if (A[i] > max)
max = A[i];

You might also like