10 Java Interview Programmatic Questions And Answers
1) FizzBuzz Problem.
Write a program in java which prints the numbers from 1 to 100. But, multiples of 3 should be replaced with “Fizz”, multiples of 5 should be replaced with “Buzz” and multiples of both 3 and 5 should be replaced with “FizzBuzz”?
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
| public class FizzBuzzProblem{ public static void main(String args[]) { for(int i = 1; i <= 100; i++) { if((i % (3*5)) == 0) { System.out.println("FizzBuzz"); } else if ((i % 5) == 0) { System.out.println("Buzz"); } else if ((i % 3) == 0) { System.out.println("Fizz"); } else { System.out.println(i); } } }} |
2) Write a Java program to find out the nearest number to 100 of the given two numbers?
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
| public class NearestNumber{ static int nearestTo100(int input1, int input2) { int diff1 = Math.abs(100 - input1); int diff2 = Math.abs(100 - input2); if(diff1 < diff2) { return input1; } else if (diff2 < diff1) { return input2; } else { return input1; } } public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter First Number"); int input1 = sc.nextInt(); System.out.println("Enter Second Number"); int input2 = sc.nextInt(); System.out.println(nearestTo100(input1, input2)); }} |
3) There are two numbers ‘a’ and ‘b’. Write a java program which should print ‘a’ if ‘a’ is bigger than ‘b’ by 2 or more or should print ‘b’ if ‘b’ is bigger than ‘a’ by 2 or more. Otherwise, it should print “INCONCLUSIVE”?
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
| public class BiggerNumber{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter First number"); int a = sc.nextInt(); System.out.println("Enter Second Number"); int b = sc.nextInt(); if((a > b) && (a - b) >= 2) { System.out.println(a); } else if ((b > a) && (b - a) >=2) { System.out.println(b); } else { System.out.println("INCONCLUSIVE"); } }} |
4) Given a string “JAVAJ2EE”, write java program to print this string in the below format?
J
J A
J A V
J A V A
J A V A J
J A V A J 2
J A V A J 2 E
J A V A J 2 E E
J A
J A V
J A V A
J A V A J
J A V A J 2
J A V A J 2 E
J A V A J 2 E E
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| public class PromrammingExample{ public static void main(String[] args) { String s = "JAVAJ2EE"; char[] c = s.toCharArray(); for(int i = 0; i < c.length; i++) { for(int j = 0; j <= i; j++) { System.out.print(c[j]+" "); } System.out.println(); } }} |
5) How do you prove that String s1 = new String(“ONE”) and String s2 = “ONE” are two different objects in the memory?
Ans : Use “==” operator to compare s1 and s2. It will return ‘false’ if they are two different objects in the memory.
1
2
3
4
5
6
7
8
9
10
11
| public class ProgrammingExamples{ public static void main(String[] args) { String s1 = "ONE"; String s2 = new String("ONE"); System.out.println(s1 == s2); //Output : false }} |
6) What will be the output of this program?
1
2
3
4
5
6
7
8
9
| public class CodingExamples{ public static void main(String args[]) { int i = (byte) + (char) - (int) + (long) - 1; System.out.println(i); }} |
Ans : 1
7) Will this program compiles successfully?
1
2
3
4
5
6
7
8
9
10
11
| public class CodingExamples{ public static void main(String args[]) { System.out.println(1); http://javaconceptoftheday.com/ System.out.println(2); }} |
Ans : Yes. Compiler will treat ‘http’ as label and remaining part (of Line 7) will be commented.
8) What will be the output of the below program?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| public class CodingExamples{ public static void main(String[] args) { String[] s1 = {"ONE", "TWO", "THREE", "FOUR"}; String[] s2 = {"THREE", "TWO", new String("ONE")}; System.out.println(s1[0] == s2[2]); System.out.println(s1[1] == s2[1]); System.out.println(s1[2] == s2[0]); }} |
Ans :
false
true
true
false
true
true
9) Open a notepad in your system through a java program?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| public class ClassesAndObjects{ public static void main(String[] args) { try { Runtime.getRuntime().exec("notepad.exe"); } catch (IOException e) { e.printStackTrace(); } }} |
10) Write a Java program to print the current date in “dd MMM yyyy” format?
1
2
3
4
5
6
7
8
9
10
11
| public class CurrentDate{ public static void main(String[] args) { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy"); System.out.println(formatter.format(date)); }} |
No comments:
Post a Comment