Hi,
For all the final year graduates and freshers, I have got a series of java coding interview questions and answers, which is good to prepare before appearing on any Java interviews. Java coding questions are based on programming, logical analysis and problem solving skill , so you stand a chance to clear the technical/coding round on your first attempt.This list mainly contains basic programs asked on-campus and off-campus placement drive.Analyze the problems and you can also create other ways to solve them.
1. List of even numbers
/*
List Even Numbers Java Example.
This List Even Numbers Java Example shows how to find and list even
numbers between 1 and any given number.
*/
public class ListEvenNumbers {
public static void main(String[] args) {
//define limit
int limit = 50;
limit);
System.out.println("Printing Even numbers between 1 and " +
for(int i=1; i <= limit; i++){
}
// if the number is divisible by 2 then it is even
if( i % 2 == 0){
System.out.print(i + " ");
}
}
}
Output of List Even Numbers Java Example would be
/*
Printing Even numbers between 1 and 50
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
*/
2. Factorial of a number
/*
This program shows how to calculate Factorial of a number.
Factorial of any number is! n.
For example, factorial of 4 is 4 * 3 * 2 * 1.
*/
public class NumberFactorial {
public static void main(String[] args) {
int number = 5;
int factorial = number;
for(int i =(number - 1); i > 1; i--)
{
factorial = factorial * i;
}
}
System.out.println("Factorial of a number is " + factorial);
}
Output of the Factorial program would be
/*
Factorial of a number is 120
*/
3. Compare Two Numbers using else-if
/*
Compare Two Numbers Java Example
This Compare Two Numbers Java Example shows how to compare two numbers
using if else if statements.
*/
public class CompareTwoNumbers {
public static void main(String[] args) {
//declare two numbers to compare
int num1 = 324;
int num2 = 234;
if(num1 > num2){
System.out.println(num1 + " is greater than " + num2);
}
else if(num1 < num2){
System.out.println(num1 + " is less than " + num2);
}
else{
System.out.println(num1 + " is equal to " + num2);
}
}
}
Output of Compare Two Numbers Java Example would be
/*
324 is greater than 234
*/
4. Determine If Year Is Leap Year
/*
Determine If Year Is Leap Year Java Example
This Determine If Year Is Leap Year Java Example shows how to
determine whether the given year is leap year or not.
*/
public class DetermineLeapYearExample {
public static void main(String[] args) {
//year we want to check
int year = 2004;
//if year is divisible by 4, it is a leap year
if(year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))
System.out.println("Year " + year + " is a leap year");
else
System.out.println("Year " + year + " is not a leap year");
}
}
Output of the example would be
/*
Year 2004 is a leap year
*/
5. Fibonacci Series
/*
Fibonacci Series Java Example
This Fibonacci Series Java Example shows how to create and print
Fibonacci Series using Java.
*/
public class JavaFibonacciSeriesExample {
public static void main(String[] args) {
//number of elements to generate in a series
int limit = 20;
long[] series = new long[limit];
//create first 2 series elements
7series[0] = 0;
series[1] = 1;
//create the Fibonacci series and store it in an array
for(int i=2; i < limit; i++){
series[i] = series[i-1] + series[i-2];
}
//print the Fibonacci series numbers
System.out.println("Fibonacci Series upto " + limit);
for(int i=0; i< limit; i++){
System.out.print(series[i] + " ");
}
}
}
Output of the Fibonacci Series Java Example would be
/*
Fibonacci Series upto 20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
*/
6. Palindrome Number
/*
This program shows how to check for in the given list of numbers
whether each number is palindrome or not
If the number is equal to it's reversed number, then
the given number is a palindrome number.
For ex,121 is a palindrome number while 12 is not.*/
public class JavaPalindromeNumberExample {
public static void main(String[] args) {
//array of numbers to be checked
int numbers[] = new int[]{121,13,34,11,22,54};
//iterate through the numbers
for(int i=0; i < numbers.length; i++){
int number = numbers[i];
int reversedNumber = 0;
int temp=0;
//reverse the number
while(number > 0){
temp = number % 10;
number = number / 10;
reversedNumber = reversedNumber * 10 + temp;
}
if(numbers[i] == reversedNumber)
System.out.println(numbers[i] + " is a palindrome");
else
System.out.println(numbers[i] + " not a palindrome ");
}
}
}
Output of Java Palindrome Number Example would be
/*
121 is a palindrome number
13 is not a palindrome number
34 is not a palindrome number
11 is a palindrome number
22 is a palindrome number
54 is not a palindrome number
*/
7. Generate prime numbers between 1 & given number
/*
Prime Numbers Java Example
This Prime Numbers Java example shows how to generate prime numbers
between 1 and given number using for loop.
*/
public class GeneratePrimeNumbersExample {
public static void main(String[] args) {
//define limit
int limit = 100;
System.out.println("Prime numbers between 1 and " + limit);
//loop through the numbers one by one
for(int i=1; i < 100; i++){
boolean isPrime = true;
//check to see if the number is prime
for(int j=2; j < i ; j++){
if(i % j == 0){
isPrime = false;
break;
}
}
}
// print the number
if(isPrime)
System.out.print(i + " ");
}
}
Output of Prime Numbers example would be
/*
Prime numbers between 1 and 100
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
*/
8. Pyramid of stars using nested for loops
/*
Java Pyramid 1 Example
This Java Pyramid example shows how to generate pyramid or triangle
like given below using for loop.
*
**
***
****
*****
*/
public class JavaPyramid1 {
public static void main(String[] args) {
for(int i=1; i<= 5 ;i++){
for(int j=0; j < i; j++){
System.out.print("*");
}
//generate a new line
System.out.println("");
}
}
}
Output of the above program would be
/*
*
**
***
****
*****
*/
9. Reversed pyramid using for loops & decrement operator.
/*
Java Pyramid 5 Example
This Java Pyramid example shows how to generate pyramid or triangle
like given below using for loop.
12345
1234
123
12
1
*/
public class JavaPyramid5 {
public static void main(String[] args) {
for(int i=5; i>0 ;i--){
for(int j=0; j < i; j++){
System.out.print(j+1);
}
}
System.out.println("");
}
}
Output of the example would be
/*
12345
1234
123
12
1
*/
10. Nested Switch
/*
Statements Example
This example shows how to use nested switch statements in a java program.
Like any other Java statements, switch statements
can also be nested in each other as given in
below example.
*/
public class NestedSwitchExample {
public static void main(String[] args) {
int i = 0;
int j = 1;
switch(i)
{
case 0:
switch(j)
{
case 0:
System.out.println("i is 0, j is 0");
break;
case 1:
System.out.println("i is 0, j is 1");
break;
default:
System.out.println("nested default
case!!");
}
break;
default:
System.out.println("No matching case found!!");
}
}
}
Output would be
/*
i is 0, j is 1
*/
Hope, the Part-I tutorial gave you some knowledge of what questions are asked in some interviews.
In the next session, we will continue with other important Java problems asked by Interviewer.
0 Comment(s)