Final exam

¡Supera tus tareas y exámenes ahora con Quizwiz!

suppose you are writing a program that uses a stream called fin that will be connected to an input file and a stream called fout that will be connected to an output file. How do you declare fin and fout? What include directive, if any, do you need to place in your program file?

#include<iostream> #include<fstream> using namespace std; ifstream fin; ofstream fout;

Give an example of an infinite loop.

#include<iostream> using namespace std; int main () { for( ; ; ) { cout<<"This is the infinite loop."<< endl; } return 0; }

List the hierarchy of the following operations. (the ones that come first should be listed on a line above those that come later. If two or more are at the same level, list them on the same line in any order.) + * () / - %

() * / % + -

In the following code, show what needs to be coded in the "while" of the outside loop to control whether or not more data is run. do { do { //various code } while(//something); //more code cout<<"more data?"<<endl; cin >> again; } while (what goes here?);

(Again = 'y' || Again = 'Y')

write the following inequality as a C++ boolean expression. 36<x<=72

(x>36&&x<=72)

Debug the follwoing code snippet for syntax error only. Identify the lines and specify what is (are) the error(s). Identifying other than syntax errors will be awarded penalties int var1, var2; float var3; int temp1 = 6.2; cout<<"Enter_value_1"<<\n; cin>>var1>>endl; cout<<"Enter_value_2_endl";

- Line 7: there isnt a ; after var2 - line 4: should read cout<< "Enter_value_1\n."; -line 5: no >> endl -line 6: should be "<<endl;

assume the following declaration apply; int X =2, Y=-3, z=4, A=5, B=6; what output, if any occurs based on the code below? if (X>0) cout << Y; cout << z;

-3 4

consider the following code: int x, y; x = 0; do { x++; y = 0; do { y++; if (x<y) cout <<x<< " "<<y<< endl; }while ( y <= 3); cout <<x << " " << y << endl; }while (x <=2); output looks like:

1 2 1 3 1 4 2 3 2 4 3 4

consider the following code: int i,j; for(i=1; i<=3; i++) for(j=2; j<=3;j++) cout<< i << " "<< j << endl; The output would be:

1 2 1 3 2 2 2 3 3 2 3 3

consider the following code: int i,j,found,A[5]={2,4,6,4,10}, B[5]; found =0; for (i=0; i<=4; i++) if(A[i] ==4) { B[found] = i; found++; } for (j=0; j<=found-1; j++) cout << B[j]<<" " ; Output looks like :

1 3

Consider the following code: int count, n; n=3; count = 1; while (count <= n) { cout << count << " "; count ++; }

1, 2, 3

consider the following code: int c, n; n =3; c = 0; do { c++; if (c < n) cout << c << " "; else cout << n << " "; }while (c <= 2 * n);

1, 2, 3, 3, 3, 3, 3

Consider the following code : int count, n; n = 3; count = 1; while (count <= n) { cout<< count << " " ; count ++; } cout << count;

1, 2, 3, 4

Consider the following code: int count, n; n = 3; count = 0; do { count ++; cout<< count << " "; }while (count <= n);

1, 2, 3, 4

consider the following code: int i; for(i=1; i <= 15; i = i+2) cout << i << " ";

1, 3, 5, 7, 9, 11, 13, 15

consider the following code: int sq(int i); int main () { int i, j; j=3; for(i=1; i <=j; i++) cout << sq(i) << " "; return 0; } int sq(int i) { return (i*i); }

1, 4, 9

What is the output of the following program? #include <iostream> using namespace std; void figure_me_out(int&x, int y, int& z); int main() { int a, b, c; a=10; b=20; c=30; figure_me_out (a, b, c); cout<< a <<""<<b<<""<<c; return 0; } void figure_me_out(int& x, int y, int& z) { cout<<x<<""<<y<<""<<z<<endl; x=1; y=2; z=3; cout<<x<<""<<y<<""<<z<<endl; }

10 20 30 1 2 3 1 20 3

in the following two statements, int a[10] and a [7], what is the meaning of the variables?

10 is your rows and 7 is your columns

what is the output of the following operation. myOutput= 17%5; cout<< myOutput;

2

What is the output of this loop? what is the relation between the value of 'n' and the value of 'log'? int i; int n = 1024; int log = 0; for (i =1; i <n; i=i+2) log++; cout << n << " " << log << endl;

2 4 8 16 32 64 128 256 512 1024 relation: log2 = 1024

What is the output of the following? for (int count = 1; count < 5; count ++) cout<< ( 2 * count) << " ";

2 4 6 8

What is the value of 15%4?

3

Express the following equation as a C++ expression: 3x^2+2x+1

3*pow(x,2)+2*x+1

assume the following declaration apply; int X =2, Y=-3, z=4, A=5, B=6; what output, if any occurs based on the code below? if (X<=0) cout<< Y; else cout << z;

4

assume the following declaration apply; int X =2, Y=-3, z=4, A=5, B=6; what output, if any occurs based on the code below? if (Y > 0) cout << A; else if (X > 2) cout << B; else cout << z;

4

conder the following code: int i; for(i =5; i >=2; i--) cout<< i << " ";

5, 4, 3, 2

consider the following code segment: int j,k; for (k=0;k<10;k++) { for(j=0; j<5; j++) cout<< "*"; } cout<< endl; how many stars are output when this code segment is executed?

50

consider a class where you have not coded an operator overload for = or ==

= works, but == does not.

what is the output of the following code: int x=4 cout<<"Begin..."<<endl; if(x<=7) if(x!=3) cout<<"My Story."; else x-=1; cout<<"The End!"<<endl; cout<<"New Beginnings" <<endl; if(x>3) if(x !=0) cout<<"End here..."; else cout<<"Will never end..."; cout<<"The End!<<endl;

Begin... My Story. The End. New Beginnings End Here... The End!

In the command fg++ prog1.cpp -o prog1, What does the file prog1.cpp contain?

C++ code

Given the function prototype: void foo(float a, float b); A call to the function should look like:

Foo (a,b);

What is the output of the following program? #include <iostream> using namespace std; void friendly(); void shy(int audience_count); int main() { friendly (); shy(6); cout<<"one more time:\n"; shy(2); friendly(); cout<<"End of program:\n" return 0; } void friendly() { cout<<"Hello\n"; } void shy(int audience_count) { if (audience_count<5) return; cout<< "Goodbye\n"; }

Hello Goodbye One more time: Hello End of program:

In the command fg++ prog1.cpp -o prog1, What does prog1 contain?

Machiene code

Can a function definition appear inside the body of another function definition?

No

indicate whether or not the following are valid assignment statements. If a statement is invalid, indicate what is wrong with it. Assume if a variable is named and used correctly, it's been declared and initialized as needed. rate * time = distance;

Invalid because it needs to be distance = rate * time

In most of the programs we have written, we've had two nested DO loops. What does the outer loop do?

It asks if we want to enter more data points

In most of the programs we have written, we've had two nested DO loops. What does the inner loop do?

It runs data and determines values or statements.

consider the follwoing code and tell whether it calculates totals for rows, or for columns. for(j=0; j<=iResCols-1; j++) { fTotal=0; for(i=0; i <= iResRows -1; i++) { fTotal+= rgfResTbl[i][j]; } cout << fTotal << " "; fGrandTotal += ftotal; }

Rows

declare an ary called studentFirstName that will store the first names of five students

String studentFirstName[5]

Every function definition can only have one return statement.

True

indicate whether or not the following are valid assignment statements. If a statement is invalid, indicate what is wrong with it. Assume if a variable is named and used correctly, it's been declared and initialized as needed. N = N + 1;

Valid

Can a function that returns a value have a call-by-reference parameter?

Yes

Is C++ a case sensative language?

Yes

May a function have both call by value and call by reference parameters?

Yes

can a function definition contain a call to another function?

Yes

what is the difference between endl and \n?

\n is a new line. endl is a new line and a flush

what is the main difference between a while loop and a do while loop?

a do while will execute the body of the loop once and then evaluate the while condition. a while loop just evaluates the while condition.

explain the concept of local scope of a varaible with the help of an example.

a local scope variable can only be used within the function it is declared. ex. int main() { int variable; } cant be used in int function() { _ }

what is the difference betwee post-decrement( i --) and pre decrement (--i) operator? Explain with an example.

a post plugs into the program than decreases a pre decrement decreases first then plugs post

When using file I/O..

all input and output file openings should be verified.

consider the following code: void foo(float x, float& y); float foo2(float x, float y); int main() { float x,y,z,a,b,c; cout<<"Enter 2 numbers"<<endl; cin >>a >>b; //assume input was 3 4 foo(a,b) x= foo2(b,a); cout<<"b="<< b << endl; cout<<"x=" << x << endl; return 0; } void foo(float x, float& y) { y = y+x } float foo2(float x, float y) { return (x-y); } Output looks like:

b = 7 x = -4

Declare a boolean variable called Done and initialize it to true.

bool Done = true

write a function definition for a function called GreaterThan that takes two arguments, both of type float. The function should compare the two numbers and if the first number is greater than the second number, the function should return true. false if vise versa. given these requirements, you should be able to figure out what return type the function must have. you dont need to write the prototype, and you dont need to write a 'main' function to invoke (call) this function. Do write pre and post conditions for this function

bool GreaterThan(float FA, float FB) { if (FA>FB) return(FA>FB) else return 0; } Pre: function expects two values Post: True expression if FA>FB, else returns false

As far as the compiler is concerned, variable declerations....

can be anywhere in your code provided a variable is declared before it is used

Declare a variable called More that can hold a single letter, such as Y or an N

char More

what data type is 'a'?

character

Determine if the following cin statements are valid or not. If they are not tell why. If they are valid, indicate what will happen if the are given the input indicated -- if it produces errors, say so, if not, tell what is stored in each variable upon completion of the statement. Assume the following declarations have also been made. int a,b; float x, y; cin >> x >>y; input is 2 4

cin is valid and x=2 y=4

Determine if the following cin statements are valid or not. If they are not tell why. If they are valid, indicate what will happen if the are given the input indicated -- if it produces errors, say so, if not, tell what is stored in each variable upon completion of the statement. Assume the following declarations have also been made. int a,b; float x, y; cin >> x >> y; input is 2.0

cin is valid but the input is invalid and will produce errors because only one value was entered.

Determine if the following cin statements are valid or not. If they are not tell why. If they are valid, indicate what will happen if the are given the input indicated -- if it produces errors, say so, if not, tell what is stored in each variable upon completion of the statement. Assume the following declarations have also been made. int a,b; float x, y; cin >> x >>y; input is 2 4 5

cin is valid but there is errors because 3 values were entered

write a cout statement to print the following message: Alice's car is brand new Toyota Camery! She is in "love" with it.

cout<<"Alice's car is brand new Toyota Camery!"<<endl; cout<<"She is in "love" with it."<<endl;

assume the declaration int i = 5; Write an output statement that writes the following output and places the cursor on the next line after the output (assume the "5" comes from the variable itself -- not a hardcoded 5 in the output statement) Output statement: the value of i is 5

cout<<"The value of i is" << i<< endl; //hit enter

what data type is "Hello"?

coutstatement

When defining the equality operator, ==, for a class, it usuall is defined as

equivalence for each of the corresponding object.

Determine if the following boolean expressions are true or false. Assume value of variable foo is 1 and that variable bar is 5. !(bar != 6)

false

Determine if the following boolean expressions are true or false. Assume value of variable foo is 1 and that variable bar is 5. (foo ==0) &&(bar <20)

false

Determine if the following boolean expressions are true or false. Assume value of variable foo is 1 and that variable bar is 5. !(((foo <10)||(x<y))&&(foo >=0))

false

suppose you are still writing the same program from the above problem and you reach the point at which you no longer need to get input from the filestuff1.dat and no longer need to send output to the file stuff2.dat. How do you close these files?

fin.close(); fout.close();

what data type is 3.0?

float

(For a 1 dimensional array) shift each element to the left and the leftmost one to the end of the array. const int iMAX = 10; int main () { float rgfA[iMAX]; int iUsed;

float Temp; Temp= rgfA[0]; for(int i=0; i <iUsed-2; i++) { rgfA[i] = rgfA[i+1]; } rgfa[iUsed-1] = Temp; return 0; }

Declare a variable called Time that can hold a number which might include a fractional part and initialize it to 6.2

float Time = 6.2

Prototypes for various functions are listed below. Tell which one is best suited for a function that receives two numbers from the calling function (which will be referred to as "a" and "b" in the function that is called) and somehow uses those to produce a number back to the calling function. a) void addtwo(float a, float b, float c); b) float addtwo (float a, float b) c) float addtwo(float a, float b, float c); d) void addtwo(float& a, float& b, float& c);

float addtwo(float a, float b)

declare a constant named PI with an appropriate data type, and give it a value of 3.1416

float double PI = 3.1416

declare a variable called employeeSalary that will store decimal values and initialize it to a starting value of one thousand

float employeeSalary; employeeSalary=1000;

consider the following code: const int max=5; int main() { int A[MAX], used; cout<< "how many numbers do you have? (<= MAX)"<<endl; cin>>used; for(i =__; i <=__; i++) { cout<<"enter a value for the array"<<endl; cin >> A[i]; } return 0; } and assume the data is 4 10 20 30 40 What should the above "for" line look like to best load the array?

for (i=0; i<=used-1; i++)

(for a 1 dimensional array) determine if the elements are all in order from low to high and if they are out of order. Determine which location holds the first element that's wrong. con int iMAX = 10; int main() { float rgfA[iMAX]; int iUsed; int iLoc; float fValue;

for (int i=0; i<iused-1; i++) { for( i=0; iLoc<0;i++) { if(rgfA[i]>rgfa[i+1]) { iLoc=i+1; cout<< "Wrong value at"<<iLoc<<endl; } else { iLoc=-1 } return 0; }

write a for loop that will pring the following design * ** ***

for(i=1; i<=3;i++) { cout <<*<<""<<endl; }

Write a (for) loop that will write the word hello to the screen ten times (when embedded in a complete program.) (You dont have to write the program, just the loop itself.)

for(int count=1; count <=10;count++) { cout<< "Hello"<<endl; }

In desigining a class, as far as the compiler is concerned, everything can be made public, but its bad programming style. Why?

generally functions are public and data is private

assume the following declarations have been made: int iA, iB =5, iC=2, iD=6, iE= 4; float rA, rB = 7.0, rC = 2.0, rD=5.0; What is stored in the variable iA after the following statement? iA = iB/ iC;

iA = 2

assume the following declarations have been made: int iA, iB =5, iC=2, iD=6, iE= 4; float rA, rB = 7.0, rC = 2.0, rD=5.0; What is stored in the variable iA after the following statement? iA = rB/ iC;

iA = 3

With classes that have private data, you need the folloing to access data correctly ( assume you did not write any constructors):

input/output functions and get/set functions

Declare a variable called People that can only hold a number which has no fraction, and initialize it to 12

int People = 12

write a for loop that sums the odd integers from 1 to 115 inclusive. this value should be stored in an integer varaible name iSum

int i,iSum; for(i=1; i<=115;i=i+2) { iSum = i + iSum; }

write a prototype for a function 'my_func' that has an argument list that contains array 'rgiA' and any other necessary variable(s) needed for the compiler and the programmer to figure out how to process the array. assume the function returns a value of type 'int' also assume the array does not need to be changed to obtain the return value. (dont worry about the actual specifications for what the function does -- you dont need to write a function definition) const int iROWS=20; const int iCOLS=20; int main() { int rgiA [iROWS][iCOLS]; int iRowsUsed, iColsUsed; // (call to fill array)

int my_func( int rgiA[][iCOLS], int iRowsUsed, int iColsUsed)

Write a statement in C++ language which will perform division between two integer variables called var1 (assume value 3) and var2 (assume value 2) and store the result in float variable called var 3. Make sure that the statement will generate the correct result

int var1=3, var2=2; float var3; var3=static_cast<float>(var1)/var2 cout<<var 3<< endl;

what data type is 3?

integar

indicate whether or not the following are valid assignment statements. If a statement is invalid, indicate what is wrong with it. Assume if a variable is named and used correctly, it's been declared and initialized as needed. "prod" = x * y;

invalid because prod shouldnt be in quotes

In the command fg++ prog1.cpp -o prog1, What does fg++ do?

it scans for errors in the program and runs it

assume the following declaration apply; int X =2, Y=-3, z=4, A=5, B=6; what output, if any occurs based on the code below? if (X<0) if (Y<0) cout << A; else cout << B else if ( z < 0 ) cout << X; else cout << Y;

no output would occur

When you have two or more different types of structs in a program, in their definitions:

only one can reference the other, and if A's code refers to B's code as part of it's definition, B's code must be listed first in your program.

assume the value of myChoice is 2. What will be the output produced? switch(myChoice) { case 1: cout<< "picked choice 1"<<endl; case 2: cout<<"picked choice two"<<endl; default: cout<<"Wrong Choice"<< endl; break; }

picked choice 2 wrong choice

what is machine language?

programs written using 0's and 1's, which can be directly understood by the computer

assume the following declarations have been made: int iA, iB =5, iC=2, iD=6, iE= 4; float rA, rB = 7.0, rC = 2.0, rD=5.0; What is stored in the variable rA after the following statement? rA = iB/ iC;

rA = 2

assume the following declarations have been made: int iA, iB =5, iC=2, iD=6, iE= 4; float rA, rB = 7.0, rC = 2.0, rD=5.0; what is stored in the variable rA after following statement? rA = rB/ iC;

rA = 3

assume the following declarations have been made: int iA, iB =5, iC=2, iD=6, iE= 4; float rA, rB = 7.0, rC = 2.0, rD=5.0; what is stored in rA after the following statement is executed? rA = iB + rB/rC *iC - iE;

rA = 8.0

assume the following declarations have been made: int iA, iB =5, iC=2, iD=6, iE= 4; float rA, rB = 7.0, rC = 2.0, rD=5.0; assume I want to divide by iB and iC and store the correct result (including any fractional part if it exisits). Write a statement that will do this.

rA = static_cast<float>(iB)/iC

a compiler can be catagorized as a what?

software

consider the following code: const int MAXROW=20, MAXCOL = 20; int i,j,tot,rows,cols,A[MAXROW] [MAXCOL]; //assume A has been partially loaded with values. "rows" rows and "cols" //columns have been loaded in already tot = 0; for(i=0; i<= rows-1; i++) for(j=0; j<=cols-1; j++) tot = tot + A[i][j]; tot contain...

sum of all elements

consider the following code: const int MAXROW=20, MAXCOL = 20; int i,j,tot,rows,cols,A[MAXROW] [MAXCOL]; //assume A has been partially loaded with values. "rows" rows and "cols" //columns have been loaded in already for( i=0; i<=cols-1; i++) { tot=0 for(j =0; j<=rows-1; j++) tot = tot + A[j][i]; } tot contains

sum of each column's elements

Since there is no one available to answer yes/no questions regarding whther or not there is more data when using a file for input it does not mean

that the file must always contain a count of how many values to process.

why do we have programs written with class definitions?

they are used to facilitate better and easier design for certain programs.

What is the header file <cmath> to be included for?

to preform mathematical opertaions

consider the following function definition: void too2(int a[], int how_many) { for( int index=0; index < how many; index++) a[index]=2; } which of the following are acceptable function calls? int my_array[29]; int your_array[100]; too2(my_array, 29); too2(my_array, 10); too2(my_array, 55); "Hey too2. Please come over here." too2(your_array, 100); too2(my_array[3], 29);

too2(my_array, 29); too2(my_array, 10); too2(your_array, 100);

Consider the following function definition: void tripler (int& n) { n = 3 * n; } which of the following are acceptable function calls? int a[3]= (4,5,6), number =2 tripler(number); tripler(a[2]); tripler(a[3]); tripler(a[number]); tripler(a);

tripler(number); tripler(a[2]); tripler(a[number]);

Determine if the following boolean expressions are true or false. Assume value of variable foo is 1 and that variable bar is 5. (bar <20)||((bar/foo)>7)

true

what (if anything is wrong with the following code? (assume you are using the definition of 'tripler' from page 8. Q1) int b[5] = (1,2,3,4,5}; for (int i =1; i <= 5; i++) tripler (b[i])

using i <= 5 will cause the array to go out of bounds as b[] only has values stored in 0 - 4

consider the following code: const int max=5; int main() { int A[MAX], used; cout<< "how many numbers do you have? (<= MAX)"<<endl; cin>>used; for(i =__; i <=__; i++) { cout<<"enter a value for the array"<<endl; cin >> A[i]; } return 0; } and assume the data is 4 10 20 30 40 what should array A look like after the code has run?

values: 10 20 30 40 ? Indices: 0 1 2 3 4

example of call by reference and value

void Func(int a, int * b, int & c) { } int main () { int x = 0; int * y = &x; int z = 0; Func(x, y, z); return 0;

what is an example of a function that will work correctly to swap two numbers (a and b) that are passed to it (and get them back to the calling function with them swapped0?

void swap(int& a, int& b) { int T; T=a; a=b; b=T; }

an example of a function prototype that is appropriate for swapping values of two int variable.

void swap(int5& a, int& b)

what is overloading a function?

when you specify more than one definition for a function name or an operator in the same scope

Can you over load an operator like == for a class?

yes


Conjuntos de estudio relacionados

Chapter 12 - Cash Flow Statement

View Set

Unit 3 AP Psychology - Sensation Quiz

View Set

Pre-peri-Post Surgical care practice questions

View Set

Unit 14 - Types of Risks and Required Disclosures

View Set

Texas Promulgated Contract Forms Chapter 7

View Set

Psychology 100 LC 7/8/9/10/11/12/13/14/15

View Set