C++ Quiz 1

15
Question 1 2 out of 2 points A function's return data type must be the same as the function's parameter(s). Answer Selected Answer: False Question 2 2 out of 2 points A preprocessor directive does not require a semicolon at the end. Answer Selected Answer: True Question 3 2 out of 2 points A statement that starts with a # is called a: Answer Selected Answer: preprocessor directive Question 4 2 out of 2 points After execution of the following code, what will be the value of input_value if 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; Answer Selected Answer: 15

description

C++ quiz 1

Transcript of C++ Quiz 1

Page 1: C++ Quiz 1

Question 1

2 out of 2 points

A function's return data type must be the same as the function's parameter(s).

Answer

Selected Answer: False

Question 2

2 out of 2 points

A preprocessor directive does not require a semicolon at the end.

Answer

Selected Answer: True

Question 3

2 out of 2 points

A statement that starts with a # is called a:

Answer

Selected Answer:

preprocessor directive

Question 4

2 out of 2 points

After execution of the following code, what will be the value of input_value if 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;

Answer

Selected Answer:

15

Page 2: C++ Quiz 1

Question 5

2 out of 2 points

Assume that a program has the following string object definition:

string name;

Which of the following statements correctly assigns a string literal to the string object?

Answer

Selected Answer: name = “Jane”;

Question 6

2 out of 2 points

Both of the following if statements perform the same operation.

if (sales > 10000)

commissionRate = 0.15;

if (sales > 10000) commissionRate = 0.15;

Answer

Selected Answer: True

Question 7

0 out of 2 points

How many times will the following loop display "Hello"?

for (int i = 0; i <= 20; i++)

cout << "Hello!" << endl;

Answer

Selected Answer:

21

Question 8

2 out of 2 points

Page 3: C++ Quiz 1

How many times will the following loop display "Hello"?

for (int i = 0; i < 20; i++)

cout << "Hello!" << endl;

Answer

Selected Answer:

20

Question 9

2 out of 2 points

How many times will the following loop display "Hello"?

for (int i = 20; i > 0; i--)

cout << "Hello!" << endl;

Answer

Selected Answer:

20

Question 10

2 out of 2 points

How would you consolidate the following declaration statements into one statement?

int x = 7;

int y = 16;

int z = 28;

Answer

Selected Answer:

int x = 7, y = 16, z = 28;

Question 11

2 out of 2 points

If you place a semicolon after the statement

if (x < y)

Answer

Selected Answer:

Page 4: C++ Quiz 1

the compiler will interpret the semicolon as a null statement

Question 12

2 out of 2 points

In C++ the = operator indicates:

Answer

Selected Answer:

assignment

Question 13

2 out of 2 points

In C++, key words are written in all lowercase letters.

Answer

Selected Answer: True

Question 14

2 out of 2 points

In memory, C++ automatically places a ________ at the end of string literals.

Answer

Selected Answer:

null terminator

Question 15

2 out of 2 points

Look at the following program and answer the question that follows it.

1 // This program displays my gross wages.

2 // I worked 40 hours and I make $20.00 per hour.

3 # include <iostream>

4 using namespace std;

5

6 int main( )

7 {

8 int hours;

Page 5: C++ Quiz 1

9 double payRate, grossPay;

10

11 hours = 40;

12 payRate = 20.0;

13 grossPay = hours * payRate;

14 cout << "My gross pay is $" << grossPay << endl;

15 return 0;

16 }

Which line(s) in this program cause output to be displayed on the screen?

Answer

Selected Answer:

14

Question 16

0 out of 2 points

Look at the following statement.

while (x++ < 10)

Which operator is used first?

Answer

Selected Answer:

x

Question 17

2 out of 2 points

One reason for using functions is to break programs into manageable units, or modules.

Answer

Selected Answer: True

Question 18

2 out of 2 points

The ________ is/are used to display information on the computer's screen.

Answer

Selected Answer:

Page 6: C++ Quiz 1

cout object

Question 19

2 out of 2 points

The first step in using the string class is to #include the ________ header file.

Answer

Selected Answer: string

Question 20

2 out of 2 points

The following code correctly determines whether x contains a value in the range of 0 through

100.

if (x >= 0 && <= 100)

Answer

Selected Answer: False

Question 21

2 out of 2 points

The function, pow(x, 5.0), requires this header file.

Answer

Selected Answer:

cmath

Question 22

2 out of 2 points

These are operators that add and subtract one from their operands.

Answer

Selected Answer:

++ and --

Question 23

Page 7: C++ Quiz 1

2 out of 2 points

This is used to mark the end of a complete C++ programming statement.

Answer

Selected Answer:

semicolon

Question 24

2 out of 2 points

This operator is known as the logical OR operator.

Answer

Selected Answer:

||

Question 25

2 out of 2 points

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

Answer

Selected Answer:

==

Question 26

2 out of 2 points

This operator performs a logical NOT operation.

Answer

Selected Answer:

!

Question 27

2 out of 2 points

To use the rand( ) function, you must #include this header file in your program.

Answer

Selected Answer:

Page 8: C++ Quiz 1

cstdlib

Question 28

2 out of 2 points

What is the output of the following code segment?

n = 1;

while (n <= 5)

cout << n << ' ';

n++;

Answer

Selected Answer:

1 1 1... and on forever

Question 29

0 out of 2 points

What is the output of the following segment of code if 4 is input by the user when asked to

enter a number?

int num;

int total = 0;

cout << "Enter a number from 1 to 10: ";

cin >> num;

switch (num)

{

case 1:

case 2: total = 5;

case 3: total = 10;

case 4: total = total + 3;

case 8: total = total + 6;

default: total = total + 4;

}

cout << total << endl;

Answer

Selected Answer:

13

Question 30

Page 9: C++ Quiz 1

2 out of 2 points

What is the value of donuts after the following code executes?

int donuts = 10;

if (donuts != 10)

donuts = 0;

else

donuts += 2;

Answer

Selected Answer:

12

Question 31

0 out of 2 points

What is the value of number after the following statements execute?

int number = 10;

number += 5;

number -= 2;

number *= 3;

Answer

Selected Answer:

39

Question 32

0 out of 2 points

What will be the output of the following code segment after the user enters 0 at the keyboard?

int x = -1;

cout << "Enter a 0 or a 1 from the keyboard: ";

cin >> x;

if (x)

cout << "true" << endl;

else

cout << "false" << endl;

Answer

Selected Answer:

false

Page 10: C++ Quiz 1

Question 33

0 out of 2 points

What will following segment of code output?

int x = 5;

if (x = 2)

cout << "This is true!" << endl;

else

cout << "This is false!" << endl;

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

Answer

Selected Answer:

This is true! This is all folks!

Question 34

2 out of 2 points

What will the following code display?

cout << "Four" << "score" << endl;

cout << "and" << "seven" << endl;

cout << "years" << "ago" << endl;

Answer

Selected Answer:

Fourscore

andseven

yearsago

Question 35

0 out of 2 points

What will the following code display?

cout << "Monday";

cout << "Tuesday";

cout << "Wednesday";

Answer

Selected Answer:

MondayTuesdayWednesday

Page 11: C++ Quiz 1

Question 36

0 out of 2 points

What will the following code display?

int number = 6;

++number;

cout << number << endl;

Answer

Selected Answer:

7

Question 37

0 out of 2 points

What will the following code display?

int number = 6;

cout << ++number << endl;

Answer

Selected Answer:

7

Question 38

2 out of 2 points

What will the following code display?

int number = 6;

cout << number++ << endl;

Answer

Selected Answer:

6

Question 39

2 out of 2 points

What will the following code display?

Page 12: C++ Quiz 1

int number = 6;

number++;

cout << number << endl;

Answer

Selected Answer:

7

Question 40

0 out of 2 points

What will the following loop display?

int x = 0;

while (x < 5)

{

cout << x << endl;

x++;

}

Answer

Selected Answer:

0 1 2 3 4

Question 41

2 out of 2 points

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

{

Page 13: C++ Quiz 1

funny = 1;

serious = 1;

}

cout << funny << " " << serious << endl;

Answer

Selected Answer:

1 1

Question 42

2 out of 2 points

What will the following segment of code output if 11 is entered at the keyboard?

int number;

cin >> number;

if (number > 0)

cout << "C++";

else

cout << "Soccer";

cout << " is ";

cout << "fun" << endl;

Answer

Selected Answer:

C++ is fun

Question 43

2 out of 2 points

What will the following segment of code output? You can assume the user enters a grade of

90 from the keyboard.

cout << "Enter a test score: ";

cin >> test_score;

if (test_score < 60);

cout << "You failed the test!" << endl;

if (test_score > 60)

cout << "You passed the test!" << endl;

else

cout << "You need to study for the next test!";

Answer

Selected Answer:

Page 14: C++ Quiz 1

You failed the test!

You passed the test!

Question 44

0 out of 2 points

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;

Answer

Selected Answer:

That's a high score! This is a test question!

Question 45

2 out of 2 points

What will the value of x be after the following statements execute?

int x;

x = 18 / 4;

Answer

Selected Answer:

4

Question 46

2 out of 2 points

When typing in your source code into the computer, you must be very careful since most of

your C++ instructions, header files, and variable names are case sensitive.

Answer

Selected Answer: True

Question 47

0 out of 2 points

Page 15: C++ Quiz 1

Which of the following expressions will determine whether x is less than or equal to y?

Answer

Selected Answer:

x <= y

Question 48

2 out of 2 points

Which statement is equivalent to the following?

x = x * 2;

Answer

Selected Answer:

x *= 2;

Question 49

2 out of 2 points

Which value can be entered to cause the following code segment to display the message "That

number is acceptable."

int number;

cin >> number;

if (number > 10 && number < 100)

cout << "That number is acceptable.\n";

else

cout << "That number is not acceptable.\n";

Answer

Selected Answer:

99

Question 50

2 out of 2 points

You may use the exit( ) function to terminate a program, regardless of which control

mechanism is executing.

Answer

Selected Answer: True