Tricky Questions or Puzzles in C
This post is about the Tricky Questions or code snippet in C or C++ asked in most of the Interviews (See Interview Experience) by some of the good companies. You may know probably the right concept but it will not strike you at the interview and to crack all those Interview Questions you should know some of them beforehand. If you are applying for Job related to JAVA then checkout the blog post Tricky Questions in JAVA.
1) what will be the output of the following Printf function.
printf("%d",printf("%d",printf("%d",printf("%s","ILOVECPROGRAM"))));
Ans-ILOVECPROGRAM1321
The above printf line gives output like this because printf returns the number of character successfully written in the output. So the inner printf("%s","ILOVECPROGRAM") writes 13 characters to the output so the outer printf function will print 13 and as 13 is of 2 characters so the next outer printf function will print 2 and then next outer printf will print 1 as 2 is one character. So is the output 1321.
printf("%d",printf("%d",printf("%d",printf("%s","ILOVECPROGRAM"))));
Ans-ILOVECPROGRAM1321
The above printf line gives output like this because printf returns the number of character successfully written in the output. So the inner printf("%s","ILOVECPROGRAM") writes 13 characters to the output so the outer printf function will print 13 and as 13 is of 2 characters so the next outer printf function will print 2 and then next outer printf will print 1 as 2 is one character. So is the output 1321.
Want to become a Data Engineer? Check out below blog posts
2) What will be the output of the following conditional operator.
a=0?(3>2?23:(2>5?(7<6?34:48):64)):1
printf("%d",a);
Ans-1
The above code snippet is very simple actually but it is made to look like that it is very tough and most of us start solving the nested part without thinking a bit although we know the concept.So first think for five minutes. The concept behind this is when we use conditional operator then if the condition is true then we select the value before the colon and if it is false then the value after the column.
For example a>b?a:b.If a is greater then b then a otherwise b. So we have 0 before ? an operator that means false so no need to see the nested thing. The answer will be 1.
3) if(condition)
printf("I love" );
else
printf("C Language");
What should be the condition inside if statement such that it will print "I Love C Language"?
Ans- if(!printf("I Love"))
As the printf returns the number of characters successfully written on output it will return 6.And making it not will invert and make it 0 so else statement will print out.
4) Program to identify even or odd number without using any arithmetic operator, conditional statement, logical operators, Relational operator.This program was asked in Microsoft written test.
Ans- int main()
{
scanf("%d",&no);
(no&1)?printf("odd"):printf("even");
}
The above question can be solved using bitwise AND operator as it is not mentioned in question not to use. And also you can use the conditional operator as a conditional statement cannot be used. Taking bitwise AND of any number with 1 will give value y where y=0 if the number is Even and y=1 if the number is Odd because even number always ends with 0 so 0001 & ---0 will always give 0. On the other hand, the odd number always have 1 in the last position so 0001 & ---1 will always give 1.
Also, See
Program to implement other Arithmetic operators using + operator
Program to add two numbers without using + and any other Arithmetic operator
5) Program to find the sum of the digits of a number in a single statement.
Ans- int sum(int x)
{
int s;
for(s=0;x>0;s+=x%10,x/=10);
return s;
}
6) Print number from 1-100 without using a loop, Recursion, and Goto.
Ans - #include <stdio.h>
#include<conio.h>
#define STEP1 step();
#define STEP2 STEP1 STEP1
#define STEP4 STEP2 STEP2
#define STEP8 STEP4 STEP4
#define STEP16 STEP8 STEP8
#define STEP32 STEP16 STEP16
#define STEP64 STEP32 STEP32
#define STEP128 STEP64 STEP64
int n = 0;
int step()
{
if (++n <= 100)
printf("%d\n", n);
}
int main()
{
STEP128;
getch();
}
7) Deallocate memory without using free() in C
Ans- void *realloc(void *ptr,size_t size);
if size=0 then call to realloc is equivalent to free(ptr)
As realloc is used to deallocate previously allocated memory and if the size=0 then it will acts as free().
I will discuss more trick questions and puzzles of C and C++ in upcoming posts.
UPDATE : For Part-2 and Part-3 of Tricky Questions or Puzzles in C click here
Tricky Questions or Puzzles in C part-2
Tricky Questions or Puzzles in C part 3
Click HERE to watch SAMSUNG R&D Interview Experience
Very good question... really helpful!!!
ReplyDeleteThanks for your appreciation..vl post some more C puzzles :-)
ReplyDeletereally good work....thanx a lot frnd
ReplyDeletereally gud. microsoft puzzle was nice,.......
ReplyDeleteyo
DeleteAwesome question Thank you for it
ReplyDeleteGood questions, Thanks you.
ReplyDeletethnxx for such questions. Please post more such questions looking for more articles from you. and I too have collected some of the tricky programming questions from my personal experience and from my friends. Please have a review on this and i hope that it will really help you to improve your logics. :) http://csetips.catchupdates.com/tricky-programming-questions-interview/
ReplyDeleteFor 3rd one
ReplyDeleteCondition could be if(fork())
Thanks...Will post some more Tricky C C++ questions and answers soon :-)
ReplyDeleteThanku very much
ReplyDeletenice questions...,
ReplyDeleteint a=0.7;
ReplyDeleteif(a>0.7)
{
printf("hello");
}
else
{
printf("bye");
}
and same do with 0.8 my question is what will be the output ??
Write a C program which takes one integer number from user and then checks whether number is Even or Odd without using Modulus (%) and Division (/) operator.
ReplyDeletevoid main()
{
int i,n =2567;
for(i=n;i>0;)
{
i=i-2;
if(i==1)
{
printf("Odd");
break;
}
else if(i==0)
printf("Even");
}
}
very very helpful
ReplyDeleteAwesome..
ReplyDeleteits very useful for me. thank u
Brilliant tricky question its so many helpful for us.
ReplyDeleteI am trying to find out all factors of a number like for 20
ReplyDelete1x20
2x10
4x5
2x2x5
please help
I am trying to find out all factors of a number like for 20
ReplyDelete1x20
2x10
4x5
2x2x5
please help
YENİ PERDE MODELLERİ
ReplyDeleteNUMARA ONAY
mobil ödeme bozdurma
Nft Nasıl Alinir
Ankara evden eve nakliyat
Trafik Sigortası
Dedektör
kurma websitesi
aşk kitapları
Smm Panel
ReplyDeleteSmm panel
iş ilanları
instagram takipçi satın al
hirdavatciburada.com
WWW.BEYAZESYATEKNİKSERVİSİ.COM.TR
Servis
Tiktok jeton hilesi indir
çekmeköy alarko carrier klima servisi
ReplyDeleteataşehir alarko carrier klima servisi
üsküdar toshiba klima servisi
kartal bosch klima servisi
ümraniye bosch klima servisi
kartal arçelik klima servisi
ümraniye arçelik klima servisi
beykoz samsung klima servisi
üsküdar samsung klima servisi
nft nasıl alınır
ReplyDeletelisans satın al
en son çıkan perde modelleri
uc satın al
minecraft premium
özel ambulans
yurtdışı kargo
en son çıkan perde modelleri