InternDoor

NTT DATA interview prep

5 practice questions on the topics candidates reported from NTT DATA interviews, with answers. The questions are ours; each one links to the interview report that names its topic.

Practise them as a timed round45 roles open at NTT DATA, Inc.

C and C++ 5

  1. C and C++Medium

    A C++ class declares its only constructor private. What does that allow?

    1. Nothing; a private constructor is a compile error
    2. Objects can still be created anywhere with new
    3. Only the class itself (and its friends) can create objects, which is how a singleton controls its one instance
    4. The class becomes abstract and can only be inherited from
    Show the answer

    C. Only the class itself (and its friends) can create objects, which is how a singleton controls its one instance

    A private constructor is legal. Code outside the class cannot construct the object, so creation goes through the class itself, typically a static member function. That is the usual first step of the singleton pattern.

    This topic is named in a candidate’s interview report: On-campus interview, 2020, on GeeksforGeeks.

  2. C and C++Medium

    A Logger class is a singleton with a private constructor. How does the rest of the program get the one Logger object?

    1. Through a public static member function, such as Logger::instance(), that creates the object once and returns it
    2. By calling new Logger() wherever it is needed
    3. By deriving a class from Logger
    4. By copying an existing Logger with its copy constructor
    Show the answer

    A. Through a public static member function, such as Logger::instance(), that creates the object once and returns it

    The static function creates the object on first use (for example a function-local static, which is thread-safe in C++11 and later) and returns a reference to it every time. The copy constructor and assignment are usually deleted so no second copy can be made.

    This topic is named in a candidate’s interview report: On-campus interview, 2020, on GeeksforGeeks.

  3. C and C++Medium

    A non-member function is declared friend inside class Account. Which statement is true?

    1. It can use Account's private and protected members, although it is not a member function and has no this pointer
    2. It becomes a member function of Account
    3. Classes derived from Account inherit the friendship
    4. It can still see only Account's public members
    Show the answer

    A. It can use Account's private and protected members, although it is not a member function and has no this pointer

    friend grants access, not membership. The function is called like any free function and receives the object as an argument. Friendship is neither inherited nor transitive.

    This topic is named in a candidate’s interview report: On-campus interview, 2020, on GeeksforGeeks.

  4. C and C++Medium

    What does this print?

    #include <iostream>
    
    class Box {
        int w = 3;
        friend int width(const Box& b);
    };
    
    int width(const Box& b) { return b.w * 2; }
    
    int main() {
        Box b;
        std::cout << width(b);
    }
    1. Compile error: w is private
    2. 3
    3. Compile error: width is not a member of Box
    4. 6
    Show the answer

    D. 6

    width is an ordinary global function, defined outside the class, but the friend declaration inside Box lets it read the private w. It returns 3 * 2.

    This topic is named in a candidate’s interview report: On-campus interview, 2020, on GeeksforGeeks.

  5. C and C++Medium

    What does this print, and what would it print if the word virtual were removed?

    #include <iostream>
    
    struct Shape {
        virtual const char* name() { return "shape"; }
    };
    struct Circle : Shape {
        const char* name() override { return "circle"; }
    };
    
    int main() {
        Shape* s = new Circle();
        std::cout << s->name();
    }
    1. circle; without virtual it would print shape
    2. shape; without virtual it would print circle
    3. circle in both cases
    4. shape in both cases
    Show the answer

    A. circle; without virtual it would print shape

    A virtual call is resolved at run time from the object's real type, so Circle::name runs. Without virtual (and with override removed, which would otherwise not compile), the call is bound at compile time from the pointer's type, Shape, and prints shape.

    This topic is named in a candidate’s interview report: On-campus interview, 2020, on GeeksforGeeks.

Prep for other employers

All employers
See who is hiring after NTT DATA

Engineering internships and entry-level jobs in India, newest first.