CSCI 152                       Quiz 1  ( 25 Jan )                       Name _______________________

Spring 2005

 

1. Circle the best answer:

 

a) if (8 < 2*3)

        cout << “Hello”;

        cout << “ There”;

   Outputs the following:

    (i) Hello There     (ii) Hello        (iii)    There      (iv)  none of these

 

b) if (7 <= 7)

       cout << 6-9*2/6;

   Outputs the following:

       (i) -1                  (ii) 3              (iii) 3.0             (iv) none of these

 

c) if (5 < 3)

        cout << “*”;

    else

        if (7 == 8)

            cout <<”&”;

    else

        cout << “$”;

   Outputs the following:

    (i) *                 (ii) &              (iii) $                 (iv) none of these

 

2. Write C++ statements that accomplish the following:

 

a) Declares int variables x and y.

 

int x;                              int x, y;

int y;

 

b) Initializes an int variable x to 10 and a char variable ch to ‘B’.

 

x = 10;

ch = ‘B’;

 

c) Updates the value of an int variable x by adding 5 to it.

 

x = x + 5;                     x += 5;

 

d) Sets the value of a double variable z to 25.3;

 

z = 25.3;

 

e) Copies the content of an int variable y into an int variable z.

 

z = y;

 

f) Swaps the contents of the int variables x and y.  (Declare additional variables, if necessary).

 

int tmp;

tmp = y;

y = x;

x = tmp;

 

3. a) Convert the following for loop into an equivalent while loop:

 

for (int i=3; i<=6; i++)

{

    cout << i*i << “ “;

}

cout << endl;

 

 

int i=3;

while (i <= 6)

{

   cout << i*i << “ “;

   i++;

}

 

 

 

b) What is the output of the loop.

 

 

9 16 25 36

 

 

 

 

 

4.  a)  Write a value-returning function, isVowel, that returns the bool value true if a character that is given as the functions parameter is a vowel (you need only check for the lower case vowels a, e, i, o, u or y) and otherwise returns false.

 

bool isVowel(char c)

{

      if ((c == ‘a’) || (c == ‘e’) || (c == ‘i’) || (c == ‘o’) || ( c == ‘u’))

              return true;

      else

              return false ;

}

 

 

     b)  Declare a character variable named ch.  Ask a user to input a character value and read one in from standard input into the ch variables you declared. 

 

char ch;

cout << “Enter a character: “;

cin >> ch;

 

     c)  Use your isVowel function to print out “You entered a vowel” if the ch that the user entered in (b) was a vowel, otherwise print out “You entered a consonant”.

 

if (isVowel(ch))

        cout << “You entered a vowel” << endl;

else

        cout << “You entered a consonant” << endl;