C - Test
C - Test
int n = 4;
++n;
n++;
cout << n << endl;
A. 4
B. 5
C. 6
D. none of the above
2) What is printed?
int n = 6;
cout << -n << " ";
--n;
n--;
cout << n << endl;
A. -6 4
B. -6 5
C. -6 -8
D. none of the above
3) What is printed?
int n = 4;
cout << ++n << ' ';
cout << n++ << ' ';
cout << n << endl;
A. 5 5 6
B. 4 5 6
C. 5 6 6
D. none of the above
4) Which of the following will read data from the default input device into variable val?
A. ifstream ins ("input.txt");
ins >> val;
B. cin >> val;
C. istream cin ("keyboard");
cin >> val;
D. More information needed
Page 2 of 15
5) Suppose that the following code fragment is executed.
const int LENGTH = 14;
char message[LENGTH];
A. 0
B. 15
C. 24
D. 42
Page 3 of 15
9) What is printed when
24ab15 42< Enter>
is typed on the keyboard?
int x = 0, y = 0;
char ch = ' ';
cin >> x >> ch >> y;
cout << y;
A. 0
B. 15
C. 24
D. 42
E. ASCII equivalent to 'b'
if(k == j}{
cout << "Okay. ";
} else
cout << "Not okay.";
A. Okay.Not okay.
B. Not okay.
C. Okay.
D. Nothing will be output
int main()
{
int n = 5;
f(n);
cout << n << endl;
return 0;
}
Page 4 of 15
return(5 * i);
}
A. 5
B. 10
C. 50
D. Compilation/Linkage Error.
A. 2
B. 4
C. 5
D. 6
int myfunc(double);
int myfunc(int);
int main()
{
cout << myfunc(3.51) << endl;
return 0;
}
int myfunc(double n)
{
return n * 2.0;
}
Page 5 of 15
int myfunc(int n)
{
return n * 3;
}
A. 7
B. 7.02
C. 10
D. 10.53
E. None of the above
A. table [i][j] = 0;
B. table [j][i] = 0;
C. table [i+1][j+1] = 0;
D. table [j+1][i+1] = 0;
E. table [i-1][j-1] = 0;
Page 6 of 15
16) Given the declaration
double alpha [5][50];
double sum = 0.0;
which of the following computes the sum of the elements in row 2 of alpha?
17) What does the following code fragment do? (All variables are of type int.)
position1 = -1;
position2 = -1;
for (i = 0; i <50; i++) {
for (j = 0; j < 50; j++) {
if (table[i][j] == searchValue {
position1 = i;
position2 = j;
}
}
}
19) Which of the following makes for really good game play that involves random numbers?
A. srand(0);
B. srand ((unsigned int)time(0));
C. both give the same result
Page 7 of 15
20) rand() returns a pseudo-random
A. integer >= 0
B. integer between 0 and RAND_MAX inclusive
C. double between 0 and 1 inclusive
int main()
{
int total;
int i, pt[5];
for (i = 0; i < 5; i++)
pt[i] = i;
total= sum(pt, 3);
cout << total << " " << i << endl;
return 0;
}
A. 3 5
Page 8 of 15
B. 3 3
C. 6 5
D. 10 3
and that oneStudent has been assigned some value. Which of the following outputs
Jane's name if oneStudent contains the value JANE?
A. if (oneStudent == JANE)
cout << oneStudent;
B. if (oneStudent == "JANE")
cout << StudentType(oneStudent);
C. if (oneStudent == JANE)
cout << JANE;
D. if (oneStudent == "JANE")
cout << "JANE";
E. if (oneStudent == JANE)
cout << "JANE";
and that someDay has been assigned some value. The variable twoBefore is to
represent the day of the week that is two days before someDay. Which of the following
stores the proper value into twoBefore?
A. twoBefore = someDay - 2;
B. twoBefore = Days(someDay - 2);
C. if (someDay < TUE)
twoBefore = SAT;
else
twoBefore = someDay - 2;
D. switch (someDay)
{
case SUN: twoBefore = FRI;
break;
case MON: twoBefore = SAT;
break;
default: twoBefore = Days(someDay - 2);
}
E. none of the above
Page 9 of 15
25) Given the declaration
struct PersonRec
{
int age;
double height;
int weight;
};
which of the following is valid for creating and initializing a PersonRec variable?
A. PersonRec me;
me.age = 19;
me.height = 66.5;
me.weight = 140;
B. PersonRec me = (19, 66.5, 140);
C. PersonRec.age = 19;
PersonRec.height = 66.5;
PersonRec.weight = 140;
D. A and B above
E. A, B, and C above
A. myReC.length = yourReC.length;
B. myRec = yourRec;
C. myReC.length = yourRec;
D. A and B above
E. none of the above
Page 10 of 15
The following declarations apply to the next 2 questions.
struct BrandInfo
{
string company;
string model;
};
struct DiskType
{
BrandInfo brand;
double capacity;
};
DiskType myDisk;
A. myDisk.capacity = 1.44;
B. myDisk.brand = "Memorex";
C. myDisk.BrandInfo.company = "Memorex";
D. A and C above
E. none of the above
Page 11 of 15
The following declarations apply to the next 2 questions.
struct SupplierType
{
int idNumber;
string name;
};
struct PartType
{
string partName;
SupplierType supplier;
};
PartType partsList[10000];
A. 4.0
B. 4
C. 5.0
D. 5
E. None of the above.
Page 12 of 15
The following declaration applies to the next 2 questions.
class SomeClass
{
public:
void foo();
private:
int m;
int n;
};
SomeClass alpha;
SomeClass beta;
...
32) Considering both pieces of code above, which identifiers are names of class members ?
A. m and n
B. alpha and beta
C. SomeClass, m, and n
D. alpha, beta, m, and n
E. foo, m, and n
33) Considering both pieces of code above, which identifiers are names of class objects?
A. m and n
B. alpha and beta
C. SomeClass, m, and n
D. alpha, beta, m, and n
E. foo, m, and n
Page 13 of 15
34) Assume that myclass.h and myclass.cpp are files for a class MyClass and that
someprog.cpp is a client of class MyClass. Which file(s) must #include the file myclass.h?
A. someprog.cpp
B. myclass.cpp
C. myclass.h
D. a and b above
E. a, b, and c above
35) Which of the following statements about structs and classes is false?
36) A class SomeClass has a member function F that has no parameter list, returns an int
value, and does not modify any of the private data. Which of the following would be the
correct function definition for F?
A. int F ( ) const
( ... )
B. const int F ( )
( ... )
C. SomeClass::int F ( ) const
( ... )
D. const int SomeClass::F ( )
( ... )
E. int SomeClass::F ( ) const
( ... )
Page 14 of 15
B. is used when multiple files need to know the same
declarations and without the appropriate directives there
would be compile errors of "re-declaration of ...."
C. #include <iostream> //is an example
D. #if ndef example
#define example
#endif
//is an example
E. all of the above
Page 15 of 15