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

c++ Hi everyone, please help me to answear these Multiple choice Question. There

ID: 643460 • Letter: C

Question

c++ Hi everyone, please help me to answear these Multiple choice Question. There is 16 of them, so hard so easy. If you are not sure of the one you're answearing please indicate that in your answear. THANKS A LOT IN advance

Select one:

a. Constructors can be very useful for handling all of the c++ code required in a program.

b. Constructors can be very useful for defining and initializing private member variables.

c. Constructors can be very useful for setting initial values for private member variables.

d. Constructors can be very useful for controlling access to private member variables by non-member functions

void printArray(int array[])
{

for (int i = 0; i < length; i++)
{

    cout << array[i] << " ";

}

}

Select one:

a. int result[];
printArray(int result[]);

b. int result[]={3, 1, 2, 6, 4, 2};
printArray(result);

c. int result[]={3, 1, 2, 6, 4, 2};
printArray({3, 1, 2, 6, 4, 2});

d. int[]{3, 1, 2, 6, 4, 2};
printArray(int[]);

struct database {
int id_number;
int age;
float salary;
};

database employee;

How would you assign a value to each of the data members in a function belonging to the same class?

Select one:

a. employee::age = 22;
employee::id_number = 1;
employee::salary = 12000.21;

b. employee->age = 22;
employee->id_number = 1;
employee->salary = 12000.21;

c. age = 22;
id_number = 1;
salary = 12000.21;

d. employee.age = 22;
employee.id_number = 1;
employee.salary = 12000.21;

Select one:

a. ofstream

b. inputstream

c. ifstream

d. iostream

void duplicate (int& a, int& b, int& c)

What do the ampersands (&) signify?

Select one:

a. The ampersands signify that their corresponding arguments are to be passed by reference instead of by value.

b. The ampersands signify that their corresponding arguments are to be passed by value instead of by reference.

c. The ampersands signify that their corresponding arguments are to be passed as a constant (read-only) value.

d. None of these. Ampersands are not allowed in a function prototype

struct pairT {
std::string key;
int val;
};

Which of the following could be used to add a new struct to the array called entries in position 0?

Select one:

pairT p;
p.key = "Bob" && p.val = 12345;
entries.0 =p;

pairT p;
p = "Bob";
p = 12345;
entries[0] =p;

pairT p;
entries[0] = {key = "Bob"; val = 12345};

pairT p;
p.key = "Bob";
p.val = 12345;
entries[0] =p;

#include

#include

using namespace std;

int main () {

ofstream myfile ("example.txt");

if (myfile.is_open())

{

    myfile << "This is a line. ";

    myfile << "This is another line. ";

    myfile.close();

}

else cout << "Unable to open file";

return 0;

}

Select one:

a. Reads lines from a file

b. Counts lines in a file

c. Writes lines to a file

d. Gets lines from the keyboard

#include

#include

#include

using namespace std;

int main () {

string line;

ifstream myfile ("example.txt");

if (myfile.is_open())

{

    while ( getline (myfile,line) )

    {

      cout << line << ' ';

    }

    myfile.close();

}

else cout << "Unable to open file";

return 0;

}

Select one:

a. Each line of the file is displayed

b. The first line of the file is displayed

c. Each line of the file is counted

d. Each line entered at the keyboard is written to a new file

Select one:

is_good()

good()

is_ready()

open()

class Line

{

   public:

      void setLength( double len );

      double getLength( void );

      Line();

   private:

      double length;

};

Which is the constructor?

Select one:

void setLength( double len );

double getLength( void );

Line();

double length;

//username.h

#include

#include

class username

{

   public:

      void getName();           

      void display_usernames();

   private:

      std::string first;

      std::string last;

     

};  

Which functions can carry out an assignment first =

Explanation / Answer

==============================================================================

1.What is the purpose of a constructor?
Select one:
   a. Constructors can be very useful for handling all of the c++ code required in a program.
   b. Constructors can be very useful for defining and initializing private member variables.
   c. Constructors can be very useful for setting initial values for private member variables.
   d. Constructors can be very useful for controlling access to private member variables by non-member functions
  
Ans:
   c. Constructors can be very useful for setting initial values for private member variables.
Explanation:
   Constructors are used only for intialization of varibles.

==============================================================================

2.Which of the following would be a correct way to call (invoke) this function?
   void printArray(int array[])
   {
       for (int i = 0; i < length; i++)
       {
       cout << array[i] << " ";
       }
   }

   Select one:
--------------
   a. int result[];
       printArray(int result[]);

   b. int result[]={3, 1, 2, 6, 4, 2};
       printArray(result);

   c. int result[]={3, 1, 2, 6, 4, 2};
       printArray({3, 1, 2, 6, 4, 2});

   d. int[]{3, 1, 2, 6, 4, 2};
       printArray(int[]);

Ans:
   b. int result[]={3, 1, 2, 6, 4, 2};
       printArray(result);
Explanation:
   You must have to pass the array with address , result will be the address of the array.

==============================================================================

3.You have the following struct defined in the private area of your class:

   struct database {
       int id_number;
       int age;
       float salary;
   };
   database employee;

   How would you assign a value to each of the data members in a function belonging to the same class?
   Select one:
   a.    employee::age = 22;
       employee::id_number = 1;
       employee::salary = 12000.21;

   b.    employee->age = 22;
       employee->id_number = 1;
       employee->salary = 12000.21;

   c.    age = 22;
       id_number = 1;
       salary = 12000.21;

   d.    employee.age = 22;
       employee.id_number = 1;
       employee.salary = 12000.21;
Ans:
   d.    employee.age = 22;
       employee.id_number = 1;
       employee.salary = 12000.21;
Explanation:
   Structure variables are accessed by . operator.
   You the structure is pointer type then it will be -> operator.

==============================================================================

4.   Which of the following classes handles reading from a file?

   Select one:
   a. ofstream
   b. inputstream
   c. ifstream
   d. iostream
Ans:
   c. ifstream
Explanation:
   Reading from input file ifstream is enough

==============================================================================

5. Consider the following prototype:
   void duplicate (int& a, int& b, int& c)
  
   What do the ampersands (&) signify?
   Select one:
       a. The ampersands signify that their corresponding arguments are to be passed by reference instead of by value.
       b. The ampersands signify that their corresponding arguments are to be passed by value instead of by reference.  
       c. The ampersands signify that their corresponding arguments are to be passed as a constant (read-only) value.
       d. None of these. Ampersands are not allowed in a function prototype
Ans:
   a. The ampersands signify that their corresponding arguments are to be passed by reference instead of by value.
Explanation:
   Arguments must be passed as address since the in parameters of type reference.

==============================================================================

6. You have an array of structs that is defined as follows.

   struct pairT {
       std::string key;
       int val;
   };
   Which of the following could be used to add a new struct to the array called entries in position 0?
   Select one:
       pairT p;
       p.key = "Bob" && p.val = 12345;
       entries.0 =p;

       pairT p;
       p = "Bob";
       p = 12345;
       entries[0] =p;

       pairT p;
       entries[0] = {key = "Bob"; val = 12345};

       pairT p;
       p.key = "Bob";
       p.val = 12345;
       entries[0] =p;
Ans:
       pairT p;
       p.key = "Bob";
       p.val = 12345;
       entries[0] =p;
Explanation:
   As it is clearly say the structure variables must be assinged using . operator.
   andd also array must with [0]

==============================================================================

7.What does the following program do?

   #include
   #include
   using namespace std;
   int main () {
       ofstream myfile ("example.txt");
       if (myfile.is_open())
       {
           myfile << "This is a line. ";
           myfile << "This is another line. ";
           myfile.close();
       }
       else cout << "Unable to open file";
       return 0;
   }
   Select one:
   a. Reads lines from a file
   b. Counts lines in a file
   c. Writes lines to a file
   d. Gets lines from the keyboard
Ans:
   c. Writes lines to a file
Explanation:
   Since you are using ofstream , this will write data to file

==============================================================================

8.What will happen if the example.txt file is found?

   #include
   #include
   #include
   using namespace std;
   int main () {
       string line;
       ifstream myfile ("example.txt");
       if (myfile.is_open())
       {
           while ( getline (myfile,line) )
           {
               cout << line << ' ';
           }
           myfile.close();
       }
       else cout << "Unable to open file";
       return 0;
   }
   Select one:
   a. Each line of the file is displayed
   b. The first line of the file is displayed
   c. Each line of the file is counted
   d. Each line entered at the keyboard is written to a new file
Ans:
   a. Each line of the file is displayed
Explanation:
   Each line of the file is displayed.Since while loop is used.

==============================================================================

9.The __________ function will return true if the file you wish to use is ready for input/output operations.

   Select one:
       is_good()
       good()
       is_ready()
       open()
Ans:
       open()
Explanation:
       open() is the function to check

==============================================================================

10.Consider the following class definition:
   class Line
   {
   public:
           void setLength( double len );
           double getLength( void );
           Line();
       private:
           double length;
   };
   Which is the constructor?
   Select one:
       void setLength( double len );
       double getLength( void );
       Line();
       double length;
Ans:
       Line();
Explanation:
   Since constructor must be same as Class Name

==============================================================================

11.Consider the following class definition:

   //username.h
   #include
   #include
   class username
   {
   public:
   void getName();   
   void display_usernames();
   private:
   std::string first;
   std::string last;
   };
   Which functions can carry out an assignment first =

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote