07:30
Here are my list of 10 Java coding interview questions and answers, which is good to prepare before appearing on any Java interviews. As I said Java coding questions are mostly based on programming, logical analysis and problem solving skill , so better to get it right in first place. Any way you may be able to solve and find answers of these Java coding questions by yourself, but if you stuck do a google, and you can get many alternative ways to solve these problem. Some times knowing more than one way to solve any programming question or coding problem in Java also helps to impress interviewer. This list mainly contains basic programs asked on Interviews.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import java.util.Scanner;
public class EvenOdd{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter a number which you want to check whether that is even or odd");
int n = in.nextInt();
if(n%2==0){
System.out.println(n+" is an even number.");
}else{
System.out.println(n+" is an odd number.");
}
}
}
|
Output
|
Enter a number which you want to check whether that is even or odd
4
4 is an even number.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import java.util.Scanner;
public class Swapping{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the 1st number: ");
int x = in.nextInt();
System.out.println("Enter the 2nd number: ");
int y = in.nextInt();
System.out.println("Initial value of x: "+x+" and y: "+y);
x = x+y;
y = x-y;
x = x-y;
System.out.println("After swapping value of x: "+x+" and y: "+y);
}
}
|
Output
|
Enter the 1st number:
43
Enter the 2nd number:
56
Initial value of x: 43 and y: 56
After swapping value of x: 56 and y: 43
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import java.util.Scanner;
public class Factorial{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the number whose factorial you want: ");
int n = in.nextInt();
int f =1;
for(int i=n; i>0; i--){
f = f*i;
}
System.out.println("Factorial of "+n+" is "+f);
}
}
|
Output
|
Enter the number whose factorial you want:
6
Factorial of 6 is 720
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package javaTutorial;
import java.util.ArrayList;
import java.util.Scanner;
public class GetPrimeNumbers{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter a number from which you want prime number: ");
int p1 = in.nextInt();
System.out.println("Enter one more number till which you want prime number: ");
int p2 = in.nextInt();
ArrayList<Integer> prime = new ArrayList<Integer>();
int i=2;
for(int p=p1; p<=p2; p++){
i=2;
for(; i<10; i++){
if(p%i==0 && p!=i){
break;
}
}
if(i==10){
prime.add(p);
}
}
System.out.println("Prime numbers between "+p1+" and "+p2+" are: ");
for(int j=0; j<prime.size(); j++){
System.out.print(prime.get(j).toString()+", ");
}
}
}
|
Output
|
Enter a number from which you want prime number:
10
Enter one more number till which you want prime number:
30
Prime numbers between 10 and 30 are:
11, 13, 17, 19, 23, 29,
|
Note- A number is prime if it is not divisible by any other number except itself.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import java.util.Scanner;
public class PrimeNumber{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter a number greater than 2 which you want to check whether that number is prime or not: ");
int p = in.nextInt();
int i=2;
for(; i<10; i++){
if(p%i==0 && p!=i){
System.out.println("Entered number "+p+" is not a prime number.");
break;
}
}
if(i==10){
System.out.println("Entered number "+p+" is a prime number.");
}
}
}
|
Output
|
Enter a number greater than 2 which you want to check whether that number is prime or not:
139
Entered number 139 is a prime number.
|
Note- A number is armstrong if the sum of the cubes of digit of number is equal to the number.
ex- 407 = 4*4*4 + 0*0*0 + 7*7*7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import java.util.Scanner;
public class ArmstrongNum{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter a number which you want to check whether that is armstrong or not: ");
int n = in.nextInt();
int a = n, r=0, s=0;
while(a!=0){
r = a%10;
a = a/10;
s = s + r*r*r;
}
if(s==n){
System.out.println("Number "+n+" is an armstrong number.");
}else{
System.out.println("Number "+n+" is not an armstrong number.");
}
}
}
|
Output
|
Enter a number which you want to check whether that is armstrong or not:
407
Number 407 is an armstrong number.
|
Note- Floyd Triangle is like
1
2 3
4 5 6
7 8 9 10
————
Code-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import java.util.Scanner;
public class FloydTriangle{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows which you want in your Floyd Triangle: ");
int r = in.nextInt();
int n=0;
for(int i=0; i<r; i++){
for(int j=0; j<=i; j++){
System.out.print(++n+" ");
}
System.out.println();
}
}
}
|
Output
|
Enter the number of rows which you want in your Floyd Triangle:
5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import java.util.Scanner;
public class PalindromeString{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the string which you want to check whether that is palindrome or not: ");
String s = in.next();
String r = "";
for(int i=s.length()-1; i>=0; i--){
r = r+s.charAt(i);
}
System.out.println("Reverse of entered string "+s+" is "+r);
if(r.equals(s)){
System.out.println("String "+s+" is palindrome.");
}else{
System.out.println("String "+s+" is not palindrome.");
}
}
}
|
Output
|
Enter the string which you want to check whether that is palindrome or not:
selenium
Reverse of entered string selenium is muineles
String selenium is not palindrome.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
import java.util.Arrays;
import java.util.Scanner;
public class BinarySearch{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the size of the array which should be greater than zero else it will throw InputMismatchException : ");
int size = in.nextInt();
int[] array = new int[size];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<size; i++){
array[i] = in.nextInt();
}
System.out.println("Enter the search element: ");
int s = in.nextInt();
Arrays.sort(array); //binary search will work on sorted array only so sort first
int first, last, middle;
first=0;
last = size-1;
middle = (first+last)/2;
int i=0;
for(; i<size; i++){
if(s>array[middle]){
first = middle+1;
}else if(s<array[middle]){
last = middle-1;
}else{
printArray(array);
System.out.println("Element "+s+" found in the array.");
break;
}
middle= (first+last)/2;
}
if(i==size){
printArray(array);
System.out.println("Element "+s+" is not found in the array");
}
}
public static void printArray(int[] a){
System.out.println("Array of elements: ");
System.out.print("{");
for(int i:a){
System.out.print(i+",");
}
System.out.println("}");
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package SeleniumMakeItEasy;
import org.apache.commons.lang3.ArrayUtils;
public class BubbleSort{
public static void main(String[] args){
int[] a = {2,3,2,5,3,3,6,1,2,5};
int l = a.length;
for(int i=0;i<l; i++){
for(int j=0; j<l-1; j++){
if(a[j]>a[j+1]){
a[j] = a[j] + a[j+1];
a[j+1] = a[j] - a[j+1];
a[j] = a[j] - a[j+1];
}else if(a[j]==a[j+1]){
a = ArrayUtils.remove(a,j);
l = a.length;
}
}
}
for(int s: a){
System.out.println(s);
}
}
}
|
Weekly most viewed
-
Top 10 Programming Languages to Learn Having spent more than 5 years in software development and the computer programming arena, one...
-
Top 30 Famous Temples in India Custodians of beliefs of more than a billion people: the holy places of India cross the boundaries of tim...
-
Introduction and Overview Introduction Python is currently one of the most popular dynamic programming languages, along with Pe...
-
HISTORICAL IMPORTANCE The Solapur District was ruled by various dynesties such ...
-
Here are my list of 10 Java coding interview questions and answers, which is good to prepare before appearing on any Java interviews. As...
-
Ancient Rome was a powerful and important civilization that ruled much of Europe for nearly 1000 years. The culture of Ancient Rome was s...
-
Java Learn Java online <click for course details. Difficulty: 3 (Scaled 1 to 5) What is it: Java, in the c...
-
Best Free Recharge apps to get free Talktime/Recharges on your Mobile (Android/PC) Today most of us have Smartphones or Android phone’s ...
-
"Come & Get It" [2x] When you're ready come and get it Na na na na [3x] When you're ready When you're r...
-
Five Thieves ( Panj Dosh or Panj Vikar ) are the five major weaknesses of the human personality at variance with its spiritua...
Hadoop Training Institute in Noida, The Hadoop
ReplyDeletecertification has considerable advantages in addition to the great demand for trained professionals. A certified Hadoop professional gets the
advantage in terms of payment package compared to other professionals working in the field without certification. When it comes to IJP, Hadoop
certification is compatible to advance the ladder and accelerate your career. big data hadoop training in Noida, big data hadoop Training institutes
in Noida, sector 2, oracle big data training,Oracle Training
in Noida, big data boot camp, nosql database training, big data appliance training, exalytics training, exadata course, big data introduction,
oracle course, oracle training, big data training in Noida, ibm big data hadoop training institutes noida
Hadoop Training Institute in Noida
CONTAC CIITN:
B-12, Sector - 2, Noida, U.P
State - Uttar Pradesh U.P
Country - India
Pin Code - 201301
Phone - +917290926565
Mon - Sun: 10am - 6pm
nice blog
ReplyDeletedatascience training in bangalore
blockchain training in bangalore
python online training
AWS Training in Bangalore - Live Online & Classroom
ReplyDeletemyTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS Training in Bangalore
using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.
IOT Training in Bangalore - Live Online & Classroom
IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.
Bodhih’s Live Virtual Classes Train The Trainer workshop is a Mix of self-paced, interactive, Handson experiential and Practical workshop that is applied to learn and produce better results as a trainer.
ReplyDeletepython training in bangalore | python online training
ReplyDeleteartificial intelligence training in bangalore | artificial intelligence online training
machine learning training in bangalore | machine learning online training
data science training in bangalore | data science online training
aws training in Bangalore | aws online training
ReplyDelete