Euronet interview questions

Some questions were from test4geeks 

void GetSomeVariable(int **i)
{
   if(*i == NULL)
   {
      *i = new int;
   }

  **i = 20;  
}

int main()
{
   int someValue = 10;
   GetSomeVariable(&someValue);
   cout << someValue <<endl;
   
   int *ptr = nullptr;
   GetSomeVariable(&ptr);
   cout << *ptr <<endl;
}

How to make the code compilable without changing GetSomeVariable?
Solution: Use Function overloading or Type traits

#include <iostream>
#include <type_traits>

void g(int*) { std::cout << 'L'; }
void g(int**) { std::cout << 'R'; }

template<typename T>
void f(T t) {
    if (std::is_same_v<T, int>) { std::cout << 1; } 
    if (std::is_same_v<T, int*>) { std::cout << 2; } 
    if (std::is_same_v<T, int**>) { std::cout << 3; } 
    g(std::forward<T>(t));
}

int main() {
    
    
    int i = 0;
    f(&i);
    int *p = &i;
    f(&p);
}

Or

type traits has is_pointer<T>

#include <type_traits>
#include <iostream>
using namespace std;
// Check if a type is a pointer
template <typename T>
struct is_pointer : std::is_pointer<T> {};

// Check if a type is a double pointer
template <typename T>
struct is_double_pointer : std::is_pointer<typename std::remove_pointer<T>::type> {};

// Check if a type is a triple pointer
template <typename T>
struct is_triple_pointer : std::is_pointer<typename std::remove_pointer<typename std::remove_pointer<T>::type>::type> {};

template <typename T>
void GetSomeVariable(T a)
{

  // Check if ppp is a triple pointer
  if(is_triple_pointer<decltype(a)>::value)
     cout << "ppp is a triple pointer" << endl;   
    // Check if pp is a double pointer
  else if(is_double_pointer<decltype(a)>::value)
     cout << "pp is a double pointer" << endl;    
     // Check if p is a pointer
  else if(std::is_pointer<decltype(a)>::value)
    cout  << "p is a pointer" << endl;

}

// Example usage:
int main() {
  int* p = nullptr;
  int** pp = nullptr;
  int*** ppp = nullptr;

  GetSomeVariable(pp);
  
  return 0;
}


int main()
{
    int i =5;
    int j=&i;
   int p = 10;
    i = p;
    std::cout << i << " " << j <<end;
    p =20;
    std::cout << i << " " << j <<end;
}
Please give the output


class A
{

  public:
A()
{
   sz= 512;
   m_buffer = new char[sz];
}
~A()
{
  delete sz;
}
   char* GetBuffer() { return  m_buffer; }
   size_t GetSize() {  return sz;}
  private:
    char* m_buffer;
size_t sz;
};

void SendBuffer(A obj)
{
   sendSocket(obj.GetBuffer(), obj.GetSize());
}

int main()
{
  {
     const A obj;
     SendBuffer(obj);
  }
}
Please review the code and find the problem in the code
  {
     const A obj;
     SendBuffer(obj);
  } // if it goes out of scope then undefined behavior problem occurs as 2 times it goes out of scope
 whenever you are sending obj inside SendBuffer() it creates a temporary object using constructor



ORIGINAL SOURCE of question --> https://tests4geeks.com/blog/cpp-interview-questions/

void SomeFunction(std::unique_ptr<std::string> str)
{
   std::cout << *str << "\n";
}

int main()
{
   std::unique_ptr<std::string> str(new std::string("some good string"));

   SomeFunction(str);
 
}
What is the problem in the above code ?
How to resolve the issue ?
SomeFunction(std::move(str));

Explain move constructor ? why it is better than assignment operator ?
std::string s;
s + s = s;
why move constructor is better than assignment operator ?
Ans:- 
The move constructor is better than the assignment operator because it avoids unnecessary copying of data. When you assign one object to another, the compiler will typically create a copy of the data from the source object to the destination object.

Comments

Popular posts from this blog

GlobalLogic Interview

Motorola(Dynamic Technologies) Kolkata