Programming 1 Final Review

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the value returned by the following function? int function() { int value = 35; return value + 5; value += 10; } choices: 45 50 40 35

40

If a function needs to modify more than one variable, it must choice: be pass by value be a void function return all values needed be a call by reference function

be a call by reference function

Which of the following are valid case statements in a switch? choices: case 1: case 105: case 406: case x<4: case 1.5: case 'ab':

case 1: , case 105: case 406: , case 'ab':

Given the following code fragment and the input value of 2.0, what output is generated? float tax; float total; cout << "enter the cost of the item\n"; cin >> total; if ( total >= 3.0) { tax = 0.10; cout << total + (total * tax) << endl; } else { cout << total << endl; }

2.0

Is there an error in the following function? If yes, what line is this error in? 0. void f1(int array[], int size) 1. { 2. int i=0; 3. while(i< size) 4. { 5. array[i] += 2; 6. cout <<array[i]; 7. i++; 8. } 9. } choices: 2 0 None of these 6 5 7

None of these

Write the include directive so that your program can use the class string.

#include <string>

If a double variable x has the original value of 0.58, what would be displayed with the following two statements. cout << static_cast<int>(x) << " "; cout << x; choices: 0 0.58 1 1 1 0.58 0.58 0.58 0.58 0

0 0.58

Given the following function definition void shift(int a, int& b) { a=b; b=a; } What is the output after the following function call? int first=0, second=10; shift(first, second); cout << first <<" "<< second << endl; choices: 10 0 10 10 0 0 0 10

0 10

Write the C++ expression for y < x < z?

((y<x)&&(x<z))

Given an array of integers of size 5, how does the computer know where the 3rd indexed variable is located? None of the above It adds 3 to the base address of the array It adds space for 3 integers to the base address of the array It remembers where all the indexed variables of the array are located.

It adds space for 3 integers to the base address of the array

Essay: what .find

It is a member function of the string class. It returns the index of characters passed by the parameter. It will return -1 if not found

Essay: What is .substr

It is a member function of the string class. It returns the sub string of the original string. The first parameter is the position it begins and the second parameter is the number of characters wanted

string line; int num; cout << "Please enter a number: "; cin >> num; cout << "Please enter your motto: "; //this line of code calls the getline function and store it in line. the c++ code is not shown but assume it is here. cout << line; Given the code above, if the user input is 50 and the user enters "I live in a multiverse!", what is displayed by cout << line; in the code segment above? hint: getline function

Nothing is displayed because there needs to be two getline functions called in order to get the users input for the line.

A string can be processed as ________ choices: integers data types doubles a char array

a char array

Write a function declaration statement. The function is named is_available; it will accept a parameter of a proper type named book_title and return a value to indicate the two scenarios: a book is available or a book is not available .

bool is_available(string book_title);

The string type in C++ is a primitive data type. true or false

false

Write a code segment that will print out "Thank you!" 5 times.

for( int i = 0; i<5; i++){ cout << "Thank You!";}

Provide a boolean expression for the following loop that prompts the user to enter a number that is in the range of 1 through 10. If the number is not in that range, he/she will be prompted repeatedly until the number is in that range. For example, if the user enters 13, he will be asked to enter again; if he enters -2, again; if he enters 9, he will be able to go on. int number; do { cout << "Please enter a number that is in the range of 1 through 10 inclusive: "; cin >> number; } while ( );

(number<1)||(number>10)

Which of the following boolean expressions tests to see if x is between 2 and 15 (including 2 and 15)? choices: (2 <= x <= 15) (x >=2 && x <=15) (x<=15 || x>=2) (2 <=x || x <=15)

(x >=2 && x <=15)

Write the boolean expression that allows a while loop to continue as long as x is negative.

(x<0)

Which of the following comments would be the best post-condition for this swap function void swap( int& left, int& right); choices: //Postcondition: left and right are unchanged //Postcondition: the values of left and right are exchanged. //Postcondition: None //Postcondition: left has the value of right

//Postcondition: the values of left and right are exchanged.

How many times is "Hi" printed to the screen with the following code segment? Read very carefully.. for (int i=0; i<14; i++); cout <<"Hi";

1

The following statement will generate a random number in the range (inclusive) of _____ and _____. Separate the two values with a space. (rand()%11) + 10;

10 and 20

Given the code segment below int result1 = 12%24; int result2 = 12/24; Fill in the blank with the two values for result1 and result2, separate them with a space. For example, if result1 is 34 and result2 is 13, then you write: 34 13

12 0

What is the value of i if i = 0? What would i be if i = 2? separate the two values with a space. switch (i) { case 0: i = 15; break; case 1: i = 25; case 2: i = 35; case 3: i = 40; break; default: i = 50; }

15 40

What is the value of the following? sqrt(sqrt(pow(2,4))); CHOICES: 16 2 1 4

2

what is the value of x after the following statements? int x; x = 0; x += 30;

30

What is the output of the following code? float value; value = 33.5; cout << value << endl;

33.5

Given the following code fragment, what is the final value of y that is displayed? int x, y; x = -1; y = 0; while(x < 3) { y += 2; x += 1; } cout << y;

8

What is the value of choice after the following statements? //function declaration void getChoice(int& par_choice, int par_count); //main function fragment int choice, count=3; getChoice(choice, count); //function definition void getChoice(int& par_choice, int par_count){ if(par_count<0) par_choice =0; if(par_count == 0) par_choice=-1; else par_choice=99; return; } choices: 3 -1 99 0 garbage because choice is not initialized

99

string s1 = "A good day is "; string s2 = "every"; string s3 = "day"; s1 = s1 + s2 + s3; cout<<s1; Given the code above, what is the output? A good day is every day A good day is everyday every day everyday

A good day is everyday

What does C++ come from? hint(language)

C++ is an object oriented language as well as a procedural language

Why is updating the Loop Control Variable important?

If you do not update the loop control variable, then the loop will be an infinite loop and run forever. The update is very important, to help the loop update as you go, until it is false and then it terminates.

/precondition: c must be an upper case letter char lower (char c) { char result; result = static_cast<char>(static_cast<int>(c) + 32); } Will the function definition above compile? Must explain your answer.

No, there is no return statement to 'char lower'

What is the output of the following function and function call? //function declaration void calculateCost(int count, float& subTotal, float& taxCost); //main function float tax = 0.0, subTotal = 0.0; calculateCost(15, subTotal,tax); cout << "The cost for 15 items is " << subTotal << ", and the tax for " << subTotal << " is " << tax << endl; //end of main function fragment //function definition void calculateCost(int count, float& subTotal, float& taxCost) { if ( count < 10) subTotal = count * 0.50; else subTotal = count * 0.20; taxCost = 0.1 * subTotal; } choices: The cost for 15 items is 0.00, and the tax for 3.00 is 0.00; The cost for 15 items is 3.00, and the tax for 3.00 is 0.00; The cost for 15 items is 3.00, and the tax for 3.00 is 0.30; The cost for 15 items is 0.00, and the tax for 3.00 is 0.30;

The cost for 15 items is 3.00, and the tax for 3.00 is 0.30;

What will the following loop statement do? Explain your answer. for(int i=0;i<10;i--) { cout << "Hello\n"; }

The following for loop, will continue to print "hello" as long as the boolean expression i<10 is true. The LCV update is "i--" which means it will count down from i. i=0, which means the boolean expression will be infinite, because i will always be less than 10.

What is the correct output of the second cout statement? //function definition int doSomething(int value) { value = 35; return value; value = 13; } //fragment of main program int myInt = 0; cout << doSomething(myInt); cout << "The value of myInt is " <<myInt; choices: The value of myInt is 35 The value of myInt is 13 The value of myInt is 48 The value of myInt is 0

The value of myInt is 0

The postcondition for a function describes: What must be true before the function executes What is true after the function is executed What the function does How to call the function

What is true after the function is executed

Given an array named student_name that stores 22 student names, write a loop statement that will assign "Unknown" to all names.

for(int i = 0; i < student_name; i ++){ student_name[i] = "Unknown";}

Which of the following declares an array of 5 characters, and initializes them to some known values? char array[5]={'a','b','c','d','e'}; char array[]={'a','b','d','e'}; all of these char array[4]={'a','b','c','d','e'};

char array[5]={'a','b','c','d','e'};

Declare a char variable named letter, ask the user to enter a letter grade. Then use a switch or if else if... statement to output an appropriate message for each grade. (Grades can be A, B, C, D, F or a b c d f) For example, if A is entered, your code should say "Excellent job!".

char letter; cout << "Please enter a letter grade: "; << endl; cin >> letter; switch (letter){ case 'A': case 'a": cout << "Excellent job!" break; case 'B': case 'b': cout << "Good job, study a little more"; break; case 'C': case 'c': cout << "Passing, do better!"; break; case 'D': case 'd': case 'F': case 'f': cout << "Failing, Study more"; break; default: cout << "Invalid entry"; }

Which of the following data types can be used in a switch controlling expression? choices: char int float double

char, int, double

Write an input statement that will read a whole number and store it in the already-declared variable named int_value;

cin >> int_value;

Essay: what is difference of cin and getline?

cin read only one token(word) only and skips any whitespace. getline reads a whole line including the white spaces.

If I need to read the space in "Hello World" into a char variable, which command/function should I use? choices: cin.get getline cout cin

cin.get

Write an output statement to display "Hello" to the screen with the quotation marks. I should see "Hello" on the screen when the program is run.

cout << "\"Hello\"";

Given the following declarations, which of the following is a legal call to this function? int myFunction(int myValue); int myArray[1000]; choices: 2 answers myArray = myFunction(myArray); cout << myFunction(myArray[0]); cout << myFunction(myArray); myArray[1] = myFunction(myArray[0]);

cout << myFunction(myArray[0]); myArray[1] = myFunction(myArray[0]);

double average (double n1, double n2); In the function declaration statement above, n1 and n2 are referred to as the ________ parameters.

formal

Which loop structure always executes at least once? choices: for do while sentinel while

do while

If you have the two functions as shown, int someFunction(int value); double someFunction(double value); and a variable x, which is a float, which function is called by the following statement? cout << someFunction(x); choices: int someFunction(int value); double someFunction(float value); double someFunction (double value); Neither, there is a syntax error because the function does not exist

double someFunction (double value);

If you need to write a function that will compute the tax due given the tax rate and the gross income, which would be (an) appropriate function declaration(s)? multiple answers choices: double tax (double tax_rate, double income); void tax (double& tax_rate, double income, double& tax_due); void tax (double& tax_rate, double& income, double tax_due); void tax (double tax_rate, double& income, double& tax_due); void tax (double tax_rate, double income, double& tax_due); double tax (double tax_rate, double income, double tax_due);

double tax (double tax_rate, double income); void tax (double tax_rate, double income, double& tax_due);

Given an int array named num1 that stores 100 integers, another int array named num2 that stores another 100 integers, given also that a third int array named total and is of size 100, write a statement that will store the sum of num1 and num2 element by element in total. For example, if num1 has 10, 11, 12 as the first three elements, and num2 has 1, 2, 3 as the first three elements, then the first three elements of total would store 11, 13, 15. num1 10 11 12 num2 1 2 3 total 11 13 15 REMEMBER FOR LOOP

for( int i = 0; i < 100; i++) { total[i] = num1[i] + num2[i]; }

Which of the following array declarations is/are legal? g. All but B e. All of the answers a. int array[10]; b. int size; cin >> size; int array[size]; d. const int size=9; int array[size]; f. All but C c. int array[3]={0,0,0};

g. All but B

Write a statement that will get the full name (including the middle name if there is one) from the user and store it in the string variable named full_name. string full_name; cout << "Please enter your full name: "; //Your statement goes here. remember getline

getline(cin, full_name);

You ask the user to enter a line of input, and you will stop reading and ignore the rest of the input as soon as you encounter the question mark. Which statement will achieve that? Assume that the string variable input has been declared. choices: cin.getline(input, '?'); getline(cin, input, '?'); ignore (input, '\?'); ignore (80, input, '?');

getline(cin, input, '?');

Given the following code, what is the final output? int i; for(i=0; i<=4;i++) { cout << "Incrementing " << endl; } cout << "i is "<<i;

i is 5

Given the function declaration and function invocation/call as follows char get_char (int index); char c = get_char (65); choices: two answers index is a local parameter 65 is the real parameter c will get an int value after the function call 65 is the actual parameter

index is a local parameter 65 is the actual parameter

Declare a variable named head_count with the appropriate type so that it can store the number of students in a class.

int head_count;

Provide the function DEFINITION for a function named pass_fail. The function will accept a student's score and decide whether the letter grade should be P or F. It will then return the letter grade.

int pass_fail(char grade){ if(grade >= 70){ return 'P';} else { return 'F';} }

Write a function DECLARATION statement. The function is named rand_gen. It will generate a random number between zero and the value passed by the parameter named upper_limit. It will return the random number generated.

int rand_gen(int upper_limit);

Given the following function definition for a search function, and the following variable declarations(assume they all have been initialized), which of the following is an appropriate function invocation? const int SIZE=1000; int search(int list[], int target, int numElements); int list[SIZE], target; choices: search(list[0], target, numElements); int result=search(list[0], target, SIZE); int result=search(list, target, SIZE); int result=search(list[], target, SIZE);

int result=search(list, target, SIZE);

Write a complete code segment that will calculate the total of the numbers between 1 and 100 inclusive. Declare and initialize variables as deemed necessary. You can use any loop structure. Display the total once after the loop.

int sum=0; for(int i=1; i<100; i++){ sum+=i; cout<< sum << i;}

What is the output of the following code segment if x is 15? if(x < 20) if(x <10) cout << "less than 10 "; else cout << "large\n"; choices: large less than 10 nothing no output, syntax error

large

string s; //assume s is initialized. int len; Given the statements above, write a statement that will assign the number of characters in s to len. remember member functions for length

len = s.length();

Given an int array named my_array that stores 25 integers,what is the equivalency of the following statement? my_array[pow(2,3) + 2] = 100; choices: my_array[8] = 100; my_array[11] = 100; my_array[9] = 100; my_array[10] = 100;

my_array[10] = 100;

What is wrong with the following function body? void calculate(int count, float price, float& cost) { if (count < 0) cost=0.0; else cost=count*price; return; } choices: void functions must return a value can not mix reference and value parameters void functions may not have a return statement. nothing

nothing

When the value of the variable will be changed in the function body, what way should you pass it?

pass by reference

Match the following with the proper choices: pass by reference pass by value mixed parameter list choices: double avg (int i, int j); void user_choice(char& choice1, int& price); void pass_fail(double points, bool& pass);

pass by reference = void user_choice(char& choice1, int& price); pass by value = double avg (int i, int j); mixed parameter list = void pass_fail(double points, bool& pass);

When the value of the parameter will only be used in the function body, what way should you pass it?

pass by value

Given a string variable named q_and_a that is declared and initialized to a string that has a question mark in it, write a statement that will find the index of the question mark and assign the index to an int variable named position, which has been declared. hint: remember member function .find

position = q_and_a.find('?');

When you are getting input from the user in a function, you should always pass the parameters by _______________?

reference

string s1 = "san Antonio"; string s2 = "san antonio"; which of the following comparisons is true? s1 < s2 s1 > s2 none of these. You cannot use the relational operators to compare strings. s1 = s2 s1 == s2

s1 < s2

Given the code below, change the letter d to y so that it is now "Gooy". string s1 = "Good";

s1[3]='y';

Given the following function declarations and local variable declarations (assume they are initialized as well), which of the following is correct? void someFunction(int& first, float second, char third); void displayOutput(float total); //main function int myInt; float myFloat; char ch; CHOICES: someFunction(1, 2.0, ch); someFunction(myInt, 2.0, ch); cout << displayOutput(myFloat); someFunction(myInt, myFloat, ch); someFunction(myInt, 2.0, "c");

someFunction(myInt, myFloat, ch);

Write a declaration statement that will declare an array named student_name that can store 22 names.

string student_name[22];

string original = "House of Cards"; string sub; Given the statements above, write a statement that will assign "House" to sub with a string function. remember member function substr

sub = original.substr(0,5);

What is the final value of x after the following fragment of code executes? Explain your answer. int x=0; do { x++; }while(x > 0);

the final value of x will be infinite because the loop will go on forever, because x will always increment indefinitely

What is wrong with the following code fragment? (for loop) const int SIZE =5; double scores[SIZE]; for(int i=0; i<=SIZE;i++) { cout << "Enter a score\n"; cin >> scores[i]; }

the for loop would end up not compiling because 'i' would become equal to the largest score, which is illegal. Must be (n-1) to be valid

Which of the following is a valid identifier? 3-com 3com three_com 3_com

three_com

Are the following two functions properly overloaded? void pass_fail (double score, char& grade); void pass_fail (char& grade, double score); true or false

true

What is the value of the following expression? (true && (4/3 || 6%2)) choices: illegal syntax 0 true false

true

What value is displayed? Read very carefully. int x=0; while( x < 5) cout << "In the loop...." << endl; x ++; cout << x << endl; choices: 4 0 5 unable to determine/infinite loop

unable to determine/infinite loop

If you were to write a function for displaying the cost of an item to the screen, which function declaration would be most appropriate? void display(); float display(float myCost); void display (float& myCost); void display(float myCost);

void display(float myCost);

void function (int arr[], int size); rewrite the function declaration above so that the function body CANNOT make any changes to arr. Make sure names are the same as what is given in the statement above.

void function (const int arr[], int size);

Provide the function DEFINITION for a function named personal_info. The function will ask and get the user's last name and age.

void personal_info ( string& name, int& age) { cout << "Please enter your name: "; cin >> name; cout << "\n Please enter your age: "; cin >> age; }


संबंधित स्टडी सेट्स

Chapter 33: Abuse in the Family and Community

View Set

WEEK SEVEN ECONOMICS AND PERSONAL FINANCE (CONSUMER PURCHASING AND PROTECTION)

View Set

Q Test 2- sets that had Unit 7 Quiz questions on them

View Set

RAKTAS 3. Pasaulio pažinimas 3 klasei. Antroji knyga. 38-39 Gamtos ženklai, padedantys orientuotis

View Set

Psych 101 Chapter 4 and Chapter 7

View Set