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

1.) What is the value of each variable after execution? int x = 2; int y = 7; do

ID: 667218 • Letter: 1

Question

1.)What is the value of each variable after execution?

int x = 2;

int y = 7;

double p = 7.0;

int z = y / x;

double w = p / x;

z:

w:

2.) What is the output of the following program fragment:

void find(int a, int& b, int& c)
{
            int temp;
            c = a + b;
            temp = a;
            a = b;
            b = 2 * temp;

}

int main()

{

            int x, y, z;

            x = 10;

            y = 20;

            z = 25;

            find(x, y, z);

            cout<< x <<" "<< y <<" "<< z <<endl;

            return 0;

}

Output:

3.) Assume you are given a variable x below:

int x = 10;

Create a pointer and save the memory address the variable x to the pointer:

Answer:

4.) You are given a class below, create a accessor and mutator function for field age.

class Student

{

public:

            string name;

private:

            int age;

}

Answer:

5.)What is the output of the code below?

for (int j = 1; j <= 10; j++)

{

            if (j == 3)

            {

                        break;

            }

            cout << j << endl;

}

Output:

for (int j = 1; j <= 10; j++)

{

            if (j == 3)

            {

                        continue;

            }

            cout << j << endl;

}

Output:

Explanation / Answer

1.)What is the value of each variable after execution?
   int x = 2;
   int y = 7;
   double p = 7.0;
   int z = y / x;
   double w = p / x;

   z:3
   w:3.5

Because x,y,z are integers / gives coefficient in integer(leaves remainder) upon division so 7/2=3
Because x is integer and p,w are double values / gives coefficient in double (leaves remainder) upon division so 7.0/2=3.5

2.) What is the output of the following program fragment:
   void find(int a, int& b, int& c)
   {
               int temp;
               c = a + b;
               temp = a;
               a = b;
               b = 2 * temp;
   }

   int main()
   {
               int x, y, z;
               x = 10;
               y = 20;
               z = 25;
               find(x, y, z);
               cout<< x <<" "<< y <<" "<< z <<endl;
               return 0;
   }

Output:10 20 30
Initial values x=20, y=20, z=30

for x
value of x is passed as argument to method find so any changes in a does not affect its value so 'a' value remain same
x=10

for z
address of z is passed as argument any changes in method affect 'z' and z is mapped to &c
c=a+b
c=10+20=30

for y
address of y is passed as argument any changes in method affect 'y'
temp=a;(temp=10)
b=2*temp=2*10=20

3.) Assume you are given a variable x below:
   int x = 10;
   Create a pointer and save the memory address the variable x to the pointer:
Answer:
   int *ptr=&x;
   *ptr represents the pointer variable
   &variable gives the address
4.) You are given a class below, create a accessor and mutator function for field age.
   class Student
   {
   public:
               string name;
   private:
               int age;
   }
Answer:
   class Student
       {
           public:
                       string name;
                       int getAge();
                       void setAge(int);
           private:
                       int age;
       }
   int Student::getAge()
   {
       return age;
   }
   void Student::setAge(int _age)
   {
       age=_age;
   }
5.)What is the output of the code below?
   for (int j = 1; j <= 10; j++)
   {
               if (j == 3)
               {
                           break;
               }
               cout << j << endl;
   }
   Output:(when j = 3 come's out of loop)
   1
   2
   3

   for (int j = 1; j <= 10; j++)
   {
               if (j == 3)
               {
                           continue;
               }
               cout << j << endl;
   }
     
   Output:(when j = 3 skip and continue iteration(so 3 doesnot get printed))
   1
   2
   4
   5
   6
   7
   8
   9
   10