Docsity

CSCI261 Lecture Questions: C++ Text File I/O and Classes - Prof. Keith E. Hellman, Exams of Computer Science

Toggle sidebar
bg1
CSCI261 Lecture Questions C++ Text File I/O
This worksheet is for your use during and after lecture. It will not be collected or graded, but I think you will find it a
useful tool as you learn C++ and study for the exams. Explain all false answers for the “True or False” questions; in
general, show enough work and provide enough explanation so that this sheet is a useful pre-exam review. I will be
happy to review your answers with you during office-hours, via Email, or instant messaging.
1. Which of the following are true of C++ classes?
A. A class is a variable that may be assigned values. False, classes are not variables.
B.
int
and
double
are examples of C++ classes. False, they are fundamental data types.
C.
string
is an example of a C++ class. True.
D. A C++ class is a special data type. True.
E. A C++ class is a special type of variable. False, classes are not variables.
2. Consider the snippet at the right and answer the following questions.
(a)
string
is to
double
as
firstName
is to ???
Solution:
GPA
or
course_grade
, variables of type double.
(b)
GPA
is to
double
as
firstName
is to ???
Solution:
string
(c) How many
double
variables are declared? What are the name(s)?
Solution: Two,
GPA
and
course_grade
.
(d) How many class instances are declared? What are the name(s)?
Solution: One,
firstName
.
(e) Explain the difference between symbols
double
and
course_grade
.
Solution:
double
is a fundamental data type,
course_grade
is a variable of type
double
.
course_grade
has a value and takes up memory to hold that value.
(f) Explain the difference between symbols
string
and
firstName
.
Solution:
string
is a C++ class,
firstName
is an instance
of the
string
class.
firstName
has a value (perhaps the
empty string
""
) and takes up memory to hold that value.
4
double GPA(3.8), course_grade ;
5
string firstName;
3. Is
cout
the
ostream
class or an instance of an
ostream
type?
Solution:
cout
is an instance of the
ostream
(short for output stream) class.
4. Is
cin
an
istream
class or an object (variable) of the
istream
class?
Solution:
cin
is an instance of the
istream
(short for input stream) class.
pf3
pf4
pf5

Partial preview of the text

Download CSCI261 Lecture Questions: C++ Text File I/O and Classes - Prof. Keith E. Hellman and more Exams Computer Science in PDF only on Docsity!

This worksheet is for your use during and after lecture. It will not be collected or graded, but I think you will find it a useful tool as you learn C++ and study for the exams. Explain all false answers for the “True or False” questions; in general, show enough work and provide enough explanation so that this sheet is a useful pre-exam review. I will be happy to review your answers with you during office-hours, via Email, or instant messaging.

  1. Which of the following are true of C++ classes? A. A class is a variable that may be assigned values. False, classes are not variables. B. int and double are examples of C++ classes. False, they are fundamental data types. C. string is an example of a C++ class. True. D. A C++ class is a special data type. True. E. A C++ class is a special type of variable. False, classes are not variables.
  2. Consider the snippet at the right and answer the following questions.

(a) string is to double as firstName is to ???

Solution: GPA or course_grade, variables of type double.

(b) GPA is to double as firstName is to ???

Solution: string

(c) How many double variables are declared? What are the name(s)?

Solution: Two, GPA and course_grade.

(d) How many class instances are declared? What are the name(s)?

Solution: One, firstName.

(e) Explain the difference between symbols double and course_grade.

Solution: double is a fundamental data type, course_grade is a variable of type double. course_grade has a value and takes up memory to hold that value.

(f) Explain the difference between symbols string and firstName.

Solution: string is a C++ class, firstName is an instance of the string class. firstName has a value (perhaps the empty string "") and takes up memory to hold that value.

4 double GPA (3.8) , course_grade ; 5 string firstName ;

  1. Is cout the ostream class or an instance of an ostream type?

Solution: cout is an instance of the ostream (short for output stream) class.

  1. Is cin an istream class or an object (variable) of the istream class?

Solution: cin is an instance of the istream (short for input stream) class.

  1. Why is this C++ statement nonsensical?

bool b( int > double);

Solution: int and double are data types. They don’t have any values associated with them, so comparing them with a relational operator doesn’t make sense.

  1. Suppose a user types the following sequence of characters at the keyboard (there are several whitespace characters, and the final character is the digit 4):

a 4.23 -2.500 -3 ,e -100 4

And the keyboard is read by a program running the code sequence to the right.

32 char a , b , c ; 33 double m , n , p ; 34 int x , y , z ; 35 36 cin >> a; 37 cin >> m; 38 cin >> x; 39 cin >> b; 40 cin >> n; 41 cin >> y; 42 cin >> c; 43 cin >> p; 44 cin >> z;

(a) Complete the table below: provide the value of all variables that are successfully read by cin before it encounters a failure state. Leave any variables that are not valued by cin blank. Note that the variable order in the table is identical to the order of input operations in the code snippet. Variable a m x b n y Value ’a’ 4.23 -2 ’.’ 500 -

Variable c p z Value ’,’

(b) In a sentence, explain why cin entered a failure state.

Solution: After c is read as a comma (’,’), a double read for p is attempted. p fails because doubles must start with a number (1e-100 is OK, but e-100 is not).

  1. Write a snippet of code that: (a) Opens a file named "the_file.txt" for writing.

Solution: ofstream outFile ( " the_file. txt " );

(b) Using the file stream object as a predicate, prints an error message to the console if part a failed.

Solution: if (! outFile ) { cout << " Error opening ’ the_file. txt ’" << endl ; }

  1. True or False: After cout.precision(x); is used, you are guaranteed x decimal points will be used for printing any float or double.

Solution: False, C++ won’t print more decimals than there is accuracy in the number.

  1. True or False:

cout.width(10); cout << 3 << 4 << endl; Prints numbers 3 and 4 with a field width of 10.

Solution: False, only the first term printed, 3, will be in a field width of 10.

  1. True or False: After cout.setf( ios::scientific ); is used, all double values will be printed in scientific notation.

Solution: False, C++ seems to have a mind of its own in this regard.

  1. True or False: After cout.setf( ios::right ) is used, output lines are right justified in the console window.

Solution: False, they are right justified in the field width used for printing.

  1. Write an application that reads a file consisting of three columns of integers and called DATA.DAT. The file does not contain column labels, and your application should meet these requirements:
    1. An error message is displayed if the file cannot be opened.
    2. When the value of the second column is zero, display the value read for the third column, and stop reading data from the file.
    3. The input file stream is closed before the program ends.
    4. If a 0 is not found in second column of data, the program ends without printing anything to the console.
    5. The program should not use any ifstream class member functions.

Solution:

include < fstream >

include < iostream >

include < string >

include < cstdlib >

using namespace std ; int main () { ifstream infile ( " DATA. DAT " ); if (! infile ) { cout << " Error opening file. " << endl ; exit (1); }

int i1 , i2 , i3 ; while ( infile >> i1 >> i2 >> i3 ) { if ( i2 == 0 ) { cout << i3 << endl ; break ; } }

infile. close ();

return 0; }