Which of the following code fragments produces the output:
z = 1.5
a)
int x = 1;
int y = 2;
float z = (x + y)/2;
std::cout << "z = " << z << '\n';
b)
int x = 1;
int y = 2;
float z = x + y;
std::cout << "z = " << z/2 << '\n';
c)
int x = 1;
int y = 2;
float z = x/2 + y/2;
std::cout << "z = " << z << '\n';
d)
int x = 1;
int y = 2;
float z = (x + y)/y;
std::cout << "z = " << z << '\n'; - Answers int x = 1;
int y = 2;
float z = x + y;
std::cout << "z = " << z/2 << '\n';
Given the declarations
int A [20];
int * p = A;
Which of the following does not output the index 3 element of A?
a)
A += 3;
std::cout << *A;
b)
std::cout << *(p + 3);
c)
std::cout << A[3];
d)
std::cout << *(A + 3);
e)
p += 3;
std::cout << *p;
f)
, std::cout << p[3]; - Answers A += 3;
std::cout << *A;
What will the following program segment do?
int counter = 1;
do
{
std::cout << counter << ' ';
}
while (++counter <= 10);
a) Print the integers 1 through 11
b) Print the integers 1 through 10
c) Print the integers 1 through 9
d) Cause a syntax error - Answers Print the integers 1 through 10
Which of the following is not true about a for loop structure?
a) The three expressions in the for header are optional
b) A for loop can always be used to replace a while loop, and vice versa
c) The initialization and increment expressions can be comma-separated lists
d) You must declare the control variable outside of the for loop - Answers You must declare the
control variable outside of the for loop
What is the output from the following code fragment?
int n = 2;
int m;
++n;
m = n--;
std::cout << m << ' ' << n << '\n';
a) 3 2
b) 2 3
c) 2 2
d) 3 3 - Answers 3 2
Which of the following is true about C-strings?
a) Using functions from <cstring> on C-strings that are not null-terminated has unpredictable results
b) There is an implicit assumption that they are null-terminated, but this is usually left to the
programmer to enforce
c) The function strcpy(str1, str2) copies the characters in str2 to str1 after first checking to make sure
that str1 has enough allocated memory to receive them
d) The function strlen(c) returns the number of characters allocated to c
e) C-strings are a proper type
f) Space for C-strings is allocated automatically - Answers Using functions from <cstring> on C-strings
that are not null-terminated has unpredictable results
There is an implicit assumption that they are null-terminated, but this is usually left to the
programmer to enforce
Consider the following function prototype:
void Funky (const int * a, size_t size);
and suppose your have a client program with an array declared as follows:
int intArray[20];
a)