C Question 1 Consider the following class definition usernam

C++ Question:

1. Consider the following class definition:

//username.h
#include <iostream>
#include <string>

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

to the private member variable first?

getName and main can carry out the assignment, but not display_usernames.

_________________________________________________________________________________

2.

Consider the following function definition that has been properly defined in the public area of the class Time in the file time.h.   In the constructor, hour and minute are both initialized to 2.
void Time::shiftBy(int dh, int dm)
{
hour += dh;
minute += dm;
}
The main program performs the following:
#include “time.h”
int main()
{
int x = 0;
int y = 2;
Time mytime;
mytime.shiftBy(x, y);
}
What are the values of hour and minute after the function shiftBy finishes?

hour is 0, but minute is 2.

_____________________________________________________________________

3.

Consider the following class.
class compare
{
public:
        void get_values();
        void compare_values();
        void evenOdd();

private:
        int val1;
        int val2;

};

Suppose that x and y are both objects of this class. Which of the following statements is legal in the client main program?

val1 = val2;

______________________________________________________

4.

Consider the following class definition.
using namespace std;

class CRectangle {
  
public:
    CRectangle();
    CRectangle (int w,int h);
    int area () {return (width*height);}
private:
    int width;
    int height;
};

A nonmember function is created in the same file with the local variables a and b. Which of the following statements would produce an error if you included it in the nonmember function after variables a and b are defined and initialized to 1?

CRectangle myrectangle;

_______________________________________________________

5.

Consider the following code from a client main function:
int main()
{
Time target;
target.setHour(5);
target.setMinute(20);
target.getTime();
target.countDown(10);
}
Which is an object of the Time class?

setHour

________________________________________________

6.

Which part(s) of a class is accessible to a nonmember function and which is not?

Only member functions have access to any part of a class.

________________________________________________________________________________________

7. Consider the following code. Note: Line numbers are provided on the left side of the code, but are not part of the file.
1 #include vector
2
3 class scores
4 {
5 public:
6    void loadScores();
7    void displayScores();
8 private:
9    std:vector stats
10
11 };
Which line contains a syntax error? Enter the line number in the box below.

____________________________________________________________________________________

8.

For a vector named v that has 12 elements, when would this for loop begin and end?
for (int i=0; i<v.size(); ++i)
{
sum += v[i];
}

It would begin at index 0 of the vector and end at index 11 of the vector.

_________________________________________________________________________________

9.

What does this line of code do (assume there are no errors)?
if (val1<val2) val2 = val1;

This line of code will display val2 = val1

______________________________________________________________________________________________________________

10.

Consider this line of code:

        std::string res = \"even\";


Which of the following statements would not produce an error if used after the above line?

res = res%2;

______________________________________________________________

11.

Comments cause the computer to ignore the text after the // on the line when the program is compiled.

False

__________________________________________________________________________________________

12.

Lists or collections of data elements can be stored in a single __________.

for loop

_________________________________________________________________

13.

Every class definition contains the keyword main followed immediately by the class’s name.

False

_____________________________________________________________________________________

14.

The ___________ statement is used to repeat one or more actions when a condition is true or skip the action(s) when the condition is false.

if

__________________________________________________

15.

All variables must be given a value to store when they are declared

False

_____________________________________________________________________

16.

The ___________keyword in a function passes data back to the function that called it.

push_back

__________________________________________________________

17.

Consider the following function prototype:
void helloworld(std::vector<std::string>& myvector)

Assuming the main function creates a vector named myvector and fills it with data, write a statement that will properly call this function. Do not put any spaces in your answer.

____________________________________________________________________________________________________________________

18.

Consider the following function implementation:
void myclass::ref_addition (int a, int b, int& sum)
{

sum = a+b;
}

The main function might have code that looks like this:
myclass foo;
int num1 = 1;
int num2 = 2;
int total = 0;
foo.ref_addition(num1, num2, total);

When this code completes, what will the value in the total variable be? Provide the integer value in the box below.

_________________________________________________________________________________________________________________________

19.

Consider the following class definition. Note: Line numbers are provided on the left side of the code, but are not part of the file.
1 using namespace <std>
2 include <iostream>
3 class CRectangle()
4 {.   
5 public;

________________________________________________________________________

20.

Assume the following is found in a class member function. The variable x is not a class member variable. There is a #include <iostream> in the client file, but not in the header file. Which of the following statements will correct the first line of this code?


For ( x = 0, x >= 100, x++ )
std::cout << x << std::endl;

for(x == 0: x > 100: x++)

_________________________________________________________________________

21.

Consider the following function implementation.
#include <iostream>

void print_me( )
{
    std::cout << \"Hello World\ \";
}

Write a statement to properly call this function. Do not put spaces in your answer.

____________________________________________________________________________________________

22. Consider the following class definition:

//helloworld.h
#include <iostream>

class world
{
public:
    void print_me( );
};

Which statement(s) would you use to add a member variable named myinteger?

__________________________________________________________________________

23.

Given this class definition:

//helloworld.h
#include <iostream>

class world
{
public:
    void print_me( );
};

Consider the implementation of the world class print_me function (see above).
1 void print_me
2 {
3    std::cout << \"Hello World\ \";
4 }

Which line contains errors? Enter the line number in the box below.

__________________________________________________________________________

24.

Which of the following lines of code will display the contents of the variable called first? Assume no errors.

std::cin << first;

_____________________________________________

25.

Consider the following function implementation:

void myvariables::getvalues()
{
        //local variables
        int count;
        std::string first, last;
}

The myvariables class contains a private data member called count that is set to the value 100 before this function is called. If this function included the following line as the last line of this function, what would the result be?
std::cout << count;

0

___________________________________________________________________

26.

Which would you use to add together all of the elements in a vector of integers?

for loop

___________________________________________________________________________

27.

Which would you use to determine whether the contents of var1 and var2 are not equal? Type what is missing from this if statement:

if(var1 ____ var2)

______________________________________________________________________________________________________________

28.

Consider the following function implementation:
void scores::displayScores()
{
    for (int i = 0; i < stats.size(); i++)
    {
   std::cout << stats[i] << std::endl;
   total = total + stats[i];
    }

Which type of variable is total?

local variable

__________________________________________________________________________

29.

Consider the following function implementation:
void check::check_input()
{
        //initialize class variable
        sum = 0;
        int n = -1;
}

Which type of function is check_input?

nonmember function

_________________________________________________________________________

30.

Consider the following function prototypes. Assume that both of these functions will add 100 to the existing value stored in the variable called num when the function is called.
void passbyValue(int num);
void passbyRef(int &num);

The following function calls are made from the main function:
int original_num = 1;
passbyValue(original_num);
passbyRef(original_num);

What is the value in original_num after these calls are completed? Provide the integer value in the box below.

_____________________________________________________________________________________________________________________

31.

Consider the following member function:
int myclass::addition (int a, int b)
{
int r;
r=a+b;
}

What is wrong with this function?

The parameter list is wrong.

____________________________________________________________________

32.

Which part of a class is used to set class member variables to an initial value?

public

______________________________________________________________________________________

33.

To protect data, which part of a class should be used to declare class member variables?

private

______________________________________________________________________________________________

34.

A class definition is typically stored in a file with which filename extension?

Only main can carry out the assignment.

Solution

1).

getName and display_usernames can carry out the assignment, but not main. The assignment first = “Hello” can be handled by the functions: getName and display_usernames.

2)

Upon performing the functionality, the correct option will be:

hour is 2 and minute is 4

3)

Since, x and y are both objects of this class, the statement which is legal in the main program is: x.compare_values();

4)

The statement which will produce an error if it is included in the nonmember functions after variables a and b are defined and initialized to 1 is CRectangle myrectangle(a);

5)

Clearly the object of the Time class is target. This is because the declaration is Time target.

6)

public is accessible to a non-member function, private is not.

7)

The line which contains error is line 9 that is std:vector stats because it lacks semicolon.

8)

Clearly, the for loop iterates from the value of i starting from 0 till 11 and the sum is performed for the values starting from the index 0 to 11.

So, the correct option is: It would begin at index 0 of the vector and end at index 11 of the vector.

9)

Clearly, the correct option is that: If val1 holds a value that is less than current value of val2, this line of code will change val2 to store a copy of val1. Otherwise, the contents of val2 will not change.

10)

The statement which will not produce an error if used after the above line:

        std::string res = “even”;

is res = res + “steven” because it contains the sub string “even”

11.

True

12.

vector

An integer variable cannot hold more than one value at a time. hence, correct option is A. vector.

13.

false

main comes only once in a program whereas a program can have multiple classes. Hence, the given statement is false.

14.

case

while and case both executes the statements when condition is true and skips the statements when condition is false but repetition comes under while loop only.

Hence, while is the correct option.

15.

false

A value can be assigned to a variable after declaration also. Hence, the given statement is false.

16.

return

ref is used to refer a variable, not to pass the data to function. return is the keyword that passes the value to the function and stored in any variable inside the function.

17.

helloworld(myvector);

This is the correct function call to call the given function. Below is given the executable program to check the function call.

#include <iostream>

#include \"string\"

#include <vector>

using namespace std;

     void helloworld(std::vector<std::string>& myvector)

     {

          for (int i = 0; i < myvector.size(); i++)

          {

               cout << \"myvector[\" << i << \"]=\" << myvector[i] << endl;

          }

    

     }

int main()

{

     std::vector<string> myvector;

     myvector = { \"ed\", \"d\", \"d\", \"ed\", \"fd\" };

     helloworld(myvector);

    return 0;

}

18.

In function ref_addition, variable total has been passed as the referenced parameter in variable sum.

Hence, the value of sum is copied to total and being returned to main.

Output: 3

19.

Option 2: include<iostream> is correct. It includes directive preprocessor in the program.

Option 1: using namespace <std> is syntactically incorrect. the correct statement is using namespace std;

Option 3: class CRectangle() is incorrect. Class name is not followed by parenthesis.

Option 4: {. The dot (.) never comes after curly braces.

Option 5: After keyword public, semicolon is written instead of semi-colon.

20.

for(int x = 0; x <= 100; x++)

It is syntactically correct. Semi-colons are used in for loop instead of commas and for is written in all small letters.

21.

print_me(); is the function call for the given function. It is not defined inside any class, thus, it does not need any object to be called.

22.

private:

int myinteger;

Variables are declared private in a class. hence, the access specifier private is used. int is used to declare an integer variable.

Hence the correct option is A.

23.

Line 1 contains error. After function name, paranthesese () are used. hence the correct syntx is given below:

void print_me()

24.

std:: cout<<first; is the correct statement. cout is used to print the statement. To print the value, inseryion operation << is used that takes the value of a variable and prints ition console.

Hence, the correct option is option 3(numbered as per the given sequence of statements).

25.

The result is unpredictable and depends upon the function call. if the value is passed using reference variable, then it will pribt value otherwise it will give error.

26.

for loop is used to add all the elements of the vector. Hence, the correct option is C.

27.

!= (not equal) operator is used to check if the cntents are not equal.

28.

total is being accessed without any object that is necessary to access a class variable. Hence, it is a local variable.

Hence, the correct option is 4.

29.

check_input is a member function of class check. Given is the syntax to define a function explicitly.

Hence, the correct option is 2.

30.

The value of variable will be 101. When the fist function call completes, it does not change the value of the variable because it is pass by value call that changes the values only inside the function.

But, the second call is passing &num as parameter that is arefernce parameter. It adds 100 in the original value of variable original_num.

Hence, the correct answer is 101.

31.

In the given function, return type of the function is given that is int. It means that the given function must return an integer value or variable at the end.

It is missing in this function.

hence, the correct option is 3.

32.

Constructors are used to initialize the member variable at the time of creation of the object.

Hence the correct option is 3. constructor.

33.

To protect the data you should define the member variables private.

protect access specifier is used when the subclasses are given permission to access that variable. But, any condition is not given.

Hence, the correct answer is option 3.

34.

A class definition is stored in .h file(header file) from where those classes are accessed in .cpp file.

Hence, the correct option is 1.

C++ Question: 1. Consider the following class definition: //username.h #include <iostream> #include <string> class username { public: void getName()
C++ Question: 1. Consider the following class definition: //username.h #include <iostream> #include <string> class username { public: void getName()
C++ Question: 1. Consider the following class definition: //username.h #include <iostream> #include <string> class username { public: void getName()
C++ Question: 1. Consider the following class definition: //username.h #include <iostream> #include <string> class username { public: void getName()
C++ Question: 1. Consider the following class definition: //username.h #include <iostream> #include <string> class username { public: void getName()
C++ Question: 1. Consider the following class definition: //username.h #include <iostream> #include <string> class username { public: void getName()
C++ Question: 1. Consider the following class definition: //username.h #include <iostream> #include <string> class username { public: void getName()
C++ Question: 1. Consider the following class definition: //username.h #include <iostream> #include <string> class username { public: void getName()
C++ Question: 1. Consider the following class definition: //username.h #include <iostream> #include <string> class username { public: void getName()
C++ Question: 1. Consider the following class definition: //username.h #include <iostream> #include <string> class username { public: void getName()
C++ Question: 1. Consider the following class definition: //username.h #include <iostream> #include <string> class username { public: void getName()
C++ Question: 1. Consider the following class definition: //username.h #include <iostream> #include <string> class username { public: void getName()

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site