Texas Previous Years Solved Sample Placement Papers
1. Can we declare a static function as virtual?
Answer: No. Static functions are not related to any object, while virtual functions depend on the specific object to determine which function to call. Hence, static functions cannot be declared as virtual.
2. Can a user-defined object be declared as a static data member of another class?
Answer: Yes. Example:
#include<iostream>
class test {
int i;
public:
test(int ii = 0) { i = ii; }
};
class sample {
static test s;
};
test sample::s(26); // Initializing the object `s`
3. What is forward referencing, and when should it be used?
Answer: Forward referencing declares a class or function before its definition to avoid compilation errors. Example:
class sample; // Forward declaration
class test {
public:
friend void fun(sample, test);
};
class sample {
public:
friend void fun(sample, test);
};
void fun(sample s, test t) {
// Implementation
}
4. What is the istream_withassign
class?
Answer: The istream_withassign
class is derived from istream
, adding overloaded assignment operators to make objects copyable. The base classes (istream
, ostream
, and iostream
) have private copy constructors and assignment operators, making them uncopyable.
5. How do I write a zero-argument manipulator that works like hex
?
Answer: Example:
#include<iostream>
using namespace std;
ostream& myhex(ostream &o) {
o.setf(ios::hex);
return o;
}
void main() {
cout << endl << myhex << 2000;
}
6. Why does the following code work without initializing p
?
const char *p;
p = "A const pointer";
cout << p;
Answer: The pointer p
is not constant, but the value it points to is. Hence, it can be assigned a string literal.
7. How do I refer to a class or function within a namespace?
Answer:
- Using Scope Resolution Operator:
namespace name1 { class sample1 {}; } name1::sample1 s1;
- Using the
using
keyword:using namespace name1; sample1 s1;
8. Can default values be provided while overloading binary operators?
Answer: No. Providing default arguments leads to ambiguity in operator usage.
sample operator+(sample a, sample b = sample(2, 3.5f)) {}
9. How do I convert one user-defined type to another?
Answer: Use a conversion function. Example:
class circle {
int radius;
public:
circle(int r = 0) : radius(r) {}
};
class rectangle {
int length, breadth;
public:
rectangle(int l, int b) : length(l), breadth(b) {}
operator circle() {
return circle(length);
}
};
void main() {
rectangle r(20, 10);
circle c = r; // Conversion occurs here
}
10. How to ensure only one instance of a class is created?
Answer: Implement a singleton design pattern. Example:
#include<iostream>
class sample {
static sample *ptr;
sample() {}
public:
static sample* create() {
if (ptr == NULL) ptr = new sample;
return ptr;
}
};
sample* sample::ptr = NULL;
void main() {
sample* a = sample::create();
sample* b = sample::create();
// Both `a` and `b` point to the same instance
}
11. How to implement get
and put
properties in a class?
Answer: Use __declspec(property)
. Example:
#include<iostream>
class sample {
int data;
public:
__declspec(property(put = set, get = get)) int x;
void set(int i) { data = (i < 0) ? 0 : i; }
int get() { return data; }
};
void main() {
sample a;
a.x = -99; // Calls `set` method
cout << a.x; // Calls `get` method
}