Very QUICK True or False and some multiple choice questions. Please answer all f
ID: 3587482 • Letter: V
Question
Very QUICK True or False and some multiple choice questions. Please answer all for positive rate.
22. T or F? The statement shown below (taken from the RATIONAL class Sub() member function) could also be written legally as return( result ); Hint Consider how the Add() member function returns its result.
result = this->Add(r);
23. T or F? When the RATIONAL class Neg() member function becomes available, all the statements in the body of the Sub() member function (shown below) could be written legally as the single statement result = Add(RHS.Neg()); Recall The Neg() member function returns the additive inverse.
RATIONAL r = RHS;
r.S *= -1;
result = this->Add(r);
24. T or F? The data definition statement shown below (taken from the RATIONAL class Mul() member function) defines the RATIONAL object r as an auto-matic variable that “lives” in the Mul() activation record.
RATIONAL r;
25. T or F? The RATIONAL constructor is referenced to initialize r when the block shown below (taken from the Problem6.cpp function MyMain()) begins to execute.
{
cout << "2. Test Input() and Print()" << endl;
RATIONAL r;
cout << "r? "; r.Input();
cout << "r = "; r.Print(); cout << endl;
}
26. T or F? The ignore() function reference shown below (taken from the RATIONAL class Input() member function) will read and discard up to 11 characters while attempting to read and discard the character '/'. Hint Read Section 13.4.2 “istream Member Functions peek, putback and ignore”.
cin.ignore(11,'/');
27. Which one of the following RATIONAL objects defined in Problem6.cpp is constructed earliest? Hints Read Section 9.8 “When Constructors and Destructors Are Called” and/or #define SHOWLIFETIME and inspect the constructor/destructor instrumentation output generated when Problem6.cpp executes.
A: GLOBAL1 B: STATIC1 C: AUTO1 D: HEAP1 E: GLOBAL2
RATIONAL GLOBAL1(100,1);
//-------------------------------------------------------
int main()
//-------------------------------------------------------
{
void MyMain();
static RATIONAL STATIC1(200,1);
RATIONAL *HEAP1 = new RATIONAL (300,1);
MyMain();
RATIONAL AUTO1(400,1);
delete HEAP1;
system("PAUSE");
return( 0 );
}
RATIONAL GLOBAL2(500,1);
//-------------------------------------------------------
void MyMain()
//-------------------------------------------------------
{
cout << "Beginning MyMain()" << endl;
static RATIONAL STATIC2(600,1);
RATIONAL AUTO2(700,1);
RATIONAL *HEAP2 = new RATIONAL (800,1);
28. (Continuing 27) Which one of the following RATIONAL objects defined in Problem6.cpp is constructed latest?
A: GLOBAL1 B: STATIC1 C: AUTO1 D: HEAP1 E: GLOBAL2
29. (Continuing 27) Which one of the following RATIONAL objects defined in Problem6.cpp is destructed earliest?
A: GLOBAL1 B: STATIC1 C: AUTO1 D: HEAP1 E: GLOBAL2
30. (Continuing 27) Which one of the following RATIONAL objects defined in Problem6.cpp is destructed latest?
A: GLOBAL1 B: STATIC1 C: AUTO1 D: HEAP1 E: GLOBAL2
31. (Continuing 27) T or F? The scope of the RATIONAL object GLOBAL2 includes the body of the function main().
32. (Continuing 27) Fact The scope of the RATIONAL object GLOBAL1 includes the body of the function main(), but T or F? not the function MyMain().
33. The statement shown below (taken from the Problem6.cpp function MyMain()) produces the output line 1. sizeof(RATIONAL) = 12. 12 what?
A: words B: RATIONAL class members C: bytes D: characters E: none of these
cout << "1. sizeof(RATIONAL) = " << sizeof(RATIONAL) << endl;
34. The code segment, LHS.Add(RHS).Print(), in the statement shown below was taken from the Problem6.cpp function MyMain().T or F? The code segment is an example of cascaded member-function calls. Hint Read Section 9.14 “Using the this Pointer”.
cout << "LHS+RHS = "; LHS.Add(RHS).Print(); cout << endl;
35. T or F? rs[], as defined in the MyMain() function in Problem6.cpp, is an array of exactly 3 RATIONAL objects.
RATIONAL rs[] =
{
RATIONAL( -1, 2),
// If this RATIONAL constructor reference causes a compile time error, delete
// the *& operator pair and make the constructor reference "look like" the
// one above.
*&RATIONAL( 2, -4),
*(new RATIONAL( 4, 10))
};
36. (Continuing 35) T or F? Each one of the RATIONAL objects listed in the array initializer used in the definition of the array rs[] is itself initialized by the RATIONAL default constructor.
37. (Continuing 35) What is the index of the RATIONAL object listed in the array initializer that is constructed first?
A: 0 B: 1 C: 2 D: 3 E: none of these
38. (Continuing 35) What is the index of the RATIONAL object listed in the array initializer that is destructed first?
A: 0 B: 1 C: 2 D: 3 E: none of these
39. (Continuing 35) How many dereferencing operations are used in the definition of the array rs[]?
A: 0 B: 1 C: 2 D: 3 E: more than 3
40. (Continuing 35) How many of the RATIONAL objects stored in the array rs[] “live” in the MyMain() activation record?
A: 0 B: 1 C: 2 D: 3 E: more than 3
41. (Continuing 35) How many of the RATIONAL objects stored in the array rs[] “live” in the heap?
A: 0 B: 1 C: 2 D: 3 E: more than 3
//-------------------------------------------------------
// Dr. Art Hanna
// Chapter #9, Problem #6
// Rational.h
//-------------------------------------------------------
#ifndef RATIONAL_H
#define RATIONAL_H
#define SHOWLIFETIME
//-------------------------------------------------------
class RATIONAL
//-------------------------------------------------------
{
private:
int S; // { +1,-1 }
int N; // (N)umerator N >= 0
int D; // (D)enominator D > 0
private:
void Normalize();
static int GCD(int x,int y)
{
if ( y == 0 )
return( x );
else
return( GCD(y,x%y) );
}
public:
static RATIONAL Mul(const RATIONAL LHS,const RATIONAL RHS);
public:
RATIONAL(int N = 0,int D = 1);
~RATIONAL();
RATIONAL Add(const RATIONAL RHS) const;
void Sub(const RATIONAL RHS,RATIONAL &result) const;
void Print() const;
void Input();
// Added to answer some Quiz Questions
// RATIONAL(const RATIONAL &LHS): S(LHS.S),N(LHS.N),D(LHS.D)
// {
// cout << " Copy construction of ", Print(), cout << endl;
// }
};
#endif
//-------------------------------------------------------
// Dr. Art Hanna
// Chapter #9, Problem #6
// Rational.cpp
//-------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
#include ".Rational.h"
//-------------------------------------------------------
//private
void RATIONAL::Normalize()
//-------------------------------------------------------
{
S = +1;
if ( N < 0 )
{
S *= -1;
N = -N;
}
if ( D < 0 )
{
S *= -1;
D = -D;
}
int gcd = GCD(N,D);
N /= gcd;
D /= gcd;
}
//-------------------------------------------------------
RATIONAL::RATIONAL(int N,int D)
//-------------------------------------------------------
{
this->N = N;
this->D = D;
Normalize();
#ifdef SHOWLIFETIME
cout << " Construction of ", Print(), cout << endl;
#endif
}
//-------------------------------------------------------
RATIONAL::~RATIONAL()
//-------------------------------------------------------
{
#ifdef SHOWLIFETIME
cout << " Destruction of ", Print(), cout << endl;
#endif
}
//-------------------------------------------------------
RATIONAL RATIONAL::Add(const RATIONAL RHS) const
//-------------------------------------------------------
{
RATIONAL r;
r.N = this->S*this->N*RHS.D + RHS.S*RHS.N*this->D;
r.D = D*RHS.D;
r.Normalize();
return( r );
}
//-------------------------------------------------------
void RATIONAL::Sub(const RATIONAL RHS,RATIONAL &result) const
//-------------------------------------------------------
{
RATIONAL r = RHS;
r.S *= -1;
result = this->Add(r);
}
//-------------------------------------------------------
//static
RATIONAL RATIONAL::Mul(const RATIONAL LHS,const RATIONAL RHS)
//-------------------------------------------------------
{
RATIONAL r;
r.N = LHS.S*LHS.N*RHS.S*RHS.N;
r.D = LHS.D*RHS.D;
r.Normalize();
return( r );
}
//-------------------------------------------------------
void RATIONAL::Print() const
//-------------------------------------------------------
{
if ( N == 0 )
cout << '0';
else
{
if ( S == -1 ) cout << '-';
cout << N;
if ( D != 1 ) cout << '/' << D;
}
}
//-------------------------------------------------------
void RATIONAL::Input()
//-------------------------------------------------------
{
cin >> N;
cin.ignore(11,'/');
cin >> D;
Normalize();
}
//-------------------------------------------------------
// Dr. Art Hanna
// Chapter #9, Problem #6
// Problem6.cpp
//-------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
#include ".Rational.h"
RATIONAL GLOBAL1(100,1);
//-------------------------------------------------------
int main()
//-------------------------------------------------------
{
void MyMain();
static RATIONAL STATIC1(200,1);
RATIONAL *HEAP1 = new RATIONAL (300,1);
MyMain();
RATIONAL AUTO1(400,1);
delete HEAP1;
system("PAUSE");
return( 0 );
}
RATIONAL GLOBAL2(500,1);
//-------------------------------------------------------
void MyMain()
//-------------------------------------------------------
{
cout << "Beginning MyMain()" << endl;
static RATIONAL STATIC2(600,1);
RATIONAL AUTO2(700,1);
RATIONAL *HEAP2 = new RATIONAL (800,1);
cout << "1. sizeof(RATIONAL) = " << sizeof(RATIONAL) << endl;
{
cout << "2. Test Input() and Print()" << endl;
RATIONAL r;
cout << "r? "; r.Input();
cout << "r = "; r.Print(); cout << endl;
}
{
cout << "3. Test Add()" << endl;
RATIONAL r,LHS(-23,75),RHS(-77,-150);
r = LHS.Add(RHS);
cout << "LHS+RHS = "; r.Print(); cout << endl;
cout << "LHS+RHS = "; LHS.Add(RHS).Print(); cout << endl;
}
{
cout << "4. Test Sub()" << endl;
RATIONAL r,LHS(-23,75),RHS(-77,-150);
LHS.Sub(RHS,r);
cout << "LHS-RHS = "; r.Print(); cout << endl;
}
{
cout << "5. Test Mul()" << endl;
RATIONAL rs[] =
{
RATIONAL( -1, 2),
// If this RATIONAL constructor reference causes a compile time error, delete
// the *& operator pair and make the constructor reference "look like" the one above.
*&RATIONAL( 2, -4),
*(new RATIONAL( 4, 10))
};
RATIONAL *product = new RATIONAL(-1,-1);
for (int i = 0; i <= sizeof(rs)/sizeof(RATIONAL)-1; i++)
{
*product = RATIONAL::Mul(rs[i],*product);
cout << " i = " << i << ", partial product = "; product->Print(); cout << endl;
}
delete product;
}
delete HEAP2;
cout << "Ending MyMain()" << endl;
}
//-------------------------------------------------------
// Dr. Art Hanna
// Chapter #9, Problem #6
// Problem6-2.cpp
//-------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
#include ".Rational2.h"
//-------------------------------------------------------
int main()
//-------------------------------------------------------
{
{
cout << "1. Test Inv()" << endl;
RATIONAL r(-3,2);
cout << "r = ", r.Print(), cout << endl;
cout << "r.Inv() = ", r.Inv().Print(), cout << endl;
}
{
cout << "2. Test Neg()" << endl;
RATIONAL r(-3,2);
cout << "r = ", r.Print(), cout << endl;
cout << "r.Neg() = ", r.Neg().Print(), cout << endl;
}
{
//-------------------------------------------------------
// RATIONAL RATIONAL::Div(const RATIONAL RHS) const;
//-------------------------------------------------------
cout << "3a. Test Div()" << endl;
RATIONAL LHS(-3,-2),RHS(-4,3);
cout << "LHS/RHS = "; LHS.Div(RHS).Print(); cout << endl;
}
{
//-------------------------------------------------------
// void RATIONAL::Div(const RATIONAL RHS,RATIONAL &result) const;
//-------------------------------------------------------
cout << "3b. Test Div()" << endl;
RATIONAL r,LHS(-3,-2),RHS(-4,3);
LHS.Div(RHS,r);
cout << "LHS/RHS = "; r.Print(); cout << endl;
}
{
//-------------------------------------------------------
// static RATIONAL RATIONAL::Div2(const RATIONAL LHS,const RATIONAL RHS);
//-------------------------------------------------------
cout << "3c. Test Div2()" << endl;
RATIONAL LHS(-3,2),RHS(4,3);
cout << "LHS/RHS = "; RATIONAL::Div2(LHS,RHS).Print(); cout << endl;
}
{
//-------------------------------------------------------
// global RATIONAL Div(const RATIONAL LHS,const RATIONAL RHS);
//-------------------------------------------------------
cout << "3d. Test Div()" << endl;
RATIONAL LHS(-3,2),RHS(4,3);
cout << "LHS/RHS = "; ::Div(LHS,RHS).Print(); cout << endl;
}
system("PAUSE");
return( 0 );
}
Explanation / Answer
Chegg policy: This is against the chegg policy for posting too many questions in a single post so i m answering first 8 questions kindly Repost the question accordingly.
22) True
23) True
24) True
25) False
26) True
27) D. HEAP
28)A. GLOBAL1
29)B. Static1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.