test sample

advertisement
CSDP222
Fall, 2011
Test 1
09-29-2011
This test is closed book.
1) Circle and correct the errors in the following program (program
continues on next page):
(there are 6 errors)
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
const int SIZE = 20;
int my_strstr (char &str_to_check [], char &str_to_find [])
{
int i = 0;
while (str_to_check[i] != '\0')
{
int j = i;
int k = 0;
while (str_to_check[j] != '\0' &&
str_to_find[k] != '\0' &&
str_to_check[j] == str_to_find[j])
{
j++;
k++;
}
if (str_to_check[i] == '\0')
return i;
i++;
}
return -1;
}
void my_strcat (char str_to_concatenate_to [], char
str_to_concatenate [])
{
int length_to_str_to_concatenate_to
= stringlen(str_to_concatenate_to);
int b = 0;
}
void main ()
{
char chr_arr [SIZE] = {'A','B','c','a','t','D'};
int index_where_found1 = my_strstr (chr_arr, "cat");
int index_where_found2 = my_strstr (chr_arr, "caz");
my_strcat(chr_arr, "cat");
system("pause");
}
2. A function __________ contains the statements that make up the function.
a.
b.
c.
d.
e.
definition
prototype
call
expression
parameter list
3. A function is executed when it is
a.
b.
c.
d.
e.
defined
prototyped
declared
called
None of these
4. The value in this type of local variable persists between function calls.
a.
b.
c.
d.
e.
global
internal
static
dynamic
None of these
5. Which of the following statements about global variables is true?
a. A global variable is accessible only to the main function.
b. A global variable is declared in the highest-level block in which it is used.
c. A global variable can have the same name as a variable that is declared locally
within a function.
d. If a function contains a local variable with the same name as a global variable, the
global variable's name takes precedence within the function.
e. All of these are true
6. If a function is called more than once in a program, the values stored in the function's
local variables do not _________ between function calls.
a.
b.
c.
d.
e.
persist
execute
communicate
change
None of these
7. What is the output of the following program?
#include <iostream>
using namespace std;
void doSomething(int&);
int main()
{
int x = 2;
cout << x << endl;
doSomething(x);
cout << x << endl;
return 0;
}
void doSomething(int& num)
{
num = 0;
cout << num << endl;
}
a.
2
0
2
c.
0
0
0
b.
2
2
2
d.
2
0
0
8. What is the output of the following program?
(Note that line numbers are NOT a part of the program)
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
a. 2
2
#include <iostream>
using namespace std;
void showDub(int);
int main()
{
int x = 2;
showDub(x);
cout << x << endl;
return 0;
}
void showDub(int num)
{
cout << (num * 2) << endl;
}
b. 4
2
c.
2
4
d. 4
4
8. b) What line contains the header for the ShowDub function?
________________________
8. c) What line contains the prototype for the ShowDub function?
________________________
8. d) What line contains a call to the ShowDub function?
________________________
9. In the space below, please show the following C++ code: Show a main function and
another function, convert, where convert follows main. The convert function
takes two arguments, a call by value argument of type float, and a call by reference
argument of type int. convert also returns an int value. You do not need to
provide any content in main or convert, except for the minimal content required for
successful compilation.
10) In the space below, please show the following C++ code: Show the declaration of an
enum datatype week_days that describes the days of the week. Also, show a for loop
that will cycle through the days of the week. You do not need to provide any content in
main or the for loop, except for the minimal content required for successful
compilation.
11. With an enumerated data type, the enumerators are stored in memory as
a. strings
b. integers
c. characters
d. doubles
12. Look at the following declaration.
enum Tree { OAK, MAPLE, PINE };
In memory, what value will the MAPLE enumerator be stored as?
a. “MAPLE”
b. 2
c. ‘M’
d. 1
e. 1.0
13. You cannot directly assign an integer value to an enum variable.
14. You cannot directly assign an enumerator to an int variable.
T
T
/
/
F
F
15. This is the escape sequence representing the null terminator.
a.
b.
c.
d.
e.
\n
\t
\0
/NULL
None of these
16. The null terminator stands for this ASCII code.
a.
b.
c.
d.
e.
57
100
1000
0
None of these
17. This function accepts a pointer to a string as an argument, and it returns the length
of the string (not including the null terminator).
a.
b.
c.
d.
e.
numchar
strlength
strlen
countstring
None of these
18. A library function that can find one string inside another is:
a.
b.
c.
d.
e.
strcmp
strstr
strfind
strsearch
None of these
19. To use the strlen function in a program, you must also write #include
______________.
a.
b.
c.
d.
e.
<strlen>
<iostring>
<cstring>
<stringlib>
None of these
20. This function concatenates the contents of one string with another string.
a.
b.
c.
d.
e.
strcopy
strappend
strcat
stradd
None of these
21. This function accepts pointers to two strings and an integer argument, which
indicates how many characters to copy from the second string to the first.
a.
b.
c.
d.
e.
strcpy
stcopy
copystring
strintcpy
None of these
22. To define an array that will store students' last names of up to 25 characters in
length, which is an appropriate statement?
a.
b.
c.
d.
e.
char lastName[25];
string lastName[25];
char lastName[26];
string lastName[24];
None of these
23. A ___________ variable is declared outside all functions.
a.
b.
c.
d.
e.
local
global
floating-point
counter
None of these
24. The value in a(n) _______ variable persists between function calls.
a.
b.
c.
d.
dynamic
local
counter
static local
25. For the following questions involving strings, assume the following:
string s1 = "rsqtvwx67b89er";
string s2 = "pqrstuvwxyz";
string s3 = "abcdefghi";
int index;
string substr1;
string substr2 = "b8";
string substr3 = "sq";
For questions fill in the blank:
(If an answer is an empty string, give the answer <empty string>)
IN ALL CASES ASSUME THAT EACH QUESTION IS INDEPENDENT
25. a)
index= s1.find_first_of('2', 5);
cout << index << endl;
program writes: _______________
25. b)
substr1 = s1.substr(5,3);
cout << substr1 << endl;
program writes: _______________
25. c)
substr1 = s1.substr(3);
cout << substr1 << endl;
program writes: _______________
25. d)
index = s1.find(substr2);
cout << index << endl;
program writes: ______________
25. e)
s2.erase (4);
cout << s2 << endl;
program writes: ______________
Download