10 tricky C++ questions in Interview

Hello Everyone Today in this blog you are going to know Most asked 10 tricky C++ questions in an Interview.

A good tricky C++ question which is mostly asked in the Interview will be asked to write code or to write the output. The coding question will check his coding skills and will help the interviewer to know his coding abilities before hiring.

1. Print text within double quotes (” “).

This may seem easy but beginners may get puzzled while printing text within double quotes.

// CPP program to print double quotes 
#include<iostream> 

int main()
{ 
std::cout << "\"fulltotech\""; 
return 0; 
} 

2. Print all natural numbers up to N without using a semi-colon.

We use the idea of recursively calling the main function.

// CPP program to print all natural numbers upto 
// N without using semi-colon 
#include<iostream> 

using namespace std; 
int N = 10; 

int main() 
{ 
static int x = 1; 
if (cout << x << " " && x++ < N && main()) 
{ } 
return 0; 
} 
Output: 1 2 3 4 5 6 7 8 9 10

3. To Swap the values of two variables without using any extra variable

// C++ program to check if two numbers are equal 
#include<iostream> 

int main() 
{ 
int x = 10; 
int y = 70; 

x = x + y; 
y = x - y; 
x = x - y; 

cout << "X : " << x << "\n"; 
cout << "Y : " << y << "\n"; 
	
return 0; 
} 
Output:
X : 70
Y : 10

4. Program to find the Maximum and minimum of two numbers without using any loop or condition.

// CPP program to find maximum and minimum of 
// two numbers without using loop and any 
// condition. 
#include<bits/stdc++.h> 

int main () 
{ 
int a = 15, b = 20; 
printf("max = %d\n", ((a + b) + abs(a - b)) / 2); 
printf("min = %d", ((a + b) - abs(a - b)) / 2); 
return 0; 
} 
Output:
max = 20
min = 15

5. To find the sum of two integers without using ‘+’ operator.

This is a very easy mathematics trick.
We know that a + b = – (-a-b). So this will work as a trick for us.

// CPP program to print sum of two integers 
// withtout + 
#include<iostream> 

using namespace std; 
int main() 
{ 
int a = 5; 
int b = 5; 
int sum = -( -a-b ); 
cout << sum; 
return 0; 
} 
Output: 10


You May Also Like:

6. Program to verify the condition inside if block.

CPP program verifies the condition inside if block, it just verifies the condition inside if block,i.e., cout << “full” which returns a non-zero value,!(non-zero value) is false, hence it executes else Hence technically it only executes else block

#include <iostream>

using namespace std; 
int main() 
{ 
	if (!(cout << "full")) 
	cout <<" to "; 
	else
	cout << "totech "; 
	
	return 0; 
}
Output: fulltotech

7. How many times will this loop execute? Explain your answer.

unsigned char half_limit = 150;

for (unsigned char i = 0; i < 2 * half_limit; ++i)
{
    // do something;
}

If you said 300, you would have been correct if i had been declared as an int. However, since i was declared as an  unsigned char, the correct answer is that this code will result in an infinite loop.

Here’s why: The expression 2 * half_limit will get promoted to an int (based on C++ conversion rules) and will have a value of 300. However, since i is an unsigned char, it is represented by an 8-bit value which, after reaching 255, will overflow (so it will go back to 0), and the loop will, therefore, go on forever.

8. What is the output of the following code:

#include <iostream>

int main(int argc, const char * argv[]) {
    int a[] = {1, 2, 3, 4, 5, 6};
    std::cout << (1 + 3)[a] - a[0] + (a + 1)[2];
}
The above will output 8, since:

(1+3)[a] is the same as a[1+3] == 5

a[0] == 1

(a + 1)[2] is the same as a[3] == 4

This question is testing pointer arithmetic knowledge and the magic behind square brackets with pointers.

While some might argue that this isn’t a valuable question as it appears to only test the capability of reading C constructs, it’s still important for a candidate to be able to work through it mentally; it’s not an answer they’re expected to know off the top of their head, but one where they talk about what conclusion they reach and how.

9. What will i and j equal after the code below is executed? Explain your answer.

int i = 5;
int j = i++;

After the above code executes, i will equal 6, but j will equal 5.

Understanding the reason for this is fundamental to understanding how the unary increment (++) and decrement (--) operators work in C++.

When these operators precede a variable, the value of the variable is modified first and then the modified value is used. For example, if we modified the above code snippet to instead say int j = ++i;i would be incremented to 6 and then j would be set to that modified value, so both would end up being equal to 6.

However, when these operators follow a variable, the unmodified value of the variable is used and then it is incremented or decremented. That’s why, in the statement int j = i++; in the above code snippet, j is first set to the unmodified value of i (i.e., 5) and then i is incremented to 6.

10. What will the line of code below print out and why?

#include <iostream>

int main(int argc, char **argv)
{
    std::cout << 25u - 50;
    return 0;
}

The answer is not -25. Rather, the answer (which will surprise many) is 4294967271, assuming 32-bit integers. Why?

In C++, if the types of two operands differ from one another, then the operand with the “lower type” will be promoted to the type of the “higher type” operand, using the following type hierarchy (listed here from highest type to lowest type): long double, double, float, unsigned long int, long int, unsigned int, int (lowest). So when the two operands are, as in our example, 25u (unsigned int) and 50 (int), the 50 is promoted to also being an unsigned integer (i.e., 50u).

Moreover, the result of the operation will be of the type of the operands. Therefore, the result of 25u - 50u will itself is an unsigned integer as well. So the result of -25 converts to 4294967271 when promoted to being an unsigned integer.

Thank you for reading this blog, share this blog as much as possible and also show it to your friends.

If you have any questions, feel free to comment.

Leave a Reply

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

Discover more from

Subscribe now to keep reading and get access to the full archive.

Continue reading