C++ Quiz (C++ MCQ-PART2): C++ Multiple Choice Questions and Answers

C++ Quiz with answers and explanations for placement tests and job interviews. This C++ MCQ-PART-2 is useful for the campus placement for all freshers including Engineering Students, MCA students, Computer and IT Engineers, etc.

Our C++ quiz (C++ multiple Choice Questions ) focuses on all areas of the C++ programming language and its concept. We will regularly update the C++ quiz and most interesting thing is that questions come in a random sequence. So every time you will feel new questions.

You may have come across several C++ courses during your search for learning the C++ language. Our team of experts has carefully analyzed some C++ courses for you. You can check the courses, Trial of some courses is free.

 

Guideline of C++ Quiz (C++ MCQ PART-2):

This C++ quiz is intended for checking your C++ knowledge. It takes 50 minutes to pass the C++ quiz. If you don’t finish the C++ quiz within the mentioned time, all the unanswered questions will count as wrong. You can miss the questions by clicking the “Next” button and return to the previous questions by the “Previous” button. Every unanswered question will count as wrong. MCQ on C++ has features of randomization which feel you a new question set at every attempt.

In this C++ quiz, we have also implemented a feature that not allowed the user to see the next question or finish the quiz without attempting the current C++ quiz.

0 votes, 0 avg
0

You have 1 hours 10 minutes to take the C++ MCQs- PART-2

Your time has been Over.


C++ MCQ-PART2

C++ MCQ

C++ MCQ-PART2: Multiple Choice Questions and Answers on C++

1 / 64

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));

 

2 / 64

For these declarations, which choice shows four equivalent ways to assign the character "y" in the string to a char variable c?

char buff[50] = "strings as arrays of characters are fun!"
char *str = buff+11;
char c;

1. 

c = buff[16];
c = str[5];
c = *(buff+16);
c = *(str+5);

2. 

c = *(buff[15]);
c = *(str[4]);
c = buff+15;
c = str+4;

3. 

c = buff[15];
c = str[4];
c = *(buff+15);
c = *(str+4);

4. 

c = *(buff[16]);
c = *(str[5]);
c = buff+16;
c = str+5;

3 / 64

What is the output of this piece of code?

int8_t a=200;
uint8_t b=100;
if(a>b)
    std::cout<<"greater";
else
    std::cout<<"less";

 

4 / 64

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

1. 

void std::mutex::lock(){
    while(!this->try_lock());
}

2. 

void std::mutex::lock(){
    return (this->try_lock());
}

3. 

void std::mutex::lock(){
    while(1)
        this->try_lock();
}

4. 

void std::mutex::lock(){
    while(this->try_lock());
}

5 / 64

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

class Animal{
    //....
}

1.

class Dog :: public Animal {
   //....
};

2. 

class Dog : public Animal {
   //....
};

3. 

public class Animal :: Dog {
   //....
};

4.

public class Dog extends Animal {
   //....
};

 

6 / 64

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;

1. 

std::sort(my_array.begin(), my_array.end(),
    [](uint32_t a, uint32_t b) {
        return a < b;
    })

2. 

lambda(uint32_t a, uint32_t b){
    return a < b;
}
std::sort(my_array.begin(), my_array.end(), lambda);

3. 

std::sort(my_array.begin(), my_array.end(),
    lambda(uint32_t a, uint32_t b){
        return a < b;
    })

4. 

lambda(uint32_t a, uint32_t b){
    return a < b;
}
std::sort(my_array.begin(), my_array.end(), &lambda);

7 / 64

Which of the following is a reason why using this line is considered a bad practice? (Alternative: Why is using this line considered a bad practice?)

Using namespace std;

 

8 / 64

Which of the following shows the contents of vector v1 and v2 after running this code?

std::vector v1{1,2,3},v2;
v2=v1;
v1.push_back(4);
v2.push_back(5);

 

9 / 64

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

1. 

#ifdef MY_LIBRARY_H
#define MY_LIBRARY_H

// my_library.h content

#endif /* MY_LIBRARY_H */

2. 

#ifndef MY_LIBRARY_H
#define MY_LIBRARY_H

// my_library.h content

#endif /* MY_LIBRARY_H */

3.

#ifdef MY_LIBRARY_H
#undef MY_LIBRARY_H

// my_library.h content

#endif /* MY_LIBRARY_H

4. 

#define MY_LIBRARY_H
#include MY_LIBRARY_H

// my_library.h content

#undef MY_LIBRARY_H

10 / 64

What is this expression equivalent to?

A->B->C->D

 

11 / 64

What is the meaning of the three sections specified between parentheses in a for loop separated by semicolons?

12 / 64

Which of the following is a true statement about the difference between pointers and iterators?

13 / 64

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

std::vector<std::vector> thematrix;

 

14 / 64

What does this program do?

#include 
#include 
using namespace std;

int main(){
    ifstream file1("text1.txt", ios::binary);
    ofstream file2("text2.txt", ios::binary);
    file2 << file1.rdbuf();
}

 

 

15 / 64

What is the statement below equivalent to?

sprite->x

 

16 / 64

What does this function do?

auto buff = new char[50];
std::memset(buff,20,50);

 

 

17 / 64

What is the ternary operator equivalent to this code snippet?

if(x)
    y=a;
else
    y=b;

 

18 / 64

What is the main difference between these two Functions?

std::mutex::lock()
std::mutex::try_lock()

 

19 / 64

What is child_t in this code?

typedef struct{
    unsigned int  age    : 4;
    unsigned char gender : 1;
    unsigned int  size   : 2;
}child_t;

 

20 / 64

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)

 

21 / 64

What is the value of x after running this code?

int x=10, a=-3;
x=+a;

 

22 / 64

What is true about the variable named ptr?

void *ptr;

 

23 / 64

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

 

24 / 64

What is printed from this code?

int i = 0;
printf("%d", i++);
printf("%d", i--);
printf("%d", ++i);
printf("%d", --i);

 

25 / 64

Consider a pointer to void, named ptr, which has been set to point to a floating point variable g. Which choice is a valid way to dereference ptr to assign its pointed value to a float variable f later in the program?

float g;
void *ptr=&g;

 

26 / 64

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

my_class *my_object = new my_class();

 

27 / 64

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

complexNumber(float real, float im)
: real_part(real),
 im_part(im){}

1. 

complexNumber(float real, float im) {
    this->real = real_part;
    this->im = im_part;
}

2. 

complexNumber(float real, float im) {
    this->real_part(real);
    this->im_part(im);
}

3. 

complexNumber(float real, float im) {
    this->real_part = real;
    this->im_part = im;
}

4. 

complexNumber(float real, float im) {
    this->real_part = ℜ
    this->im_part = &im;
}

 

28 / 64

Which statement is true?

29 / 64

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

30 / 64

What is the output of this code?

printf("1/2 = %f",(float)(1/2));

 

31 / 64

What results from executing this code snippet?

int x=5, y=2;
if(x & y) {
    /*_part A_*/
}
else {
    /*_part B_*/
}

 

32 / 64

What is the meaning of the two parts specified between parentheses in a range-based for loop, separated by a colon?

33 / 64

What would be the output of this code?

int32_t nums[3]={2,4,3};
std::cout << ( nums[0] << nums[1] << nums[2] );

 

34 / 64

What's the storage occupied by u1?

union {
    unit16_t a;
    unit32_t b;
    int8_t c;
} u1;

 

35 / 64

xWhich 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.

36 / 64

Suppose you need to keep a data struct with permission to access some resource based on the days of the week, but you can't use a bool variable for each day. You need to use one bit per day of the week. Which of the following is a correct implementation of a structure with bit fields for this application?

1. 

typedef struct {
    int sunday:1;
    int monday:1;
    // more days
    int friday:1;
    int saturday:1;
} weekdays;

2. 

typedef char[7]: weekdays;

3. 

typedef struct {
    bit sunday:1;
    bit monday:1;
    // more days
    bit friday:1;
    bit saturday:1;
} weekdays;

4. 

typedef struct {
    bit sunday;
    bit monday;
    // more days
    bit friday;
    bit saturday;
} weekdays;

37 / 64

What is printed from this code?

vector v(22);
bool b = (v[6]);
printf("%d", !b);

 

38 / 64

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

#include "library.h"

 

39 / 64

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;
}

 

40 / 64

What is the result from executing this code snippet?

bool x=true, y=false;
if(~x || y){
    /*part A*/
}
else{
    /*part B*/
}

 

41 / 64

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;
}

1. 

i=1;
while(i<10){
    cout<<++i<<endl;
}

2. 

for (int i:{1,2,3,4,5,6,7,8,9}) {
    cout<<i<<endl;
}

3. 

i = 1;
do {
    cout<<i++<<endl;
} while(i<10);

4.

i = 1;
loop:
    cout<<i++<<endl;
    if(i<10) goto loop;

 

42 / 64

What is the output of this code?

int c=3; char d='A';
std::printf("c is %d and d is %c",c,d);

 

43 / 64

What is the output of this code?

#include 

int main(){
    int x=10, y=20;
    std::cout << "x = " << x++ << " and y = " << --y << std::endl;
    std::cout << "x = " << x-- << " and y = " << ++y << std::endl;
    return(0);
}

 

44 / 64

Which of the following is not a difference between a class and a struct?

45 / 64

What does auto type specifier do in this line of code (since C++11)?

auto x = 4000.22;

 

46 / 64

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

47 / 64

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];

 

48 / 64

What is the purpose of a destructor?

49 / 64

What is the smallest size a variable of the type child_t may occupy in memory?

typedef struct{
    unsigned int  age    : 4;
    unsigned char gender : 1;
    unsigned int  size   : 2;
}child_t;

 

50 / 64

What is a class template?

51 / 64

What is a valid definition for the get_length function, which returns the length of a null-terminated string?

1.

int get_length(char *str) {
    int count=0;
    while(str[count++]);
    return count-1;
}

2.

int get_length(char *str) {
    int count=0;
    while(str!=NULL){
        count++;
        str++;
    }
    return count;
}

3.

int get_length(char *str) {
    int count=0;
    while((*str)++)
        count++;
    return count;
}

4.

int get_length(char *str) {
    int count=0;
    while(str++)
        count++;
    return count;
}

52 / 64

Which of the following shows the contents of vector pointed by v1 and v2 after running this code?

std:: vector *v1 = new std::vector({1,2,3});
std:: vector *v2;
v2=v1;
v1->push_back(4);
v2->push_back(5);

 

53 / 64

Which of the following operators is overloadable?

54 / 64

How can C++ code call a C function?

55 / 64

What is the output of this code?

#include 
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;
}

 

56 / 64

Which STL class is the best fit for implementing a collection of data that is always ordered so that the pop operation always gets the greatest of the elements? Suppose you are interested only in push and pop operations.

57 / 64

What's a benefit of declaring the parameter as a const reference instead of declaring it as a regular object?

int median(const my_array& a);

 

58 / 64

What is the .* operator and what does it do?

59 / 64

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;

1. 

typedef struct coord {
    int x;
    int y;
};

2. 

typedef struct coord {
    int x;
    int y;
} coord;

3. 

typedef struct {
    int x;
    int y;
} coord;

4. 

struct coord {
    int x;
    int y;
};

typedef struct coord coord;

60 / 64

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;

 

61 / 64

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.

62 / 64

What is the difference between a public and a private class member?

63 / 64

What is an lvalue?

64 / 64

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);

1. 

bool is_even(float f);
bool is_even(char *str);

2. 

bool is_even(float f);
bool is_even(char str);

3. 

bool is_even_float(float f);
bool is_even_str(char *str);

4. 

float is_even(float f);
char *is_even(char *str);

Your score is

The average score is 0%

0%

Recommended Articles for you:

Leave a Reply

Your email address will not be published. Required fields are marked *