For the following 3 questions, select the value of the given C++ arithmetic expression.  Note that the presence of a decimal indicates a double value, rather than an int.

 

                1        2        3        4        5

 

1)    9/4 + 3.0        5.25   5.0        5        4        none of these

2)    8/5 – 4/5        1        0.8        3        4        none of these

3)    16%4/3-2        -1   2   -0.666   not allowed        none of these

                                                             

 

For the next 2 questions, select the value assigned to the relevant variable, given the declarations:

 

      int    IntIdent;

      double DecIdent;

                1        2        3        4        5

 

4)    DecIdent = 19 / 5;        3        3.0        3.8        4        none of these

5)    IntIdent = 5 + 2.7;        7        7.7        8        8.0        none of these

                                                             

 

For the following 4 questions, suppose the (file) input stream In contains the following 5 lines of data (there's one tab character between columns and a newline character immediately after the last character on each line):

 

      19      62      36      21      Otis

      17      30      95      28      Goob

      55      23      72      40      Gom

       6      34      82      66      Op   

       8      49      45      33      B

 

What is the value of each of the indicated variables after the execution of the following program segment?

 

int Zero = 0, One = 1, Two = 2, Three = 3, Four = 4, Five = 5;

string First = "Andge", Second = "Barn";

 

In >> Zero >> One;

In >> Zero >> One;

In.ignore(10, '\n');

In >> Two;

In.ignore(200, ‘\n’);

In >> Three >> Four >> Five;

In.ignore(200, '\t');

In >> First >> Second;

In.ignore(200, '\n');

 

                1        2        3        4        5

 

6)    One   19   62   21   30   none of these

 

7)    Three   6   17   55   23   none of these

 

8)    First   "\t"   "Gom"   "Op"   "B"   none of these

 

9)    Second   "\t"   " 40"   "Gom"   "Op"   none of these

 

 

For the next 2 questions, assume the input file stream iFile is connected to an input file whose contents are:

 

3     456A78     

 

 (There's a single tab separating the '3' from the '4'.)  Consider execution of the following code fragment immediately after the file stream has been opened:

 

int i1 = 0;

char ch1 = 'x', ch2 = 'y';

 

iFile.get(ch1);

iFile >> ch2;

iFile >> i1;

 

10)      The resulting value of the variable ch1 would be:

 


1)  '\t'

2)     ' ' (a space)

3)  '3'

4)     '4'

5)  '5'

6)     '6'

7)  'A'

8)     'x'

9)  none of these


 

11)      The resulting value of the variable i1 would be:

 


1)  456

2)     56

3)  6

4)     4

5)  5

6)            ASCII code for 'A'

7)  none of these


                             

 

For the next 2 questions, consider writing a program that must read lines of data formatted in the following way.  Each line will contain the name of a employee, followed by a tab, followed by the number of hours worked, followed by a tab, followed by the hourly pay rate, followed by a tab, followed by the number of dependents, followed by a newline.  For example:

 

John Doe<tab>45<tab>20.00<tab>2<newline>

 

Assume that an input stream variable, In, has just been opened on such a file, and that the following variables have been declared:

 

string Emp1, Emp2;

int    Dependents;

double Hours, PayRate;

 

12)      Which of the following statements will correctly read the first employee name into the variable Emp1?

 


1)     In >> Emp1;

2)            getline(In, Emp1, '\n');

3)            getline(In, Emp1, '\t');

4)     1, 2 and 3

5)     1 and 2 only

6)     1 and 3 only

7)     2 and 3 only

8)     none of these


 

13)      Assuming that the Employee name, and the tab following it, have been read, which of the following statements will correctly read the hours worked into the variable Hours?

 


1)     In >> Hours;

2)            getline(In, Hours, '\n');

3)            getline(In, Hours, '\t');

4)     1, 2 and 3

5)     1 and 2 only

6)     1 and 3 only

7)     2 and 3 only

8)     none of these


 

 

 

For the next 2 questions, assume the input file stream iFile is connected to an input file whose contents are:

 

3     456A78     

 

 (There's a single tab separating the '3' from the '4'.)  Consider execution of the following code fragment immediately after the file stream has been opened:

 

int i1 = 0;

char ch1 = 'x', ch2 = 'y', ch3 = 'z';

 

iFile >> ch1 >> ch2 >> i1 >> ch3;

 

14)      The resulting value of the variable i1 would be:

 

 


1)  456

2)     56

3)  6

4)     4

5)  5

6)            ASCII code for 'A'

7)  none of these


 

15)      The resulting value of the variable ch2 would be:

 


1)  '\t'

2)     ' ' (a space)

3)  '3'

4)     '4'

5)  '5'

6)     '6'

7)  'A'

8)     'x'

9)  none of these


                             

 

For the next 3 questions, assume the following variable declarations and initializations:

 

bool Turing,

     Church = true;

int  a = 0, b = 1, c = 3;

 

Determine the value assigned by each of the following statements to the relevant Boolean variable, or if there's something (syntactically) wrong with the expression; choose from the following answers:

 

                                1                                true                                2                                false                                3                                syntax error

 


16)  Turing =  c > b + 2 * b ; 2

 

 

17)  Turing  = &&( Church ); 3

 

 

18)  Turing = (c – b == b + b && c < a - b); 2


For the next 4 questions, assume the input file stream Test1 is connected to an input file whose contents are:

 

3.14 2.71828 43 0 27

45.738 17.9 19 80 91

-12 24 1.8 51 8

 

(There's a newline character at the end of each line, and a single space separating values on the same line.)  Consider execution of the following code fragment immediately after the file stream has been opened:

 

      int   anInt1, anInt2, anInt3;

      float aFloat1, aFloat2, aFloat3;

      cout.setf(ios::floatfield, ios::fixed);

      cout.setf(ios::showpoint);

 

      Test1 >> anInt1 >> aFloat1;

      cout << anInt1 << aFloat1;            // A

 

      Test1.ignore(100, '\n');

      cout << setprecision(1);

Test1 >> aFloat2;

      cout << setprecision(2) << aFloat2;   // B

 

      Test1.ignore( 80, '\n');

      Test1.ignore(  5, '\n');

      Test1 >> anInt2 >> aFloat3 >> anInt3;

      cout << anInt2 << setprecision(1) << aFloat3 << anInt3;  // C

 

19)  In the statement labeled A, the value printed for the variable anInt1 would be:

 


1) 3

2) 14

3) 314

4) 0

5) 2

6) 71828

7) 43

8) 27       

9) none of these


 

20)      In the statement labeled B, the value printed for the variable aFloat2 would be:

 


1) 2.71

2) 43

3) 45.7

4) 45.73

5) 45.74

6) 45.738

7) 4

8) 4.0      

9) none of these


 

21)  In the statement labeled C, the value printed for the variable anInt2 would be:

     


1) 17

2) -12

3) 24

4) 4

5) 1

6) 51

7) 91

8) 8        

9) none of these


 

 

22)  In the statement labeled C, the value printed for the variable aFloat3 would be:

 


1) 1.8

2) -12.0

3) 24.0

4) 2

5) 51.0

6) 0.8

7) 4.0

8) 8.0      

9) none of these


 

 


23)      What is the value of the variable Z after the following code is executed?

 

int W = 7, X = 3, Y = 9, Z = 0;

if (Z - X <= Y / 7) {

   Z--;

   if (W*2-X != Y+X-1)

      Z++;

   else

      Z--;

}

else {

   Z = 3;

}

 


1) -1

2) -2

3) 0

4) 1

5) 3

6) the code contains a syntax error

7) none of the above


                             

 

24)      What output will the following program produce? (Be careful this may be tricky.)

 

#include <iostream>

using namespace std;

 

void main( ) {

   int Score = 87, Rank = 2;

 

   if (Rank > 2) 

   if (Score < 90)

       cout << "Not bad!";

   else

       cout << "Pretty Good!";

}

 


1)     Not bad!

2)     Pretty Good!

3)  " Not bad!"

4)  " Pretty Good!"

5)  both 1 and 2

6)  both 3 and 4

7)  No output is produced.

8)  none of these


                             

 

25)      What is the value printed for the variable Beta if the following code is executed?

 

int Beta = 0, X = 5;

 

if ( X % 3 == 1 )

   Beta = Beta + X;

X--;

 

if ( X % 2 == 0 )

   Beta = Beta + X;

X--;

 

if ( X / 3 == 0 )

   Beta = Beta + X;

 

cout << "Beta = " << Beta << endl;

 


1)     0

2)     1

3)     2

4)  3

5)            4

6)            5

7)  6

8)     None of these



For the next 2 questions, assume the declarations:

 

string FName = "Charles", LName = "Brown", Comma = ", ";

int    Age = 50;

 

26)      What would be printed by the statement:  cout << LName + Comma + FName;

 


1)            Charles Brown

2)            Charles, Brown

3)            Brown, Charles

4)             Charles+, +Brown

5)     The statement is not allowed.

6)     None of these


 

27)      Which of the following statements would print the name Charles, followed by the value of Age, so that the value 50 would be right-justified to column 25?

 

1)     cout << FName << Age;

2)     cout << FName << setw(25) << Age;

3)     cout << FName << setw(25 – FName.length()) << Age;

4)     All of these

5)     2 and 3 only

6)     None of these

                                                                             

 

For the next 2 questions, consider execution of the following statements:

 

int Enter = -1;

cin >> Enter;

 

if (Enter == 1)

      Enter = 3;

else if (Enter == 3)

      Enter = 5;

else if (Enter == 7)

      Enter = Enter;

else if (Enter == 9)

      Enter = 1;

else  Enter = 0;

 

What would the value of Enter be after execution of this code if the value read for Enter were:

 

        Enter        1        2        3        4        5        6        7        8                            

 

28)      4       1       7       3       9       5       0       -1       none of these

 

29)      7       1       7       3       9       5       0       -1       none of these

                                                                             

 

30)          If a program compiles successfully, it is guaranteed to execute correctly.

                  1) True                 2) False