Here I will be mainly focusing on **OnCampus technical Interviews** because I have attended a couple of them. and I observed these are common questions to check you know the basics of coding or not.

#### 1.To swap two numbers without using an extra variable

```
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int a,b;
System.out.println("Enter two numbers");
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
b = sc.nextInt();
//logic to swap without using extra variable.
a = a+b;
b = a - b;
a = a - b;
System.out.println("After swapping: "+ "a="+ a +"," + "b="+ b );
}
}
```

#### 2.Check given number is Amstrong Number or not

To Know more about Amstrong Number refer the link here

```
import java. util.*;
public class Main
{
public static void main(String[] args)
{
int a;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
a = sc.nextInt();
//to find length or number of digits in number
int num = a;
int len = 0;
int rem;
while(num!=0)
{
rem = num%10;
num = num/10;
len++;
}
//logic to check Armstrong number or Not
int sum = 0;
num = a;
while(num!=0)
{
rem = num%10;
int mul= 1;
for(int i=0;i<len;i++)
{
mul = mul * rem;
}
sum +=mul;
num= num/10;
}
if(sum==a)
{
System.out.println("Amstrong Number");
}
else{
System.out.println("Not an Amstrong number");
}
}
}
```

#### 3.To find out the Fibonacci value

```
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
n = sc.nextInt();
int n1 = 0 , n2 = 1;
int res = 0;
for(int i=2;i<=n;i++)
{
res = n1+n2;
n1 = n2;
n2 = res;
}
System.out.println("Fib of n is " + res);
}
}
```

#### 4.to print Fibonacci series

```
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
n = sc.nextInt();
int n1 = 0 , n2 = 1;
int res = 0;
System.out.print("Fibonacci series is " + n1 + " " + n2 + " ");
for(int i=2;i<=n;i++)
{
res = n1+n2;
n1 = n2;
n2 = res;
System.out.print(res + " ");
}
}
}
```

#### 5.Factorial of a number

```
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
n = sc.nextInt();
int fact = 1;
for(int i=1;i<=n;i++)
{
fact = fact * i;
}
System.out.println("Factorial of n is:" + fact);
}
}
```

#### 6.LCM and GCD of a number

```
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n1,n2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
n1 = sc.nextInt();
n2 = sc.nextInt();
int numerator,denominator;
if(n1>n2)
{
numerator = n1;
denominator = n2;
}
else{
numerator = n2;
denominator = n1;
}
int rem = numerator%denominator;
while(rem!=0)
{
numerator = denominator;
denominator = rem;
rem = numerator%denominator;
}
System.out.println("HCF of numbers:" + denominator );
int lcm = (n1*n2)/denominator;
System.out.println("LCM of numbers:" + lcm );
}
}
```

#### 7.Program to check leap year or not

```
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n1,n2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the year");
int year = sc.nextInt();
if(year%4==0)
{
//check for century year
if(year%100==0)
{
//check it is divisible by 400
if(year%400==0)
{
System.out.println("Leap year");
}
else{
System.out.println("Not a leap year");
}
}
else{
System.out.println("Leap year");
}
}
else{
System.out.println("Not a leap year");
}
}
}
```

I hope this helps for your OnCampus technical Interviews.

## Discussion (6)

A. I think it’s super cool that you are sharing this. I’m sure it will help people interviewing.

B. Not related to you, but I’m generally confused why anyone would test a candidates ability to creatively solve problems by having them swap out variables without using a placeholder or printing out a Fibonacci sequence.

ad B) simple "follow the herd" principle applied by companies doing those interviews ;) and in return they'll get the candidates who most diligently rote memorized the questions and the answers

yes I shudder at all of this, so glad they never made me go through this kind of nonsense when I took my first steps as a junior ... customers pay us to solve their problems, not to generate Fibonacci numbers

Thank you for reading the post and sharing your views

I often ask this when doing interviews and I reject any candidate that doesn't point out that swapping two variables without using a temp variable is

extremely bad practiceandshould never, never, never, never, never, never be done in a real project.Likewise, the correct answer to this question is that dates are non-trivial and built-in packages should be used for any date handling or manipulation; one shouldn't be rolling their own logic for such things.

EDIT: I just noticed who the author is and I have to say I'm glad you are writing more articles. I love your vocabulary building series. :)

Thank you for reading the post and sharing your views

## 1. To swap 2 numbers without using an extra variable

The real insight here is that the solution presented could cause

arithmetic overflowif the user entered sufficiently large inputs.An interviewer would likely ask the question to see how fluent programmer is with java data types and to see good the programmer is at spotting potential errors.

Anyone with further questions like this can ask away. I am on Twitter at @whiteowled .