CSC101 Summer 2005Please Place Your Name

CSC101 Summer 2005Please Place Your Name

CSC101 Summer 2005Please place your name

Miller on back of last page

1.Modify this code by replacing "while" loop (that executes 10 times) with a "for" loop. Throw away what you don't need and add what you do.

1 // Fig. 2.7: fig02_07.cpp

3 #include <iostream>

5 using namespace std;

9

10 int main()

11 {

12 int total=0; // sum of grades input by user

13 int gradeCounter=1;

14 int grade; // grade value

15 int average; // average of grades

22 while ( gradeCounter <= 10 ) {

23 cout < "Enter grade: ";// prompt for input

24 cin > grade; // read grade

25 total = total + grade;

26 gradeCounter = gradeCounter + 1;

27 }

30 average = total / 10;

33 cout < "Class average is " < average < endl;

35 return0;

37 }

MULTIPLE CHOICE

2.A statement that starts with a # is called a:

a.Comment

b.Function

c.Preprocessor directive

d.Key word

e.None of the above.

3.______represent storage locations in the computer's memory.

a.Literals

b.Variables

c.Comments

d.Integers

e.None of the above

4.You must have a ______for every variable you intend to use in a program.

a.purpose

b.definition

c.comment

d.constant

e.None of the above

5.The numeric data types in C++ are broken into two categories:

a.numbers and characters

b.singles and doubles

c.integer and floating point

d.real and unreal

e.None of the above

6.A character literal is enclosed in ______quotation marks, whereas a string literal is enclosed in ______quotation marks.

a.double, single

b.triple, double

c.open, closed

d.single, double

e.None of the above

7.What is the modulus operator?

a.+

b.*

c.

d.%

e.||

8.Which data type requires only one byte of storage on most computer systems?

a.short

b.int

c.float

d.char

e.double

9.What is the output of the following statement?

cout < 4 * (15 / (1+3)) < endl;

a.15

b.12

c.63

d.72

e.None of these

10.______must be included in any program that uses the cout object.

a.Opening and closing braces

b.The header file iostream

c.Comments

d.Escape sequences

e.None of the above

11.The float data type is considered _____ precision, and the double data type is considered ______precision.

a. single, double

b. float, double

c. integer, double

d. short, long

e. None of the above

12.Every complete C++ program must have a ______.

a.comment

b.function named main

c.preprocessor directive

d.symbolic constant

e.cout statement

13. Identify and correct the errors (Correct it or point to it and say what’s wrong) in each of the following lines of code.

a. while (c =< 5){

product =* c;

}

b.If ( a =< 0 ); cout > “a is not positive. /n;

14.What is the value written by the following code segment?

int num =6;

int sum = 1;

for (int i =2; i<= num; i++)

if (num%i == 0)

sum+= i;

cout < sum;

15.The ______operator always follows the cin object, and the ______operator follows the cout object.

a. binary, unary

b. conditional, binary

c. ,

d. ,

e. None of the above

16.In any program that uses the cin object, you must include the ______.

a. compiler

b. iostream header file

c. linker

d. and operators

e. None of the above

17.The character array company[12] can hold

a. Twelve characters

b. Thirteen characters

c. Eleven characters and the null terminator

d. Twelve characters and the null terminator

e. None of the above

18.What is the value stored at x, given the statements:

int x;

x = 3 / static_cast<int>(4.5 + 6.4);

a. .3

b. 0

c. .275229

d. 3.3

e. None of these

19.If you wanted the user to enter the length, width, and height from the

keyboard, which cin statement is correctly written?

a. cin < length, width, height;

b.cin.get(length, width, height);

c. cin > length > width > height;

d. cin > length, width, height;

e.cin < length; width; height;

20.______can override the rules of operator precedence.

a.[Brackets]

b.(Parentheses)

c.{Braces}

d.The escape character \

e.None of these

21.When the final value of an expression is assigned to a variable, it will be converted to:

a. The smallest possible data type

b.The largest possible data type

c.The data type of the variable

d.The data type of the expression

e.None of the above

22.This manipulator is used to establish a field width for the value immediately following it.

a.field_widthb.set_field

c.setwd.iomanip

e.None of the above

23.This reads an entire line of text, until the [Enter] key is pressed.

a.cin

b.cin.getline()

c.cin.get()

d.cin.ignore()

e.cin.input()

24.Which is true about the following statement?

cout < setw(4) < num4 < " ";

a.It allows four spaces for the value in the variable num4.

b.It outputs "setw(4)" before the value in the variable num4.

c.It should use setw(10) to output the value in the variable num10.

d.It inputs up to four characters stored in the variable num4.

e.None of these

25.This stream manipulator forces cout to print the digits in fixed-point notation.

a.setprecision(2)

b.setw(2)

c.fixed

d.setfixed(2)

e.None of these

26.When using the sqrt function you must include this header file.

a.cstdlib

b.cmath

c.cstring

d.iostream

e.iomanip

27.Relational operators allow you to ______numbers.

a.add

b.multiply

c.compare

d.average

e.None of these

28.After execution of the following code, what will be the value of

input_valueif the value 0 is entered at the keyboard at run time?

cin > input_value;

if (input_value > 5)

input_value = input_value + 5;

else if (input_value > 2)

input_value = input_value + 10;

else

input_value = input_value + 15;

input_value = ______

29.If you place a semicolon after the statement if (x < y)

a.The code will not compile.

b.The compiler will interpret the semicolon as a null statement.

c.The if statement will always evaluate to false.

d.All of the above.

e.None of these.

30.When a relational expression is false, it has the value _____.

a.one

b.zero

c.zero, one, or minus one

d.less than zero

e.None of these

31.What will following segment of code output? (Careful!)

int x = 5;

if (x = 2)

cout < "This is true!" < endl;

else

cout < "This is false!" < endl;

cout < "This is all folks!" < endl;

a.This is true!

b.This is false!

c.This is true!

This is false!

d.This is true!

This is all folks!

e.None of these

32.What will the following segment of code output?

score = 40;

if (score > 95)

cout < "Congratulations!\n";

cout < "That's a high score!\n";

cout < "This is a test question!" < endl;

a.This is a test question!

b.Congratulations!

That's a high score!

This is a test question!

c.That's a high score!

This is a test question!

d.Congratulations!

That's a high score!

e.None of these

33.This operator represents the logical AND.

a.++

b.||

c.

d.@

e.None of these

34.This operator takes an operand and reverses its truth or falsehood.

a.||

b.relational

c.arithmetic

d.!

e.None of these

35.Input values should always be checked for

a.Appropriate range

b.Reasonableness

c.Division by zero

d.All of these

e.None of these

36.Without this statement appearing in a switch construct, the program "falls through" all of the statements below the one with the matching case expression.

a.break

b.exit

c.switch

d.scope

e.None of these

37.This operator is used in C++ to represent equality.

a.=

b.

c.!!

d.==

e.None of these

38.If you intend to place a block of statements within an if statement, you must place these around the block.

a.parentheses ( )

b.square brackets [ ]

c.quotation marks ? ?

d.curly braces { }

e.None of these

39.What will the following program segment display?

int funny = 7, serious = 15;

funny = serious % 2;

if (funny != 1)

{

funny = 0;

serious = 0;

}

else if (funny == 2)

{

funny = 10;

serious = 10;

}

else

{

funny = 1;

serious = 1;

}

cout < funny < " " < serious < endl;

a.7 15

b.0 0

c.10 10

d.1 1

e.None of these

ANS:E

25.When a program lets the user know that an invalid choice has been made, this is known as ______.

a.input validation

b.output correction

c.compiler criticism

d.output validation

e.None of these

ANS:A

26.These operators connect two or more relational expressions into one, or reverse the logic of an expression.

a.relational

b.logical

c.irrational

d.negation

e.None of these

ANS:B

27.What will the following program display?

#include <iostream>

int main()

{

int a = 0, b = 2, x = 4, y = 0;

cout < (a == b) < " ";

cout < (a != b) < " ";

cout < (b <=x) < " ";

cout < (y > a) < endl;

}

a.0 1 1 0

b.0 0 1 0

c.1 1 0 1

d.1 0 0 1

e.None of these

ANS:A

28.This operator is known as the logical OR operator.

a.--b.//c.#

d.||

e.None of these

ANS:D

29.This operator performs a logical NOT operation.

a.--

b.!

c.d.

e.None of these

ANS:B

30.Given the following program, what is output after “result = “?

int main ()

{

int x = 1, y = 1, z = 1;

y = y + z;

x = x + y;

cout < "result = " < (x < y ? y : x) < endl;

return 0;

}

a.0

b.1

c.2

d.3

e.None of these

ANS:D

31.Which statement allows you to properly check the char variable code to determine whether it is equal to a “C” and then output “This is a check” and then advance to a new line?

a.if code is equal to C

cout < "This is a check\n";

b.if (code = "C")

cout < "This is a check" < endl;

c.if (code == 'C')

cout < "This is a check\n";

d.if (code == C)

cout < "This is a check" < endl;

ANS:C

32.The ______of a variable is limited to the block in which it is declared.

a.precedence

b.associativity

c.scope

d.branching ability

e.None of these

ANS:C

33.Given that x =2, y = 1, and z = 0, what will the following cout statement display?

cout < "answer = " < (x || !y & z) < endl;

a.answer = 0

b.answer = 1

c.answer = 2

d.None of these

ANS:B

34.The default section of a switch statement performs a similar task as the ______portion of an if/elseif statement.

a.conditional

b.break

c.trailing else

d.All of these

e.None of these

ANS:C

TRUE/FALSE

1.If the sub-expression on the left side of the || operator is true, the expression on the right side will not be checked.

ANS:T

2.The default section is optional in a switch statement.

ANS:T

3.Both of the following if statements perform the same operation.

if (sales > 10000)

commissionRate = 0.15;

if (sales > 10000) commissionRate = 0.15;

ANS:T

4.You should be careful when using the equality operator to compare floating point values because of potential round-off errors.

ANS:T

5.An expression that has any value other than 0 is considered true by an if statement.

ANS:T

6.If the sub-expression on the left side of an operator is false, the expression on the right side will not be checked.

ANS:T