10 tricky C++ questions of which are mostly asked in Interview


Hello Everyone Today in this blog you are going to know the 10 tricky C++ questions of which are mostly asked in Interview.

A good tricky C++ question which is mostly asked in 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.

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

We use the idea of recursively calling the main function.

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

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

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.

You May Also Like:

6. Program to verifies the condition inside if block.

CPP program to 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

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

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:

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.

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.

You May Also Like:

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

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.


No Comment.