Introduction to Programming with C++, Third Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

Chapter 4 Mathematical Functions, Characters, and Strings

Section 4.2 Mathematical Functions
4.1  What is ceil(3.6)?
A. 2
B. 3
C. 4
D. 5

4.2  What is floor(3.6)?
A. 2
B. 3
C. 4
D. 5.0

4.3  What is sqrt(4.0)?
A. 2
B. 2.5
C. 1
D. 3.0

4.4  What is max(min(3, 6), 2)?
A. 2
B. 3
C. 4
D. 5.0

4.5  What is sin(PI / 2) for a constant PI = 3.14159?
A. 1
B. 0.5
C. 0.4
D. 1.5

4.6  What is sin(PI / 6) for a constant PI = 3.14159?
A. 1
B. 0.5
C. 0.4
D. 1.5

4.7  What is asin(0.5) for a constant PI = 3.14159?
A. 90
B. 30
C. PI / 6
D. PI / 2

4.8  Which of the following are not the functions in C++?
A. srand
B. rand
C. rint
D. pow
E. random

Section 4.3 Character Data Type and Operations
4.9  Which of the following is the correct expression of character 4?
A. 4
B. "4"
C. '\0004'
D. '4'

4.10  A character is stored in __________.
A. one byte
B. two bytes
C. three bytes
D. four bytes

4.11  What is the output of the following code?

char ch = 'F';
if (ch >= 'A' && ch <= 'Z')
  cout << ch << endl;
A. F
B. f
C. nothing
D. F f

4.12  Suppose x is a char variable with a value 'b'. What will be displayed by the statement cout << ++x?
A. a
B. b
C. c
D. d

4.13  Which of the following statement prints smith\exam1\test.txt?
A. cout << "smith\exam1\test.txt";
B. cout << "smith\\exam1\\test.txt";
C. cout << "smith\"exam1\"test.txt";
D. cout << "smith"\exam1"\test.txt";

4.14  Suppose i is an int type variable. Which of the following statements display the character whose ASCII is stored in variable i?
A. cout << i;
B. cout << static_cast<char>i;
C. cout << static_cast<int>i;
D. cout << i;

4.15  The ASCII of 'a' is 97. What is the ASCII for 'c'?
A. 96
B. 97
C. 98
D. 99

4.16  What will be displayed by cout << 'z' - 'a'?
A. 25
B. 26
C. a
D. z

4.17  You can assign a value in _____ to an int variable.
A. 'x'
B. 120
C. 120.0
D. "x"
E. "120"

4.18  Which of the following assignment statements is correct?
A. char c = 'd';
B. char c = 100;
C. char c = "d";
D. char c = "100";

4.19  Note that the ASCII for character A is 65. The expression 'A' + 1 evaluates to ________.
A. 66
B. B
C. A1
D. Illegal expression

Section 4.7 Character Functions
4.20  To check whether char variable ch is a digit, use the function
A. isdigit(ch)
B. isalpha(ch)
C. isalnum(ch)
D. isprint(ch)
E. islower(ch)

4.21  To return an uppercase letter from char variable ch, use
A. isdigit(ch)
B. isalpha(ch)
C. toupper(ch)
D. tolower(ch)
E. islower(ch)

Section 4.8 The string Type
4.22  Suppose a string is declared as string s = "abcd". What is s.length()?
A. 0
B. 1
C. 2
D. 3
E. 4

4.23  Suppose a string is declared as string s = "abcd". What is s.size()?
A. 0
B. 1
C. 2
D. 3
E. 4

4.24  Suppose a string is declared as string s. What is s.size()?
A. 0
B. 1
C. 2
D. 3
E. 4

4.25  Suppose a string is declared as string s = "abcde". What is s.at(0)?
A. a
B. b
C. c
D. d
E. e

4.26  Suppose a string is declared as string s = "abcde". What is s[0]?
A. a
B. b
C. c
D. d
E. e

4.27  Suppose two strings are declared as string s1 = "ABC" and string s2 = "DEFG". Which of the following is an incorrect expression?
A. s1 + s2
B. "ABC" + "DEFG"
C. s1 + "TEMP"
D. "TEMP" + s1
E. s1 + s2 + "TEMP"

4.28  Suppose two strings are declared as string s1 = "ABC" and string s2 = "DEFG". Which of the following is a correct expression?
A. s1 > s2
B. s1 >= s2
C. s1 < s2
D. s1 <= s2
E. s1 == s2

4.29  Suppose two strings are declared as string s1 = "ABC" and string s2 = "DEFG". Which of the following expression evaluates to true?
A. s1 > s2
B. s1 >= s2
C. s1 < s2
D. s1 <= s2
E. s1 == s2

4.30  If you read a string input: PROGRAMMING IS FUN using the following code, what will be is s?

cout << "Enter a string:";
string s;
cin >> s;
A. PROGRAMMING IS FUN
B. PROGRAMMING
C. IS
D. FUN

4.31  If you read a string input: PROGRAMMING IS FUN using the following code, what will be is s?

cout << "Enter a string:";
string s;
getline(cin, s);
A. PROGRAMMING IS FUN
B. PROGRAMMING
C. IS
D. FUN

Section 4.10 Formatting Console Output
4.32  You can use _______ to set the width of a print field.
A. setw(width)
B. setprecision(n)
C. fixed
D. showpoint
E. left

4.33  Which of the following is a stream manipulator function?
A. setw(width)
B. setprecision(n)
C. fixed
D. showpoint
E. left

4.34  Which of the following justifies the output to the left?
A. setw(width)
B. right
C. fixed
D. showpoint
E. left

4.35  The following code displays ________.

cout << "a" << setw(6) << "abc";
A. a abc
B. aabc
C. a
D. abc

4.36  The following code displays ________.

cout << "a" << setw(6) << left << "abc";
A. a abc
B. aabc
C. a
D. abc

Section 4.11 Simple File Input and Output
4.37  To create an object for reading data from file test.txt, use ________.
A. ofstream("test.txt");
B. ofstream input("test.txt");
C. ifstream("test.txt");
D. ifstream input("test.txt");

4.38  To create an object for writing data to file test.txt, use ________.
A. ofstream("test.txt");
B. ofstream output("test.txt");
C. ifstream("test.txt");
D. ifstream output("test.txt");