C++ Code Snippets For Competitive Exams page 1

C++ Code Snippets For Competitive Exams

1. What will be the output if the input provided is 5?

#include < iostream.h > 
#include < conio.h > 
int a = 1000; 
class Test 
{ 
   int a; 
   public: 
  Test() 
  { 
     a = 0; 
  } 
  void get_a() 
 { 
    int a; 
    cout << “Enter Value of A :”; 
    cin >> a; 
  } 
  void put_a() 
  { 
     cout << endl << a << “t” << ::a; 
  } 

}; 
void main() 
{ 
   Test t1; 
   t1.get_a(); 
   t1.put_a(); 
} 




Show Answer ?

2. What will be the output?

class d 
{ 
    int day, mon; 
    public: 
    d() {  cout << “nconst”;  } 
  ~d() { cout << “ndest”; } 

}; 
void main() 
{ 
   d *d1 = new d; 
   cout << “Process”; 
} 




Show Answer ?

3. Referring to the below code, why is the keyword virtual added before the destructor ‘computer’?

class computer 
{ 
    public: computer(){ } 
   virtual ~computer() { } 
}; 
class memory: public computer 
{ 
     public: 
     memory() { } 
   ~memory() { } 
}; 

void main() 
{ 
} 




Show Answer ?

4. What will happen after compilation?

class test 
{ 
    int a; static int cnt; 
    public: 
    test() 
    { 
        a=0; 
        cout << ++cnt; 
    } 
    test( int p) 
    { 
        a=p; cout << ++cnt; 
    } 
    ~test() 
    { 
        cout << cnt–; 
    } 
}; 
int test::cnt; 
void main() 
{ 
    test ob,ob1(10); 
    ob = test(); 
    test(); 
}




Show Answer ?

5. What is correct? ( assuming there is no error. )

class test 
{ 
    int a; 
    static int cnt; 
    public: 
    test() 
    { 
        a=0; 
        cout << ++cnt; 
    } 
    test( int p) 
    { 
        a=p; 
        cout << ++cnt; 
    } 
    ~test() 
    { 
        cout << cnt–; 
    } 
}; 
int test::cnt; 
void main() 
{ 
    test ob,ob1(10); 
    ob = test(); 
    test(); 
} 
 




Show Answer ?

6. What will be the output?

#include < iostream.h > 
#include < conio.h > 
class test 
{ 
     int a; 
     public: 
     test(){ a = 10; } 
     test(int p){ a = p; } 
     test(char x)
     { 
          if ( x = = ‘i’ || x = = ‘I’ ) 
               test(59); 
          else 
               test(); 
     } 
          void display()
     { 
          cout << endl << a; } 
     }; 
void main() 
{ 
     clrscr(); 
     test t1,t2(100),t3(‘i’); 
     t1.display(); 
     t2.display(); 
     t3.display(); 
}




Show Answer ?

7. What will be the output of the following programme?

#include < iostream.h > 
#include < conio.h > 
class test 
{ 
     int a; 
     public: 
     ~test() { ; } 
     test(){   a = 10;   } 
     test(int p){ a = p; } 
     virtual void display() { cout << endl << a; } 
}; 
class subtest:public test 
{ 
     int b; 
     public: 
     subtest() {    b = 20; } 
     subtest(int p,int q):test(p) {     b = q;    } 
     void display() { cout << endl << a << “ ” << b; } 
}; 

void main() 
{ 
     test *t; 
     t = new test(100); 
     t->display()’ 
     t = new subtest(20,40); 
     t->display(); 
     getch(); 
} 




Show Answer ?

8. What will be the output?

#include < iostream.h > 
#include < conio.h > 
#include < stdio.h > 

class test 
{ 
     protected: 
     int a; 
     public: 
     test()    { a = 10; } 
     virtual void display(){ cout << endl << a; } 
}; 

class subtest:private test 
{ 
     int b; 
     public: 
     subtest() {    b = 20; } 
     void display() { cout << endl << a << " " << b;   } 
}; 

void main() 
{ 
     clrscr(); 
     test *t; 
     t = new test; 
     t->display(); 
     t = new subtest; 
     t->display(); 
     getch(); 
} 




Show Answer ?

9. Find the out put?

void main() 
{ 
    cout << setfill(‘/’); 
    cout.width(5); 
    cout << “abcd”; 
    cout.width(5) << 980; 
} 




Show Answer ?

10. What will be the output?

class data 
{ 
     public: 
     int val; 
     data() 
     { 
          cout << endl << a; 
          val = a; 
     } 
     ~data () 
     { 
          cout << endl << a; 
     } 
}ob1(1),ob2(2); 
void main() 
{ 
     data ob3(3), ob4(4); 
}




Show Answer ?