- Which of the following correctly declares an array
a
such that the initial value of a[2] is 10 and a[3] is 0?
-
int a[4] = {5,10,15};
-
int a[4] = {5,10,15,0};
-
int a[] = {1,5,10};
-
int a[4] = {1,5,10};
next prev start
- Which line in the code below has a correct declaration
of an array?
1 int main()
2 {
3 const int n = 50;
4 int m = 50;
5 double a[];
6 char b[n];
7 float c[m];
8 const int d[50];
9 ...
10 }
- line 5
- line 6
- line 7
- line 8
next prev start
- The function
computeSum
below is supposed to compute
and return the sum of the n
elements in the array
a
. What should cond at line 4 and expr
at line 5 be?
1 int computeSum(int a[], int n)
2 {
3 sum = 0;
4 for(int i = 0; /* cond */; i++) {
5 sum += /* exp */;
6 }
7 return sum;
8 }
- cond:
i <= n
and expr:
a[n]
- cond:
i < n
and expr:
a[n]
- cond:
i <= n
and expr:
a[i]
- cond:
i < n
and expr: a[i]
next prev start
- What is the output of the following?
1 int main()
2 {
3 int a[] = {10,12,14,16,18};
4
5 for(int i = 1; i < 5; i += 2) {
6 cout << a[i] << ",";
7 }
8 cout << endl;
9 return 0;
10 }
- 10,14,
- 12,16,
- 10,12,14,
- 10,14,18,
next prev start
-
An input file, "data.txt" contains the following:
1 2
3 4
5
Which of the following would correctly read all the
integers, store them in the array nums
and print "5
values were read and stored"?
-
1 int main()
2 {
3 int nums[50];
4 int n;
5 ifstream ifs("data.txt");
6 if (ifs.fail()) {
7 cout << "Unable to open input file data.txt" << endl;
8 exit(0);
9 }
10 n = 0;
11 while( !ifs.eof() ) {
12 ifs >> nums[n++];
13 }
14 cout << n << " values were read and stored." << endl;
15 return 0;
16 }
-
1 int main()
2 {
3 int nums[50];
4 int n;
5 ifstream ifs("data.txt");
6 if (ifs.fail()) {
7 cout << "Unable to open input file data.txt" << endl;
8 exit(0);
9 }
10 n = 0;
11 ifs >> nums[n];
12 while( !ifs.eof() ) {
13 n++;
14 ifs >> nums[n];
15 }
16 cout << n << " values were read and stored." << endl;
17 return 0;
18 }
-
1 int main()
2 {
3 int nums[50];
4 int n;
5 ifstream ifs("data.txt");
6 if (ifs.fail()) {
7 cout << "Unable to open input file data.txt" << endl;
8 exit(0);
9 }
10 n = 0;
11 ifs >> nums[n];
12 while( !ifs.eof() ) {
13 ifs >> nums[n];
14 n++;
15 }
16 cout << n << " values were read and stored." << endl;
17 return 0;
18 }
-
1 int main()
2 {
3 int nums[50];
4 int n;
5 ifstream ifs("data.txt");
6 if (ifs.fail()) {
7 cout << "Unable to open input file data.txt" << endl;
8 exit(0);
9 }
10 n = 0;
11 int tmp;
12 ifs >> tmp;
13 while( !ifs.eof() ) {
14 nums[n] = tmp;
15 ifs >> tmp;
16 }
17 cout << n << " values were read and stored." << endl;
18 return 0;
19 }
next prev start
-
A function to print the values in an array of n
doubles
is declared as:
void print(double a[], int n);
Which line in the code below has a correct call to this
print
function?
1 int main()
2 {
3 double a[5]= {1.0, 1.5, 2.0, 2.5, 3.0};
4 double b[5]= {1.0, 1.5, 2.0, 2.5, 3.0};
5 double c[5]= {1.0, 1.5, 2.0, 2.5, 3.0};
6 double d[5]= {1.0, 1.5, 2.0, 2.5, 3.0};
7
8 print(a[]);
9 print(b[5]);
10 print(c[], 5);
11 print(d, 5);
12 }
- line 8
- line 9
- line 10
- line 11
next prev start
-
How should the variable str
be declared?
1 int main()
2 {
3
4 char a[] = "Hello";
5 char b[] = "World";
6
7 strcpy(str, a);
8 strcat(str, " ");
9 strcat(str, b);
10 }
11
-
char str[];
-
char str[10];
-
char str[11];
-
char str[12];
next prev start
-
What is the output of the following code?
1 int main()
2 {
3 char str[] = "minimum";
4
5 str[3] = '\0';
6 cout << str << endl;
7 return 0;
8 }
- minimum
- min0mum
- min mum
- min
- mi
next prev start
-
Which declaration of str
would cause no
compilation error and would guarantee the output to be ABC
?
1 int main()
2 {
3
4
5 cout << a << endl;
6 return 0;
7 }
-
char str[3] = {'A', 'B', 'C'};
-
char str[4] = {'A', 'B', 'C' };
-
char str[3] = {'A', 'B', 'C', '\0'};
-
char str[] = { "A", "B", "C", 0};
next prev start
-
What is the output of this program?
1 int main()
2 {
3 char nm1[] = "cat";
4 char nm2[] = "cabinet";
5 char nm3[] = "cab";
6 char nm[20] = "catalogue";
7
8 if ( strcmp(nm1,nm2) < 0 ) {
9 if ( strcmp(nm2, nm3) < 0 ) {
10 strcpy(nm, nm3);
11 } else {
12 strcpy(nm, nm2);
13 }
14 } else {
15 if ( strcmp(nm1, nm3) < 0 ) {
16 strcpy(nm, nm3);
17 } else {
18 strcpy(nm, nm1);
19 }
20 }
21 cout << nm << endl;
22
23 return 0;
24 }
- cat
- cabinet
- cab
- catalogue
next prev start
-
The function computeBonus
is
overloaded as shown below.
1 double computeBonus(int val)
2 {
3 double result = val;
4 return 2*result/100.0;
5 }
6
7 double computeBonus(float val)
8 {
9 double result = val;
10 return 2*result;
11 }
12
13 int main()
14 {
15 int n = 12345;
16 double x = 12345.0;
17
18 cout << computeBonus(n) << ", " << computeBonus(x) << endl;
19 return 0;
20 }
Which of the following are correct?
- Output is: 246.9, 24690
- Output is: 24690, 24690
- Output is: 246.9, 246.9
- Output is: 24690, 246.9
- Compilation error: Overload resolution fails.
next prev start
-
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
next prev start
-
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
next prev start
-
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
next prev start
-
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.
next prev start
-
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() { 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
next prev start
-
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();
9 const char * getName();
10 };
File Employee.cpp
should contain the
definitions of the member functions. In that file, the definition of
getId()
should be:
-
int getId() { return id; }
-
int Employee.getId() { return id; }
-
Employee::int getId() { return id; }
-
int Employee::getId() { return id; }
next prev start
-
If A
is a class and an array is declared
by:
A arr[10];
which 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
.
next prev start
-
A Date
is given as
class Date
{
public:
int yr;
int mth;
int dy;
Date();
Date(int y, int m, int d);
};
Write a function
int cmp(const Date& d1, const Date&);
that
- returns a negative value if
Date d1
comes before d2
- returns 0 if
Date d1
is equal to d2
- returns a positive value if
Date d1
comes after
d2
-
Write a function:
bool arrayEquals(int a[], int b[], int n);
that returns true
if the first
n
values in array a
are equal
to the corresponding values in array b
.
-
Write a function:
double avg(int a[], int n);
that computes the average of the n
integer
values in the array a
.
-
Write a function:
int numGreater(int a[], int n, int value);
that returns how many of the n
numbers in the
array a
are > value
.
-
Given this Date class:
1 class Date
2 {
3 int yr;
4 int mth;
5 int d;
6 public:
7 Date()
8 {
9 yr = 2005;
10 mth = 1;
11 d = 1;
12 }
13 Date(int year, int month, int day)
14 {
15 yr = year;
16 mth = month;
17 d = day;
18 }
19 int getYear();
20 int getMonth();
21 int getDay();
22 };
write a declaration of a variable today
of type
Date
initialized with today's date.