Java Freshers Questions
Java Freshers Questions
A Constructor is a special method used to provide initial values to the data members of the
class
Creating a zero argument constructor and parameterized constructor for a class in called
constructor overloading
super() this()
Definition super() - refers immediate parent class this() - refers current class
instance. instance.
Constructo super() acts as immediate parent class this() acts as current class
r constructor and should be first line in constructor and can be used in
child class constructor. parametrized constructors.
Inheritance is the process of acquiring the functionalities of one class into another class
a. Single Inheritance
b. Multiple Inheritance
c. MultiLevel Inheritance
9. explain multiple inheritance & why it is not possible, how to achive multiple inheritance
Inheriting more than one class or more than one interface or one class and one interface
into a class is called multiple inheritance
Multiple inheritance from two classes into a class in NOT possible in java. Because when
both base classes are having the same method then there will be an ambiguity in calling the method
Multiple inheritance will be implemented by inheriting two interfaces into a class or one
class and interface into a class
The "diamond problem" (sometimes referred to as the "Deadly Diamond of Death") is an ambiguity
that arises when two classes B and C inherit from A, and class D inherits from both B and C. ... It is
called the "diamond problem" because of the shape of the class inheritance diagram in this
situation.
11.explain overLoading / overRiding with real time example
Overloading is about same function have different signatures. Overriding is about same function,
same signature but different classes connected through inheritance. Overloading is an example of
compiler time polymorphism and overriding is an example of run time polymorphism.
Overloading Example:
return (a + b);
}
return (a + b + c) ;
return (a + b);
ob.add(15,25);
ob.add(15,25,35);
ob.add(11.5 , 22.5);
Overriding Example:
class Father {
void shoot() {
System.out.println("I am the father! I am a right-handed shooter! ");
}
}
public class Child extends Father {
void shoot() {
System.out.println("I am the son! I am a left-handed shooter!");
}
public static void main(String[] args) {
Father f = new Father();
f.shoot(); //Here the parent class method gets called.
Father fc = new Child();
fc.shoot(); //This is dynamic methpod dispatch. The compiler decides method call at runtime.
}
}
Data abstraction is the process of hiding certain details and showing only essential information to
the user.
yes
yes
No
yes
Constructor call
no
yes
no
yes
no
yes
26.What is exception?
yes
no
The Throwable class is the superclass of all errors and exceptions in the Java language
Exception
73. difference between Constant pool & non constant pool in String class
85.what is serialization?
86.what is deserialisation?
92.what is synchronisation?
Programs
1.W.A.P PALINDROME?
1. class PalindromeExample{
2. public static void main(String args[]){
3. int r,sum=0,temp;
4. int n=454;//It is the number variable to be checked for palindrome
5.
6. temp=n;
7. while(n>0){
8. r=n%10; //getting remainder
9. sum=(sum*10)+r;
10. n=n/10;
11. }
12. if(temp==sum)
13. System.out.println("palindrome number ");
14. else
15. System.out.println("not palindrome");
16.}
17.}
3.armstrong number?
Ex: 153
1. 153 = (1*1*1)+(5*5*5)+(3*3*3)
2. where:
3. (1*1*1)=1
4. (5*5*5)=125
5. (3*3*3)=27
6. So:
7. 1+125+27=153
1. class ArmstrongExample{
2. public static void main(String[] args) {
3. int c=0,a,temp;
4. int n=153;//It is the number to check armstrong
5. temp=n;
6. while(n>0)
7. {
8. a=n%10;
9. n=n/10;
10. c=c+(a*a*a);
11. }
12. if(temp==c)
13. System.out.println("armstrong number");
14. else
15. System.out.println("Not armstrong number");
16. }
17.}
Prime number in Java: Prime number is a number that is greater than 1 and
divided by 1 or itself only. In other words, prime numbers can't be divided by other
numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime
numbers.
factorial of each digit of the number, which is equal to the number itself. To
below example:
The number 145 is a strong number. This is because if we add the factorials
of each digit of this number, you will get the number, which is 145 itself, as
import java.util.*;
public class Main
{
public static void main(String[] args) {
int n,i;
int fact_n,lastdig;
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter the number : " );
n = sc.nextInt();
int total = 0;
int temp_n = n;
while(n != 0)
{
i = 1;
fact_n = 1;
lastdig = n % 10;
while(i <= lastdig)
{
fact_n = fact_n * i;
i++;
}
total = total + fact_n;
n = n / 10;
}
if(total == temp_n)
System.out.println(temp_n + " is a strong number\n");
else
System.out.println(temp_n + " is not a strong number\
n");
System.out.println();
}
}
Output :
Enter the Number : 145
145 is strong number
7.perfect number?
package FrequentPrograms;
import java.util.Scanner;
sc = new Scanner(System.in);
Number = sc.nextInt();
if(Number % i == 0) {
Sum = Sum + i;
}
if (Sum == Number) {
else {
Output :
Enter a number : 6
6 is a string number
Output:
9.fibonacci?
In fibonacci series, next number is the sum of previous two numbers for example 0,
1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. The first two numbers of fibonacci series are 0
and 1.
1. class FibonacciExample1{
2. public static void main(String args[])
3. {
4. int n1=0,n2=1,n3,i,count=10;
5. System.out.print(n1+" "+n2);//printing 0 and 1
6.
7. for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
8. {
9. n3=n1+n2;
10. System.out.print(" "+n3);
11. n1=n2;
12. n2=n3;
13. }
14.
15.}}
10.factorial?
Factorial Program in Java: Factorial of n is the product of all positive descending
integers. Factorial of n is denoted by n!. For example:
1. 4! = 4*3*2*1 = 24
2. 5! = 5*4*3*2*1 = 120
Here, 4! is pronounced as "4 factorial", it is also called "4 bang" or "4 shriek".
1. class FactorialExample{
2. public static void main(String args[]){
3. int i,fact=1;
4. int number=5;//It is the number to calculate factorial
5. for(i=1;i<=number;i++){
6. fact=fact*i;
7. }
8. System.out.println("Factorial of "+number+" is: "+fact);
9. }
10.}
11.sum of digits?
1.import java.util.Scanner;
2.public class Digit_Sum
3.{
4. public static void main(String args[])
5. {
6. int m, n, sum = 0;
7. Scanner s = new Scanner(System.in);
8. System.out.print("Enter the number:");
9. m = s.nextInt();
10. while(m > 0)
11. {
12. n = m % 10;
13. sum = sum + n;
14. m = m / 10;
15. }
16. System.out.println("Sum of Digits:"+sum);
17. }
18. }
13.bubblesort?
In bubble sort algorithm, array is traversed from first element to last element. Here,
current element is compared with the next element. If current element is greater
than the next element, it is swapped.
class GFG
{
// Prints numbers from 1 to n
static void printNos(int n)
{
if(n > 0)
{
printNos(n - 1);
System.out.print(n + " ");
}
return;
}
// Driver Code
public static void main(String[] args)
{
printNos(100);
}
}
15.Can a method return more than one value?if not how to return?
17.binary search?
Binary search is used to search a key element from multiple elements. Binary search
is faster than linear search.
In case of binary search, array elements must be in ascending order. If you have
unsorted array, you can sort the array using Arrays.sort(arr) method.
1. class BinarySearchExample{
2. public static void binarySearch(int arr[], int first, int last, int key){
3. int mid = (first + last)/2;
4. while( first <= last ){
5. if ( arr[mid] < key ){
6. first = mid + 1;
7. }else if ( arr[mid] == key ){
8. System.out.println("Element is found at index: " + mid);
9. break;
10. }else{
11. last = mid - 1;
12. }
13. mid = (first + last)/2;
14. }
15. if ( first > last ){
16. System.out.println("Element is not found!");
17. }
18. }
19. public static void main(String args[]){
20. int arr[] = {10,20,30,40,50};
21. int key = 30;
22. int last=arr.length-1;
23. binarySearch(arr,0,last,key);
24. }
25.}
We can add two matrices in java using binary + operator. A matrix is also known as
array of arrays. We can add, subtract and multiply matrices.
19.transpose matrix?
Converting rows of a matrix into columns and columns of a matrix into row is called
transpose of a matrix.
20.contigious array?
//array of 10 numbers
int arr[] = new int[]{12,56,76,89,100,343,21,234};
}
System.out.println("Smallest Number is : " + smallest);
System.out.println("Largest Number is : " + largest);
}
}
When you run above program, you will get below output:
1
2
Largest Number is : 343
3
Smallest Number is : 12
4
25.reverese string?
Reverse string by CHARACTERS:
public static void main(String[] args) {
// Using traditional approach
String result="";
for(int i=string.length()-1; i>=0; i--) {
result = result + string.charAt(i);
}
System.out.println(result);
26.reverse sentence?
27.palindrome in string?
import java.util.Scanner;
class ChkPalindrome
System.out.println("Enter a string:");
str = sc.nextLine();
if (str.equals(rev))
System.out.println(str+" is a palindrome");
else
Program Output:
Enter a string:
radar
radar is a palindrome
int i = 0;
for(char c: str.toCharArray()) {
i++;
}
}
class Main {
line = line.toLowerCase();
for (int i = 0; i < line.length(); ++i) {
char ch = line.charAt(i);
if (carray[i] == carray[j]) {
break;
32.anagram?
Two strings are called anagrams if they contain same set of characters but in
different order.
1. import java.util.Arrays;
2.
3. public class AnagramString {
4. static void isAnagram(String str1, String str2) {
5. String s1 = str1.replaceAll("\\s", "");
6. String s2 = str2.replaceAll("\\s", "");
7. boolean status = true;
8. if (s1.length() != s2.length()) {
9. status = false;
10. } else {
11. char[] ArrayS1 = s1.toLowerCase().toCharArray();
12. char[] ArrayS2 = s2.toLowerCase().toCharArray();
13. Arrays.sort(ArrayS1);
14. Arrays.sort(ArrayS2);
15. status = Arrays.equals(ArrayS1, ArrayS2);
16. }
17. if (status) {
18. System.out.println(s1 + " and " + s2 + " are anagrams");
19. } else {
20. System.out.println(s1 + " and " + s2 + " are not anagrams");
21. }
22. }
23.
24. public static void main(String[] args) {
25. isAnagram("Keep", "Peek");
26. isAnagram("Mother In Law", "Hitler Woman");
27. }
28.}
Output:
class BlankSpace {
public static void main(String[] args)
{
String str = " Geeks for Geeks ";
System.out.println(str);
}
}
34.w.a.p to find to count the number of words present in the text file?
Finding a year is a leap or not is a bit tricky. We generally assume that if a year
number is evenly divisible by 4 is a leap year. But it is not the only case. A year is a
leap year if −
import java.util.Scanner;
int year;
year = sc.nextInt();
else
}
}
Output 1
Enter an Year ::
2020
Specified year is a leap year
Star Pattern
1. Right Triangle Star Pattern
Output:
Output:
Output:
1. import java.util.Scanner;
2. public class DiamondPattern
3. {
4. public static void main(String args[])
5. {
6. int row, i, j, space = 1;
7. System.out.print("Enter the number of rows you want to print: ");
8. Scanner sc = new Scanner(System.in);
9. row = sc.nextInt();
10.space = row - 1;
11.for (j = 1; j<= row; j++)
12.{
13.for (i = 1; i<= space; i++)
14.{
15.System.out.print(" ");
16.}
17.space--;
18.for (i = 1; i <= 2 * j - 1; i++)
19.{
20.System.out.print("*");
21.}
22.System.out.println("");
23.}
24.space = 1;
25.for (j = 1; j<= row - 1; j++)
26.{
27.for (i = 1; i<= space; i++)
28.{
29.System.out.print(" ");
30.}
31.space++;
32.for (i = 1; i<= 2 * (row - j) - 1; i++)
33.{
34.System.out.print("*");
35.}
36.System.out.println("");
37.}
38.}
39.}
Output:
Output:
6. Mirrored Right Triangle Star Pattern
Output:
Output:
Output:
1. import java.util.Scanner;
2. public class RightPascalTrianglePattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, rows;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows you want to print: ");
9. rows = sc.nextInt();
10.for (i= 0; i<= rows-1; i++)
11.{
12.for (j=0; j<=i; j++)
13.{
14.System.out.print("*"+ " ");
15.}
16.System.out.println("");
17.}
18.for (i=rows-1; i>=0; i--)
19.{
20.for(j=0; j <= i-1;j++)
21.{
22.System.out.print("*"+ " ");
23.}
24.System.out.println("");
25.}
26.}
27.}
Output:
1. import java.util.Scanner;
2. public class LeftPascalTrianglePattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, k, rows;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows you want to print: ");
9. rows = sc.nextInt();
10.for (i= 1; i<= rows ; i++)
11.{
12.for (j=i; j <rows ;j++)
13.{
14.System.out.print(" ");
15.}
16.for (k=1; k<=i;k++)
17.{
18.System.out.print("*");
19.}
20.System.out.println("");
21.}
22.for (i=rows; i>=1; i--)
23.{
24.for(j=i; j<=rows;j++)
25.{
26.System.out.print(" ");
27.}
28.for(k=1; k<i ;k++)
29.{
30.System.out.print("*");
31.}
32.System.out.println("");
33.}
34.sc.close();
35.}
36.}
Output:
1. import java.util.*;
2. public class AlphabetPattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, n=8;
7. // Outer for loop for number of lines
8. for (i = 0; i<=n; i++)
9. {
10.// Inner for loop for logic execution
11.for (j = 0; j<= n / 2; j++)
12.{
13.// prints two vertical lines
14.if ((j == 0 || j == n / 2) && i != 0 ||
15.// print first line of alphabet
16.i == 0 && j != n / 2 ||
17.// prints middle line
18.i == n / 2)
19.System.out.print("*");
20.else
21.System.out.print(" ");
22.}
23.System.out.println();
24.}
25.}
26.}
Output:
1. import java.util.Scanner;
2. public class TrianglePattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, k, rows=9;
7. for (i=1; i<= rows ; i++)
8. {
9. for (j = i; j < rows ; j++)
10.{
11.System.out.print(" ");
12.}
13.for (k = 1; k <= (2*i -1) ;k++)
14.{
15.if(k==1 || i == rows || k==(2*i-1))
16.{
17.System.out.print("*");
18.}
19.else
20.{
21.System.out.print(" ");
22.}
23.}
24.System.out.println("");
25.}
26.}
27.}
Output:
1. import java.util.Scanner;
2. public class DownTrianglePattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, k, rows=9;
7. for (i=rows; i>= 1 ; i--)
8. {
9. for (j = i; j<rows ; j++)
10.{
11.System.out.print(" ");
12.}
13.for (k = 1; k <= (2*i -1) ;k++)
14.{
15.if( k==1 || i == rows || k==(2*i-1))
16.{
17.System.out.print("*");
18.}
19.else
20.{
21.System.out.print(" ");
22.}
23.}
24.System.out.println("");
25.}
26.}
27.}
Output:
1. import java.util.*;
2. public class DiamondPattern
3. {
4. public static void main(String[] args)
5. {
6. Scanner sc = new Scanner(System.in);
7. System.out.println("Enter the number of rows you want to print: ");
8. int rows = sc.nextInt();
9. for (i=1; i<= rows ; i++)
10.{
11.for (j = rows; j > i ; j--)
12.{
13.System.out.print(" ");
14.}
15.System.out.print("*");
16.for (k = 1; k < 2*(i -1) ;k++)
17.{
18.System.out.print(" ");
19.}
20.if( i==1)
21.{
22.System.out.println("");
23.}
24.else
25.{
26.System.out.println("*");
27.}
28.}
29.for (i=rows-1; i>= 1 ; i--)
30.{
31.for (int j = rows; j > i ; j--)
32.{
33.System.out.print(" ");
34.}
35.System.out.print("*");
36.for (int k = 1; k < 2*(i -1) ;k++)
37.{
38.System.out.print(" ");
39.}
40.if(i==1)
41.System.out.println("");
42.else
43.System.out.println("*");
44.}
45.}
46.}
Output:
Number Pattern
1. Pattern-1
Output:
2. Pattern-2
Output:
3. Pattern-3
Output:
4. Pattern-4
5. Pattern-5
1. import java.util.*;
2. public class Pattern5
3. {
4. public static void main(String[] args)
5. {
6. int i, j, rows;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows you want to print: ");
9. rows = sc.nextInt();
10.for (i = 1; i <= rows; i++)
11.{
12.for (j = 1; j <= i; j++)
13.{
14.System.out.print(i+" ");
15.}
16.System.out.println();
17.}
18.}
19.}
Output:
6. Pattern-6
1. import java.util.*;
2. public class Pattern6
3. {
4. public static void main(String[] args)
5. {
6. int i, j, rows;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows youy want to print: ");
9. rows = sc.nextInt();
10.for (i = rows; i >= 1; i--)
11.{
12.for (j = rows; j >= i; j--)
13.{
14.System.out.print(j+" ");
15.}
16.
17.System.out.println();
18.}
19.}
20.}
Output:
7. Pattern-7
1. import java.util.Scanner;
2. public class Pattern7
3. {
4. public static void main(String[] args)
5. {
6. int i, j, n;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows you want to print: ");
9. n = sc.nextInt();
10.for (i = 1; i <= n; i++)
11.{
12.for (j = i; j >= 1; j--)
13.{
14.System.out.print(j+" ");
15.}
16.System.out.println();
17.}
18.}
19.}
Output:
8. Pattern-8
Output:
9. Pattern-9
1. import java.util.Scanner;
2. public class Pattern9
3. {
4. public static void main(String[] args)
5. {
6. int i, j, rows=9;
7. for (i = 1; i <= rows; i++)
8. {
9. for (j = 1; j <= i; j++)
10.{
11.if(j%2 == 0)
12.{
13.System.out.print(0);
14.}
15.else
16.{
17.System.out.print(1);
18.}
19.}
20.System.out.println();
21.}
22.}
23.}
Output:
10. Pattern-10
Output:
11. Pattern-11
Output:
12. Pattern-12
1. public class Pattern12
2. {
3. public static void main(String[] args)
4. {
5. int rows=9;
6. for (int i = 1; i <= rows; i++)
7. {
8. for (int j = rows; j >= i; j--)
9. {
10.System.out.print(j+" ");
11.}
12.System.out.println();
13.}
14.}
15.}
Output:
13. Pattern-13
Output:
14. Pattern-14
Output:
15. Pattern-15
Output:
16. Pattern-16
Output:
17. Pattern-17
1. public class Pattern17
2. {
3. public static void main(String[] args)
4. {
5. int rows=9;
6. //Prints upper half pattern
7. for (int i = 1; i <= rows; i++)
8. {
9. //Prints i spaces at the beginning of each row
10.for (int j = 1; j < i; j++)
11.{
12.System.out.print(" ");
13.}
14.//Prints i to rows value at the end of each row
15.for (int j = i; j <= rows; j++)
16.{
17.System.out.print(j);
18.}
19.System.out.println();
20.}
21.//Prints lower half pattern
22.for (int i = rows-1; i >= 1; i--)
23.{
24.//Prints i spaces at the beginning of each row
25.for (int j = 1; j < i; j++)
26.{
27.System.out.print(" ");
28.}
29.//Prints i to rows value at the end of each row
30.for (int j = i; j <= rows; j++)
31.{
32.System.out.print(j);
33.}
34.System.out.println();
35.}
36.}
37.}
Output:
18. Pattern-18
Output:
19. Pattern-19
Output:
20. Pattern-20
Output:
21. Pattern-21
1. import java.util.Scanner;
2. public class Pattern21
3. {
4. public static void main(String[] args)
5. {
6. int i, j, min, n; //n is the number up to you want to print
7. System.out.print("Enter the value of n: ");
8. Scanner sc= new Scanner(System.in);
9. n=sc.nextInt();
10.//loo for upper left part
11.for (i = 1; i <= n; i++)
12.{
13.for (j = 1; j <= n; j++)
14.{
15.min = i < j ? i : j;
16.System.out.print(n - min + 1+ " ");
17.}
18.//loop for upper right part
19.for (j = n - 1; j >= 1; j--)
20.{
21.min = i < j ? i : j;
22.System.out.print(n - min + 1+ " ");
23.}
24.System.out.println();
25.}
26.//loop for lower left part
27.for (i = n - 1; i >= 1; i--)
28.{
29.for (j = 1; j <= n; j++)
30.{
31.min = i < j ? i : j;
32.System.out.print(n - min + 1+ " ");
33.}
34.//loop for lower right part
35.for (j = n - 1; j >= 1; j--)
36.{
37.min = i < j ? i : j;
38.System.out.print(n - min + 1+ " ");
39.}
40.System.out.println();
41.}
42.}
43.}
Output:
Character Pattern
1. Right Triangle Alphabetic Pattern
Output:
2. Repeating Alphabet Pattern
Output:
3. K-shape Alphabet Pattern
Output:
4. Triangle Character Pattern
Output:
5. Diamond Character Pattern
1. import java.util.Scanner;
2. public class DiamondCharacterPattern
3. {
4. public static void main(String[] args)
5. {
6. char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
7. int alphabet _number = 0;
8. String[] diamond = new String[26]; // array of strings
9. System.out.print("Enter a Character between A to Z : ");
10.Scanner reader = new Scanner(System.in);
11.try
12.{
13.char user_ alphabet = reader.next("[A-Z]").charAt(0);
14.// search for letter number in the array letter
15.for (int i = 0; i < alphabet.length; i++)
16.{
17.if (letter[i] == user_ alphabet)
18.{
19.alphabet _number = i;
20.break;
21.}
22.}
23.//construct diamond
24.for (int i = 0; i <= alphabet _number; i++)
25.{
26.diamond[i] = "";
27.//add initial spaces
28.for (int j = 0; j < alphabet _number - i; j++)
29.{
30.diamond[i] += " ";
31.}
32.// add alphabet
33.diamond[i] += alphabet
34.//add space between letters
35.if (alphabet[i] != 'A')
36.{
37.for (int j = 0; j < 2 * i - 1; j++)
38.{
39.diamond[i] += " ";
40.}
41.// add alphabet
42.diamond[i] += alphabet[i];
43.}
44.// Draw the first part of the diamond
45.System.out.println(diamond[i]);
46.}
47.for (int i = alphabet _number - 1; i >= 0; i--)
48.{
49.// Draw the second part of the diamond
50.// prints the diamondArray in the reverse order
51.System.out.println(diamond[i]);
52.}
53.}
54.catch (Exception e)
55.{
56.e.printStackTrace();
57.}
58.finally
59.{
60.reader.close();
61.}
62.}
63.}
Output: