Guess the output of this simple C program. You can check these 2 video tutorials listed below so that you get to know todays interview / viva question.
#include < stdio.h >
int main()
{
int num;
printf("Enter a number\n");
scanf("%d", &num);
if(num = 5)
{
printf("You entered number 5\n");
}
else
{
printf("You entered %d \n", num);
}
return 0;
}
If user enters num value as 10. What does the above C Program output?
Answer: Did you answer – You entered 10 as output? If yes, then its wrong.
Note: 1. If at first glance of the source code you didn’t get the answer right, then go through the videos I’ve posted in ‘Related Read” section above. 2. After going through the 2 videos I’ve posted in ‘Related Read’ section above, if you still didn’t get the answer, then please open your editor and type the program and execute it one line at a time.
Hint: assignment operator(=) and equality operator(==) are different and work differently in C programming.
As you can see in the condition of if, it’s written as num = 5. = is assignment operator. So here 5 is being assigned to variable num, and thus it returns 5. In C program, any non-zero number is considered as true. So every time the condition in if becomes true, so the block of code present in if gets executed. i.e., value of num is 5.
#include < stdio.h >
int main()
{
int num;
printf("Enter a number\n");
scanf("%d", &num);
if(num == 5)
{
printf("You entered number 5\n");
}
else
{
printf("You entered %d \n", num);
}
return 0;
}
Output 1: Enter a number 10 You entered 10
Output 2: Enter a number 5 You entered number 5
Output 3: Enter a number 1000 You entered 1000
Note: Both programs are correct syntactically. That’s the reason compiler did not throw any error. It’s a logical error, so we need to be very careful while writing programs. If we do this simple logical error in a big program, it might take a lot of time to get to know the bug and fix it. So careful. Happy coding.
All sparse indexes are unique index, but not all unique indexes are sparse! There are situations where we want to create unique index on key/field which is not present in all documents. Wherever the key is not present, it’s value will be treated as NULL. If more than 1 document has NULL value to the key we want to make as unique, then it violates unique key rule. In such situations we can make use of Sparse index and create unique key on only those documents which has the key in it.
Since last 2 documents do not have “product” key, its value will be treated as “NULL”. Since both these documents have value of “product” as “NULL”, trying to create unique key on “product” throws duplicate key error.
This command sorts all the documents in lexicographical order. The output includes all the documents. Those documents which do not have “product” key in them are listed first.
Since the command is even listing/sorting the documents which do not have “product” key in them, it’s simply making use of Basic Cursor – which do not help in optimizing the command/query performance.
hint() method tells the mongoDB server to operate only on the sparse key.. hence only retrieving and operating on the documents which has the sparse key in them.
After using hint() method, mongoDB server is only looking for documents which has the sparse index key in them, so it can directly look for the sparse index key inside “system.indexes” collection, hence it’s using Btree Cursor – and is efficient.