- 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 "))
{
}
}