- HubPages
*»* - Technology
*»* - Computers & Software
*»* - Computer Science & Programming

# 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 "))

{

}

}

## Comments

I agree with Omi.

No need t write so much coding.

do with while loop easily

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);

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).

helping containts..

good job..

I need more tricky questions like this

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

main() should not contain more than 3 characters

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.

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!!!

Try some of these also:

1. Print hello without having main program in C

2. Print hello without having semicolon in the program

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

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.

14