Chapter 10 Flashcards

1
Q

What header file must you include in a program using character testing functions such as
isalpha and isdigit ?

A

cctype

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What header file must you include in a program using the character conversion functions
toupper and tolower ?

A

cctype

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Assume c is a char variable. What value does c hold after each of the following statements
executes?
Statement Contents of c
c = toupper(‘a’); ___________
c = toupper(‘B’); ___________
c = tolower(‘D’); ___________
c = toupper(‘e’); __________

A

‘A’
‘B’
‘d’
‘E’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Look at the following code. What value will be stored in s after the code executes?
char name[10];
int s;
strcpy(name, “Jimmy”);
s = strlen(name);

A

5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What header file must you include in a program using string functions such as strlen and
strcpy ?

A

cstring

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What header file must you include in a program using string/numeric conversion functions
such as atoi and atof ?

A

cstdlib

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. What header file must you include in a program using string class objects?
A

string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. How do you compare string class objects?
A

With the standard relational operators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The _________ function returns true if the character argument is uppercase.

A

isupper

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. The _________ function returns true if the character argument is a letter of the alphabet.
A

isalpha

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. The _________ function returns true if the character argument is a digit.
A

isdigit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. The _________ function returns true if the character argument is a whitespace character.
A

isspace

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. The _________ function returns the uppercase equivalent of its character argument.
A

toupper

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. The _________ function returns the lowercase equivalent of its character argument.
A

tolower

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. The _________ file must be included in a program that uses character testing functions.
A

cctype

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. The _________ function returns the length of a string.
A

strlen

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
  1. To _________ two strings means to append one string to the other.
A

concatenate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
  1. The _________ function concatenates two strings.
A

strcat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
  1. The _________ function copies one string to another.
A

strcpy

20
Q
  1. The _________ function searches for a string inside of another one.
A

strstr

21
Q
  1. The _________ function compares two strings.
A

strcmp

22
Q
  1. The _________ function copies, at most, n number of characters from one string to another.
A

strncpy

23
Q
  1. The _________ function returns the value of a string converted to an integer.
A

atoi

24
Q
  1. The _________ function returns the value of a string converted to a long integer.
A

atol

25
Q
  1. The _________ function returns the value of a string converted to a float.
A

atof

26
Q
  1. The _________ function converts an integer to a string.
A

itoa

27
Q
  1. The following if statement determines whether choice is equal to ‘ Y ’ or ‘ y ’.
    if (choice == ‘Y’ || choice == ‘y’)
    Simplify this statement by using either the toupper or tolower function
A

if (toupper(choice) == ‘Y’)

28
Q
  1. Assume input is a char array holding a C-string. Write code that counts the number
    of elements in the array that contain an alphabetic character.
A

int numAlpha = 0; // Accumulator
int i = 0; // Loop counter
while (input[i] != ‘\0’)
{
if (isalpha(input[i]))
numalpha++;
}

29
Q
  1. Look at the following array definition.
    char str[10];
    Assume that name is also a char array, and it holds a C-string. Write code that copies the
    contents of name to str if the C-string in name is not too big to fit in str .
A

if (strlen(name) <= 9)
strcpy(str, name);

30
Q
  1. Look at the following statements.
    char str[] = “237.89”;
    double value;

Write a statement that converts the string in str to a double and stores the result in value .

A

value = atof(str);

31
Q
  1. Write a function that accepts a pointer to a C-string as its argument. The function should
    count the number of times the character ‘w’ occurs in the argument and return that number.
A

int wCount(char str)
{
int num = 0;
while (
str != ‘\0’)
{
if (*str == ‘w’)
num++;
}
return num;
}

32
Q
  1. Assume that str1 and str2 are string class objects. Write code that displays “They are the
    same!” if the two objects contain the same string.
A

if (str1 == str2)
cout &laquo_space;“They are the same!”;

33
Q

Character testing functions, such as isupper , accept strings as arguments and test each
character in the string

A

false

34
Q

If toupper ’s argument is already uppercase, it is returned as is, with no changes.

A

true

35
Q

If tolower ’s argument is already lowercase, it will be inadvertently converted to
uppercase

A

false

36
Q

The strlen function returns the size of the array containing a string.

A

false

37
Q

If the starting address of a C-string is passed into a pointer parameter, it can be
assumed that all the characters, from that address up to the byte that holds the null terminator, are
part of the string

A

true

38
Q

C-string handling functions accept as arguments pointers to strings (arraynames or
pointer variables), or literal strings

A

true

39
Q

The strcat function checks to make sure the first string is large enough to hold both
strings before performing the concatenation

A

false

40
Q

The strcpy function will overwrite the contents of its first string argument.

A

true

41
Q

The strcpy function performs no bounds checking on the first argument.

A

true

42
Q
  1. T F There is no difference between “847” and 847.
A

false

43
Q
  1. char str[] = “Stop”;
    if (isupper(str) == “STOP”)
    exit(0);
A

The isupper function can only be used to test a character, not a string

44
Q
  1. char numeric[5];
    int x = 123;
    numeric = atoi(x);
A

atoi converts a string to an integer, not an integer to a string

45
Q
  1. char string1[] = “Billy”;
    char string2[] = “ Bob Jones”;
    strcat(string1, string2);
A

The compiler will not allocate enough space in string1 to accommodate both
strings

46
Q
  1. char x = ‘a’, y = ‘a’;
    if (strcmp(x, y) == 0)
    exit(0);
A

strcmp