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

1. Suppose num1 and num2 are int variables and symbol is a char variable. Consid

ID: 667793 • Letter: 1

Question

1. Suppose num1 and num2 are int variables and symbol is a char variable. Consider the following input:

47 18 * 28 $

What value (if any) is assigned to num1, num2, and symbol after each of the following statements executes? (Use the same input for each statement.)

a.  cin >> num1 >> symbol >> num2;
b.  cin >> symbol >> num1 >> num2;
c.  cin >> num1;
  cin.get(symbol);
  cin >> num2;
d.  cin >> num1 >> num2;
  cin.get(symbol);
e. cin.get(symbol);
  cin >> num1 >> num2;

2. Suppose num1 and num2 are int variables and x and y are double variables. Assume the following input data:

35 28.30 67 12.50

What value (if any) is assigned to num1, num2, x, y after each of the following statements executes? (Use the same input for each statement.)

c.  cin >> x >> y >> num1 >> num2;
d.  cin >> num1 >> num2 >> x >> y;
e.  cin >> num1 >> x >> y >> num2;
f.  cin >> x >> num1 >> num2 >> y;

3. Suppose x and y are int variables and symbol is a char variable. Assume the following input data:

38 26 * 67 33
24 $ 55 # 34
# & 63 85

What value (if any) is assigned to x, y, and symbol after each of the following statements executes? (Use the same input for each statement.)

a.  cin >> x >> y;
   cin.ignore(100, ' ');
   cin >> symbol;

b.  cin >> x;
  cin.ignore(100, '*');
  cin >> y;
  cin.get(symbol);

c.  cin >> y;
  cin.ignore(100, ' ');
  cin >> x >> symbol;

d.  cin.get(symbol);
  cin.ignore(100, '*');
  cin >> x;
  cin.ignore(100, ' ');
  cin >> y;

e.  cin.ignore(100, ' ');
  cin >> x >> symbol;
  cin.ignore(100, ' ');
  cin.ignore(100, '&');
  cin >> y;

4. Suppose that x and y are int variables, z is a double variable, and ch is a char variable. Suppose the input statement is:

cin >> x >> y >> ch >> z;

What values, if any, are stored in x, y, z, and ch if the input is:

a. 35 62.78
b. 86 32A 92.6
c. 12 .45A 32

5. Suppose that num is an int variable and discard is a char variable. Assume the following input data:

#34

What value (if any) is assigned to num and discard after each of the following statements executes? (Use the same input for each statement.)

a.  cin.get(discard);
    cin >> num;
b. discard = cin.peek();
    cin >> num;
c.  cin.get(discard);
    cin.putback(discard);
    cin >> discard;
    cin >> num;

6. Given the input:

12.8 Bill 15

and the variable declaration:

double x = 3.5;
int y = 18;
string name = "Lisa";

What is the output, if any? Use the same input for each part.

a. cin >> x >> name >> y;
cout << x << " " << y << " " << name << endl;

b. cin >> y >> x >> name;
cout << x << " " << y << " " << name << endl;

c. cin >> x >> y >> name;
cout << x << " " << y << " " << name << endl;

7. Suppose that name is variable of type string. What is the effect of the following statement?

getline(cin, name);

8. Suppose that age is an int variable and name is a string variable. What are the values of age and name after the following input statements execute:

cin >> age;
getline(cin, name);

if the input is:

a. 23 Lance Grant
b. 23
Lance Grant

Explanation / Answer

1.

a.

num1 = 47

symbol = 1

num2 = 8

Explanation:

·       The value of num1 is 47 because 47 is assigned to num1.

·       The value of symbol is 1 because the next character is 1.

·       The value of num2 is 8 because 8 is next integer value after the character “1”.

b.

num1 = 7

symbol = 4

num2 = 18

Explanation:

·       The value of symbol is 4 because the first character is 4.

·       The value of num1 is 7 because 7 is the next integer value after the character “4”.

·       The value of num2 is 18 because the next integer value is 18.

c.

num1 = 47

symbol = space

num2 = 18

Explanation:

·       The value of num1 is 47 because 47 is assigned to num1.

·       The value of symbol is space ( ) because the next character is space.

·       The value of num2 is 18 because 18 is next integer value after the character

“ ”.

d.

num1 = 47

symbol = space

num2 = 18

Explanation:

·       The value of num1 is 47 because 47 is assigned to num1.

·       The value of num2 is 18 because 18 is next integer value

·       The value of symbol is space ( ) because the next character is space.

e.

num1 = 7

symbol = 4

num2 = 18

·       The value of symbol is 4 because the first character is 4.

·       The value of num1 is 7 because 7 is the next integer value after the character “4”.

·       The value of num2 is 18 because the next integer value is 18.

2.

a.

num1 = 28

num2 = 67

x = 35

y = 0.3

Explanation:

·       The value of num1 is 28 because num1 is of int type.

·       The value of num2 is 67 because 67 is assigned to num2.

·       The value of x is 35 because first value is 35.

·       The value of y is 0.3 because of num1 is of type int, the compiler considered the next value i.e. y is 0.3.

b.

num1 = 35

num2 = 67

x = 28.3

y = 12.50

Explanation:

·       The value of num1 is 35 because num1 is of int type.

·       The value of num2 is 67 because 67 is assigned to num2.

·       The value of x is 28.3.

·       The value of y is 12.50.

c.

num1 = 67

num2 = 12

x = 35

y = 28.3

Explanation:

·       The value of num1 is 67 because 67 is assigned to num1.

·       The value of num2 is 12 because num2 is of type int.

·       The value of x is 35 because first value is 35.

·       The value of y is 28.3.

d.

num1 = 35

num2 = 28

x = 0.3

y = 67

Explanation:

·       The value of num1 is 35 because first value is 35.

·       The value of num2 is 28 because num2 is of type int.

·       The value of x is 0.3 because of num2 is of type int, the compiler considered the next value i.e. x is 0.3.

·       The value of y is 67.

e.

num1 = 35

num2 = 12

x = 28.3

y = 67

Explanation:

·       The value of num1 is35 because first value is 35.

·       The value of num2 is 12 because num2 is of type int.

·       The value of x is 28.3.

·       The value of y is 67.

f.

num1 = 28

num2 = 0

x = 35

y = garbage

Explanation:

·       The value of num1 is 28 because num1 is of type int.

·       The value of num2 is 0 because num2 is of type int. Thus, it will take 0.

·       The value of x is 35.

·       The value of y is Garbage.

3.

a.

x = 38

y = 26

symbol = 2

Explanation:

·       The value of x is 38.

·       The value of y is 26

·       The value of symbol is 2 because of the statement cin.ignore(100,’ ’), the execution will come at next line and print 2 on the screen.

b.

x = 38

y = 26

symbol = space

Explanation:

·       The value of x is 38.

·       The value of y is 26

·       The value of symbol is 2 because of the statement cin.ignore(100,’*’), the execution will come at next character which is space.

c.

x = 24

y = 38

symbol = $

Explanation:

·       The value of x is 24 because of the statement cin.ignore(100,’ ’), the execution will come at next line and print 24 on the screen.

·       The value of y is 38 because of first value.

·       The value of symbol is $ because of the statement cin.ignore(100,’ ’), the execution will come at next line and print $ on the screen.

d.

x = 67

y = 24

symbol = 3

Explanation:

·       The value of x is 67 because of the statement cin.ignore(100,’*’).

·       The value of y is 24 because of the statement cin.ignore(100,’ ’), the execution will come at next line and print 24 on the screen.

·       The value of symbol is 3 because of first character.

e.

x = 24

y = 63

symbol = $

Explanation:

·       The value of x is 24 because of the statement cin.ignore(100,’ ’), the execution will come at next line and print 24 on the screen.

·       The value of y is 67 because of the statement cin.ignore(100,’&’).

·       The value of symbol is $ because of the statement cin.ignore(100,’ ’), the execution will come at next line and print $ on the screen.

4.

a.

x = 35

y = 62

z = 78

ch = .

Explanation:

·       The value of x is 35.

·       The value of y is 62.

·       The value of z is 78 because next number after character is 78.

·       The value is ch is..

b.

x = 86

y = 32

z = 92.6

ch = A

Explanation:

·       The value of x is 86.

·       The value of y is 32.

·       The value of z is 92.6 because next number after character is 92.6.

·       The value is ch is A.

c.

x = 12

y = 0

z = garbage value

ch = space

Explanation:

·       The value of x is 12.

·       The value of y is 0 because y is of int type.

·       The value of z is garbage because the program will not take the next value.

·       The value is ch is space.

5.

a.

num = 34

discard = #

Explanation:

·       The value of num is 34.

·       The value of discard is # because discard is of char type.

b.

num = 0

discard = #

Explanation:

·       The value of num is 0 because of the statement discard = cin.peek(),all other value will be 0.

·       The value of discard is # because discard is of char type.

c.

num = 34

discard = #

Explanation:

·       The value of num is 34.

·       The value of discard is # because discard is of char type.

6.

a.

12.8 15 Bill

Explanation:

·       The value 12.8 is assigned to x.

·       The value 15 is assigned to y.

·       Bill will assign to name.

b.

0.8 12 Bill

Explanation:

·       The value 0.8 is assigned to x because first 12 will assign to y.

·       The value 12 is assigned to y.

·       Bill will assign to name.

c.

12.8 0 Lisa

Explanation:

·       The value 12.8 is assigned to x.

·       The value 0 is assigned to y because in the cin statement string value is assigned to integer.

·       Lisa will assign to name.

7.

getline(cin, name) reads the whole string till it encounters the newline.

8.

a.

age = 23

name = Lance Grant

Explanation:

·       23 will be assigned to age.

·       Lance Grant will be assigned to name.

b.

age = 23

name = NULL

Explanation:

·       23 will be assigned to age.

·       Null will be assigned to name because input is in next line.