Upload
tran-thanh-tuan
View
223
Download
0
Embed Size (px)
Citation preview
7/27/2019 Inclass Test
http://slidepdf.com/reader/full/inclass-test 1/14
Page 1
Q 1. How many values can be returned by a C++ function?
1
Infinity
0
None of the Above
Q 2. Virtual functions are defined in
Derived class
Base class
Both A and B
None of the Above
Q 3. cin in C++ program uses the operator
>>
<
<<
>
Q 4. Which of the following denotes advantages of operator overloading?
Operator not limited to operate only with primitive Data Type
Extensibility
Both A and B
None of the Above
Q 5. The friend function of a class in C++ can access
Private members of the class
protected members of the class
Both A and B
None of the Above
Q 6. When dynamically allocated memory is lost to the C++ program then
warning occurs
memory leak occurs
The program does not executes successfully
None of the above
7/27/2019 Inclass Test
http://slidepdf.com/reader/full/inclass-test 2/14
Page 2
Q 7. The operator that denotes address of a variable in C++ program is
*
%
$
&
Q 8. The overriding method must have the which of the followingsame as that of the method in the super class definition
same identifier
same signature
Both A and B
None of the Above
Q 9. Which of the following is used in C++ to create a copy of an object?
Assignment operator
Copy constructor
Both A) and B)
None of the Above
Q 10. Which of the following is used to group a set of global classes,
objects and functions under a name?
Area named
Namearea
Namespaces
None of the above
Q 11. Inheritance in C++ have default access specifier as
Private
Public
Protected
None of the Above
Q 12. The members of a class are by default
Private
Public
Protected
None of the Above
Q 13.
7/27/2019 Inclass Test
http://slidepdf.com/reader/full/inclass-test 3/14
Page 3
// All the necessary libraries have been included
// Consider the following code:
void swap(int& a, int b)
{
long temp;
temp=a;
a=b;
b=temp;
}
// If we have int x = 5 and y = 3,
// what are values of x and y after swap(x,y) being called?
x=3, y=3
x=5, y=3
x=3, y=5
x=5, y=5
Q 14.
// All the necessary libraries have been included
// Consider the following code:
void g(int& a) {
a = 13 ;
}
int f() {
int a = 42 ;
g( a ) ;
return a ;
}
// What value is returned if f() is called?
13
42
0
There is a compile error in the code
Q 15.
void f() {
7/27/2019 Inclass Test
http://slidepdf.com/reader/full/inclass-test 4/14
Page 4
int *p = new(nothrow) int ;
if ( p!=0 ) {
*p = 99 ;
delete p ;
cout << *p << endl ;
}
}
// What value is printed if f() is called?
Unknown value
99
0
The code has a compile error
Q 16.
// All the necessary libraries have been included
// Consider the following code:
void f() {
int *p = new(nothrow) int ;
if ( p!=0 ) {
*p = 99 ;
delete p ;
p = NULL;
cout << *p << endl ;
}
}
// What value is printed if f() is called?
The program will crash
99
0
NULL
Q 17.
// All the necessary libraries have been included
// Consider the following code:
void f() {
7/27/2019 Inclass Test
http://slidepdf.com/reader/full/inclass-test 5/14
Page 5
int x = 10;
int* const p = &x;
*p = 5;
cout << *p << endl;
}
// What value is printed if f() is called?
5
10
0
The code has a compile error.
Q 18.
// All the necessary libraries have been included
// Consider the following code:
void f() {
int x = 10;
int y = 20;
int* const p = &x;
*p = 5;
p = &y;
cout << *p << endl;
}
// What value is printed if f() is called?
The code has a compile error.
5
20
0
Q 19.
// All the necessary libraries have been included
// Consider the following code:
void f() {
const int x = 10;
const int* p = &x;
*p = 5;
ko thay doi bien
7/27/2019 Inclass Test
http://slidepdf.com/reader/full/inclass-test 6/14
Page 6
cout << *p << endl;
}
// What value is printed if f() is called?
The code has a compile error.
5
10
0
Q 20.
// All the necessary libraries have been included
// Consider the following code:
class A {
int x, y;
public:
void fa(int a) {
cout << a + x + y;
}
};
class B : public A {
public:
void fb() {
A a;
a.x = 3;
a.y = 5;
a.fa(10);
}
};
// What value is printed if fb() method of class B is called?
The code has a compile error
18
10
0
Q 21.
private
7/27/2019 Inclass Test
http://slidepdf.com/reader/full/inclass-test 7/14
Page 7
// All the necessary libraries have been included
// Consider the following code:
class A {
int x, y;
public:
void fa(int a) {
cout << a + x + y;
}
friend class B;
};
class B : A {
public:
void fb() {
A a;
a.x = 3;
a.y = 5;
a.fa(10);
}
};
// What value is printed if fb() method of class B is called?
18
10
0
The code has a compile error.
Q 22.
// All the necessary libraries have been included
// Consider the following code:
class A {
protected:
int x, y;
public:
void fa(int a) {
cout << a + x + y;
7/27/2019 Inclass Test
http://slidepdf.com/reader/full/inclass-test 8/14
Page 8
}
};
class B : public A {
public:
void fb() {
A a;
a.x = 3;
a.y = 5;
a.fa(10);
}
};
// What value is printed if fb() method of class B is called?
The code has a compile error.
10
0
18
Q 23.
// All the necessary libraries have been included
// Consider the following code:
class A {
int x, y;
public:
void fa(int a, int b) {
cout << a + b;
}
void fa(double a, double b) {
cout << a + b;
}
void fa(int a, double b) {
cout << a + b;
}
};
// What value is printed if the following code is executed?
7/27/2019 Inclass Test
http://slidepdf.com/reader/full/inclass-test 9/14
Page 9
// A a; a.fa(2, 3.2);
5.2
5
0.2
The code has a compile error.
Q 24.
// All the necessary libraries have been included
// Consider the following code:
class A {
int x, y;
public:
void f() {
cout << "father";
}
};
class B : public A {
public:
void f() {
cout << "son";
}
};
// What value is printed if the following code is executed?
// A* a = new B; a->f();
Father
Son
Fatherson
The code has a compile error
Q 25.
// All the necessary libraries have been included
// Consider the following code:
class A {
int x, y;
public:
7/27/2019 Inclass Test
http://slidepdf.com/reader/full/inclass-test 10/14
Page 10
void virtual f() {
cout << "father";
}
};
class B : public A {
public:
void virtual f() {
cout << "son";
}
};
// What value is printed if the following code is executed?
// A* a = new B; a->f();
Son
Father
Sonfather
The code has a compile error.
Q 26.
// All the necessary libraries have been included
// Consider the following code:
class A {
public:
int age;
char * name;
void print() const {
++age;
cout << age << ", " << name << endl;
}
};
// What value is printed if the following code is executed?
// A a1;
// a1.age = 20; a1.name = "peter";
// a1.print();
The code has a compile error.
7/27/2019 Inclass Test
http://slidepdf.com/reader/full/inclass-test 11/14
Page 11
21, peter
20, peter
22, peter
Q 27.
// All the necessary libraries have been included
// Consider the following code:
#define SIZE 5
class A {
public:
void increase(const int* grade) const {
for (int i = 0; i < SIZE; i++) {
grade[i] +=1;
}
}
void sum() {
int sum = 0;
int grade[] = {1,2,3,4,5};
increase(grade);
for (int i = 0; i < SIZE; i++) {
sum += grade[i];
}
cout << sum/SIZE << endl;
}
};
// What value is printed if the following code is executed?
// A a1; a1.sum();
There is a compile error in the code.
4
3
5
7/27/2019 Inclass Test
http://slidepdf.com/reader/full/inclass-test 12/14
Page 12
Q 28
class Person{ public:
Person(int p1){
cout << this->p1;}
};
class Student : public Person{ public:
Student(int i){
cout << this->i;}
};
int _tmain(int argc, _TCHAR* argv[])
{Person(123);Student(111);getch();return 0;
}//What is the output??
123, 111
111, 123
The program has compiling errors.
The program will crash when executing
Q 29: What are the roles of constructor?
Q 30: If class A inherits from class B, which inherits from class C, what is the order of calling constructors and destructors?
Q 31: class Student has 2 private members: int age, string address. The Student class has a constructor that takes age and addrarguments.
Implement an overloaded assignment operator that allows comparing two objects of the Student class. The two student’s objeare equal if both their age and address are equal.
7/27/2019 Inclass Test
http://slidepdf.com/reader/full/inclass-test 13/14
Page 13
Q 32 Find errors if any
Q 33 Find errors if any
Q 34 Find errors if any