Most Frequently Asked Java Interview Programs. PART-2

       Hi friends, This blog is about 10 most frequently asked java interview programs, As I live in northern India that's why I can say these are the most trending asked java programs in interviews at this place. And I am sure if you will have the knowledge of these java programs you can also do some complex programming on behalf of this logic. 


1. Find Even and Odd Number with and without % (Modulus)

    Note: Even number finding is an easy task but some times interviewer may ask you to not use % modulus symbol then it may look challenging. But it is easy using the bitwise operator.

public class EvenOdd {
public static void main(String arg[]) {
int n = 4;
System.out.println("Find Even or Odd number without % (Modulus)");
if ((n&1) == 0) {
System.out.println(n+ ": is Even number");
} else {
System.out.println(n+ ": is Odd number");
}
System.out.println("Find Even or Odd number with % (Modulus)");
if (n%2==0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
}
}
OUTPUT
Find Even or Odd number without % (Modulus)
4: is Even number
Find Even or Odd number with % (Modulus)
Even number

2. Find Factorial

    Note: This java program is showing the 1-5 factorials. Factorial something works like :
    1*1 = 1
    1*2 = 2
    2*3 = 6
    6*4 = 24
    24*5 = 120


public class Factorial {
public static void main(String arg[]) {
int fact = 1;
int n=5;
for (int i=1; i<=n; i++) {
fact *= i;
System.out.println(fact);
}
System.out.println(fact);
}
}

OUTPUT

1 2 6 24 120 120


3. Fibonacci Series

    Note: You can find fibonacci series by adding previous two digits,  In this program we are repeating this process for 12 times.

public class Fibonacci { 
public static void main(String arg[]) {
int x, f1, f2, f3;
f1=0; f2=1; x=12;
System.out.print("\t"+f1+"\t"+f2);
for(int i=2; i<=x; i++) {
f3=f1+f2;
System.out.print("\t"+f3);
f1=f2;
f2=f3;
}
}
}
OUTPUT
0	1	1	2	3	5	8	13	21	34	55	89	144

4. Frequency Of Character

    Note: In this java program, We have given a sentence and we need to find all the characters with their total count or occurence.


public class Frequency {

public static void main(String arg[] ) {

int k; char ch;

String str = "quick brown fox jumps over the lazy little dog";

int i=str.length();

for(char c='A'; c<='z'; c++) {

k=0;

for(int j=0; j<i; j++) {

ch=str.charAt(j);

if(ch==c) {

k++;

}

}

if (k>0) {

System.out.println("Char: "+c+" count: "+k);

}

}

} }
OUTPUT
Char: a count: 1
Char: b count: 1
Char: c count: 1
Char: d count: 1
Char: e count: 3
Char: f count: 1
Char: g count: 1
Char: h count: 1
Char: i count: 2
Char: j count: 1
Char: k count: 1
Char: l count: 3
Char: m count: 1
Char: n count: 1
Char: o count: 4
Char: p count: 1
Char: q count: 1
Char: r count: 2
Char: s count: 1
Char: t count: 3
Char: u count: 2
Char: v count: 1
Char: w count: 1
Char: x count: 1
Char: y count: 1
Char: z count: 1

5. Missing Number In Array

         Note: There is an integer array we need to find a missing number. It is also a very frequently asked java program by interviewer.

public class MissingNumber { 
public static void main(String arg[]) {
int arr[] = {1,2,3,4,5,6,8,9,10};
int sum = 0;
for(int i=0; i<arr.length; i++) {
sum += arr[i];
}
int total = (arr.length+1) * (arr.length+2) / 2;
System.out.println("Missing number is: "+ (total-sum));
} }
OUTPUT
Missing number is: 7

6. Print Counting Without Loop (Recursion)

      Note: There is recursive method which is an alternative aproach for iteration like loop. Recursive method call itself until some certain condition met.

public class Recursion {
public static void main(String arg[]) {
rec(1);
}

public static void rec(int n) {
if(n <= 10) {
System.out.println(n);
rec(n+1);
}
} }

OUTPUT

1 2 3 4 5 6 7 8 9 10


7. Perfect Number

       Note: A perfect number is a positive integer number that is equal to the sum of its positive divisors, but excluding the number itself.

public class PerfectNumber { 
public static void main(String arg[]) {
int n, sum = 0;
n = 6;
for(int i=1; i<n; i++) {
if(n % i == 0) {
sum += i;
}
}
if(sum == n) {
System.out.println("This is perfect number: "+ n);
}
} }
OUTPUT
This is perfect number: 6

8. Print ASCII Values

        Note: This will return ASCII values in the range or 1-225.

public class Asciivalues { 
public static void main(String arg[]) {
int i;
for(i=1; i<=225; i++) {
System.out.println(i+ " "+ (char)i);
}
}
}
OUTPUT
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 0
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9
58 :
59 ;
60 <
61 =
62 >
63 ?
64 @
65 A
66 B
67 C
68 D
69 E
70 F
71 G
72 H
73 I
74 J
75 K
76 L
77 M
78 N
79 O
80 P
81 Q
82 R
83 S
84 T
85 U
86 V
87 W
88 X
89 Y
90 Z
91 [
92 \
93 ]
94 ^
95 _
96 `
97 a
98 b
99 c
100 d
101 e
102 f
103 g
104 h
105 i
106 j
107 k
108 l
109 m
110 n
111 o
112 p
113 q
114 r
115 s
116 t
117 u
118 v
119 w
120 x
121 y
122 z
123 {
124 |
125 }
126 ~
127 
128 ?
129 ?
130 ?
131 ?
132 ?
133 ?
134 ?
135 ?
136 ?
137 ?
138 ?
139 ?
140 ?
141 ?
142 ?
143 ?
144 ?
145 ?
146 ?
147 ?
148 ?
149 ?
150 ?
151 ?
152 ?
153 ?
154 ?
155 ?
156 ?
157 ?
158 ?
159 ?
160  
161 ¡
162 ¢
163 £
164 ¤
165 ¥
166 ¦
167 §
168 ¨
169 ©
170 ª
171 «
172 ¬
173 ­
174 ®
175 ¯
176 °
177 ±
178 ²
179 ³
180 ´
181 µ
182 ¶
183 ·
184 ¸
185 ¹
186 º
187 »
188 ¼
189 ½
190 ¾
191 ¿
192 À
193 Á
194 Â
195 Ã
196 Ä
197 Å
198 Æ
199 Ç
200 È
201 É
202 Ê
203 Ë
204 Ì
205 Í
206 Î
207 Ï
208 Ð
209 Ñ
210 Ò
211 Ó
212 Ô
213 Õ
214 Ö
215 ×
216 Ø
217 Ù
218 Ú
219 Û
220 Ü
221 Ý
222 Þ
223 ß
224 à
225 á

9. Random Number

    Note: This java program takes a minimum number and a maximum number and returns a random number that falls into this range.

public class RandomNumber { 
public static void main(String arg[]) {
int minimum = 1;
int maximum = 6;
int randomNumber = minimum + (int) (Math.random() * maximum);
System.out.println("Random Number: "+ randomNumber);
}
}
OUTPUT
Random Number: 4

10. Reverse Number

    Note: Reverse any integer number using this algorithom.

public class ReverseNumber { 
public static void main(String arg[]) {
int r = 0, n = 12345, s1 = 0;
while(n > 0) {
r = n % 10;
s1 = r + (s1*10);
n /= 10;
}
System.out.println(s1);
} }
OUTPUT
54321

Read below posts.
Most Frequently Asked Java Interview Programs. PART-1
Most Frequently Asked Java Interview Programs. PART-3

Comments

Popular Posts