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

Note: For all the questions, in addition to the answer and output, please also i

ID: 3745610 • Letter: N

Question

Note: For all the questions, in addition to the answer and output, please also include the explanations. If only providing the correct output and answer, half of the grades will be given.

1. int x=6;

x+=x-=x*x;

what is the value of x?

2. int j=0;   

while (j<25)         

     j+=3;   

cout << j;

What is the output?

3. int a=1, b=2;

What is the value for expression (a++)+b? How about a+++b?

4. char a ='3';    

switch(a)    

{          

case '3': cout << "3";       

case '2': cout << "2"; break;       

default: cout <<"1";   

}

What is the output?

5.    int a=1,b=2;    

       bool c=1;   

if((a>b)||c)                

cout<<"true"<<endl;      

else        

cout<<"false"<<endl;

       What is the output?

6.      for(int a=0, b=1; b&&a<5; a++)       

cout <<"*";

How many * will be printed out?

7. int x, y = 0;  

for (x = 1; x <= 10; x++)

   {       

if (y >= 10)

break;       

y = y + x;}   

cout << y <<endl << x << endl;

What is the output? If we change break to continue, what is the output?

8. The function func is defined below.

void func(int a, int &b)

{

a++;

b++;

}

What are the values of x and y respectively if we run the following codes?

int x=0, y=1;

func(x,y);

9. Read the following program, and figure out its output.

#include<iostream>

#include <cstring>

using namespace std;

void func(const char* s, char& c)

{

c = s[strlen(s) / 2];

}

int main()

{

char str[] = "ABCDE";

char ch = str[1];

func(str, ch);

cout << ch << endl;

return 0;

}

10. Read the following loops, and figure out the infinite loops.

(1) int i=10;       

while(1)      

{        

     i--;          

   if(i==1)           

     break;       

}

(2) for(int i=1; i<10; i++)      

{     

    i--;    

}

(3) int i=0;       

do

      { i++;}      

while (i>=0);

(4) int i=1;     

for(;;) i=i+1;

Explanation / Answer

1. -60

Here the expression executes from right to left

a. x * x = 6 * 6 = 36

b. x = x - x * x = 6 - 36 = -30 (x = -30)

c. x = x + x = -30 + -30 = -60

2. j = 27

Here the while loop iterates until the value of y is less than 25. As 3 is added to J for every iteration, the immediate 3 multiple after 25 is 27. So the loop breaks there.

3. Answer is 3 ((a++)+b)

Here for a the post increment wont affect the value of a. So the value of a will be 1 and b = 2. So 1 + 2 = 3

Answer is 3

Here also the post increment wont affect the value of a. So the value of a will be 1 and b = 2. So 1 + 2 = 3

4. Answer is 32

As there is not break after case '3' the next case statement also executes

5. Answer is true

a > b -- return false

c = 1 -- means true

false || true == true

6.Answer is *****

Here b&&a means bit wise operation between b and a. THe loop iterates until the condition is satisfied.

7.

Answer is

10

5

The loop break when the value of y is greater than or equal to 10.

The value of x increments by one for every iteration, whereas the value of y increases by x for every iteration.

Loop--X--Y

1--1--1

2--2--3

3--3--6

4--4--10

Loop breaks here

8. Answer is 0 2

Here the only the value of y is passed by reference so any changes in the function are effected only to y.

9. Answer is C

Here the string is passed to function. In the function the middle character is taken out by dividing the length of the string by 2.

10. First loop is not an infinite loop because the value of i decrease by 1 for every iteration and breaks the loop at value 1.

Second loop is an infinite loop because the value of i will be constant for every iteration because it's value is decremented for every iteration and incremented in the for statement.

Third loop is not an infinite loop because the value of i starts with 0 and increments by 1. So it's value will be > 0 and the loop breaks

Fourth loop is an infinte loop as there is no condition for the for loop.

**COmment for any queries

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