linkedin-skill-assessments-quizzes

C++

Q1. What is the output of this code?

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

Q2. 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;

Reference

Q3. 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;

Reference

Q4. What are the vectors v1 and v2 after executing the code?

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

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

Reference

Q6. What’s the storage occupied by u1?

union {
    uint16_t a;
    uint32_t b;
    int8_t c;
} u1;

Reference

Q7. Which of the following operator can be overloaded?

Reference

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

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

v1 and v2 point to the same vector.

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

Templates can be used with both classes and structs Refernce Reference

Q10. 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?

typedef struct {
    int sunday:1;
    int monday:1;
    // more days
    int friday:1;
    int saturday:1;
} weekdays;
typedef char[7]: weekdays;
typedef struct {
    bit sunday:1;
    bit monday:1;
    // more days
    bit friday:1;
    bit saturday:1;
} weekdays;

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

Reference NOTE: Correct syntax is that each variable size is 1 bit. bit is not a type in C++.

Q11. What is an lvalue?

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

auto x = 4000.22;

Q13. A class template is a _?

Reference

Q14. What is the ternary operator equivalent to this code snippet?

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

Reference

Q15. What is the output of the code given below?

#include <iostream>

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

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

Q17. What is the output of the code given below?

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

Note: a variant of the question below.

Q18. What is the output of this block of code?

int8_t a=200;
uint8_t b=100;
std::cout<<"a="<<(int)a;
std::cout<<", b="<<(int)b;

Note: Implicit conversion from ‘int’ to ‘int8_t’ (aka ‘signed char’) changes value from 200 to -56

Q19. What is the output after executing this code snippet?

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

Reference

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

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

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

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

Q23. What does this code print?

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

Reference

Q24. What is true about the variable named ptr?

void *ptr;

Reference

Q25. What is the output of the code given below?

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

Q26. What is the output of this code?

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

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

Reference

Q28. What is the value of x after executing this code?

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

Q29. Which statement is true?

Q30. 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;

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

Reference

Q32. 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;
c = buff[16];
c = str[5];
c = *(buff+16);
c = *(str+5);
c = *(buff[15]);
c = *(str[4]);
c = buff+15;
c = str+4;
c = buff[15];
c = str[4];
c = *(buff+15);
c = *(str+4);
c = *(buff[16]);
c = *(str[5]);
c = buff+16;
c = str+5;

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

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

Q34. What is the output of this code given below?

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

Technically, whether a char is signed or unsigned is implementation-defined; in the latter case, the second answer would be correct. Reference

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

Q36. 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;
typedef struct coord {
    int x;
    int y;
};
typedef struct coord {
    int x;
    int y;
} coord;
typedef struct {
    int x;
    int y;
} coord;
struct coord {
    int x;
    int y;
};

typedef struct coord coord;

Q37. 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;
}
i=1;
while(i<10){
    cout<<++i<<endl;
}
for (int i:{1,2,3,4,5,6,7,8,9}) {
    cout<<i<<endl;
}
i = 1;
do {
    cout<<i++<<endl;
} while(i<10);
i = 1;
loop:
    cout<<i++<<endl;
    if(i<10) goto loop;

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

#include "library.h"

Q39. 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);
bool is_even(float f);
bool is_even(char *str);
bool is_even(float f);
bool is_even(char str);
bool is_even_float(float f);
bool is_even_str(char *str);
float is_even(float f);
char *is_even(char *str);

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

#ifdef MY_LIBRARY_H
#define MY_LIBRARY_H

// my_library.h content

#endif /* MY_LIBRARY_H */
#ifndef MY_LIBRARY_H
#define MY_LIBRARY_H

// my_library.h content

#endif /* MY_LIBRARY_H */
#ifdef MY_LIBRARY_H
#undef MY_LIBRARY_H

// my_library.h content

#endif /* MY_LIBRARY_H */
#define MY_LIBRARY_H
#include MY_LIBRARY_H

// my_library.h content

#undef MY_LIBRARY_H

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

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

Q42. What is the statement below equivalent to?

Q42.b. What is the statement equivalent to?

sprite->x

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

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

Q44. What is the result of executing this code snippet?

bool x=true, y=false;

if (~x || y) {
    /*part A*/
} else {
    /*part B*/
}

Q45. What would be the output of this code?

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

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

Q47. 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 a lookup by name.

Reference

Q48. 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();
}

Reference

Q49. Which of the following is not a consequence of declaring the member variable count of my_class as static? / Alt.: Which statement is true when declaring the member variable count as static?

class my_class {
    public: static int count;
}

Reference

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

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

my_class *my_object = new my_class();

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

Reference

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

Reference

Q54. Does this code cause a compiler error? If so, why, and if not, what is child_t?

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

Reference

Q55. What is this expression equivalent to?

A->B

Note: a simpler variant of the question below.

Q56. What is this expression equivalent to?

A->B->C->D

Note: a more complex variant of the question above.

Q57. What does this function do?

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

Reference

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

Reference

Q59. 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;
std::sort(my_array.begin(), my_array.end(),
    [](uint32_t a, uint32_t b) {
        return a < b;
    })
lambda(uint32_t a, uint32_t b){
    return a < b;
}
std::sort(my_array.begin(), my_array.end(), lambda);
std::sort(my_array.begin(), my_array.end(),
    lambda(uint32_t a, uint32_t b){
        return a < b;
    })
lambda(uint32_t a, uint32_t b){
    return a < b;
}
std::sort(my_array.begin(), my_array.end(), &lambda);

Reference

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

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

Note: variant of the question below.

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

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

Note: variant of the question above.

Reference

Q62. What is the purpose of a destructor?

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

int calculateMedian(const my_array& a)

Reference

Q64. What is an include guard?

Q65. What would be the correct declaration of a default constructor for a class named Sprite?

Q66. What is the purpose of this line in a header file?

#pragma once

reference here

Q67. What is a variable of type double?

Reference

Q68. Other than shifting bits to the left, what is the « operator used for?

Q69. Which choice is a reason to specify the type of a pointer instead of using void *, which works as a pointer to any type?

Reference

Q70. What is wrong with this piece of code?

#include <iostream>
char str[20];
int main(){
    std::cout << "What's your name? ";
    str << std::cin
    std::cout << "Hello, " << str;
    return 0;
}

Q71. When placed in a valid execution context, which statement will dynamically allocate memory from the heap for an integer of value 11?

Q72. Which choice best describes the type long?

Q73. Which of the following types has the closest functionality to a class?

Reference

Q74. Given these records in a map, how will you update the value for the key “Sinead” to 22?

image

Q75. Why can the std::sort receive a function object as one of its parameters?

Q76. What will happen when you execute this code snippet?

#include <iostream>

int main() {
float a = 5.51;
int b = static_cast<int>(a);
std::cout << b;
}

Q77. Which access specifier does not allow class members to be accessed from outside the class, but allows them to be accessed by derived classes?

Q78. The default executable generation on UNIX for a C++ program is _

Q79. What will be the output of the following program?

#include <iostream>
using namespace std;
int main() {
  int a=1;
  cout<<(a++)*(++a)<<endl;
  return 0;
}

Q80. What does “c” stand for in cout and cin?

Q81. What is the use of tellp()?

Q82. What is callback function?

Q83. What is the correct syntax to output “Hello World” in C++?

Q84. How many categories of iterators are there in C++?

Q85. What is the meaning of base class in C++ ?

Q86. The size of C++ objects is expressed in terms of multiples of the size of a ** and the size of a char is **.

Q87. Implementation-dependent aspects about an implementation can be found in

Q88. What is a default constructor?

Q89. When protecting a header file, why would you use ‘#pragma once’ instead of ‘include’ guard?

Q90. Which of the following statements is valid?

Q91. Which of the following is/are automatically added to every class, if we do not write our own?

Q92. The if-else statement can be replaced by which operator?

Q93. Which choice would be a recursive solution to the factorial n! problem?

void fact(int n) {
    if (n <= 0)
        return 0;
    else
        return 1;
}
int fact(int n) {
    if (n <= 0)
        return 1;
    else
        return (fact(n) * (n-1));
}
int fact(int n) {
    if (n >= 0)
        return 1;
    else
        return (fact(n-1) * n);
}
int fact(int n) {
    if (n <= 0)
        return 1;
    else
        return (fact(n-1) * n);
}

Q94. A class destructor can be called when a variety of situations occur. Which choice is not one of those situations?

Q95. You are designing a foreign exchange payments system in C++, You need to model a transaction of a currency that has an integer as its quantity and a float as its price. You then want to declare an actual object of this type. How will you achieve this?

struct currencyDeal {
    float price;
    int quantity;
};

currencyDeal firstDeal;
union currencyDeal {
    float price;
    int quantity;
};

currencyDeal firstDeal;
struct currencyDeal {
    float price;
    int quantity;
};
union currencyDeal {
    float price;
    int quantity;
};

Q96. What will happen if you attempt to call this function with checkConcatThreshold(“a”);?

int checkConcatThreshold(string a, string b) {
    return (a + b).length () > 120;
}

Q97. You need to define a C++ lambda function. You want the function to have access to only the variables that are local to it. The function should receive a single parameter, and a name, and construct a simple greeting. How will you achieve this?

auto myVeryFirstLambda = [=] (string name) {
        return "Hello " + name + "!";
    };
myVeryFirstLambda = [&] (string name) {
        return "Hello " + name + "!";
    };
auto myVeryFirstLambda = [] (string name) {
        return "Hello " + name + "!";
    };
myVeryFirstLambda = [] (string name) {
        return "Hello " + name + "!";
    };

Reference

Q98. What is the value of X after running this code?

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

Explanation : += means increasing value. So x += a is equivalent to x = x + a

Q99. Once you are done writing to a file, what method will you call on the ofstream to notify the operating system?

Q100. Which choice is not a C++ keyword?

Q101. The size_in_bits function seems to take any type of parameter. This can be done by overloading the function, or by letting the compiler take care of it by writing a template. Which choice is an implementation of that template?

int main()
{
cout « size_in_bits(21) « endl;
cout « size_in_bits('f') « endl;
cout « size_in_bits(32.1f) « endl;
cout « size_in_bits(32.1) « endl;
return 0;
}
template <typename T>
size_t size_in_bits(const T& a){
return sizeof(a)*8;
}
template size_t size_in_bits(const {int,float,double,char,long}& a){
return sizeof(a)*8;
}
template <typename T {int,float,double,char,long>
size_t size_in_bits(const T& a){
return sizeof(a)*8;
}
size_t size_in_bits(void * a){
return sizeof(a)*8;
}

Q102. To use the keyboard as input the iostream library is included. To read input from files as input what library is needed?

Q103. What will this object-oriented program print?

#include <iostream>
#include <string>
using namespace std;

class Vehicle {
public:
	string fuel = "none";
};

class MotorizedVehicle : public Vehicle {
public:
	string fuel = "fossil";
};

class NextgenMotorizedVehicle : public MotorizedVehicle {
public:
	string fuel = "hydrogen";
};

int main() {
	MotorizedVehicle aCar;
	cout << aCar.fuel;
	return 0;
}

Q104. The program below is handling a stack container. What is the output of running the program?

#include <iostream>
#include <stack>

int main()
{
	std::stack<int> stack;
	stack.push(1);
	stack.push(2);
	stack.push(3);

	stack.pop();

	stack.push(4);

	stack.top();
	stack.pop();

	std::cout << stack.top();
}

#Detailed explanation:

Now here we are supposed to implement a stack data structure that follows the FILO or (First IN Last Out) principle, stack.push() -> pushes an element into the from the end array. stack.pop() -> removes an element from the end of the array. stack.top() -> Just gives us the topmost element of the array. Now following the sequences of push and pop: [1,2,3] then pop function is used, The newly formed array is: [1,2,4] then the top is used to retrieve the topmost element ‘4’ then again the pop function is used which removes 4. thus, the resulting array is: 1,2. Then it prints the topmost element (ie: 2).

Q105. Which choice is a valid way to overload the ternary conditional operator?

Reference

Q106. Which class hierarchy represents an example of multilevel inheritance?

Q107. Which of the following is the correct syntax to print the message in C++ language?

Reference

Q108. Consider the following program. What will be the output/error?

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
bool compare(char c1, char c2){
return tolower(c1) > tolower(c2);    //LINE-1
}
int main(){
char arr1[20] = "C++ Program", arr2[20] = "C Program";
cout << lexicographical_compare(arr1, arr1+strlen(arr1), arr2, arr2+strlen(arr2),
compare);
return 0;
}

Reference

Q109. Consider the following code segment. Fill in the blank at LINE-1 so that the program will print “not found”?

#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int data[] = {1,2,3,4,5};
int key = 5;
if(binary_search(__________))    //LINE-1
cout << "found";
else
cout << "not found";
return 0;
}

Reference

Q110. Consider the following code segment. What will be the output?

#include <iostream>
#include <algorithm>
using namespace std;
int main () {
int data[] = {50, 30, 40, 10, 20};
sort (&data[1], &data[4]);
for (int i = 0; i < 5; i++)
cout << data[i] << " ";
return 0;
}

Reference

Q111. Consider the following code segment. What will be the output?

#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int element[5];
for(int i = 1; i <= 5; i++)
*(element + i - 1) = i * 5;
rotate(element, element + 4, element + 5);
rotate(element, element + 1, element + 4);
for (int i = 0; i < 5; ++i)
cout << element[i] << " ";
return 0;
}

Reference

Q112. Consider the following code segment. What will be the output?

#include <iostream>
#include <vector>
using namespace std;
int main() {
const int size = 3, c = 65;
vector<char> vc(size, A);
for (int i = 1; i <= 2; i++)
vc.push_back(65 + i);
vc.resize(10, 90);
vc.resize(8);
for (int i = 0; i < vc.size(); i++)
cout << vc[i] << " ";
return 0;
}

Reference

Q113. Consider the following code segment. Choose the appropriate option to fill in the blank at LINE-1, such that the output of the code would be: a C++ Program.

#include <iostream>
#include <string>
using namespace std;
int main(void) {
string s1 = "C++ ";
string s2 = "Program";
__________________;    //LINE-1
cout << s1;
return 0;
}

Reference

Q114. Consider the following code segment. Fill in the blank at LINE-1 such that the output is 5 2 3 4 5

#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int data[] = { 1, 2, 3, 4, 5 };
for (int i = 0; i < 1; i++) {
int j = data[i];
replace(data, data + 5, j, *(_________________));    //LINE-1
}
for (int i = 0; i < 5; ++i)
cout << data[i] << " ";
return 0;
}

Reference

Q115. Consider the following code segment. What will be the output?

#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
int main(){
char str[10] = "123456789";
stack<char> s1, s2;
int i;
for(i = 0; i < strlen(str)/2; i++)
s1.push(str[i]);
for(i=i-1; i < strlen(str); i++)
s2.push(str[i]);
while (!s1.empty()) {
s2.push(s1.top()); s1.pop();
}
while (!s2.empty()) {
cout << s2.top(); s2.pop();
}
return 0;
}

Reference

Q116. Consider the following code segment. Which statement/statements is/are correct?

int i = 5;
const int *p = &i;
int * const q = &i;
int const *r = &i;
int const * const s = &i;
*p = 10; //STMT-1
*q = 10; //STMT-2
*r = 10; //STMT-3
*s = 10; //STMT-4

Reference

Q117. Consider the following program. What will be the output/error(s)?

#include <iostream>
using namespace std;
char add(char c1 = a) { return c1; }
char add(char c1 = a, char c2 = b) { return c1 + c2 - a;}
char add(char c1 = a, int d1 = 100){ return c1 + d1 - a; }
char add(char c1 = a, char c2 = b, char c3) { return c1 + c2 + c3 - a; }
int main() {
char c = add(o, k);
cout << c << endl;
return 0;
}

Reference

Q118. Consider the following code segment. What will be the output?

#include <iostream>
using namespace std;
#define SQR(x) (x)*(x)
int main() {
int a=3;
cout << SQR(a++) << endl;
return 0;
}

Reference

Q119. Consider the following code segment. Which line/s will give you an error?

#include<iostream>
#define X 1
using namespace std;
int main(){
int i;
const int i1 = 2;
const int i2 = i1; //LINE-1
i2 = X;
i = i1;
i1 = i;
return 0;
//LINE-2
//LINE-3
//LINE-4
}

Q120. Consider the following code segment. What will be the output/error?

#include<iostream>
using namespace std;
int main(){
int a = 5;
int &b = a+1;
a = a*b;
cout << a << " " << b;
return 0;
}

#Detailed explanation: The error is occurring because it is trying to create a reference to a temporary value. In the line int &b = a+1; we are attempting to create a reference b to the result of the expression a + 1, which is a temporary value. References must be bound to an actual object, not a temporary value or an expression that does not have a memory location.

Q121. Consider the following code segment. What will be the output?

#include <iostream>
using namespace std;
int& func(int& i) {     //LINE-1
return i = i+5;
}
int main() {
int x = 1, y = 2;
int& z = func(x);
cout << x << " " << z << " ";
func(x) = y;
cout << x << " " << z;
return 0;
}

Reference

Q122. Consider the following code segment. Choose the appropriate option to fill in the blanks at LINE-1, such that the output of the code would be: 300 20000.

#include <iostream>
using namespace std;
void compute(int n1, int n2, ________, ________){ //LINE-1
n3 = n1 + n2;
*n4 = n1 * n2;
}
int main(){
int a = 100, b = 200, c = 0, d = 0;
compute(a, b, c, &d); //LINE-2
cout << c << ", ";
cout << d;
return 0;
}

Q123. Consider the following code segment. What will be the output/error?

#include <iostream>
using namespace std;
int main() {
int a = 2, *b;
*b = 5;
int * const ptr;    // LINE-1
// LINE-2
ptr = b;
cout << *ptr;
return 0;
}

Q124. Consider the following code segment. What will be the output/error?

#include <iostream>
using namespace std;
void fun(int a = 5) { cout << a << endl; }
//LINE-1
int fun(int x = 10) { cout << x << endl; return 0; }    //LINE-2
int main() {
fun();
return 0;
}

Q125. Consider the following code segment. Fill in the blank at LINE-1 such that the program will print 5 + i3

#include<iostream>
using namespace std;
struct complex{
int re, im;
void show(){ cout << re << " + i" << im; }
};
______________________________________{ //Line-1
c2.re = c1.re+c2.re;
c2.im = c1.im+c2.im;
return c2;
}
int main(){
struct complex c1={2,5},c2{3,-2};
struct complex t = c1 + c2;
t.show();
return 0;
}

Reference

Q126. Consider the following program. Which line/s will generate an error?

#include<iostream>
using namespace std;
class myClass{
int pra = 5;
public:
int pub = 10;
void set_pr(int x){ pra = x; }
void set_pu(int x){ pub = x; }
};
int main(){
myClass m;
int a, b;
a = m.pra; //LINE-1
b = m.pub; //LINE-2
m.set_pr(100); //LINE-3
m.set_pu(200); //LINE-4
return 0;
}

Reference

Q127. Consider the following class. Fill in the blanks with proper access specifiers so that member y can be accessed from outside of the class but member x cannot be accessed.

class Test{
________:
int x;
________:
int y;
/* Some more code */
};

Q128. Which C++ Standard did add in-class default member initializers?

Q129. Can you use auto type deduction for non-static data members?

Q130. Do you need to define a static inline data member in a cpp file?

Reference

Q131. What’s the output of the following code:

struct S {
    int a { 10 };
    int b { 42 };
};
S s { 1 };
std::cout << s.a << ", " << s.b;

Q132. Can a static inline variable be non-constant?

Q133. Consider the following code:

struct C {
    C(int x) : a(x) { }
    int a { 10 };
    int b { 42 };
};
C c(0);

Q134. What happens when you throw an exception from a constructor?

Q135. What happens when you compile this code?

struct Point { int x; int y; };
Point pt {.y = 10, .x = 11 };
std::cout << pt.x << ", " << pt.y;

Q136. Will this code work in C++11?

struct User { std::string name = "unknown"; unsigned age { 0 }; };
User u { "John", 101 };

Q137. Assume you have a std::map<string, int> m;. Select the single true statement about the following loop:

for (const pair<string, int>& elem : m)

Q138. Identify the correct extension of the user-defined header file in C++.

Q139.Identify the incorrect constructor type.

auto x = 4000.22;

Q140.Which of the following data types is supported in C++ but not in C?

Q141. Identify the correct syntax for declaring arrays in C++.

Q142.Size of wchat_t is.

Q143.Which of the following loops is best when we know the number of iterations?

Q144.Which keyword is used to define the macros in C++?

Q145.Which of the following operators should be preferred to overload as a global function rather than a member method?

Q146.How can we restrict the dynamic allocation of objects of a class using new?

Q147.What is the time complexity of the below code?

for(int i=0;i<n;i++){
   for(int j=0;j<n;j++){
       cout<<"hello";
    }
}

Q148.What is the output of the code given below?

int a=10;
int k=++a;
int m=a++;
cout<<k+m;

Both ++a and a++ increase the value of a by 1 (ie: 11) and hence k+m becomes 22.

Q149. Which C++ construct is used for exception handling?

Q150. What is the purpose of the break statement in a loop in C++?

Q151. In C++, what is the purpose of the const keyword when used with a variable?

Q152. Which is more effective while calling the C++ functions??

Q153. What will be the output of the following C++ program?

#include <iostream>
using namespace std;
class A{
public:
	A(){
		cout<<"Constructor called\n";
	   }
	~A(){
		cout<<"Destructor called\n";
	    }
};
int main(int argc, char const *argv[])
{
	A *a = new A[5];
	delete[] a;
	return 0;
}

Q.154 What is the output of this code?

printf("1/2 = %f",(float)(1/2));
class my_class{
    public: static int count;
}

Q156. What is the purpose of the constexpr keyword in C++?

Ref

Q157. What is a template metaprogram in C++?

Q158.Identify the correct example for a pre-increment operator.

Q159. What will be the output of following code?


int matrix[3][3] = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};
for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){
        int a = mat[i][j];
        mat[i][j] = mat[j][i];
        mat[j][i] = a;
    }
}