Google Interview Question for Software Engineer / Developers


Team: codeblock
Country: India




Comment hidden because of low score. Click to expand.
2
of 2 vote

#include <iostream>
using namespace std;


class integer
{
public:
  int output(){return inte;  }
  

protected:
 void input()
  {
    cout<<"Enter Integer: ";
    cin>>inte;
       
  }  

private:
  int inte;
};


class character : public integer
{
public:
  void calc();
  void display();
  void useinput(){input();}
  
protected:

private:
  char ch;
  
};


void character::calc()
{
  cout<<"Enter Character: ";
  cin>>ch;
}

void character::display()
{  

  if( (int(ch)-48) % this->output() ==0 ){
    cout<< "Divides Completely"<<endl;
  }
  else{
    cout<< "Does not divide"<<endl;
  }
  
}


int main()
{
  character a;
  a.useinput();
  a.calc();
  a.display();
  
  
  return 0;
}

- Joanna8848 October 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

As per the question integer class has only one member function input..but u have used one more member function in public section..instead of using one more function in public block..why cant we make code the fn input() as...int input()
{ cout<<"enter integer"; cin>>inte;return inte}..and use this func in division as u used the output fn..we dont have condition that the protected memebr doesnt return int.do we?

- sastry.soft November 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I only know how to do this using friend in the base class or adding some getters in the base class.. Was there any constraint on that?

- Gianluca October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
class integer
{
      friend class character;
      private:
            const static int x=10;
      
};
class character
{
     public:
      char c;
      void calc()
      {
           cout<<"Enter char: ";
           cin>>c;
      }
      void disp()
      {
           integer a;
           if(a.x%(c-48)==0)
           cout<<"Divides Completely";
           else
           cout<<"
      }
};

int main()
{
    character b;
    b.calc();
    b.disp();
   // system("pause");
    return 0;
}

- Ashish Dixit October 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Integer
{
	 int sb;
public:
	Integer();
	Integer(int a);
protected :

	int Input(int);

};

class Character : public Integer
{
	public:
	Character();
	Character(char ch);
	void calc();
	void display();
	private:
	char c;
};


int main()
{
	Integer *i = new Integer();
	int *p = (int *)i;
	cout << *p << endl;
}

- Tina December 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You *can*, but that doesn't mean you *should*. That's some seriously illegal hackery. It happens to work with usual C compilers but there's nothing that keeps the compiler from breaking it at any time, like by plugging in a garbage collecting allocator that makes the first field of any object an internal-use link item.

- Matthias April 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The private data member can be accessed as int *p = (int *)i; Here i contains the address of the object.

- Tina December 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As it is not mentioned how many parameter I can take in function calc of class Character, I have used it to take two parameters.

#include <iostream>
using namespace std;

class Integer
{
    protected:
        void input(int data)
        {
            m_data = data;
        }
    private:
        int m_data;
};
class Character : public Integer
{
    public:
        void calc(char inputChar, int inputInt)
        {
            Integer::input(inputInt);
            if((inputChar <= 48) && (inputChar >=57))
            {
                cout<< "wrong input "<<endl;
                return;
            }
            else
            {
                int temp = inputChar - 48;
                if(temp%inputInt == 0)
                {
                    display();
                }
                else
                {
                    cout<< "Does not divide"<< endl;
                }
            }
        }
        void display()
        {
            cout<< "Divides Completely"<< endl;
        }
    private:
        char m_char;
};

int main()
{
    int tempInt;
    char tempChar;
    cout<< "Enter Integer - ";
    cin>> tempInt;
    cout<< "Enter Character - ";
    cin>> tempInt;
    Character tempCharObj;
    tempCharObj.calc(tempChar, tempInt);
    return 0;
}

- Kunjesh May 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

class Integer
{
protected:
void input(int data)
{
m_data = data;
}
private:
int m_data;
};
class Character : public Integer
{
public:
void calc(char inputChar, int inputInt)
{
Integer::input(inputInt);
if((inputChar <= 48) && (inputChar >=57))
{
cout<< "wrong input "<<endl;
return;
}
else
{
int temp = inputChar - 48;
if(temp%inputInt == 0)
{
display();
}
else
{
cout<< "Does not divide"<< endl;
}
}
}
void display()
{
cout<< "Divides Completely"<< endl;
}
private:
char m_char;
};

int main()
{
int tempInt;
char tempChar;
cout<< "Enter Integer - ";
cin>> tempInt;
cout<< "Enter Character - ";
cin>> tempInt;
Character tempCharObj;
tempCharObj.calc(tempChar, tempInt);
return 0;
}

- Anonymous May 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

class Integer
{
    protected:
        void input(int data)
        {
            m_data = data;
        }
    private:
        int m_data;
};
class Character : public Integer
{
    public:
        void calc(char inputChar, int inputInt)
        {
            Integer::input(inputInt);
            if((inputChar <= 48) && (inputChar >=57))
            {
                cout<< "wrong input "<<endl;
                return;
            }
            else
            {
                int temp = inputChar - 48;
                if(temp%inputInt == 0)
                {
                    display();
                }
                else
                {
                    cout<< "Does not divide"<< endl;
                }
            }
        }
        void display()
        {
            cout<< "Divides Completely"<< endl;
        }
    private:
        char m_char;
};

int main()
{
    int tempInt;
    char tempChar;
    cout<< "Enter Integer - ";
    cin>> tempInt;
    cout<< "Enter Character - ";
    cin>> tempInt;
    Character tempCharObj;
    tempCharObj.calc(tempChar, tempInt);
    return 0;
}

- kunjesh.singh May 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

class Integer
{
public:
	Integer()
	{}
	
protected:
	int input()
	{
		cout << "Enter integer: ";
		cin >> data;
		return data;
	}
	
private:
	int data;
}

class Character : public Integer
{
public:
	Character()
	{}
	
	int calc()
	{
		int data = input();

		cout << "Enter character: ";
		cin >> sign;
		
		return (data % (static_cast<int>(sign) - 48));
	}
	
	void display()
	{
		if(calc() == 0)
		{
			cout << "Divides Completely\n";
		}
		else
		{
			cout << "Does not divide\n"
		}
	}
	
private:
	char sign;
}

- Tadeusz Biela April 24, 2022 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More