-
A program contains these two lines to read integer input
values:
int x, y, z, w;
cin >> x >> y;
cin >> z >> w;
The user types two input lines:
5 10 15
20 25 30
What values are read into z and w?
- 5 and 10
- 10 and 15
- 15 and 20
- 20 and 25
- 25 and 30
next prev start
-
Which of the following would NOT correctly open an input file
named "infile.txt" located in the same directory as the
program?
-
#include <iostream>
using namespace std;
int main()
{
ifstream ifs;
ifs.open("infile.txt");
...
}
-
#include <fstream>
using namespace std;
int main()
{
ifstream ifs;
ifs = open("infile.txt");
...
}
-
#include <iostream>
using namespace std;
int main()
{
ifstream ifs;
ifs.open("infile.txt");
...
}
-
#include <fstream>
using namespace std;
int main()
{
ifstream ifs;
ifs.open("infile.txt");
...
}
-
Which of the following would output a double value x with 3
digits to the right of the decimal?
double x = 1234.56789;
-
cout.setf(ios::scientific);
cout.precision(3);
cout << x << endl;
-
cout.setf(ios::scientific);
cout.precision(9);
cout << x << endl;
-
cout.setf(ios::fixed);
cout.precision(3);
cout << x << endl;
-
cout.setf(ios::fixed);
cout.precision(9);
cout << x << endl;
-
What is the output?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int x = 5;
int y = 10;
bool b = x > y;
cout << boolalpha b << endl;
}
- 0
- 1
- 5
- false
- true
-
Which of the following is a correct statement about
functions f and g?
1 double f(int x);
2 int g(int x) {
3 if (x % 2 == 0) {
4 return x/2;
5 } else {
6 return 3*x + 1;
7 }
8 }
9
10 int main()
11 {
12 double z = f(5);
13 int w = g(5);
14 }
- Line 12 is a compile error since f is not declared.
- Line 12 is a compile error since f is not defined
- Line 13 is a compile error since g is not declared.
- Line 13 is a compile error since g is not defined
- Neither line 12 nor line 13 is a compile error.
-
Which assignment(s) will cause a compiler error?
1 char x = 'a';
2 int y = 5;
3 float w = 2.5F;
4 double z = 3.145;
5
6 z = w;
7 z = y;
8 z = x;
9 w = z;
10 y = z;
11 x = z;
- lines 9 - 11, only
- line 11 only
- line 10 and 11 only
- None will cause an error
-
What are the values of a, b, and c after the call to f
below?
int f(int& x, int y)
{
x++;
y++;
return x + y;
}
int main()
{
int a = 5;
int b = 10;
int c = f(a,b);
}
- a = 5, b = 10, c = 17
- a = 5, b = 11, c = 17
- a = 6, b = 10, c = 17
- a = 6, b = 11, c = 17
-
What are the values of a[3], *p, *p + 1 and *(p+1) ?
int a[10] = {2,4,6};
int *p = a;
- a[3] is 0, *p is 2, *p + 1 is 3, *(p+1) is 4
- a[3] is 6, *p is 2, *p + 1 is 3, *(p+1) is 4
- a[3] is 0, *p is 2, *p + 1 is 4, *(p+1) is 4
- a[3] is 6, *p is 2, *p + 1 is 4, *(p+1) is 4
- a[3] is 0, *p is 2, *p + 1 is 3, *(p+1) is 6
- a[3] is 6, *p is 2, *p + 1 is 4, *(p+1) is 6
-
What is the output of the following code fragment?
int b[] = {10,11,12,13,14,15};
int *p;
p = &a[2];
for(int k = 0; k < 3; k++) {
cout << p[k] << " ";
}
- 10 11 12
- 11 12 13
- 12 13 14
- 13 14 15
-
What is strlen(a)?
char a[5] = {'A', 'B', '\0', 'C', 'D', '\0'};
- 6
- 5
- 4
- 3
- 2
- Run time error
-
What is int n = strcmp(s, t)?
char s[] = "Hello";
char t[] = "Help";
- n > 0
- n == 0
- n < 0
-
What is the value of s < t?
#include <string>
using namespace std;
int main()
{
string s = "Hello";
string t = "Help";
...
}
- true
- false
- Compiler Error
-
A class Pair has a public member, int
getX(). If p is a pointer to a Pair instance, which
of the following are correct way(s) to call this member function
using the pointer p and assign the return
value to n?
Pair m;
int n;
Pair *p;
p = &m;
n = *p.getX(); // (ans1)
n = (*p).getX(); // (ans2)
n = p.getX(); // (ans3)
n = p->getX(); // (ans4)
- only ans1 and ans4
- only ans2 and ans4
- only ans1
- only ans2
- only ans3
- only ans4
-
A class Date is defined below. Which line
has a syntax error?
1 class Date
2 {
3 public:
4 int y;
5 int m;
6 int d;
7 Date();
8 Date(int yr, int mth, int day);
9 }
- line 1
- line 3
- line 7
- line 8
- line 9
-
Which data members of the class A below are
private?
1 class A
2 {
3 int w;
4 public:
5 int x;
6 private:
7 int y;
8 int z;
9 };
- only y
- only y and z
- only w and y
- all except x
-
For the class B defined below,
which lines in main will cause a compiler error?
1 class B
2 {
3 private:
4 int x;
5 public:
6 int y;
7 int z;
8 };
9
10 int main()
11 {
12 B b;
13 b.x = 5;
14 x = 5;
15 b.y = 10;
16 b.z = 15;
17
18 }
- line 13 only
- line 14 only
- line 13 and line 14 only
- all lines 13 - 16
-
Which one of the following is false?
- The purpose of a class constructor is to initialize the data
members of the class.
- The return type of a constructor must be void.
- The name of a class constructor must be the same as the class name.
- A class can have more than one constructor.
-
Here is a class Quad. It has two private members of type
Pair. The only public constructor in Pair takes two
integer parameters. There is no no-argument
constructor. Here are two candidate ways of writing a
constructor for Quad. Are both candidates correct?
class Quad
{
Pair pr1;
Pair pr2;
public:
Quad(int x1, int y1, int x2, int y2);
...
};
// Candidate A
Quad::Quad(int x1, int y1, int x2, int y2)
: pr1(x1, y1), pr2(x2, y2)
{ }
// Candidate B
Quad::Quad(int x1, int y1, int x2, int y2)
{
pr1 = Pair(x1, y1);
pr2 = Pair(x2, y2);
}
- Either A or B would be correct.
- Only A would be correct.
- Only B would be correct.
- Neither A or B would be correct.
-
Suppose the Quad class had a static Pair member named zero
which should be initialized with both integers being 0.
Which of the following would be a correct way of
initializing this static member?
- In a no-argument Quad() constructor, zero =
Pair(0,0);
- Include zero(0,0) in the initialization list of each constructor.
zero(0,0)
- Inside the class defintion (Quad.h) include an initialization with the
declaration of zero:
class Quad
{
static Pair zero(0,0);
...
};
-
Outside the Quad definition (in Quad.cpp) write,
Pair Quad::zero(0,0);
-
Outside the Quad definition (in Quad.cpp) write,
static Pair Quad::zero(0,0);
-
For the class Employee definition and main
routine below, what is the output?
1 class Employee
2 {
3 char nm[20];
4 int id;
5 public:
6 Employee()
7 {
8 strcpy(nm, "missing");
9 id = 999999;
10
11 }
12 Employee(char name[], int empId)
13 {
14 strcpy(nm, name);
15 id = empId;
16 }
17 int getId() { return id; }
18 const char * getName() const { return nm; }
19 };
20
21 int main()
22 {
23 Employee e1, e2;
24 Employee e3("Al", 555111);
25
26 e2 = Employee("Joan", 561222);
27 cout << e1.getId() << " "
28 << e2.getId() << " "
29 << e3.getId() << " " << endl;
30
31 return 0;
32
33 }
- 999999 999999 555111
- 999999 561222 561222
- 561222 555111
- 999999 561222 555111
-
The class definition of Employee shown above is
revised to show only the member function declarations (not their
definitions).
1 class Employee
2 {
3 char nm[20];
4 int id;
5 public:
6 Employee();
7 Employee(char name[], int empId);
8 int getId() const;
9 const char * getName() const;
10 };
File Employee.cpp should contain the
definitions of the member functions. In that file, the definition of
getId() should be:
-
int getId() const { return id; }
-
int Employee.getId() const { return id; }
-
Employee::int getId() const { return id; }
-
int Employee::getId() const { return id; }
-
If A is a class and an array is declared
by:
A arr[10];
which one of the following is false?
- class A must have a have a default constructor.
- The following code will swap the elements in arr[i] and arr[j].
int tmp;
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
- A function
f returns an int. The function
f also has two parameters: an array
of elements of type A and the number of elements in the array. The
function f could be correctly declared like this:
int f(A a[], int n);
- If
A is a class and a and
b are variables of type A,
then the assignment: a = b; will copy each data member of
b into the corresponding data member of a.
-
A Date is given as
class Date
{
int year;
int month;
int day;
public:
Date();
Date(int m, int d, int y);
int getYear() const;
int getMonth() const;
int getDay() const;
};
If d1 is declared to be of type Date in main:
int main()
{
Date d1;
...
}
which one of the following is a legal assignment?
- d1.year = 2006;
- d1 = Date(2006);
- d1 = (2,9,2006);
- d1 = Date(2,9,2006);
-
Referring to the Date class above, which
of the following lines in the main
function is an incorrect way to call member function
getYear()of the Date
class?
1 int main()
2 {
3 Date d1, d2;
4 Date d3(2,5,2006);
5
6 cout << Date.getYear() << endl;
7 cout << d1.getYear() << endl;
8 cout << d2.getYear() << endl;
9 cout << d3.getYear() << endl;
10
11 }
- Line 6
- Line 7
- Line 8
- Line 9
-
Which of the following best describes the role of class
constructors?
- The purpose of a class constructor is to allocate memory
for values of that class type.
- The purpose of a class constructor is to initialize
exactly one data member of the class.
- The purpose of a constructor for a class is to
initialize the data members of a class value just after
memory is allocated for the value.
- The purpose of a constructor for a class is to allocate
memory for the data members of a class value and to initialize them.
-
(Default parameter values)
The function g below is called three
times as shown. What are the three return values?
1 int g(int x, int y = 5, int z = 10);
2
3 int g(int x, int y, int z)
4 {
5 return x * y * z;
6 }
7
8 int main()
9 {
10 cout << g(1) << " "
11 << g(1,2) << " "
12 << g(1,2,3) << " ";
13
14 return 0;
15 }
- No output, the calls at lines 10 and 11 are illegal
- 1 2 6
- 50 50 50
- 1 20 50
- 50 20 6
- 50 20 1
-
Assertion: If a parameter of a function has a default
value, all following parameters must also provide a default
value.
- True
- False
-
A class M has only one constructor:
class M
{
...
public:
M(int x);
...
};
Which of the following declarations in
main is legal?
M a;
M b[5];
M c(5);
- Only declarations of
a and c
- Only declarations of
b and c
- Only the delcaration of
c
- All three delcarations are legal
- Suppose the single constructor in
class M is
modified like this:
class M
{
...
public:
M(int x = 1);
...
};
Now which of the following declarations in
main is legal?
M a;
M b[5];
M c(5);
- Only declarations of
a and c
- Only declarations of
b and c
- Only the delcaration of
c
- All three delcarations are legal
-
The following declaration of an Emp class
is in file Emp.h, its implementation is in the file Emp.cpp.
1 class Emp
2 {
3 static int nextid;
4 static int numEmps;
5 int id;
6 public:
7 Emp();
8 int getId() const;
9 static int numCreated();
10 };
The nextid variable should be initialized
exactly once to the value 1001 for the whole program no matter how
many Emp values are created. Each time an Emp value is created the
value of nextid should be used to initialize that Emp value's id. Then
nextid should be incremented.
The numEmps variable should be initialized to 0
exactly once for the whole program and should be incremented each time
an Emp value is created.
Which one of the following is a correct statement?
- All three variables nextid, numEmps and id should be
initialized in the constructor Emp in the Emp.cpp file like
this:
Emp::Emp()
{
numEmps = 0;
nextid = 1001;
id = nextid;
numEmps++;
nextid++;
}
- The id variable should be initialized in the constructor Emp in
the Emp.cpp file; nextid and numEmps should be
initialized in the Emp.h file but after the Emp class like
this:
Emp::Emp()
{
id = nextid++;
numEmps++;
}
class Emp
{
...
};
static int Emp::nextid = 1001;
static int Emp::numEmps = 0;
- The id variable should be initialized in the
constructor Emp in the Emp.cpp file; nextid and
numEmps should be initialized in the Emp.cpp file
outside any function like this:
Emp::Emp()
{
id = nextid++;
numEmps++;
}
static int nextid = 1001;
static int numEmps = 0;
- The id variable should be initialized in the
constructor Emp in the Emp.cpp file; nextid and
numEmps should be initialized in the Emp.cpp file
outside any function like this:
Emp::Emp()
{
id = nextid++;
numEmps++;
}
static int Emp::nextid = 1001;
static int Emp::numEmps = 0;
- The id variable should be initialized in the
constructor Emp in the Emp.cpp file; nextid and
numEmps should be initialized in the Emp.cpp file
outside any function like this:
Emp::Emp()
{
id = nextid++;
numEmps++;
}
int Emp::nextid = 1001;
int Emp::numEmps = 0;
-
Consider the statements:
(i) The getId() member function of Emp can access all three data
members: id and the static data members nextid and numEmps.
(ii) The getId() member function of Emp can access only the
non-static data member id.
(iii) The numCreated static member function of Emp can access all
three data members: id and the static members nextid and numEmps.
(iv) The numCreated static member function of Emp can access only the
static data members: nextid and numEmps.
- (i) and (iii) are true
- (i) and (iv) are true
- (ii) and (iii) are true
- (ii) and (iv) are true
-
Calling the static numCreated member function. Which
one of the assertions below is true:
1 int main()
2 {
3 Emp e1, e2;
4
5 cout << Emp::numCreated() << endl;
6 cout << e1.numCreated() << endl;
7 ...
8 }
- Function call at line 5 is legal, but line 6 is illegal
- Function call at line 6 is legal, but line 5 is illegal
- Function call at line 5 and at line 6 are both legal.
-
For the Emp class above, assuming the static variables
are initialized and maintained as in problem xx, what is the
output of the following program?
int main()
{
Emp e1, e2, e3;
cout << "Employee: " << e1.getId() << "; "
<< "Employee: " << e3.getId() << endl;
return 0;
}
- Employee: 1001; Employee: 1001
- Employee: 1001; Employee: 1002
- Employee: 1001; Employee: 1003
- Employee: 1002; Employee: 1002
- Employee: 1003; Employee: 1003
-
Consider the assetion:
Here are 3 candidates for how to write the static member function
numCreated in the Emp.cpp file:
int Emp::numCreated()
{
return ->numEmps;
}
int Emp::numCreated()
{
return numEmps;
}
static int Emp::numCreated()
{
return numEmps;
}
- Only Version 1 is legal
- Only Version 2 is legal
- Only Versions 1 and 2 are legal
- Only Version 3 is legal
-
This question and following ones below refer to the class mstring which is
intended to define a string type that grows dynamically. It does
this by having a private data member that is a dynamic array of
char that is null terminated. The len data member keeps
track of the number of characters stored not including the null
byte and capacity + 1 is the actual size of the array.
1 class mstring
2 {
3 char *str;
4 int len;
5 public:
6 mstring();
7 mstring(const char *s);
8 mstring(const mstring* other);
9 ...
10 ~mstring();
11 };
Which is the copy constructor?
- line 6
- line 7
- line 8
- line 7 and line 8
- There is no copy constructor.
- True or false. The constructor at line 8 could also have
been written to have its argument passed by value:
mstring(mstring other);
- True
- False
-
A copy constructor is used at which lines of the
following code:
1 void f(const mstring& w);
2 void g(mstring s);
3 int main()
4 {
5 mstring a("Hello");
6 mstring b(a);
7 mstring c;
8
9 c = a;
10
11 f(a);
12 g(a);
13
14 return 0;
15 }
- lines 5, 6, and 9
- lines 6 and 9
- lines 6, 9, 11 and 12
- lines 6 and 11
- lines 6 and 12
- lines 9 and 11
- lines 9 and 12
next prev start
-
When does the destructor for mstring execute for the code below?
1 void f(const mstring& w)
2 {
3 cout << w << endl;
4 }
5
6 void g(mstring s)
7 {
8 cout << s << endl;
9 }
10
11 void h()
12 {
13 mstring a("Hello");
14 mstring b("World");
15 f(a);
16 g(b);
17
18 }
19 int main()
20 {
21 h();
22 return 0;
23 }
- once when f returns; once when g returns; and once when h
returns.
- once when f returns; once when g returns; and twice when h
returns.
- once when f returns; and once when h
returns.
- once when g returns; and once when h
returns.
- once when f returns; and twice when h
returns.
- once when g returns; and twice when h
returns.
-
When does the destructor for mstring execute in the code below:
1 mstring* h()
2 {
3 mstring *pa;
4 pa = new mstring("Hello");
5 return pa;
6
7 }
8 int main()
9 {
10 mstring *ps;
11
12 ps = h();
13 cout << *ps << endl;
14 delete ps;
15 return 0;
16 }
- When h returns and at line 12
- When h returns and at line 14
- At line 12
- At line 14
- When main returns
-
For the Emp class below, which one of the following is not
syntactically correct implementation of the constructor?
1 class Emp
2 {
3 string fname;
4 string lname;
5 double salary;
6 public:
7 Emp(const string& fn, const string& ln, double sal);
8 string getFname();
9 string getLname();
10 double getSalary();
11 void setSalary(double sal);
12 void incrSalary(double sal);
13 };
-
Emp::Emp(const string& fn, const string& ln, double sal)
: fname(fn), lname(ln), salary(sal) {}
-
Emp::Emp(const string& fn, const string& ln, double sal)
: fn(fname), ln(lname), sal(salary) {}
-
Emp::Emp(const string& fn, const string& ln, double sal)
: salary(sal)
{
fname = fn;
lname = ln;
}
-
Emp::Emp(const string& fn, const string& ln, double sal)
{
fname = fn;
lname = ln;
salary = sal;
}