Table Of Contents

- 1 Java Program Asked in Interviews & Exams 2018 {With Output)
- 1.1 Java Program to Check Entered Character is Vowel or Consonant
- 1.2 Java Program to Find the Largest Number
- 1.3 Java Program to calculate the sum of natural numbers using for loop
- 1.4 Java Program to Find a Reverse String – Using Static Method
- 1.5 Java Program to Print a square pattern
- 1.6 Java Program to print Odd and Even Numbers from an Array
- 1.7 Java Program to Find Smallest Number in an Array
- 1.8 Linear Search in Java
- 1.9 Binary Search in Java
- 1.10 Factorial Program Using a Loop in Java
- 1.11 Fibonacci Series in Java Without Using Recursion
- 1.12 Prime Number Program in Java
- 1.13 Java Program to multiply two matrices
- 1.14 Java Program to add two matrices

Searching Java Program? We have curated a list of Java programs frequently asked in Interviews and Exams in 2018. We have also attached their output with their source code that will help you to learn easily. As you may be knowing, Java is a programming language which helps a lot in web development and creation of the various type of applications like – mobile app, and web application. Moreover, it finds its use in game consoles and data centers — there will be many websites and applications which will cease to work if Java is not there. Read>

**Share this post> [DISPLAY_ULTIMATE_SOCIAL_ICONS]**

## Java Program Asked in Interviews & Exams 2018 {With Output)

Java released in 1996 due to the rigorous efforts of Sun Microsystems. It is the fast, simple, and secure object-oriented programming language. In addition, it is a case-sensitive language, this means Classytec and classytec are not the same in this language. Thus, while writing a Java program, you have to put an extra care of uppercase and lowercase letters. Install java free

Today, everyone wants to get a good job by clearing interviews. Cracking an Interview depends on the hard work that you’ve put in. To help you crack interviews & exams, we have curated a list of Java program frequently asked in Interviews and competitive exams in 2018. We hope that practicing these programs will definitely help you to clear your interviews, tests, and exams. All the best! Also, Don’t miss out our previous post to know about C programs asked in interviews – C Programs that are Asked in Interviews & Exams in 2018 {With C output}

### Java Program to Check Entered Character is Vowel or Consonant

Here, we have illustrated a program in which program will ask the user to enter a character, then the entered character will be checked for the Vowel or consonant. First, see what are vowels and consonants in the English language.

**Vowels** – The English language has 26 alphabets, out of which five alphabet letters – A, E, I, O, and U are vowels. Vowels basically represent a speech sound that is created by the vocal cords. Because Java is case sensitive, we will consider vowels – A, E, I, O, U, a, e, i, o, and u.

**Consonant** – The 21 alphabets which are not vowels, are termed as consonants. Consonant represents a speech sound which is non-vowel. Basically, this sound is created by the vocal tract closure (completely or partially).

public static void main(String[] args) { { int i=0; Scanner sc=new Scanner(System.in); System.out.println("Enter a character : "); char ch=sc.next( ).charAt(0); switch(ch) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : case 'A' : case 'E' : case 'I' : case 'O' : case 'U' : i++; } if(i==1) System.out.println("Entered character "+ch+" is Vowel"); elseif((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) System.out.println("Entered character "+ch+" is Consonant"); else System.out.println("Not a word "); } } }

### Java Program to Find the Largest Number

This Java program finds the largest of three numbers and then prints it. The program will first ask the user to enter the three numbers, then it will find out the largest of the three number. After that, this will print the output result, i.e. the largest number.

public static void main(String[] args) { { int x, y, z; System.out.println("Enter three integers "); Scanner in = new Scanner(System.in); x = in.nextInt(); y = in.nextInt(); z = in.nextInt(); if ( x > y && x > z ) System.out.println("First number is largest."); else if ( y > x && y > z ) System.out.println("Second number is largest."); else if ( z > x && z > y ) System.out.println("Third number is largest."); else System.out.println("Entered numbers are not distinct."); } } }

### Java Program to calculate the sum of natural numbers using for loop

The numbers starting from 1 are termed as natural numbers, i.e., 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,…., etc. We have illustrated a program which will display the sum of the first ten natural numbers. In mathematics, the sum of n natural numbers is –

1+2+3+….+n = (n (n+1) ) / 2

Sum of first 10 numbers = 1+2+3+….+10 = (10(10+1))/2 = 55

See, how the sum of first ten natural numbers can be calculated via Java programming>

public static void main(String[] args) { { int number = 10, i, total = 0; for(i = 1; i <= number; i++) { total = total + i; } System.out.println("Sum of first 10 natural numbers is: "+total); } }

### Java Program to Find a Reverse String – Using Static Method

In this program, we have illustrated how a word or a string can be reversed. For example – java is reversed to avaj or classytec is reversed to cetyssalc. Reversing a string is just like its mirror image, it is a fun to see the reversed string. Do it yourself, and have fun with Java coding.

public static void main(String[] args) { String str; Scanner scan=new Scanner(System.in); System.out.print("Enter a string : "); str=scan.nextLine(); System.out.println("Reverse of a String '"+str+"' is :"); int rev=str.length(); while(rev>0) { System.out.print(str.charAt(rev-1)); rev--; } }

### Java Program to Print a square pattern

Print a pattern program use a for loop and a particular logic. See how we have printed * pattern following symmetry. You can also try to print patterns of your choice like number, alphabets etc. These are usually asked in interviews, so we have illustrated here * pattern. You can also try other patterns following the method as shown below.

public static void main(String[] args) { int size = 5; for (int row = 1; row <= size; ++row) { for (int i = 1; i<= size; ++i) { System.out.print("*"); } System.out.println(); }

### Java Program to print Odd and Even Numbers from an Array

Numbers divisible by 2 are **EVEN numbers**, For example, even numbers are: 0,2,4,6,8,10,12,14,16,18,20, etc. The numbers which are not divisible by 2 are **ODD numbers**, For example, Odd numbers are: 1,3,5,7,9,11,13,15,17,19, etc. The program which we have illustrated below will help you print odd and even numbers from the array, that we have already given. You can also create a program to ask the user to input his own array and then check further which is odd and which is even number in the array.

public static void main(String[] args) { int a[]={22,126,3,2,123,44,55}; System.out.println("Odd Numbers:"); for(int i=0;i<a.length;i++) { if(a[i]%2!=0) { System.out.println(a[i]); } } System.out.println("Even Numbers:"); for(int i=0;i<a.length;i++) { if(a[i]%2==0) { System.out.println(a[i]); } } }

### Java Program to Find Smallest Number in an Array

The program below illustrates the way to find the smallest number in an array. We have used two arrays – a and b, you can also create a program to ask for the input arrays from the user. For simplicity, we have used arrays a and b with predefined values. The program will call a function to find out the smallest number in an array.

public static void main(String[] args) { int a[]={1,2,5,6,3,2}; int b[]={44,66,99,77,33,22,55}; System.out.println("Smallest in a: "+getSmallest(a,6)); System.out.println("Smallest is b: "+getSmallest(b,6)); } private static int getSmallest(int[] a, int total) { int temp; for (int i = 0; i < total; i++) { for (int j = i + 1; j < total; j++) { if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } return a[0]; } }

### Linear Search in Java

Linear search helps to find a key number which is the target value in an array. We have used key = 70, you can use some other value to search from the given array. Also, we have predefined an array with values. You can also create a program to get the input array from the user.

public static void main(String[] args) { int[] a1= {10,20,30,50,70,90}; int key = 70; // key to find in an array...you can try a different number System.out.println(key+" is found at index: "+linearSearch(a1, key)); } public static int linearSearch(int[] arr, int key){ for(int i=0;i<arr.length;i++){ if(arr[i] == key){ return i; } } return -1; } }

### Binary Search in Java

Binary search is very similar to linear search but here, the key value is compared with the middle value of the array. We took key = 50, this is our target value. We have to search for this value and the program given below will help in the search of this value and will give us the index of this value as output.

public static void main(String[] args) { int arr[] = {10,20,30,40,50}; int key = 50; int last=arr.length-1; binarySearch(arr,0,last,key); } public static void binarySearch(int arr[], int first, int last, int key) { int mid = (first + last)/2; while( first <= last ) { if ( arr[mid] < key ) { first = mid + 1; } else if ( arr[mid] == key ) { System.out.println("Element is found at index: " + mid); break; } else { last = mid - 1; } mid = (first + last)/2; } if ( first > last ) { System.out.println("Element is not found!"); } }

### Factorial Program Using a Loop in Java

**Factorial** in mathematics is the product of all non-negative integers (starting from 1). n! is the factorial for a positive integer n. The rule is the factorial of a negative number doesn’t exist and the factorial of 0 is 1, that why we have initialized the value of ” i”=1 in the program below.

n! = n*(n-1)*(n-2)*(n-3)…3.2.1

0! = 1.

For example – 5! = 5*4*3*2*1 = 120

public class JavaApplication3 { public static void main(String[] args) { int i,fact=1; int number=5;//It is the number to calculate factorial for(i=1;i<=number;i++) { fact=fact*i; } System.out.println("Factorial of "+number+" is: "+fact); } }

### Fibonacci Series in Java Without Using Recursion

The Fibonacci series is a special sequence of numbers following a rule. Fibonacci series is – 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,…, etc. Can you make out the rule this series form? If no, then let us tell you – the next number is calculated by adding two numbers preceding it. For example, 0+1 =1, 1+1=2, 1+2=3, 2+3=5, 5+3=8,…, and so on. The series by default start with whole numbers – 0 and 1, then the above rule of adding two numbers preceding the number follows up. Let’s see how the Java program can help us find out the Fibonacci series.

public class JavaApplication3 { public static void main(String[] args) { int n1=0,n2=1,n3,i,count=10; System.out.print(n1+" "+n2); //printing 0 and 1 for(i=2;i<count;++i) //loop starts from 2 because 0 and 1 are already printed { n3=n1+n2; System.out.print(" "+n3); n1=n2; n2=n3; } } }

### Prime Number Program in Java

A prime number is a natural number (greater than 1) which has only two factors 1 and the number itself. Prime numbers till 200 are: 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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199. The program below will help you check which is the prime number and which is not.

public class JavaApplication3 { public static void main(String[] args) { int i,m=0,flag=0; int n=3; //it is the number to be checked m=n/2; if(n==0||n==1) { System.out.println(n+" is not prime number"); } else { for(i=2;i<=m;i++) { if(n%i==0) { System.out.println(n+" is not prime number"); flag=1; break; } } if(flag==0) { System.out.println(n+" is prime number"); } }//end of else } }

### Java Program to multiply two matrices

Let us multiply two matrices using Java language. In the program below, we have already created two matrices, if you like, you can also get the input matrices from the user. If you get the input from the user, you have to verify the condition, like we used in the C program. The rule is – the number of columns in the first matrix must be equal to the number of rows in the second matrix. If this rule satisfies then the matrix multiplication is possible otherwise matrix multiplication is not possible.

package javaapplication3; import java.util.Scanner; /* @author Ijazullah */ public class JavaApplication3 { public static void main(String[] args) { //creating two matrices int a[][]={{1,2,3},{4,5,6},{7,8,9}}; int b[][]={{1,5,1},{2,2,2},{3,3,3}}; int c[][]=new int[3][3]; //3 rows and 3 columns for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { c[i][j]=0; for(int k=0;k<3;k++) { c[i][j]+=a[i][k]*b[k][j]; } System.out.print(c[i][j]+" "); } System.out.println(); } } }

### Java Program to add two matrices

The program shown below will add two matrices. Here, you can also create a program in which you ask the user to enter the two matrices. But for that, you have to write codes to check the rule of addition of matrices. The rule is – The column and rows of the first matrix must be equal to rows and column of the second matrix.

package javaapplication3; /*@author Ijazullah*/ public class JavaApplication3 { public static void main(String[] args) { int a[][]={{1,3,4},{2,4,3},{3,4,5}}; int b[][]={{1,3,4},{2,4,3},{1,2,4}}; //creating another matrix to store the sum of two matrices int c[][]=new int[3][3]; //3 rows and 3 columns //adding and printing addition of 2 matrices for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { c[i][j]=a[i][j]+b[i][j]; //use - for subtraction System.out.print(c[i][j]+" "); } System.out.println();//new line } } } }

We hope that this post will definitely help you excel in your interviews. Do check out our previous post on C programming, if you don’t till now. Please like, share and follow (Facebook, Twitter,…) to receive daily updates of published posts which will further help you to excel in your interviews. Also, comment below if you have any question related to this post.

Read more in this Interview series>

Python Program Asked in Interviews & Exams 2018 – 2019 {With Output)

Interview Preparation 2018 (Frequently Asked Questions to Programmer)