Caterpillar C & C++ Interview Questions

Home » List of Companies » Caterpillar

It was very techinal interview. Started with Introduction of about project, team and work culture. Couple of questions were asked about the previous projects, and then switched to coding test by sharing codepad link.

Questions 1:
Analyse the following C++ program, and fix if any error:


#include <iostream>
#include <memory>

class IColor
{
    public:
        virtual void printRed() = 0;
        virtual void printBlue() = 0;
};

class Red: public IColor
{
    public:
        virtual ~Red() = default;
        Red() {};

        void printRed() override { std::cout << "This is Red" << std::endl; }
        void printBlue() override { std::cout << "This is NOT Blue" << std::endl; } 
};

class Green: public IColor
{
    public:
        virtual ~Green() = default;
        Green() {};
    void printRed() override final { std::cout << "This is NOT Red" << std::endl; } 
        virtual void printGreen() { std::cout << "This is Green" << std::endl; }
};

class Blue: public Red, public Green
{
    public:
        virtual ~Blue() = default;
        Blue() {};
        Blue(const Blue &B) {};
        void printRed() override final { std::cout << "This is NOT Red" << std::endl; }
        void printGreen() override final { std::cout << "This is NOT Green" << std::endl; }
        void printBlue() override final { std::cout << "This is Blue" << std::endl; } 
};

//Will this compile and run?
//If not, what is wrong?
//If so, what is the output of the below 3 cases
//Has the copy constructor been used? if not how can we use it?

int main()
{
    std::shared_ptr<IColor> first = std::make_shared<Red>();
    first->printRed();
    first->printBlue();

    std::cout<<std::endl;

    std::shared_ptr<IColor> second = std::make_shared<Green>();
    second->printRed();
    second->printBlue();

    std::cout<<std::endl;

    std::shared_ptr<Green> third = std::make_shared<Blue>();
    third->printRed();
    third->printBlue();
    third->printGreen();

    return 0;
}

Question 2: Analyse below C++ program and complete it so that its ouput is 11223344


#include <iostream>
#include <vector>
#include <iomanip>

int main() {
    std::vector<int> numbers;
    uint8_t   a = 0x11;
    uint16_t  b = 0x22;
    uint32_t  c = 0x33;
    uint64_t  d = 0x44;

    return 0;
} 

Question 3: Analyse below C++ program and if it has errors then fix


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

const unsigned char DATA_SIZE = 10;

int main()
{    
    char * pData = new char[DATA_SIZE];

    for(auto i=0; i<=DATA_SIZE;++i)
    {
        pData[i] = i | 0x30;
        std::cout << pData[i] << std::endl;
        pData++;
    }    
    
    delete pData;
    
    return 0;
} 

Question 4: Analyse below C++ program and if it has errors then fix


#include <iostream>
#include <memory>

using namespace std;

class MyObject1 
{
private:
    int data;
    
public:
    MyObject1(int aNumber) {data = aNumber;}
    int getData() {return data;}
};

class MyObject2
{
public:
    void doSomething(unique_ptr<MyObject1> ptr)
    {
        cout << "data = " << ptr->getData();
    }
};

//Will the code below compile? 
//If so, what will be the run result? 
//If not, how would you suggest to fix it?

int main()
{
    auto upObj1 = make_unique<MyObject1>(5);
    auto upObj2 = make_unique<MyObject2>();
    
    upObj2->doSomething(upObj1);
    return 0;
}

Other Categories

MCQ on C Programming MCQ on C++ Programming Basic Computer Questions Solved C programs Solved C++ programs


MCQs

About Us | Contact Us | Privacy Policy | Career  | Online Training
Youtube   LinkedIn   Facebook   Twitter   Instagram  
Copyright©CppBuzz.com
Like many websites, we use cookies to ensure best browsing experience on our website. While using this website, you acknowledge to have read and accepted our cookie and privacy policy.