LinkedIn: C++ (CPP) | Skill Assessment Quiz Solutions-2 | APDaga

▸ C++ (CPP) | LinkedIn Skill Assessment Quiz Solutions-2

LinkedIn: C++ (CPP) | Skill Assessment Quiz Solutions-2 | APDaga


  1. Which choice is the correct declaration for the class named Dog, derived from the Animal class?

    class Animal{
        //....
    }
    
    • A
    class Dog :: public Animal {
       //....
    };
    
    • B
    class Dog : public Animal {
       //....
    };
    
    • C
    public class Animal :: Dog {
       //....
    };
    
    • D
    public class Dog extends Animal {
       //....
    };
    


  1. What is the output of this code?

    #include <cstdio>
    using namespace std;
    
    int main(){
        char c = 255;
        if(c>10)
            printf("c = %i, which is greater than 10", c);
        else
            printf("c = %i, which is less than 10", c);
        return 0;
    }
    
    • c = -1, which is less than 10
    • c = 255, which is greater than 10
    • c = -1, which is greater than 10
    • c = 255, which is less than 10


  1. How can C++ code call a C function?

    • by simply calling the C code
    • there is no way for C++ to call a C function
    • by using extern "C"
    • by importing the source C code




  1. Which choice is not a valid type definition of a structure that contains x and y coordinates as integers, and that can be used exactly as shown for the variable named center?

    coord center;
    center.x = 5;
    center.y = 3;
    
    • A
    typedef struct coord {
        int x;
        int y;
    };
    
    • B
    typedef struct coord {
        int x;
        int y;
    } coord;
    
    • C
    typedef struct {
        int x;
        int y;
    } coord;
    
    • D
    struct coord {
        int x;
        int y;
    };
    
    typedef struct coord coord;
    


  1. Which choice does not produce the same output as this code snippet? Assume the variable i will not be used anywhere else in the code.

    for (i=1;i<10;i++){
        cout<<i<<endl;
    }
    
    • A
    i=1;
    while(i<10){
        cout<<++i<<endl;
    }
    
    • B
    for (int i:{1,2,3,4,5,6,7,8,9}) {
        cout<<i<<endl;
    }
    
    • C
    i = 1;
    do {
        cout<<i++<<endl;
    } while(i<10);
    
    • D
    i = 1;
    loop:
        cout<<i++<<endl;
        if(i<10) goto loop;
    


  1. What does this part of a main.cpp file do?

    #include "library.h"
    
    • It causes the toolchain to compile all the contents of library.h so that its executable code is available when needed by the final application.
    • It cherry picks library.h for the declarations and definitions of all data and functions used in the remainder of the source file main.cpp, finally replacing the #include directive by those declarations and definitions.
    • It informs the linker that some functions or data used in the source file main.cpp are contained in library.h, so that they can be called in run time. This is also known as dynamic linking.
    • It causes the replacement of the #include directive by the entire contents of the source file library.h. This is similar to a Copy-Paste operation of library.h into main.cpp.


  1. Consider this function declaration of is_even, which takes in an integer and returns true if the argument is an even number and false otherwise. Which declarations are correct for overloaded versions of that function to support floating point numbers and string representations of numbers?

    bool is_even(int);
    
    • A
    bool is_even(float f);
    bool is_even(char *str);
    
    • B
    bool is_even(float f);
    bool is_even(char str);
    
    • C
    bool is_even_float(float f);
    bool is_even_str(char *str);
    
    • D
    float is_even(float f);
    char *is_even(char *str);
    


  1. Which choice is an include guard for the header file my_library.h?

    • A
    #ifdef MY_LIBRARY_H
    #define MY_LIBRARY_H
    
    // my_library.h content
    
    #endif /* MY_LIBRARY_H */
    
    • B
    #ifndef MY_LIBRARY_H
    #define MY_LIBRARY_H
    
    // my_library.h content
    
    #endif /* MY_LIBRARY_H */
    
    • C
    #ifdef MY_LIBRARY_H
    #undef MY_LIBRARY_H
    
    // my_library.h content
    
    #endif /* MY_LIBRARY_H */
    
    • D
    #define MY_LIBRARY_H
    #include MY_LIBRARY_H
    
    // my_library.h content
    
    #undef MY_LIBRARY_H
    




  1. What’s wrong with this definition when using a pre-C++11 compiler?

    std::vector<std::vector<int>> thematrix;
    
    • There’s nothing wrong with it.
    • An std::vector cannot contain more std::vector containers as its elements.
    • The correct syntax should be: std::vector[std::vector[int]] thematrix;
    • >> is parsed as the shift-right operator, and thus results in a compile error.


  1. What is the statement below equivalent to?

    sprite->x
    
    • sprite.x
    • sprite.*x
    • (*sprite).x
    • *sprite.x


  1. Consider a class named complexNumber. Which code will result in an equivalent object?

    complexNumber(float real, float im)
    : real_part(real),
     im_part(im){}
    
    • A
    complexNumber(float real, float im) {
        this->real = real_part;
        this->im = im_part;
    }
    
    • B
    complexNumber(float real, float im) {
        this->real_part(real);
        this->im_part(im);
    }
    
    • C
    complexNumber(float real, float im) {
        this->real_part = real;
        this->im_part = im;
    }
    
    • D
    complexNumber(float real, float im) {
        this->real_part = &real;
        this->im_part = &im;
    }
    


  1. What is the result from executing this code snippet?

    bool x=true, y=false;
    if(~x || y){
        /*part A*/
    }
    else{
        /*part B*/
    }
    
    • Part A executes because the expression (~x || y) always results in true if y==false.
    • Part B executes because the statement (~x || y) is invalid, thus false.
    • Part A executes because ~x is not zero, meaning true.
    • Part B executes because ~x is false and y is false, thus the OR operation evaluates as false.


  1. What would be the output of this code?

    int32_t nums[3]={2,4,3};
    std::cout << ( nums[0] << nums[1] << nums[2] );
    
    • The output is the addresses of nums[0], nums[1], and nums[2], in that order, with no spaces.
    • 256
    • 0
    • 243




  1. What is the output of this code?

    float values[5]={0.54f, 2.71828f, 3.14159f, 5.499999f, 10.0f};
    for(auto f:values)
        printf("%i ",(int)(f+0.5f));
    
    • 0.54 2.71828 3.14159 5.499999 10.0
    • 1 3 4 6 11
    • 0 2 3 5 10
    • 1 3 3 5 10


  1. Which of the following STL classes is the best fit for implementing a phonebook? Suppose each entry contains a name and a phone number, with no duplicates, and you want to have lookup by name.

    • std::priority_queue
    • std::list
    • std::vector
    • std::map

    Reference


  1. What does this program do?

    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main(){
        ifstream file1("text1.txt", ios::binary);
        ofstream file2("text2.txt", ios::binary);
        file2 << file1.rdbuf();
    }
    
    • It renames text1.txt to text2.txt.
    • It makes a directory called text2.txt and moves text1.txt there.
    • It copies the contents of text1.txt into text2.txt - i.e., it makes a copy of text1.txt, named text2.txt.
    • It appends the contents of text1.txt into text2.txt - i.e., replaces the contents of text2.txt by the concatenation of text2.txt and text1.txt.

    Reference


  1. Which of the following is not a consequence of declaring the member variable count of my_class as static?

    class my_class {
        public: static int count;
    }
    
    • The variable cannot be modified by any part of the code in the same application or thread. However, other threads may modify it.
    • The variable exists even when no objects of the class have been defined so it can be modified at any point in the source code.
    • The variable is allocated only once, regardless of how many objects are instantiated because it is bound to the class itself, not its instances.
    • All objects that try to access their count member variable actually refer to the only class-bound static count variable.

    Reference


  1. What is the assumed type of a constant represented in the source code as 0.44?

    • double
    • long float
    • long double
    • float




  1. What is the output of this piece of code?

    int8_t a=200;
    uint8_t b=100;
    std::cout<<"a="<<(int)a;
    std::cout<<", b="<<(int)b;
    
    • a=-56, b=100
    • a=-55, b=100
    • a=200, b=-156
    • a=200, b=100


  1. What is an appropriate way of removing my_object as shown below?

    my_class *my_object = new my_class();
    
    • delete(my_object);
    • free(my_object);
    • The garbage collector will destroy the object eventually.
    • Exiting the scope will destroy the object.


  1. What is the correct way to call the count member function for the object pointer called grades?

    class my_array{
        public:
            int count();
    };  // ... more members above
    
    int main(){
        my_array *grades = new my_array();
    };  // ... more code above
    
    • grades.count();
    • my_array->count();
    • grades->count();
    • my_array.count();

    Reference


  1. What would be the output of this code?

    int i0=4, i1=6, i2=8;
    int& nums[3]={i2,i0,i1};
    std::cout<<nums[0]<<nums[1]<<nums[2];
    
    • There is no output. The code causes a compiler error because nums is an array of references, which is illegal.
    • 846
    • The output is the addresses of i2, i0, and i1, in that order, with no spaces.
    • 468

    Reference


  1. What is child_t in this code?

    typedef struct{
        unsigned int  age    : 4;
        unsigned char gender : 1;
        unsigned int  size   : 2;
    }child_t;
    
    • It is a type defined as a structure with three unsigned fields initialized as age=4, gender=1, and size=2.
    • It is a type defined as a structure with bit fields, with 4 bits for age, 1 bit for gender, and 2 bits for size.
    • This code causes a compiler error because the colon character is not allowed in struct definitions.
    • It is a type defined as a structure with three arrays. The size and length of these arrays are age:int[4], gender:char[1], and size:int[2], all signed.

    Reference




  1. What is this expression equivalent to?

    A->B->C->D
    
    • A.B.C.D
    • *A.*B.*C.*D
    • &A.&B.&C.&D
    • *(*((*A).B).C).D


  1. What does this function do?

    auto buff = new char[50];
    std::memset(buff,20,50);
    
    • It declares a memory buffer named buff that starts at address 20 and ends at address 70.
    • It sets all bits in the array named buffer from its element at index 20 to its element at index 50.
    • It writes the value 20 in every memory address from buff to buff+49.
    • It declares a memory buffer named buff that starts at address 20 and ends at address 50.

    Reference


  1. Consider a class named CustomData. Which choice is a correct declaration syntax to overload the postfix ++ operator as a class member?

    • CustomData& operator++();
    • void operator++(CustomData);
    • CustomData operator++(CustomData);
    • CustomData operator++(int);

    Reference


  1. You want to sort my_array, declared below. Which choice is the correct call to std::sort, using a lambda expression as the comparison function?

    std::array<uint32_t, 50> my_array;
    
    • A
    std::sort(my_array.begin(), my_array.end(),
        [](uint32_t a, uint32_t b) {
            return a < b;
        })
    
    • B
    lambda(uint32_t a, uint32_t b){
        return a < b;
    }
    std::sort(my_array.begin(), my_array.end(), lambda);
    
    • C
    std::sort(my_array.begin(), my_array.end(),
        lambda(uint32_t a, uint32_t b){
            return a < b;
        })
    
    • D
    lambda(uint32_t a, uint32_t b){
        return a < b;
    }
    std::sort(my_array.begin(), my_array.end(), &lambda);
    

    Reference


  1. Which choice is the most reasonable implementation of the function std::mutex::lock() by using std::mutex::try_lock()?

    • A
    void std::mutex::lock(){
        while(!this->try_lock());
    }
    
    • B
    void std::mutex::lock(){
        return (this->try_lock());
    }
    
    • C
    void std::mutex::lock(){
        while(1)
            this->try_lock();
    }
    
    • D
    void std::mutex::lock(){
        while(this->try_lock());
    }
    




  1. What is the purpose of a destructor?

    • It allows the programmer to write the necessary code to free the resources acquired by the object prior to deleting the object itself.
    • It deletes an object. One example of a destructor is the delete() function.
    • It terminates a program. This may be achieved as a regular function call or as an exception.
    • There are no destructors in C++.


  1. Which STL class is the best fit for implementing a phonebook? Suppose each entry contains a name and a phone number, with no duplicates, and you want to have lookup by name.

    • std::priority_queue
    • std::map
    • std::vector
    • std::list


  1. What is the main difference between these two Functions?

    std::mutex::lock()
    std::mutex::try_lock()
    
    • lock() has a higher privilege over try_lock(). This means that you have a better chance of acquiring a mutex with lock().
    • Both attempt to acquire a lock, but lock() blocks if the mutex is not available, whereas try_lock() returns whether the mutex is available or not.
    • lock() enforces preemption, whereas try_lock() suggests preemption.
    • If the mutex is not available, try_lock() returns with a corresponding code, whereas lock() snatches the mutex from the thread that currently has it.

    Reference


  1. What is one benefit of declaring the parameter as a const reference instead of declaring it as a regular object?

    int median(const my_array& a)
    
    • Actually, objects cannot be passed as regular variables, because they require a constructor call. Therefore, a const reference is the only way to pass class instances to functions.
    • There are no benefits because a reference and an object are treated as the same thing.
    • The const qualifier Forbids the code to modify the argument, so the programmer can rest assured that the source object will remain unchanged.
    • The argument is passed as a reference, so the Function receives a copy that can be modified without affecting the original variable.

    Note: This one is similar to Q6, but focuses on the const keyword.


CREDITS: (Source)


Click here to see solutions for all HackerRank SQL practice questions.
&
Click here to see solutions for all Machine Learning Coursera Assignments.
&
Click here to see more codes for Raspberry Pi 3 and similar Family.
&
Click here to see more codes for NodeMCU ESP8266 and similar Family.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.

Feel free to ask doubts in the comment section. I will try my best to answer it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.

Thanks & Regards,
- APDaga DumpBox
Post a Comment (0)
Previous Post Next Post