======================================================================================= Q-1: Write the output of the following: char *mychar; //points to memory location 1000 short *myshort; //points to memory location 2000 long *mylong; //points to memory location 3000 mychar++; ++myshort; mylong++; cout << mychar << myshort << mylong; ======================================================================================= Q-2: What is the value of y at the end of main? const int x = 5; int main(int argc, char** argv) { int x[x]; int y = sizeof(x)/sizeof(int); return 0; } ======================================================================================= Q-3: What will the line of code below print out? #include int main(int argc, char **argv) { std::cout << 25u - 50; return 0; } ======================================================================================= Q-4: What is the output of the following code snippet assuming user enters the side as 4? class square { public: double side; double area() { return(side*side); } }; int main(){ double area=0; square c1,c2; cout << "Enter the length of the square" << endl; cin >> c1.side; cout << "The area of the square is : " << c1.area() << endl; return(0); } ======================================================================================= Q-5: What is the output of the following program? #include using namespace std; int fun1(int p) { ++p; return p++; } int fun2(int &p) { ++p; return p++; } int main(void) { int a = 1, b, c; b = fun1(a); c = fun2(b); cout << a + b + c << endl; return 0; } ======================================================================================= Q-6: What is the output of the following program? #include using namespace std; char f1(char c) { return c == 'z' ? 'a' : c + 1; } char f2(char &c) { c = f1(c); return c; } int main(void) { char x = 'x'; cout << f2(x); cout << f2(x); cout << f2(x) << endl; return 0; } ======================================================================================= Q-7: What is the output of the following program? #include using namespace std; int main(void) { int *t[2] = { new int[2], new int[2] }; for(int i = 0; i < 4; i++) t[i % 2][i / 2] = i; cout << t[0][1] + t[1][0] << endl; delete [] t[0]; delete [] t[1]; return 0; } ======================================================================================= Q-8:: What happens when you attempt to compile and run the following code? #include #include #include using namespace std; int main(){ int t[] = {3,4,2,1,6,5,7,9,8,0}; vector v(t,t+10); multiset s1(v.begin(),v.end()); s1.insert(v.begin(),v.end()); pair::iterator,multiset::iterator> range; range = s1.equal_range(6); while (range.first != range.second){ cout << *range.first << " "; range.first++; } return 0; } ======================================================================================= Q-9:: What happens when you attempt to compile and run the following code: #include #include #include using namespace std; template struct Out { ostream &out; Out(ostream &o): out(o){} void operator()(const T &val){ cout << val << " "; } }; struct Sequence { int start; Sequence(int start):start(start){}; int operator()(){ return start++; } }; int main(){ vector v1(10); generate(v1.rbegin(),v1.rend(),Sequence(1)); rotate(v1.begin(), v1.begin()+1, v1.end()); for_each(v1.begin(), v1.end(), Out(cout)); cout << endl; return 0; } ======================================================================================= Q-10:: What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: one two three ? #include #include using namespace std; int main(){ string a; cin >> a; cout << a << endl; return 0; } ======================================================================================= Q-11: What is the problem with the following code? class A{ public: A() { } ~A() { } }; class B: public A{ public: B(): A() { } ~B() { } }; int main(void){ A* a = new B(); delete a; } ======================================================================================= Q-12: What is the output of the following program? struct A { int a; float b; }; struct B { int b; float a; }; struct C { A a; B b; }; int main(void) { C c1 = {1, 2, 3, 4}, c2 = {5, 6, 7, 8}; cout << c1.b.a + c2.a.b << endl; return 0; } ======================================================================================= Q-13: What is the output of the following program? int main(void) { int i = 1, j = 2; if(i > j && j > i) i++; if(i > j || j > i) j++; if(i | j) i++; if(i & j) j++; cout << i * j << endl; return 0; } ======================================================================================= Q-14: What is the result of compiling and running the following program. class Test { private : float data; public : void setData( int& value) { data = value;}; static float getData() { return data;}; Test( int value ) : data( value ) {}; }; int main(){ Test me = 10.5; cout << me.getData() << endl; } ======================================================================================= Q-15:What is the output of the following program? class abc { public: static int x; int i; abc() { i = ++x; } }; int abc::x; main() { abc m, n, p; cout << m.x << " " << m.i << endl; } ======================================================================================= Q-16: What will happen when you attempt to compile and run the following code? #include #include #include #include #include using namespace std; int main(){ int t[] = {3,4,2,1,0,3,4,1,2,0}; vector v(t, t+10); multimap m; for (vector::iterator i = v.begin(); i != v.end(); i++){ stringstream s; s << *i << *i; m.insert(pair(*i, s.str())); } pair::iterator, multimap::iterator> range; range = m.equal_range(2); for (multimap::iterator i = range.first; i != range.second; i++){ cout << i->first << " "; } return 0; } ======================================================================================= Q-17: What is the output of the following code snippet? class test { public: static int n; test () { n++; }; ~test () { n--; }; }; int test::n=0; int main () { test a; test b[5]; test * c = new test; cout << a.n << endl; delete c; cout << test::n << endl; return 0; } ======================================================================================= Q-18: What is the output of the following code? #include class A { public: A() {} ~A() { throw 42; } }; int main(int argc, const char * argv[]) { try { A a; throw 32; } catch(int a) { std::cout << a; } } ======================================================================================= Q-19: Given a 2D matrix of 0s and 1s, find the largest square that contains only 1s. Return its area. ======================================================================================= Q-20: Given an array of integers of size n, in which each element appears twice except one single element. Find the number that appears only once in O(n). ======================================================================================= Q-21: Trace the program and give the output for parts (a) through (d). #include using namespace std; class baseCL { public: baseCL(): dataBCL(3) {} virtual void output() { cout << dataBCL << " "; } protected: int dataBCL; }; class derivedCL: public baseCL { public: derivedCL(int n = 1): dataDCL(n) { dataBCL = n + 1; } void output() { baseCL::output(); cout << dataDCL << endl; } private: int dataDCL; }; int main() { baseCL *pb; derivedCL *pd1, *pd2; pb = new baseCL(); pb->output(); // part (a). Output is _______ cout << endl; pd1 = new derivedCL(8); pd2 = new derivedCL(); pd1->output(); // part (b) Output is _______ pd2->output(); // part (c) Output is _______ pb = pd1; pb->output(); // part (d) Output is _______ cout << endl; return 0; }