Exercises 5

CSC126

 

 

A.      What is the output of the following program?

#include<iostream>

using namespace std;

 

const char FIRST = 'A';

const char LAST = 'Z';

 

int main()

 

{

 

      char letter;

      int number=45;

     

      letter = 'V';

 

      if (letter >= FIRST && letter <= LAST)

            cout<<"the letter is capitalized";

      else

            cout<< "not a capital letter";

 

      if (letter == 'V' && number == 46 || number == number)

            cout<<"\ntrue!";

      else

            cout<<"\nfalse!";

 

      if (6 + 7 < number && letter > 'M')

            cout << "\ntrue!";

      cout<< "\nfalse!";

      return 0;

}

 

B.       What is the output of the following program segment?

 

int x = 14;

int y = 60;

 

while(((y - x) % 3) != 0)

{

     cout<<y<<" ";

     y = y - 5;

}

 

C.     Give the output of the pgm segment if the input is 4 7 8 5 2

 

int sum = 0;

int num;

int j;

 

for(j = 1; j <= 5; j++)

{

     cin>>num;

     if(num < 0)

        break;

 

     sum = sum + num;

}

 

cout<<sum<<endl;

 

 

D.     What is the output of the following C++ code?

 

int x = 7;

bool found = false;

 

do

{

     cout<<x<<" ";

     if(x <= 2)

         found = true;

     else

         x = x 5;

}

while(x > 0 && !found);

 

E.      Trace the following program given the input: 

3   4   5   6   0   17   18

 

#include<iostream>

#include<fstream>

using namespace std;

int main()

{

     bool correct = true;

     int num1;

     

     cin>>num1;

     while(correct && num1 < 100)

     {

          cout<<num1<<endl;

          cout<<"continuing...";

          if (num1 == 0)

correct = false;

          cin >>num1;

     }

cout<<num1;

return 0;

}