Programming Fundamentals I Chapter 3

Ace your homework & exams now with Quizwiz!

If input failure occurs in a C++ program, the program terminates immediately and displays an error message. a. True b. False

ANSWER: False FEEDBACK: Correct If input failure occurs in a C++ program, the program does not terminate immediately displaying an error message, instead program quietly continues to execute with whatever values are stored in the variables and produces incorrect results.

In the statement cin >> x;, x can be a variable or an expression. a. True b. False

ANSWER: False FEEDBACK: Correct In the given C++ statement, the extraction operator >> is binary. Left-side to extraction operator is the input stream variable cin and right-side to extraction operator is variable x. The purpose of cin is to read and store values in variables, where variables refer to memory locations.

It is a good idea to redefine cin and cout in your programs. a. True b. False

ANSWER: False FEEDBACK: Correct Redefining cin and cout in the programs is not a good idea as its already defined and have specific meanings in C++.

The following statements will result in input failure if the input values are not on a separate line. (Assume that x and y are int variables.) cin >> x; cin >> y; a. True b. False

ANSWER: False FEEDBACK: Correct The input values can be in the same line separated by whitespaces or in separate lines.

In an output statement, each occurrence of endl advances the cursor to the end of the current line on an output device. a. True b. False

ANSWER: False FEEDBACK: Correct The manipulator endl is used to move the insertion point to the beginning of the next line.

You can use the function getline to read a string containing blanks. a. True b. False

ANSWER: True FEEDBACK: Correct Function getLine reads all characters in the line including any blanks, if any, until it reaches end of the current line.

The number of input data extracted by cin and >> depends on the number of variables appearing in the cin statement. a. True b. False

ANSWER: True FEEDBACK: Correct The number of input data extracted by cin and >> is based on the number of variables in the statement. If more number of inputs are given, then it just skips the additional input and take only required number of inputs.

When reading data into a char variable, after skipping any leading whitespace characters, the extraction operator >> finds and stores only the next character; reading stops after reading a single character. a. True b. False

ANSWER: True FEEDBACK: Correct To input data into a char variable, after skipping any leading whitespaces, the extraction operator >> extracts the next character from the input stream and stores it in the char variable appearing in the input statement.

Entering a char value into an int variable causes serious errors, called input failure. a. True b. False

ANSWER: True FEEDBACK: Correct While giving the inputs one should be more cautious because computers does not tolerate with the mismatch of variables.

Suppose that x is an int variable, ch is a char variable, and the input is: ​ 276. ​ Choose the values after the following statement executes: ​ cin >> ch >> x; ​ a. ch = '2', x = 76 b. ch = '276', x = '.' c. ch = ' ', x = 276 d. ch = 'b', x = 76 ANSWER: a FEEDBACK: a. Correct. In the cin statement cin >> ch >> x;, the first variable after the first >> is ch and the second variable is x. Also ch is a char variable and x is an int variable. Therefore, the input statement first reads 2 and stores it as '2' into ch and the reading stops just after 2. Next since x is an int variable, the input statement will read 76 and store it into x.

ANSWER: a FEEDBACK: a. Correct. In the cin statement cin >> ch >> x;, the first variable after the first >> is ch and the second variable is x. Also ch is a char variable and x is an int variable. Therefore, the input statement first reads 2 and stores it as '2' into ch and the reading stops just after 2. Next since x is an int variable, the input statement will read 76 and store it into x.

Manipulators without parameters are part of the ____ header file. a. iostream b. iomanip c. ifstream d. pmanip

ANSWER: a FEEDBACK: a. Correct. Manipulators without parameters are part of the iostream header file. b. Incorrect. Manipulators without parameters are part of the iostream header file. To use a parameterized stream manipulator in a program, you must include the header file iomanip. c. Incorrect. Manipulators without parameters are part of the iostream header file. Ifstream is a data type in fstream header file. d. Incorrect. Manipulators without parameters are part of the iostream header file. There is no header file called pmanip in C++ programming.

Suppose that x is an int variable, y is a double variable, and ch is a char variable. The input is: ​ 15A 73.2 ​ Choose the values after the following statement executes: ​ cin >> x >> ch >> y; ​ a. x = 15, ch = 'A', y = 73.2 b. x = 15, ch = 'A', y = 73.0 c. x = 15, ch = 'a', y = 73.0 d. This statement results in an error because there is no space between 15 and A.

ANSWER: a FEEDBACK: a. Correct. The extraction operators first reads and stores 15 into x and reading stops after 5. Next the extraction operator reads 'A' and stores into ch and reading stops after 'A'. Then the extraction operator reads and stores 73.2 into y. In the given input, the first integer value 15 will be stored into the variable x, the next character value 'A' will be stored into the variable ch, and the last decimal value 73.2 will be stored into the variable y. b. Incorrect. Since the last variable y is declared as double, the last decimal value 73.2 will be stored into the variable y. c. Incorrect. Since 'A' is entered uppercase, 'A' will be stored into ch. Also since y is a double variable, 73.2 will be stored into y. The char data type will print the given character as it is, so it is not possible to get 'a' instead of 'A' and since the last variable y is declared as decimal, the last decimal value 73.2 will be stored into the variable y. d. Incorrect. The extraction operators first reads and stores 15 into x and reading stops after 5. Next the extraction operator reads 'A' and stores into ch and reading stops after 'A'. Then the extraction operator reads and stores 73.2 into y. Therefore, not having a space between 15 and 'A' will not result in an error since ch is a char variable.

Suppose that ch1 and ch2 are char variables, and alpha is an int variable. The input is: ​ A 18 ​ What are the values after the following statement executes? ​ cin.get(ch1); cin.get(ch2); cin >> alpha; ​ a. ch1 = 'A', ch2 = ' ', alpha = 18 b. ch1 = 'A', ch2 = '1', alpha = 8 c. ch1 = 'A', ch2 = ' ', alpha = 1 d. ch1 = 'A', ch2 = '\n', alpha = 1

ANSWER: a FEEDBACK: a. Correct. The function get does not skip leading whitespaces and reads the next character, including a whitespace, if any. The first statement cin.get(ch1), reads and stores 'A' into ch1, the reading stops just after A. The second statement, cin.get(ch2), reads the space after A and stores it into ch2. The third statement, cin >> alpha;, reads and stores 18 into alpha. Therefore, after the given statements execute, ch1 = 'A', ch2 = ' ', and alpha = 18.

Suppose that x = 55.68, y = 476.859, and z = 23.8216. What is the output of the following statements? cout << fixed << showpoint;cout << setprecision(3);cout << x << ' ' << y << ' ' << setprecision(2) << z << endl; a. 55.680 476.859 23.82 b. 55.690 476.860 23.82 c. 55.680 476.860 23.82 d. 55.680 476.859 23.821

ANSWER: a FEEDBACK: a. Correct. The manipulator fixed is used to output numbers in a fixed decimal format, the manipulator showpoint is used to output number with the decimal point and trailing zeros, if any, and the manipulator setprecision is used to output numbers to a fixed number of decimal places. The statement, cout << setprecision(3);, sets the output of numbers to three decimal places. The third statement after outputting the values of x and y with three decimal place, uses setprecision to set the output of numbers to two decimal places. So the value of z is output with two decimal places. Since x = 55.68 has only two decimal places and the precision is three decimal places, x is printed as 55.680.

What is the output of the following statements? cout << "123456789012345678901234567890" << endlcout << setfill('#') << setw(10) << "Mickey"<< setfill(' ') << setw(10) << "Donald" << setfill('*') << setw(10) << "Goofy" << endl; a. 123456789012345678901234567890 ####Mickey Donald*****Goofy b. 123456789012345678901234567890 ####Mickey####Donald*****Goofy c. 123456789012345678901234567890 ####Mickey####Donald#####Goofy d. 23456789012345678901234567890 ****Mickey####Donald#####Goofy

ANSWER: a FEEDBACK: a. Correct. The manipulator setw is used to output the value of an expression in a specific number of columns. If the number of columns specified by setw exceeds the number of columns required by the expression, the output of the expression is rightjustified and the unused columns to the left are filled with spaces. The manipulator setfill is used to fill the unused columns with a character other than a space. The expression setfill('#') fills the unused columns with the character #. The expression setfill(' ') fills the unused columns with the space character. The expression setfill('*') fills the unused columns with the character *. "Mickey" is output in 10 columns and it requires only six columns, the left four columns are filled with #. "Donald" is output in 10 columns and it requires only six columns, the left four columns are filled with the space character. "Goofy" is output in 10 columns and it requires only five columns, the left five columns are filled with *.

Suppose that x and y are int variables, and z is a double variable. The input is: ​ 28 32.6 12 ​ Choose the values of x, y, and z after the following statement executes: ​ cin >> x >> y >> z; ​ a. x = 28, y = 32, z = 0.6 b. x = 28, y = 32, z = 12.0 c. x = 28, y = 12, z = 32.6 d. x = 28, y = 12, z = 0.6

ANSWER: a FEEDBACK: a. Correct. The statement cin >> x >> y >> z; first reads and stores 28 into x, then reads and stores 32 into y, since y is an int variable. After reading and storing 32 into y, the reading stops after 32, just before the dot. Next the value .6, which is a decimal value, will be read and stored into z since z is a double variable. After reading .6 into z, the reading stops after 6. The last input value 12 will remain into the input stream for further input, if any.

Suppose that ch1 and ch2 are char variables and the input is: ​ WXYZ ​ What is the value of ch2 after the following statements execute? ​ cin.get(ch1); cin.putback(ch1); cin >> ch2; ​ a. W b. X c. Y d. Z

ANSWER: a FEEDBACK: a. Correct. The stream function putback lets you put the last character extracted from the input stream by the get function back into the input stream. The first statement, cin.get(ch1);, reads W and stores it into ch1. Next, the statement, cin.putback(ch1);, puts W back into the input stream. So after the execution of this statement, the reading marker is before W. Therefore, the next statement, cin >> ch2;, reads W and stores it into ch2. Therefore, after the last statement executes, ch2 = 'W'.

Consider the following program segment. ifstream inFile; //Line 1int x, y; //Line 2... //Line 3 inFile >> x >> y; //Line 4 Which of the following statements at Line 3 can be used to open the file progdata.dat and input data from this file into x and y at Line 4? a. inFile.open("progdata.dat"); b. inFile(open,"progdata.dat"); c. open.inFile("progdata.dat"); d. open(inFile,"progdata.dat");

ANSWER: a FEEDBACK: a. Correct. inFile.open("progdata.dat") is correct syntax to open the file. b. Incorrect. The syntax to open a file is fileStreamVariable.open(sourceName);. Therefore, the correct statement to open the file progdata.dat and input data from this file into x and y is: inFile.open("progdata.dat"); c. Incorrect. The syntax to open a file is fileStreamVariable.open(sourceName);. Therefore, the correct statement to open the file progdata.dat and input data from this file into x and y is: inFile.open("progdata.dat"); d. Incorrect. The syntax to open a file is fileStreamVariable.open(sourceName);. Therefore, the correct statement to open the file progdata.dat and input data from this file into x and y is: inFile.open("progdata.dat");

Suppose that x and y are int variables. Which of the following is a valid input statement? a. cin >> x >> cin >> y; b. cin >> x >> y; c. cin << x << y; d. cout << x << y;

ANSWER: b FEEDBACK: a. Incorrect. The statement cin >> x >> cin >> y; will throw syntax error since the cin keyword has been used twice in the statement. b. Correct. The statement cin >> x >> y; is used to read values for the variables x and y. c. Incorrect. Insertion operator << is used to output the data. d. Correct. The statement cout << x << y; is used to print the values stored in the variables x and y.

Suppose that outFile is an ofstream variable and output is to be stored in the file outputData.out. Which of the following statements opens the file outputData.out and associates outFile to the output file? a. outFile("outputData.out"); b. outFile.open("outputData.out"); c. open(outFile,"outputData.out"); d. open.outFile("outputData.out");

ANSWER: b FEEDBACK: a. Incorrect. The syntax to open a file is fileStreamVariable.open(sourceName);. Therefore, the correct statement to open the file outputData.out and associate outFile to it is: outFile.open("outputData.out"); b. Correct. outFile.open("outputData.out") opens the file to store the output data. c. Incorrect. The syntax to open a file is fileStreamVariable.open(sourceName);. Therefore, the correct statement to open the file outputData.out and associate outFile to it is: outFile.open("outputData.out"); d. Incorrect. The syntax to open a file is fileStreamVariable.open(sourceName);. Therefore, the correct statement to open the file outputData.out and associate outFile to it is: outFile.open("outputData.out");

In C++, the dot is an operator called the ____ operator. a. dot access b. member access c. data access d. member

ANSWER: b FEEDBACK: a. Incorrect. There's no operator called dot access in C++. b. Correct. In C++, dot is an operator, called member access operator. c. Incorrect. There's no operator called data access in C++. d. Incorrect. There's no operator called member in C++.

Suppose that ch1, ch2, and ch3 are variables of the type char. The input is: ​ A B C ​ What is the value of ch3 after the following statements execute? ​ cin.get(ch1); cin.get(ch2); cin.get(ch3); ​ a. 'A' b. 'B' c. 'C' d. '\n'

ANSWER: b FEEDBACK: b. Correct. The function get does not skip leading whitespaces and reads the next character, including a whitespace, if any. The first statement cin.get(ch1), reads and stores 'A' into ch1, the reading stops just after A. The second statement, cin.get(ch2), reads the space after A and stores it into ch2. The third statement, cin.get(ch3), reads and stores 'A' into ch3. Therefore, after the given statements execute ch3 = 'B'.

What is the output of the following statements? cout << setfill('*');cout << "12345678901234567890" << endl cout << setw(5) << "18" << setw(7) << "Happy"<< setw(8) << "Sleepy" << endl; a. 12345678901234567890 ***18 Happy Sleepy b. 12345678901234567890 ***18**Happy**Sleepy c. 12345678901234567890 ***18**Happy Sleepy d. 12345678901234567890 ***18**Happy Sleepy**

ANSWER: b FEEDBACK: b. Correct. The manipulator setw is used to output the value of an expression in a specific number of columns. If the number of columns specified by setw exceeds the number of columns required by the expression, the output of the expression is rightjustified and the unused columns to the left are filled with spaces. The manipulator setfill is used to fill the unused columns with a character other than a space. The statement, cout << setfill('*');, fills the unused columns with the character *. The value of 18 is output in five columns. Because it only requires two columns, the three columns to the left of 18 are filled with *. The value of "Happy" is output in 7 columns and it only requires five columns, the first two columns are filled with *. The value of "Sleepy" is output in eight columns and requires only six columns, so the left two columns are filled with *.

Suppose that ch1 and ch2 are char variables and the input is: ​ WXYZ ​ What is the value of ch2 after the following statements execute? ​ cin >> ch1; ch2 = cin.peek(); cin >> ch2; ​ a. W b. X c. Y d. Z

ANSWER: b FEEDBACK: b. Correct. The peek function returns the next character from the input stream but does not remove the character from that stream. The firs statement, cin >> ch1;, reads and stores the character W into ch1 and the reading marker is after just before X. The next statement, ch2 = cin.peek();, reads and stores the next character X into ch2, but does not remove X from the input stream. Therefore, after the second statement executes, the reading marker is still before X. Next, the third statement, cin >> ch2;, reads and stores the next (non whitespace) character X into ch2. After the execution of the given statements, ch2 = 'X'.

Suppose that ch1, ch2, and ch3 are variables of the type char. The input is: ​ A B C ​ Choose the value of ch3 after the following statement executes: ​ cin >> ch1 >> ch2 >> ch3; ​ a. 'A' b. 'B' c. 'C' d. '\n'

ANSWER: c FEEDBACK: a. Incorrect. The first value, A, in the first line will be stored into the variable ch1, the second value, B, in the first line will be stored into the variable ch2, and the third value, C, in the second line will be stored into the variable ch3, so it is not possible to get 'A' in ch3. b. Incorrect. The first value, A, in the first line will be stored into the variable ch1, the second value, B, in the first line will be stored into the variable ch2, and the third value, C, in the second line will be stored into the variable ch3, so it is not possible to get 'B' in ch3. c. Correct. The first value, A, in the first line will be stored into the variable ch1, the second value , B, in the first line will be stored into the variable ch2, and the third value, C, in the second line will be stored into the variable ch3. d. Incorrect. The first value, A, in the first line will be stored into the variable ch1, the second value, B, in the first line will be stored into the variable ch2 and the third value, C, in the second line will be stored into the variable ch3, so it is not possible to get '\n' in ch3.

Suppose that x = 25.67, y = 356.876, and z = 7623.9674. What is the output of the following statements? ​ cout << fixed << showpoint; cout << setprecision(2); cout << x << ' ' << y << ' ' << z << endl; ​ a. 25.67 356.87 7623.96 b. 25.67 356.87 7623.97 c. 25.67 356.88 7623.97 d. 25.67 356.876 7623.967

ANSWER: c FEEDBACK: a. Incorrect. The setprecision manipulator will throw two decimal digits of the y value from 356.876 to 356.88 and z value from 7623.9674 to 7623.97; so it is not possible to get 356.87 for y and 7623.96for z. b. Incorrect. The setprecision manipulator will throw two decimal digits of the y value from 356.876 to 356.88; so it is not possible to get 356.87 for y. c. Correct. The manipulator setprecision to control the output of floating-point numbers. When executed the output values of x, y, and z are 25.67, 356.88, and 7623.97, respectively. d. Incorrect. The setprecision manipulator will throw two decimal digits of the y value from 356.876 to 356.88 and z value from 7623.9674 to 7623.97; so it is not possible to get 356.876 for y and 7623.967 for z.

Suppose that alpha is an int variable and ch is a char variable. The input is: ​ 17 A ​ What are the values after the following statements execute? ​ cin >> alpha; cin >> ch; ​ a. alpha = 17, ch = ' ' b. alpha = 1, ch = 7 c. alpha = 17, ch = 'A' d. alpha = 17, ch = 'a'

ANSWER: c FEEDBACK: a. Incorrect. The space between 17 and A is a whitespace. The first cin statement will read 17 and stores it into the variable alpha and after skipping all leading whitespaces the second cin statement will read A and stores it into the variable Ch. b. Incorrect. The first cin statement will read 17 and store it into the variable alpha and the second cin statement will read A and store it into the variable ch, so it is not possible to get 1 in alpha and 7 in ch. c. Correct. The first cin statement will read 17 and store it into the variable alpha and the second cin statement will read A and store it into the variable ch. d. Incorrect. In the input, A is uppercase. The first cin statement will read 17 and store it into the variable alpha and the second cin statement will read A and store it into the variable ch.

Suppose that x is an int variable, y is a double variable, and z is an int variable. The input is: ​ 15 76.3 14 ​ Choose the values after the following statement executes: ​ cin >> x >> y >> z; ​ a. x = 15, y = 76, z = 14 b. x = 15, y = 76, z = 0 c. x = 15, y = 76.3, z = 14 d. x = 15.0, y = 76.3, z = 14.0

ANSWER: c FEEDBACK: a. Incorrect. The value of y will be 76.3 since y is declared as double. b. Incorrect. The value of y will be 76.3 since the y is declared as double and the value of z will be 14 since z is declared as int. c. Correct. The first value 15 will be stored into the variable x, the second value 76.3 will be stored into the variable y, and the last value 14 will be stored into the variable z. d. Incorrect. Since x and z are int variables, x is 15 and z is 14.

Suppose that x is an int variable and y is a double variable. The input is: ​ 10 20.7 ​ Choose the values after the following statement executes: cin >> x >> y;. a. x = 10, y = 20 b. x = 10, y = 20.0 c. x = 10, y = 20.7 d. x = 10, y = 21.0

ANSWER: c FEEDBACK: c. Correct. Since x is an int variable and y is a double variable, after reading 10 into x, the extraction operator reads and stores 20.7 into y. Thus, after the statement cin >> x >> y; executes x = 10 and y = 20.7.

Suppose that x = 1565.683, y = 85.78, and z = 123.982. What is the output of the following statements? cout << fixed << showpoint;cout << setprecision(3) << x << ' '; cout << setprecision(4) << y << ' ' << setprecision(2) << z << endl; a. 1565.683 85.8000 123.98 b. 1565.680 85.8000 123.98 c. 1565.683 85.7800 123.98 d. 1565.683 85.780 123.980

ANSWER: c FEEDBACK: c. Correct. The manipulator fixed is used to output numbers in a fixed decimal format, the manipulator showpoint is used to output number with the decimal point and trailing zeros, if any, and the manipulator setprecision is used to output numbers with a fixed number of decimal places. The value of x is output with three decimal places, the value of y is output with four decimal places, and the value of z is output with two decimal places. Because y = 85.78 has only two decimal places, it is output as 85.7800. Also, because z = 123.982 has three decimal places, the output value of z, which is 123.98, is rounded to two decimal places.

Suppose that alpha, beta, and gamma are int variables and the input is: ​ 100 110 120 200 210 220 300 310 320 ​ What is the value of gamma after the following statements execute? ​ cin >> alpha; cin.ignore(100, '\n'); cin >> beta; cin.ignore(100,'\n'); cin >> gamma; ​ a. 100 b. 200 c. 300 d. 320

ANSWER: c FEEDBACK: c. Correct. The statement cin.ignore(100, '\n'); ignores either the next 100 characters or all characters until the newline character is found, whichever comes first. The first statement, cin >> alpha;, reads and stores 100 into alpha, and the second statement, cin.ignore(100, "\n");, skips the remaining input in the first line. Next, the third statement, cin >> beta;, reads and stores 200 into beta, and the fourth statements, cin.ignore(100, "\n");, skips the remaining input in the second line. Then the last statement, cin >> gamma;, reads and stores 300 into gamma. Therefore, after the given statements execute, gamma = 300.

C++ has a special name for the data types istream and ostream. They are called ____________________.

ANSWER: classes

When you want to process only partial data, you can use the stream function ____ to discard a portion of the input. a. clear b. skip c. delete d. ignore

ANSWER: d FEEDBACK: a. Incorrect. The stream function, clear, restores input stream to a working state. The stream function, ignore, is used to process partial data and to discard portion of input. b. Incorrect. There's no built-in stream function skip. The stream function, ignore, is used to process partial data and to discard portion of input. c. Incorrect. There's no built-in stream function delete. The stream function, ignore,is used to process partial data and to discard portion of input. d. Correct. The stream function ignore is used to discard portion of the input within a line. In other words, stream function ignore processes only partial data within a line.

____ is a parameterized stream manipulator. a. endl b. fixed c. scientific d. setfill

ANSWER: d FEEDBACK: a. Incorrect. endl is a manipulator without parameter that's used to insert new line. b. Incorrect. fixed is a manipulator without parameter used to output floating-point numbers in a fixed decimal format. c. Incorrect. scientific is a manipulator without parameter used to output floating-point numbers in a scientific format. d. Correct. Manipulators with parameters are called parameterized stream manipulator. Example setprecision, setw, and setfill.

Suppose that x and y are int variables, ch is a char variable, and the input is: ​ 4 2 A 12 ​ Choose the values of x, y, and ch after the following statement executes: ​ cin >> x >> ch >> y; ​ a. x = 4, ch = 2, y = 12 b. x = 4, ch = A, y = 12 c. x = 4, ch = ' ', y = 2 d. This statement results in input failure

ANSWER: d FEEDBACK: d. Correct. In the cin statement cin >> x >> ch >> y;, the first variable after the first >> is x, the second variable is ch, and the third variable is y. This input statement first read 4 and stores it into x, then reads 2 and stores it as '2' into ch. The next value in the input stream is A, which is not an integer. Therefore, trying to read and store A into y will result into input failure since y is an int variable. This statement results into input failure.

C++ provides a header file called ____________________, which is used for file I/O.

ANSWER: fstream

The function ____________________ returns the next character in the input stream; it does not remove the character from the input stream

ANSWER: peek

C++ comes with a wealth of functions, called ____________________ functions, that are written by other programmers.

ANSWER: predefined

The extraction operator >> skips only all leading blanks when searching for the next data in the input stream. a. True b. False

FEEDBACK: Correct The extraction operator >> not only skips leading blanks but also skips all leading whitespaces. Whitespaces consist of blanks and certain non-printable characters such as tabs and new line characters.

In C++, the dot is an operator called the ____________________operator

POINTS: 1


Related study sets

Medical-Surgical Nursing Chapter 19 Intraoperative Care

View Set

Master National Questions: part 2

View Set