Q and A 1 What is the value of x after the following stateme
Q and A
1. What is the value of x after the following statements?
int x,y,z;
y = 10;
z = 3;
x = (y * (z + 3));
a. 60 b. 30 c. 36 d. none of these
2. Which of the following lines correctly reads a value from the keyboard and stores it in the variable named myFloat?
a. cin << myFloat;
b. cin >> myFloat;
c. cin >> \"myFloat\";
d. cout >> myFloat >> endl;
3. What is the output of the following code fragment?
int index = 5 // initalize i to 5
switch(index) // switch on cases of i
{ // assign a new value to I when the case matches
case 0: index = 15; break;
case 1: index = 25; break;
case 2: index = 35; break;
case 3: index = 40; break;
default: index = 0;
}
cout << index << endl;
a. 15 b. 25 c. 35 d. 0
4. What is the value returned by the following function?
int function ()
{
int value = 35;
return (value + 5);
value += 10;
}
a. 35 b. 40 c. 50 d. 10
5. What is the value of x after the following code fragment executes?
double x = 36.0;
x = sqrt (x);
a. 72 b. 36.0 c. 6.0 d. 2.456
6. What is the output of the following code fragment?
double size, volume = 16.0;
size = (sqrt(sqrt(volume))) / 2
cout.setf (ios::fixed);
cout.setf (ios::showpoint);
cout.precision (2);
cout << size;
a. 2 b. 2.00 c. 1.00 d. 0.00
7. What is the output of the following function and function call?
void calculateCost (int count, float& subTotal, float& taxCost); // declaration
float tax = 0.0, subTotal = 0.0; // program fragment
calculateCost (15, subTotal, tax);
cout << \"The cost for 15 items is \" << subTotal << \" , and the tax for \" << subTotal << \" is \" << tax << endl;
// end of fragment
// Function definition
void calculateCost (int count, float& subTotal, float& taxCost); // definition
{
if (count < 10)
subTotal = count * 0.50;
else
subTotal = count * 0.20;
taxCost = 0.1 * subTotal;
}
a. The cost for 15 items is 3.00, and the tax for 3.00 is 0.30
b. The cost for 15 items is 0.00, and the tax for 3.00 is 0.00
c. The cost for 15 items is 0.00, and the tax for 3.00 is 0.30
d. The cost for 15 items is 3.00, and the tax for 3.00 is 0.00
8. Which of the following function prototypes are valid?
a. void doSomething(int&x, int y);
b. void doSomething( int& x, int& y);
c. void doSomething( int x, int y);
d. all are valid
9. You should make a parameter a reference parameter if:
a. You need the function to change the value of the argument passed to the function
b. You need to be able to change the value of the parameter in the function, but not the value of the argument
c. Always
d. If any of the other If any of the other paramenter are reference parameters
10. Which of the following is the correct way to close a file stream named outFile?
a. outFile.close();
b. outFile.close;
c. outFile.close(\"project.txt:);
d. close(outfile);
11. The manipulator used to make the number of decimal places displayed 2, is:
cout.precision(2);
a. True b. False
12. What is the value of choice after the following statements?
void getChoice (int& par_choice, in par_count);
int choice, count = 3
getChoice (choice, count)
void getChoice (int& par_choice, in par_count);
{
if (par_count < 0)
par_choice = 0;
if (par_count == 0)
par_choice = -1;
else
par_choice = 99;
return;
}
a. 3 b. 0 c. -1 d. 99
13. Which statement correctly opens an input stream named in_file and attaches it to a file named project.txt?
a. in_file=project.txt
b. in_file=\"project.txt\"
c. in_file.open(\"project.txt\");
d. in_file.open(porject.txt);
14. When is the external name of the file used in the program?
a. Any time you read or write to the file
b. Never
c. Only when reading from the file
d. When opening the file stream
15. Generally speaking, a function that returns a value is like a small program. The arguments to a function are like inputs to the \"program\", and the value returned is like the output of the \"program\".
a. True b. False
16. A variable that is declared inside a function defintion is only local in scope to the function.
a. True b. False
17. Given the following function definition and program fragment, what is the output?
void function_One1 (int& z, int& q) // definiton
{
int temp;
temp = q;
q = z;
z = temp;
}
void function_Two2 (int& a, int& b) //definition
if (a < b)
function_One1 (a, b);
else
a = b;
}
int x = 3, y = 4 // code fragment
function_Two2 (y, x);
cout << x << \" \" << y << endl;
a. 3 3 b. 4 3 c. 3 4 d. 4 4
18. A variable that is declared globally is accessible by all the functions in the program including main.
a. True b. False
19. What value, if any, is modified in the following function body?
void calculate (int count, float price, float& cost)
{
if (count < 0)
cost = 0.0;
else
cost = count * price;
return;
}
a. Void functions don\'t modify parameters
b. Nothing is modified
c. The argument cost
d. The argument price and count
20. When the function below is called with 1 dependent and $400 as grossPay, what value is returned?
double computeWithholding (int dependents, double grossPay)
{
double withheldAmount;
if (dependents > 2)
withheldAmount = 0.15;
else if (dependents == 2)
withheldAmount = 0.18;
else if (dependnets == 1)
withheldAmount = 0.2;
else // no dependents
withheldAmount = 0.28;
withheldAmount = grossPay * withheldAmount;
return (withheldAmount);
}
a. 60.0 b. 80.0 c. 720.0 d. None of these
21. When the function declared below is called, what would be returned?
double computeTaxes (int hours, double grossPay);
a. hours and grossPay
b. A double
c. Nothing, the function is void
d. The declaration is illegal
22. Referring to the function declaration below, which of the following statements is true?
void calculateOvertimePay(int hours, double& timeHalfPay, double& doubleTimePay)
a. Nothing is returned
b. It is a void function
c. timeHalfPay and doubleTimePay are modified
d. They are all true
23. The body of a while loop may execute once or never.
a. True b. False
Solution
Answer :
1. What is the value of x after the following statements?
int x,y,z;
y = 10;
z = 3;
x = (y * (z + 3));
a. 60 b. 30 c. 36 d. none of these
Answer :
a.60
Explanation :
y=10,
z=3;
x = (y * (z + 3));
=(10*(3+3))
=(10*(6))
=60
..............
2. Which of the following lines correctly reads a value from the keyboard and stores it in the variable named myFloat?
a. cin << myFloat;
b. cin >> myFloat;
c. cin >> \"myFloat\";
d. cout >> myFloat >> endl;
Answer :
a. cin << myFloat;
Explanation :
cin << used reads a value from the keyboard and stores it in the variable named myFloat.
3. What is the output of the following code fragment?
int index = 5 // initalize i to 5
switch(index) // switch on cases of i
{ // assign a new value to I when the case matches
case 0: index = 15; break;
case 1: index = 25; break;
case 2: index = 35; break;
case 3: index = 40; break;
default: index = 0;
}
cout << index << endl;
a. 15 b. 25 c. 35 d. 0
Answer :
d.0
Explanation :
switch(5)
{
case 0: index = 15; break;
case 1: index = 25; break;
case 2: index = 35; break;
case 3: index = 40; break;
default: index = 0;// true
}
it will print 0
...............
4. What is the value returned by the following function?
int function ()
{
int value = 35;
return (value + 5);
value += 10;
}
a. 35 b. 40 c. 50 d. 10
Answer :
c.50
Explanation :
int function ()
{
int value = 35;
return (35 + 5);
value += 10; // 40+=10; // 50 answer
}
...................
5. What is the value of x after the following code fragment executes?
double x = 36.0;
x = sqrt (x);
a. 72 b. 36.0 c. 6.0 d. 2.456
Answer :
c.6.0
Explanation :
double x=36.0;
x = sqrt (36);
=6.0
........................
7. What is the output of the following function and function call?
void calculateCost (int count, float& subTotal, float& taxCost); // declaration
float tax = 0.0, subTotal = 0.0; // program fragment
calculateCost (15, subTotal, tax);
cout << \"The cost for 15 items is \" << subTotal << \" , and the tax for \" << subTotal << \" is \" << tax << endl;
// end of fragment
// Function definition
void calculateCost (int count, float& subTotal, float& taxCost); // definition
{
if (count < 10)
subTotal = count * 0.50;
else
subTotal = count * 0.20;
taxCost = 0.1 * subTotal;
}
a. The cost for 15 items is 3.00, and the tax for 3.00 is 0.30
b. The cost for 15 items is 0.00, and the tax for 3.00 is 0.00
c. The cost for 15 items is 0.00, and the tax for 3.00 is 0.30
d. The cost for 15 items is 3.00, and the tax for 3.00 is 0.00
Answer :
a. The cost for 15 items is 3.00, and the tax for 3.00 is 0.30
Explanation :
void calculateCost (int count, float& subTotal, float& taxCost); // definition
{
if (count < 10) //15<10 false
subTotal = count * 0.50;
else //it will executes
subTotal = count * 0.20; // 15*0.20=3
taxCost = 0.1 * subTotal; // 0.1*3=0.3
}
o/p:The cost for 15 items is 3.00, and the tax for 3.00 is 0.30
..............................
10. Which of the following is the correct way to close a file stream named outFile?
a. outFile.close();
b. outFile.close;
c. outFile.close(\"project.txt:);
d. close(outfile);
Answer :
a. outFile.close();
.................
12. What is the value of choice after the following statements?
void getChoice (int& par_choice, in par_count);
int choice, count = 3
getChoice (choice, count)
void getChoice (int& par_choice, in par_count);
{
if (par_count < 0)
par_choice = 0;
if (par_count == 0)
par_choice = -1;
else
par_choice = 99;
return;
}
a. 3 b. 0 c. -1 d. 99
Answer :
d.99
...............
13. Which statement correctly opens an input stream named in_file and attaches it to a file named project.txt?
a. in_file=project.txt
b. in_file=\"project.txt\"
c. in_file.open(\"project.txt\");
d. in_file.open(porject.txt);
Answer :
c. in_file.open(\"project.txt\");
............
14. When is the external name of the file used in the program?
a. Any time you read or write to the file
b. Never
c. Only when reading from the file
d. When opening the file stream
Answer :
c. Only when reading from the file
....................
17. Given the following function definition and program fragment, what is the output?
void function_One1 (int& z, int& q) // definiton
{
int temp;
temp = q;
q = z;
z = temp;
}
void function_Two2 (int& a, int& b) //definition
if (a < b)
function_One1 (a, b);
else
a = b;
}
int x = 3, y = 4 // code fragment
function_Two2 (y, x);
cout << x << \" \" << y << endl;
a. 3 3 b. 4 3 c. 3 4 d. 4 4
Answer :
b.4 3
...........
20. When the function below is called with 1 dependent and $400 as grossPay, what value is returned?
double computeWithholding (int dependents, double grossPay)
{
double withheldAmount;
if (dependents > 2)
withheldAmount = 0.15;
else if (dependents == 2)
withheldAmount = 0.18;
else if (dependnets == 1)
withheldAmount = 0.2;
else // no dependents
withheldAmount = 0.28;
withheldAmount = grossPay * withheldAmount;
return (withheldAmount);
}
a. 60.0 b. 80.0 c. 720.0 d. None of these
Answer :
b.80.0
Explanation :
double computeWithholding (int dependents, double grossPay)
{
double withheldAmount;
if (dependents > 2) // 1>2 false
withheldAmount = 0.15;
else if (dependents == 2) //1==2 false
withheldAmount = 0.18;
else if (dependnets == 1) //1==1 true
withheldAmount = 0.2;
else // no dependents
withheldAmount = 0.28;
withheldAmount = grossPay * withheldAmount; //400*0.2=80.0
return (withheldAmount);
}
Answer :80.0
...........
21. When the function declared below is called, what would be returned?
double computeTaxes (int hours, double grossPay);
a. hours and grossPay
b. A double
c. Nothing, the function is void
d. The declaration is illegal
Answer :
b. A double
.................
22. Referring to the function declaration below, which of the following statements is true?
void calculateOvertimePay(int hours, double& timeHalfPay, double& doubleTimePay)
a. Nothing is returned
b. It is a void function
c. timeHalfPay and doubleTimePay are modified
d. They are all true
Answer :
d. They are all true
.................
23. The body of a while loop may execute once or never.
a. True b. False
Answer :
a.True












