Instructor - C. BoyleFall Semester - 2015 [email protected].

29
Instructor - C. Boyle Instructor - C. Boyle Fall Semester - 2015 Fall Semester - 2015 [email protected] [email protected]

Transcript of Instructor - C. BoyleFall Semester - 2015 [email protected].

Page 1: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Instructor - C. BoyleInstructor - C. Boyle Fall Semester - 2015 Fall Semester - 2015 [email protected]@cs.odu.edu

Page 2: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

This was an in-class exercise, and something may have changed, SO

Do not count on the accuracy of the solutions in this presentation!

Verify With Your Textbook!

Look Up The Answers Yourself!

Page 3: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#1 Question

Which of the following methods for inserting comments in a C++ program are valid?

Answer

•   \\ comments in your program

•   \* comments in your program *\

•   /* comments in your program */

•   #comments in your program

Page 4: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#1-Solution

There are two methods for inserting comments in a C++ program: a comment can begin with the characters // or a comment can begin with the characters /* and then end with the characters */

Page 5: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#2 Question

Question • True or False: The setw manipulator cannot use a

string as an argument.

Answer• True• False

Page 6: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#3 Question

Consider the following preprocessor directive:  

#include <iostream.h>The directive specifies that the statements in the file:

ANSWER • contains a constant we use in executing loops. • contains information related to the input statement used in

the program • contains info related to functions used to compute the sum of

a value. • contains a comment we use in exiting the program.

Page 7: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#4 Question

Multiple Answer Question

The main function contains two types of statements:

Answer• statements that define memory locations which will be used

in the program •   pre-processor statements that define additional code to be

inserted.•   statements that specify actions to be taken. •   post-processor statements that define additional code to

executed.

Page 8: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#5 Question

Question True or False: The if…else statement is a one-way selection structure.

Answer

• True

• False

Page 9: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#6 Question

Multiple Answer Question

//define and initialize variables

double quizscore1, quizscore3, quiz4=10, averagescore=4.0, avg, sumscores;

This statement specifies that

Answer• the program will use five variables.• the variable quiz4 should be initialized to the value of 10.0.• the variable average should be initialized to the value of 10.0.• nothing, because it should all be on one line.

Page 10: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#7 Question

Question Rules for selecting a valid identifier are:

Answer

  • An identifier cannot begin with an alphabetic character or the

underscore character.•   Alphabetic characters in an identifier must be lower case

letters. •   An identifier can be of any length. •   Alphabetic characters in an identifier can be lower case or

upper case letters.

Page 11: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#8 Question

Question The following is a valid identifier:

1_quiz

Answer

• True

• False

Page 12: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#9 Question

Question The following is a valid identifier:

max-value

Answer

• True

• False

Page 13: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#10 Question

Question The following is a valid identifier:

$uperMan

Answer

• True

• False

Page 14: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#11 Question

Question True or False: File stream variables are predefined in the fstream header file and associated with input/output sources. 

Answer

• True

• False

Page 15: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#12 Question

Question C++ is case sensitive, that is, it distinguishes upper case letters from lowercase letters.  Thus Sum, sum, and SUM represent three ways of safely working with the same variable value.

Answer

• True

• False

Page 16: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#13 Question

Question

The difference between the float, double, and long double types relates to the

Answer•   exponent (or accuracy) and length of the values represented. •   precision (or accuracy) and range of the values represented. •   exponent (short accuracy), range (long accuracy) and length

of the values represented. • precision (or accuracy) and range of the libraries included.

Page 17: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#14 Question

Question A float value will have a wider range of values for exponents than a double value.

Answer

• True

• False

Page 18: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#15 Question

Multiple Answer Question Select the true statements, concerning symbolic constants:

Answer•   The compiler will replace each occurrence of the constant

identifier with the constant value. • Constant declarations, like variable declarations, and with a

colon. (:)

•   Several symbolic constants can be declared in one statement if they have the same data type.

•   A symbolic constants is declared by prefixing a declaration with the const specifier.

Page 19: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#16 Question

Question

The assignment statements below define and initialize the variables at the same time.

double sum, total;

total = 10;

sum = total;

Answer• True• False

Page 20: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#17 Question

Question • Multiple assignments are allowed in C++, as in

the following statement, which assigns a value of zero to each of the variables x,y, and z.

 x = y = z = 0;

Answer

• True

• False

Page 21: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#18 Question

Question What is an enumeration type?

Answer•   C++’s method for allowing programmers to create

their own simple data types. •   C++’s method for allowing programmers to use

numerals to update control structures.•   One of C++’s simple integer data types, such as int.•   C++’s library of available numerical variables.

Page 22: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#19 Question

Question An expression that has operands of different data types is called a(n)

Answer

•   mixed expression

•   homogenous expression

•   simple

•   enumerated expression

Page 23: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#20 Question

Question What is the syntax for declaring single or multiple variables?

Answer•   dataType identifier, identifier, …; •   variableName dataType identifier, identifier, …; •   dataName identifierType, identifierType, …; •   identifier, identifier, …;

Page 24: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#21 Question

Question True or False: The semantic rules of a language tell you what is legal and what is not legal.

Answer

• True

• False

Page 25: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#22 Question

Question What is the syntax of the ignore function?

Answer

•   cin.ignore(intExp, chExp);

•   cout<< stringName.ignore(intExp, intExp);

•   cin>> stringName.ignore(intExp, intExp);

•   int value.stringName.ignore(intExp, intExp);

Page 26: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#23 Question

Question The header file ______________ needs to be included to use the setw function.

Answer

•   iomanip

•   iostream

•   iosetw

•   cmath

Page 27: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#24 Question

Question Which of the following would you use as a statement to format the output of decimal numbers to two decimal places?

Answer

•   cout << setprecision(2);

•   cin << setprecision(2);

•   cout << setdotwidth( );

•   cout >> setw.setprecision(2);

Page 28: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

Q#25 Question

Question Which of the following are true?

Answer

•   The if…else statement is a two-way selection structure.

•   A unary operator takes three operands.

•   The equality operator in C++ is =

•   A relational operator allows you to make comparisons in a program.

Page 29: Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu.

QUESTIONS?QUESTIONS?

Assignments:Assignments:

Quiz due before class on TuesdayQuiz due before class on Tuesday