Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1 1. Which of the following variable declarations are correct? If a variabl

ID: 667787 • Letter: P

Question

Part 1

1. Which of the following variable declarations are correct? If a variable declaration is not correct, give the reason(s) and provide the correct variable declaration.

a.double conversion = 2.5; //Line 1
b.char grade = 'B+'; //Line 2
c.double 28.5 = num //Line 3
d.string message = ''First C++ course'; //Line 4
e.int age = 18 years //Line 5
f.int perfectSquare; //Line 6
g. float x, y, decimal; //Line 7

2. What actions must be taken before a variable can be used in a program?

3. Which of the following are valid C++ identifiers?

new-Assignment

a._nextQuiz

b. 3rdProject

c.$twoHundred

d. CPP_Project

e.OneInchIs2.2Centimeters

f.Weekly Quiz

g.Jack'sHomework

h.first#

i.overPayment

4. Which of the following is a reserved word in C++?

a.main

b.#include

c.double

d.Const

e.cin

f.bool

5. What is the difference between a reserved word and a user-defined identifier?

6. Are the identifiers quizNo1 and quizno1 the same?

7. Give meaningful identifiers for the following variables and write a proper C++ declaration and initialization to a reasonable value.

a.A variable to store the first name of a student.

b.A variable to store the discounted price of an item.

c.A variable to store the number of juice bottles.

d.A variable to store the number of miles traveled.

e.A variable to store the highest test score.

8. Given:

int num1, num2, newNum;
double x, y;

Which of the following assignments are valid? If an assignment is not valid, state the reason.

a.num1 = 35;

b.newNum = num1 – num2;

c.num1 = 5; num2 = 2 + num1; num1 = num2 / 3;

d.num1 * num2 = newNum;

e.x = 12 * num1 - 15.3;

f.num1 * 2 = newNum + num2;

g.x / y = x * y;

h.num2 = num1 % 2.0;

i.newNum = static_cast<int> (x) % 5;

j.x = x + y - 5;

k.newNum = num1 + static_cast<int> (4.6 / 2);

9. Suppose that x, y, z, w, and t are int variables. What is stored in x, y, z, w, and t after the following statements execute?

a.x = 5;
b.y = x + 2;
c.z = x % (y - 2) + 4;
d.w = (x * y) / z - 5;
e.t = z + (x + y + 2) % w;

10. Which of the following are valid C++ assignment statements? Assume that i is an int variable, and x and percent are double variables.

i - 5 = x;

i = i++;

x = x * percent / 100;

percent = 0.05%;

Part 2.

1. Which header file must be included to use the function pow?

2. Which header file must be included to use the function sqrt?

3. Which header file must be included to use the function setprecision?

4. Which header file must be included to use the manipulators fixed and showpoint?

5. Which header file must be included to use the function setw?

6. To use the functions peek and putback in a program, which header file(s) must be included in the program?

7. Write a C++ statement that uses the manipulator setfill to output a line containing 35 stars, as in the following line:

***********************************

8. What may cause an input stream to enter the fail state? What happens when an input stream enters the fail state?

9. Which header file needs to be included in a program that uses the data types ifstream and ofstream?

10. Suppose that infile is an ifstream variable and employee.dat is a file that contains employees’ information. Write the C++ statement that opens this file using the variable infile.

11. A program reads data from a file called inputFile.dat and, after doing some calculations, writes the results to a file called outFile.dat. Answer the following questions:

a.After the program executes, what are the contents of the file inputFile.dat?

b.After the program executes, what are the contents of the file outFile.dat if this file was empty before the program executed?

c.After the program executes, what are the contents of the file outFile.dat if this file contained 100 numbers before the program executed?

d.What would happen if the file outFile.dat did not exist before the program executed?

12. Suppose that infile is an ifstream variable and it is associated with the file that contains the following data: 27306 savings 7503.35. Write the C++ statement(s) that reads and stores the first input in the int variable acctNumber, the second input in the string variable accountType, and the third input in the double variable balance.

13. Suppose that you have the following statements:

ofstream outfile;
double distance = 375;
double speed = 58;
double travelTime;

Write C++ statements to do the following:

Open the file travel.dat using the variable outfile.

a.Write the statement to format your output to two decimal places in fixed form.

b.Write the values of the variables day, distance, and speed in the file travel.dat.

c.Calculate and write the travelTime in the file travel.dat.

d.Which header files are needed to process the information in (a) to (d)?

Explanation / Answer

Ans 1 :
   a.double conversion = 2.5; //Line 1 - Correct
   b.char grade = 'B+'; //Line 2 - Incorrect
       Reason : Character type hold only single value
       Correct : char grade = "B";
   c.double 28.5 = num //Line 3 - Incorrect
       Reason : Identifier cannot be a number
       Correct: double num = 28.5 ;
   d.string message = ''First C++ course'; //Line 4 - Coorect
   e.int age = 18 years //Line 5 - Incorrect
       Reason : Integer type cannot contain string.
       Correct : int age = 18;
   f.int perfectSquare; //Line 6 - Correct
   g. float x, y, decimal; //Line 7 - Correct
  
Ans 2 :
   There are two actions that should be taken before using variable in program.That are :
       a- Declaration - The variable should be declared first with it's proper datatype before using in the program.This tells the compiler to reserve a block of memory for the variable.
                   Eg., int x;
       b Initialization - The variable can be initialized with a proper value to avoid getting the garbage value.
                   Eg., int x = 0;
                  
Ans 3 :
   Following are the valid C++ identifiers :
       a._nextQuiz
       c.$twoHundred
       d. CPP_Project
       i.overPaymen
      
Ans 4 :
   Following are the reserved word in C++ :
       c - double
       d - Const
       f - bool
   main and cin are not reserved word but for technical distinction it is advised to consider them as reserved word.
  
Ans 5 :
   Reserved word has got a special meaning and does a controlled task.It could be something that identifies the type of data a variable can hold or it could define start of a function definition. It could be a modifier that defines behavior/accessibility of a program member (variable or function).
       Whereas, an Identifier is something that is user-defined. It could be a name of a variable that holds data or name of a function that does some specific user-define task. It could be a class name (in case of object oriented programs).

Ans 6:
   Because the C++ identifiers are case sensitive, quizNo1 and quizno1 are different.

Ans 7 :
   a- String firstName;
   b- float discountedPrice;
   c- int juiceBottlesCount;
   d- float milesTravelled;
   e- float highestScore;