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 | publicclassFizzBuzzProblem{    publicstaticvoidmain(String args[])    {        for(inti = 1; i <= 100; i++)        {            if((i % (3*5)) == 0)            {                System.out.println("FizzBuzz");            }            elseif((i % 5) == 0)            {                System.out.println("Buzz");              }            elseif((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 | publicclassNearestNumber{    staticintnearestTo100(intinput1, intinput2)    {        intdiff1 = Math.abs(100- input1);        intdiff2 = Math.abs(100- input2);        if(diff1 < diff2)        {            returninput1;        }        elseif(diff2 < diff1)        {            returninput2;        }        else        {            returninput1;        }    }    publicstaticvoidmain(String args[])    {        Scanner sc = newScanner(System.in);        System.out.println("Enter First Number");        intinput1 = sc.nextInt();        System.out.println("Enter Second Number");        intinput2 = 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 | publicclassBiggerNumber{    publicstaticvoidmain(String[] args)    {        Scanner sc = newScanner(System.in);        System.out.println("Enter First number");        inta = sc.nextInt();        System.out.println("Enter Second Number");        intb = sc.nextInt();        if((a > b) && (a - b) >= 2)        {            System.out.println(a);        }        elseif((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 | publicclassPromrammingExample{    publicstaticvoidmain(String[] args)    {        String s = "JAVAJ2EE";        char[] c = s.toCharArray();        for(inti = 0; i < c.length; i++)        {            for(intj = 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 | publicclassProgrammingExamples{    publicstaticvoidmain(String[] args)    {        String s1 = "ONE";        String s2 = newString("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 | publicclassCodingExamples{    publicstaticvoidmain(String args[])    {        inti = (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 | publicclassCodingExamples{    publicstaticvoidmain(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 | publicclassCodingExamples{    publicstaticvoidmain(String[] args)    {        String[] s1 = {"ONE", "TWO", "THREE", "FOUR"};        String[] s2 = {"THREE", "TWO", newString("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 | publicclassClassesAndObjects{    publicstaticvoidmain(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 | publicclassCurrentDate{    publicstaticvoidmain(String[] args)    {        Date date = newDate();        SimpleDateFormat formatter = newSimpleDateFormat("dd MMM yyyy");        System.out.println(formatter.format(date));    }} | 
 
No comments:
Post a Comment