Predict Output for C++ program

 Predict the output of the below set of programs

[1] What is the issue with below program, it prints " After x = 20" where it is expected to print x = 50.
Please fix it.
#include <iostream>
#include <thread>
using namespace std;
void fun(int x)
{
    cout<<"I am in thread function"<<endl;
    cout<<"x="<<x<<endl;
     x = 50;
}
int main() {
    int x =20;
    cout<<"Before x="<<x<<endl;
    std::thread thobj(fun, std::ref(x));
    cout<<"After x="<<x<<endl;
    thobj.join();
    return 0;
}

void foo()

{

    //...

    char* p = new char[ 5 ];

    p = "Hello";

    free( p );

    //...

}

Predict the output of the below program

void fun(int &x)
{
    cout<<"I am in thread function"<<endl;
    cout<<"x="<<x<<endl;
     x = 50;
}
int main() {
    int x =20;
    cout<<"Before x="<<x<<endl;
    std::thread thobj(fun, std::ref(x));
    cout<<"After x="<<x<<endl;
    thobj.join();
    return 0;
}

[c] Example for const_cat
int const & x
int & y = const_cast<int &>(x);

1. Predict the output of the below program

#include <iostream>
#include<thread>
using namespace std;
int fun(int* ptr)
{
    *ptr = *ptr + 10;
    return (*ptr);
}
int main(void)
{
    int val = 10;
    const int *ptr = &val;
    int *ptr1 = const_cast <int *>(ptr);
    fun(ptr1);
    cout << val;
    return 0;
}

2. Predict the output of the below program
int fun(int* ptr)
{
    *ptr = *ptr + 10;
    return (*ptr);
}
int main(void)
{
    const int val = 10;
    const int *ptr = &val;
    int *ptr1 = const_cast <int *>(ptr);
    fun(ptr1);
    cout << val;
    return 0;
}


Output: undefined where some compiler may return as 10


N.B: It it fine to modify a value which is not initially declared as const. For example, in the above program, if we remove const from declaration of val, the program will produce 20 as output.

3. What is the output of the below program ?
#include <iostream>
using namespace std;
  
int main(void)
{
    int a1 = 40;
    const int* b1 = &a1;
    char* c1 = const_cast <char *> (b1); 
    *c1 = 'A';
    return 0;
}

Ans: Compilation error

4. 
#include <iostream>
#include <typeinfo>
using namespace std;  
int main(void)
{
    int a1 = 40;
    const volatile int* b1 = &a1;
    cout << "typeid of b1 " << typeid(b1).name() << '\n';
    int* c1 = const_cast <int *> (b1);
    cout << "typeid of c1 " << typeid(c1).name() << '\n';
    return 0;
}

Output: typeid of b1 PVKi typeid of c1 Pi

N.B:
const_cast can also be used to cast away volatile attribute. For example, in the following program, the typeid of b1 is PVKi (pointer to a volatile and constant integer) and typeid of c1 is Pi (Pointer to integer)

[2] Predict the output of the below program

#include <iostream>
#include <thread>
using namespace std;
void threadCallback(int & x)
{
    x++;
    std::cout<<"Inside Thread x = "<<x<<std::endl;
}
int main()
{
    int x = 9;
    std::cout<<"In Main Thread : Before Thread Start x = "<<x<<std::endl;
    std::thread threadObj(threadCallback, std::ref(x));
    threadObj.join();
    std::cout<<"In Main Thread : After Thread Joins x = "<<x<<std::endl;
    return 0;
}

[3] 
void fun(int & x)
{
    x = 90;
    std::cout<<"Inside Thread x = "<<x<<std::endl;
}
int main()
{
    int x = 9;
    std::cout<<"In Main Thread : Before Thread Start x = "<<x<<std::endl;
    std::thread threadObj(fun, std::ref(x));
    threadObj.join();
    std::cout<<"In Main Thread : After Thread Joins x = "<<x<<std::endl;
    return 0;
}
[4]
#include <iostream>
#include<thread>
using namespace std;
  
class sample
{
  public:
  sample()
  {
      
  }
  void fun(int &x)
  {
      x++;
      cout<<x<<endl;
  }    
};
int main()
{
    sample sp;
    int x = 10;
    std::thread thObj(&sample::fun, &sp, std::ref(x));
    thObj.join();
    cout<<x<<endl;
}

Operating System/System Programming
Study C++ FAQ



Note: derived to base conversion will happen without any issue using dynamic cast
whereas base to derived conversion will return either NULL or std::bad_cast (in case of reference)



#include <iostream>
using namespace std;
class Base
{
public:
virtual void fun()
{
cout<<"A::fun()"<<endl;
}
};
class Derived:public Base
{
public:
void fun()
{
cout<<"B::fun()"<<endl;
}
};

int main()
{
    Base b;
    //From Base to Derived casting
    Derived *dptr = dynamic_cast<Derived*>(&b);
    if (dptr == NULL)
    {
        cout<<"NULL";
    }
    else
    {
        cout<<"Not NULL"<<endl;
    }
return 0;
}

o/p: 
NULL

Predict the output of the below program:-
#include <iostream>
#include<exception>
using namespace std;
class Base
{
    public:
virtual void fun()
{
cout<<"A::fun()"<<endl;
}
};
class Derived:public Base
{
public:
void fun()
{
cout<<"B::fun()"<<endl;
}
};
int main()
{
    Base b;
    //From Base to Derived casting
    try{
        Derived &dptr = dynamic_cast<Derived&>(b);
    }
    catch(std::exception &e)
    {
        cout<<e.what()<<endl;
    }
    return 0;
}

output:
std::bad_cast

Example 3:
// Online C++ compiler to run C++ program online
#include <iostream>
#include<exception>
using namespace std;
class Base
{
    public:
void fun()
{
cout<<"A::fun()"<<endl;
}
};
class Derived:public Base
{
public:
void fun()
{
cout<<"B::fun()"<<endl;
}
};
int main()
{
    Derived d;
    //Derived to base conversion
    Base *bp = dynamic_cast<Base*>(&d);
    bp->fun();
    return 0;
}

output:
A::fun()

Example 4:
class A {virtual void f();};
class B {virtual void f();};
void f() {
   A* pa = new A;
   B* pb = dynamic_cast<B*>(pa);   // fails at runtime, not safe;
   // B not derived from A
}

What is the output of the below program.
#include <iostream>
#include <map>
class B 
{
public:
   virtual void f(float) {std::cout << "B::f" << std::endl;}
};

class D : public B
{
public:
   virtual void f(int) override {std::cout << "D::f" << std::endl;}
};
int main ()
{
  B *b = new D;
  b->f(20);
  return 0;
}
What is the output of the below program.
#include <iostream>
#include <map>
class B 
{
public:
   virtual void f(int) {std::cout << "B::f" << std::endl;}
};

class D : public B
{
public:
   virtual void f(int) override final {std::cout << "D::f" << std::endl;}
};

class F : public D
{
public:
   virtual void f(int) override {std::cout << "F::f" << std::endl;}
};
int main ()
{
  D *d = new F;
  d->f(20);
  return 0;
}

What is the output of the below program.
#include <iostream>
#include <map>
class B 
{
public:
   virtual void f(int) {std::cout << "B::f" << std::endl;}
};

class D : public B
{
public:
   virtual void f(int) override {std::cout << "D::f" << std::endl;}
};

class F : public D
{
public:
   virtual void f(int) override {std::cout << "F::f" << std::endl;}
};
int main ()
{
  D *d = new F;
  d->f(20);
  return 0;
}

Therefore, only one thread object is responsible for one task at a time. However, the task associated with a thread object is movable:
auto task(){/* some computation */}
std::thread t1(task);
std::thread t2 = t1; //Error: do not copy threads
std::thread t3{t1}; // Error: do not copy-construct threads
auto task(){/* some computation */}
std::thread t1(task);
std::thread t2 = std::move(t1);


Predict the output of the following programs
[1]
#include <iostream>
using namespace std;
struct sample
{
    sample()
    {
        cout<<"sample::sample()"<<endl;
    }
    explicit sample(const char *txt)
    {
        cout<<"Init contructor"<<endl;
    }
};
void baz(const sample &obj)
{
}
int main() {
    baz("abc");
    return 0;
}

[2]
#include <iostream>
using namespace std;
struct sample
{
    sample()
    {
        cout<<"sample::sample()"<<endl;
    }
    explicit sample(const char *txt)
    {
        cout<<"Init contructor"<<endl;
    }
};
void baz(sample &obj)
{
}
int main() {
    // Write C++ code here
    baz("abc");
    return 0;
}

[3]
#include <iostream>
using namespace std;
struct sample
{
    sample()
    {
        cout<<"sample::sample()"<<endl;
    }
    sample(const char *txt)
    {
        cout<<"Init contructor"<<endl;
    }
};
void baz(sample obj)
{
}
int main() {
    // Write C++ code here
    baz("abc");
    return 0;
}

output:

[1] Compile error
[2] Compile error

/tmp/C4oGsaBhBl.cpp:20:9: error: invalid initialization of reference of type 'sample&' from expression of type 'const char [4]'
   20 |     baz("abc");

[3] What will be output of the below programs.

#include <bits/stdc++.h>
using namespace std;
class derived;
class sample
{
  private:
  sample() {
      cout<<"sample class constructor is called"<<endl;
  }
  friend class derived;
};
class derived:virtual sample
{
public:
    derived()
    {
        cout<<"derived class constructor is called"<<endl;
    }
};
int main()
{
    cout<<"Rakesh";
    derived d;
}

What is the output of the below program.
#include <bits/stdc++.h>
using namespace std;
class derived;
class sample
{
  private:
  sample() {
      cout<<"sample class constructor is called"<<endl;
  }
  friend class derived;
};
class derived:virtual sample
{
public:
    derived()
    {
        cout<<"derived class constructor is called"<<endl;
    }
};

class derived1:public derived
{
    public:
    
}
int main()
{
    cout<<"Rakesh";
    derived1 d;
}

  • void fun(int x)
    void fun(int x, int y=10)
    fun(5); 
    which function will be called?
Predict the output of the below program
#include <iostream>
using namespace std;
class B 
    private:
    virtual void fun()
    {
        
    }
};
class D: public B 
{
    public:
    void fun()
    {
        cout<<"D::fun()"<<endl;
    }
};
int main()
{
   B *b = new D;
   D *d = dynamic_cast<D*>(b);
   if(d != NULL)
       cout<<"works";
   else
       cout<<"cannot cast B* to D*";
   getchar();
   return 0;
}

output: Work






Comments

Popular posts from this blog

Design Pattern and Architecture in C++

Google Test (gtest) Notes

Stack & Queue interview Questions