You are on page 1of 14

1) What is printed?

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];

cout << "Enter a sentence on the line below." << endl;


cin >> message;

cout << message << endl;


Suppose that in response to the prompt, the interactive user types the following line and
presses Enter:
Exam: Not bad so far.
What will the output of the code fragment look like?
A. Exam: Not bad so far.
B. Exam:
C. Exam: Not bad
D. Compilation/Linkage Error.

6) Which of the following will write to the default output device?


A. ofstream outs ("monitor");
outs << "Pick this one";
B. cout << "Pick this one";
C. ofstream cout ("monitor");
cout << "Pick this one";
D. More information needed

7) What is printed when


24.15 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

8) Which is an incorrect initialization?


A. char plant[] = "Tree";
B. char plant[] = {'T', 'R' ,'E', 'E'};
C. char plant[]= {'T','R','E','E','\0'};
D. Char plant[80] = "Tree";
E. None of the above.

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'

10) What would be printed by the following statements?


int k = 2;
double j = 2.0;

if(k == j}{
cout << "Okay. ";
} else
cout << "Not okay.";

A. Okay.Not okay.
B. Not okay.
C. Okay.
D. Nothing will be output

11) What would be printed by the following statements?


#include <iostream>
using namespace std;

int f(int &i);

int main()
{
int n = 5;
f(n);
cout << n << endl;
return 0;
}

int f(int &i)


{
i= 10;

Page 4 of 15
return(5 * i);
}

A. 5
B. 10
C. 50
D. Compilation/Linkage Error.

12) What would be printed by the following statements?


int y[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
cout << y[1][2];

A. 2
B. 4
C. 5
D. 6

13) Given the declaration


double x [300];
double y [75] [4];
double z [79];

which of the following statements is true?

A. x has more components than y.


B. y has more components than x.
C. y and z have the same number of components.
D. x and y have the same number of components.
E. A and C above
14) What will be printed given the following code? (Read Carefully)
#include <iostream>
using namespace std;

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

15) The following program fragment is intended to zero-out a 2-dimensional array:


int table [10][20];
int i, j:

for (j =0; j <20; j++)


for (i =0; i <10; i++)
//Statement is missing here

What is the missing statement?

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?

A. for (i =0; i < 5; i++)


sum = sum + alpha [i][2];
B. for (i =0; i < 50; i++)
sum = sum + alpha [i][2];
C. for (i =0; i < 5; i++)
sum = sum + alpha [2][i];
D. for (i =0; i < 50; i++)
sum = sum + alpha [2][i];

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;
}
}
}

A. It searches the table in row order for the first occurrence


of searchValue.
B. It searches the table in row order for the last occurrence
of searchValue.
C. It searches the table in column order for the first
occurrence of searchValue.
D. It searches the table in column order for the last
occurrence of searchValue.

18) Why is it a good idea to seed a random number generator?


A. Without being seeded, you won't get random numbers
B. Without being seeded, you will always get the same
sequence of random numbers
C. To confuse the students trying to write it

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

21) After execution of the program fragment


int table [3][3];
int i, j;
for (i =0; i < 3; i++)
for (j = 0; j < 3; j++)
table [i][j] = i + 2*j;
what are the contents of the table array?
A. 0 2 4 C. 0 2 4
1 3 5 1 3 5
2 4 6 0 0 0
B. 0 1 2 D. 0 1 0
2 3 4 2 3 0
4 5 6 4 5 0

22) What's printed?


#include <iostream>
using namespace std;

int sum (int pt [ ], int n)


{
int temp = 0;
for (int i = 0; i < n; i++) {
temp += pt[i];
}
return temp;
}

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

23) Assume you have the following declarations:


enum StudentType {MARY, JANE, BILL};
StudentType oneStudent;

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";

24) Assume you have the following declarations:


enum Days {SUN, MON, TUE, WED, THU, FRI, SAT};
Days someDay;
Days twoBefore;

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

26) Given the declarations


struct RecType1
{
int length;
double width;
};
RecType1 myRec;
RecType2 yourRec;

which of the following assignment statements is valid?

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;

27) Which of the following assignment statements is valid?

A. myDisk.capacity = 1.44;
B. myDisk.brand = "Memorex";
C. myDisk.BrandInfo.company = "Memorex";
D. A and C above
E. none of the above

28) What is the datatype of myDisk.brand.company?


A. char
B. double
C. string
D. BrandInfo
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];

29) Which of the following expressions is valid?


A. partsList[72].supplier[4].idNumber
B. partsList[72].supplier.name[4]
C. partsList[72].partName[4]
D. A and B above
E. B and C above

30) What is the type of partsList[1].supplier.name[1]?


A. string
B. PartType
C. char
D. int
E. SupplierType

31) What does the following print?


class Complex {
public:
double re, im;
};
Complex x, y;
x.re = 4.0;
x.im = 5.0;
y = x;
x.re = 5.0;
cout << y.re << endl;

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.

Consider the class declaration

class SomeClass
{
public:
void foo();
private:
int m;
int n;
};

and client code

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?

A. Both structs and classes can have member functions.


B. Both structs and classes can have public and private
members.
C. By default, members of structs and classes are publiC.
D. Members of structs and classes are selected by using dot
notation.
E. Aggregate assignment is permitted for both structs and
classes.

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
( ... )

37) What was the focus of Robert's usability study?


A. Usability of Virtual Machines
B. Anti-virus Software
C. Cryptographic Software
D. Usability of Social Networking Sites
E. Usability of Chat Clients

38) A pre-processor directive is


A. indicted by a # and only the compiler pays attention to the
code

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

You might also like