Docsity

C++ Exam 5: Questions and Answers on Loops, Files, and String Manipulation, Exams of Advanced Education

Toggle sidebar
bg1
C++ EXAM 5 QUESTION AND ANSWERS
What is an increment operator? What does it do? - ++ , it adds one to a variable
What does Val++ mean? - val = val+ 1
What is a decrement operator? What does it do? - -- it subtracts one from the variable. It must
have a location in memory
What does an increment or a decrement operator do in prefix mode? - It increments or
decrements and then returns the value to the variable
What does an increment or a decrement operator do in the postfix mode? - It returns the value
of the variable and then increments or decrements.
What is a loop? - A control structure that causes a statement or statements to repeat
General format of a while loop - while (expression)
statement;
Pretest loop - The expression is evaluated before the loop executes.
What is the main problems with an infinite loop? - It drains the resources because it makes it
too slow.
What is a counter, and what is it used for? What must you do before it is in the loop? - A
variable that is incremented or decremented each time a loop repeats. Used for controlling.
Must be initialized first
pf3
pf4
pf5

Partial preview of the text

Download C++ Exam 5: Questions and Answers on Loops, Files, and String Manipulation and more Exams Advanced Education in PDF only on Docsity!

C++ EXAM 5 QUESTION AND ANSWERS

What is an increment operator? What does it do? - ++ , it adds one to a variable What does Val++ mean? - val = val+ 1 What is a decrement operator? What does it do? - -- it subtracts one from the variable. It must have a location in memory What does an increment or a decrement operator do in prefix mode? - It increments or decrements and then returns the value to the variable What does an increment or a decrement operator do in the postfix mode? - It returns the value of the variable and then increments or decrements. What is a loop? - A control structure that causes a statement or statements to repeat General format of a while loop - while (expression) statement; Pretest loop - The expression is evaluated before the loop executes. What is the main problems with an infinite loop? - It drains the resources because it makes it too slow. What is a counter, and what is it used for? What must you do before it is in the loop? - A variable that is incremented or decremented each time a loop repeats. Used for controlling. Must be initialized first

What kind of loop is a do while loop? What is required after the expression. - A posttest loop. A semicolon is required. Why are do-while loops executed at least once? - They are a post test loop. It executes and then tests that statement. How long will a do while loop execute? What are they useful for? - Until the expression becomes false. They are useful for menu driven programs to brin user back to menu to make another choice. What are for loops useful for? - for counter controlled loops general format of for loop. - for (initialization; test; update) statement; Is there a semicolon after the update expression? Can you have more then one initialization? - no yes, seperate them with commas, but you can only update one and can have only two semicolons. What will happen if you have a for loop like this for (;;) - Infinite loop, never stops. When do you use a for loop? - In any situation that clearly requires an initialization, a false condition to stop the loop, an update to occur at the end of each iteration. Why won't for (count=11; count <=10; count++) ever initate? - It's a pretest loop and the count is more then 10 already.

What are the steps to open files? - Open file or initialize. use the file (read from, write, or both) close the file (different for different programs. What header file do you need to close and open files? - What is ifstream infile? - it is for input from a file like reading, What is ofstream outfile? - It is for writing to a file What is fstream? - For both writing and reading from a file How do you open a file? - Create a link between file name outside of the program and the file stream object inside of the program. such as infile.open("inventory.dat"); What must a filename include? - drive, path info How do you send info to a file? - can use output file obect and << to send information. such as outfile << "Inventory report:"; how do you close a file? - use the close member function, such as infile.close(); Why shouldn't you wait for operating systems to close files at the end of a program? - May be limit on the number of open files. May be buffered output data waiting to send to file.

How are string literals stored in older C++ - Stored in memory as nul terminated c strings. String objects however, are not. Older ones require you pass the name of the file as a null terminated string, which is known as C-string. ex inputFile.open(filename.c_str()); Example of c_str member function - stringObject.c_str() How do you break a loop? - Can use break to terminate the loop. Use sparingly. How does break work in an inner loop? - terminates the loop only and goes back to the outer loop. Continue statement - Can use to go to the end of a loop to pepare for the next repetition. Use sparingly. What would you use to break two loops at once? - goto