Some tricky programming questions for interview

Tricky interviews

Programming logic is something really that is hard to achieve.And in most interviews you are asked such programming questions,and you are asked to solve it in a limited time.In some cases,the interviewer directly asks you the coding while you are face to face .And what if they are tricky?..You may be familiar with those questions,but all on a moment you may not get clicked.That doesn't mean that you are bad at logic.Frequent reviewing of some codings can surely help you develop your logics.In my hub,i have collected some codings which are indeed simple,some from my personal interview experiences and some from my friends.Please have a review on this and i hope that it will really help you to improve your logics.

Here are some programming questions...have a glance

Program to add two numbers without using arithmetic operator

public static void main(String args[])

{

int a=3,b=5;

While(b--)

a++;

System.out.println("sum is"+a);

}

Program to swap two mumbers without using a temporary variable

public static void main()

{

int a=4,b=5;

a=a+b; //a=9

b=a-b; //b=4

a=a-b; //a=5

System.out.println("After swapping"+a,+b);

}

Program to find largest of 2 numbers without using if-else

void main()

{

cin>>a>>b;; //let a=3and b=2

float c=|(a+b)|/2+|(a-b)|/2; //c=2.5+0.5=3.0

cout<<"largest number is"<<c;

}

Program to print the pattern:

         *
      *  *
   *  *  *
*  *  *  *


void main()

{

char st='*';

int n=4;

for(int i=1;i<=5;i++)

{

for(int s=n;s>=1;s--)

{

cout<<"";

}

for(int j=1;j<=i;j++)

cout<<st;

}

cout<<"\n";

--n;

}

Program to find largest and smallest among 5 numbers without using array

public static void main()

{

int a,b,c,d,e;

min=Math.min(a,Math.min(b,Math.min(c,Math.min(d,e))));

max=Math.max(a,Math.max(b,Math.max(c,Math.max(d,e))));

}

Seems funny?Try to answer tricky questions in a tricky way!

Program to find reverse of a number without using String or Array

public static void main()

{

int num ; //Let number = 1234

int rev = 0

last_digit = num - ((num / 10) * 10) // = 4

rev = last_digit // = 4

next_digit = (num / 10) - ((num / 100) * 10) //= 3

rev = (rev * 10) + next_digit = (4 * 10) + 3 //= 43

next_digit = (num / 100) - ((num /1000) * 100) //= 2

rev = (rev * 10) + next digit = (43 * 10) + 2// = 432

next_digit = (num / 1000) - ((num / 10000) * 1000) //= 1

rev = (rev * 10) + next_digit = (432 * 10) + 1 // = 4321

System.out.println("Reversed number is"+rev);

}

Program to print hello without using semicolumn

void main()

{

switch(printf("Hello "))

{

}

}


More by this Author


Comments 14 comments

Cogerson profile image

Cogerson 5 years ago from Virginia

Interesting hub...not that I understand very much of it...but I showed it to my wife(she is a computer programmer as well)...and she agreed with your logic...voted up and interesting.


leeba ann varghes profile image

leeba ann varghes 5 years ago from India Author

@Cogerson..thanks for that comment..


Sara 4 years ago

Huh!, I liked the variable swapping logic, great job!, keep it up.


Anuj 4 years ago

Try some of these also:

1. Print hello without having main program in C

2. Print hello without having semicolon in the program


leeba ann varghes profile image

leeba ann varghes 4 years ago from India Author

hi anuj,Ihave added one of your suggestions to my hub.And the other one-Print hello without having main program in C,I hope that is not possible in C..


Anirudhdhan 4 years ago

Very nice logic.. Was great but couldn't understand that finding reverse of a number... ((num/10)*10) = num itself know then, num-((num/10)*10) becomes zero??? And why is this like this min=Math.min(a,Math.min(b,Math.min(c,Math.min(d,e)); and not like min=Math.min(a,Math.min(b,Math.min(c,Math.min(d,e))));.. i mean the no. of braces.. Please reply!!!


leeba ann varghes profile image

leeba ann varghes 4 years ago from India Author

hi Anirudhdhan ,

In the case of ((num/10)*10),th result is not the numbr itself.If the number is 1234,then num/10 is 123(since the datatype is int,it wont take the value aftr decimal),and hence ((num/10)*10) is 123*10=1230.Hence

num-((num/10)*10) will be 1234-1230 =4.

And in the case of the second one,you are right.Thanks for that and I have changed that in my hub.


ruchinshah 3 years ago

Hi,

Don't know exactly, but isn't the increment operator used in the first question an arithmetic operator? bitwise-xor and shift operators can also be used to add to variables.


Shrinivas Shukla 3 years ago

write a C program to print "C is helps to develop logic".

main() should not contain more than 3 characters


Mary Mini A 3 years ago

I need more tricky questions like this


guest 2 years ago

helping containts..

good job..


Nitin Jaiswal 2 years ago

Add a question where user will be able to print numbers from 1 to 1000 without using any loop statement(while, for, do...while, etc).


Omi Tewary 23 months ago

Reverse number can be made easier with following code:

int rn=0,n=1234;

int t=n;

while(t!=0){

rn=rn*10+t%10;

t=t/10;

}

S.o.p(rn);


Manu 15 months ago

I agree with Omi.

No need t write so much coding.

do with while loop easily

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working