java_interview_specific_codes
java_interview_specific_codes
md 2024-05-28
if (num > 0)
System.out.println ("The number is positive");
else if (num < 0)
System.out.println ("The number is negative");
else
System.out.println ("Zero");
}
}
// 0 is a positive number because if we represent 0 in terms of binary :
0000 0000
// in the above representation the Most Significant Bit (MSD - First bit)
is 0 which
// indicate a positive number
1 / 57
Java Interview Specific Codes.md 2024-05-28
if (isEven (number))
System.out.println ("Even");
else
System.out.println ("Odd");
}
int n = 10;
int sum = 0;
//using formula
public class Main
{
public static void main (String[]args)
{
int n = 10;
System.out.println (n*(n+1)/2);
}
}
2 / 57
Java Interview Specific Codes.md 2024-05-28
int sum = 0;
//using formula
public class Main
{
public static void main(String[] args) {
int num1 = 2;
int num2 = 5;
int sum = num2*(num2+1)/2 - num1*(num1+1)/2 + num1;
System.out.println("The Sum is "+ sum);
}
}
}
}
4 / 57
Java Interview Specific Codes.md 2024-05-28
}
}
}
if (n < 2)
return false;
// checking the number of divisors b/w 1 and the number n-1
for (int i = 2; i < n; i++){
if (n % i == 0)
return false;
}
// if reached here then must be true
return true;
}
}
### 13. Find the Armstrong Numbers in a given Range using Java
```java
import java.util.Scanner;
{
len++;
num = num/10;
}
return len;
}
private static int getArmstrongSum(int num, int order) {
if(num == 0)
return 0;
import java.util.Arrays;
public class Anagram {
public static boolean isAnagram(String str1, String str2) {
// Remove all whitespace and convert to lowercase
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
// Check if the two strings have the same length
if (str1.length() != str2.length()) {
return false;
}
// Convert both strings to char arrays and sort them
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
17. Write a program to count the number of words in the given string
. check for spaces in the given string
. count the spaces inside a third party variable
. Add 1 to the total space count to find the total number of words.
10 / 57
Java Interview Specific Codes.md 2024-05-28
for(int i=0;i<str.length();i++){
if(str.charAt(i)==' ' || i==str.length()-1){
wordCount++;
}
}
return wordCount;
}
18. Write a program to find the count of all the uppercase letters in the given string
. Identify the ASCII value for A and Z (A=65 and Z=90)
. use a conditional statement to check if the character being checked falls under the given ranges
. if yes increment count
11 / 57
Java Interview Specific Codes.md 2024-05-28
19. Write a program to print the smallest and biggest numbers in an array.
import java.util.Arrays;
import java.util.Scanner;
int factorial = 1;
12 / 57
Java Interview Specific Codes.md 2024-05-28
import java.util.Scanner;
int num = n;
int sum = 0;
int digits = String.valueOf(n).length();
13 / 57
Java Interview Specific Codes.md 2024-05-28
}
}
if (isPerfectSquare) {
System.out.println(n + " is a perfect square.");
} else {
System.out.println(n + " is not a perfect square.");
}
}
}
23. Program for Finding out the Prime Factors of a number in Java
import java.util.Scanner;
import java.util.Scanner;
26. Check Whether or Not the Given Number is a Strong Number in Java Language
import java.util.Scanner;
if (sum == originalNum) {
System.out.println(originalNum + " is a strong number.");
} else {
System.out.println(originalNum + " is not a strong number.");
}
}
}
27. Program to check if the given number is an Automorphic number or not using Java
import java.util.Scanner;
if (lastDigits == originalNum) {
System.out.println(originalNum + " is an Automorphic
number.");
} else {
System.out.println(originalNum + " is not an Automorphic
number.");
}
}
}
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
16 / 57
Java Interview Specific Codes.md 2024-05-28
17 / 57
Java Interview Specific Codes.md 2024-05-28
System.out.println("Mins : "+mins);
System.out.println("Seconds : "+secs);
}
}
18 / 57
Java Interview Specific Codes.md 2024-05-28
//0 : 0 0 0 0 0 0 0 0
/*
* input-1 0 0 1 1
* input-2 0 1 0 1
* output 0 1 1 0
*
*
* if n is the number ---> 42
* then n is said to be even if n^1 == n+1 : 42 ^ 1 == 43
* then n is said to be odd if n^1 == n-1 : 43 ^ 1 == 42
*
* 42 in binary
* 2^0 2^1 2^2 2^3 2^4 2^5 2^6
19 / 57
Java Interview Specific Codes.md 2024-05-28
* 1 2 4 8 16 32 64
* 0 1 0 1 0 1
*
* --> 101010
* 1
* ----------
* 101011
*
* 43 in binary
* 2^0 2^1 2^2 2^3 2^4 2^5 2^6
* 1 2 4 8 16 32 64
* 1 1 0 1 0 1
* 101011
* 1
* ------
* 101010
*/
System.out.println("Armstrong");
}
else {
System.out.println("Not Armstrong");
}
}
public static boolean armstrongCheck(int n) {
int temp=n, power=0, last=0;
double sum=0;
while(temp>0) {
temp = temp/10;//153/10 --> 15/10-->1/10-->0
power++;//1,2,3
}
temp = n;
while(temp>0) {
last = temp % 10;//153%10-->3, 15%10-->5, 1%10-->1
sum = sum + (Math.pow(last,
power));//0+27=27,27+125=152,152+1=153
temp = temp/10;//153/10-->15, 15/10-->1
}
if(sum==n)
{
return true;
}
else {
return false;
}
}
}
Inheritance:
--> It refers to the hierarchy of classes
--> It represents Parent - Child relationship
--> Inheritance can be achieved by making used of extends key word
--> Advantages of Inheritance:
1. re-usability of code
2. coding time reduces
3. profit is increased
--> We cannot include private members for inheritance
--> We cannot inherit constructors
21 / 57
Java Interview Specific Codes.md 2024-05-28
class A{
A(){}
}
class B extends A{
A(){};//error
}
--> We cannot have multiple inheritance
class Parent1{
}
class Parent2{
}
class Child extends Parent1,Parent2{ //error
}
--> We can have multi-level inheritance in Java
class Shankar{
}
class Gopal extends Shankar{
}
class Vignesh extends Gopal{
}
--> We cannot have cyclic inheritance in java
class Shankar extends Gopal{
}
class Gopal extends Shankar{
}
class Vignesh extends Gopal{
}
--> We have 3 types of methods in inheritance
1. Inherited Method
2. Overridden method
3. Specialized method
Polymorphism:
--> Refers to 1:M relation
--> 2 categories
1. Complie time polymorphism
(Method overloading)
2. Run time polymorphism
(Method Overriding)
Abstraction:
--> Refers to the process of hiding the implementation
--> if a method does not have a implementation then we call such
methods as abstract method
--> An abstract method is indicated using "abstract" keyword
--> A class can be called as abstract class if it contains atleast one
abstract method
--> Can we create the object of an abstract class??
YES */
abstract class Shape {
double area;
abstract void input();//collecting input
abstract void calculate();//to calculate area
void display()
22 / 57
Java Interview Specific Codes.md 2024-05-28
{
System.out.println(area);
}
}
class Circle extends Shape {
private double radius;
void input(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the radius : ");
radius = sc.nextDouble();
}
void calculate() {
area = 3.147 * radius * radius;
}
}
class Square extends Shape {
private double side;
void input(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the side : ");
side = sc.nextDouble();
}
void calculate() {
area = side * side;
}
}
//
//class Rectangle extends Shape {
// // your code goes here
//}
int vow = 0;
int con = 0;
for(int i=0;i<s1.length();i++)
{
if(s1.charAt(i)=='A' || s1.charAt(i)=='E' || s1.charAt(i)=='I'
||
s1.charAt(i)=='O' || s1.charAt(i)=='U') {
vow++;
}
else if(s1.charAt(i)=='a' || s1.charAt(i)=='e' ||
s1.charAt(i)=='i' ||
s1.charAt(i)=='o' || s1.charAt(i)=='u') {
vow++;
}
else if(s1.charAt(i)!=' '){
con++;
}
}
System.out.println("Vowel : "+vow);
System.out.println("Consonant : "+con);
}
}
38. Find max and min value in array with using built in class Arrays
24 / 57
Java Interview Specific Codes.md 2024-05-28
25 / 57
Java Interview Specific Codes.md 2024-05-28
40. Anagram
import java.util.Arrays;
if (s1.length() == s2.length()) {
for (int i = 0; i <= s1.length() - 1; i++) {
if (s1.charAt(i) == ' ') {
} else {
s1_mod = s1_mod + s1.charAt(i);
}
}
for (int i = 0; i <= s2.length() - 1; i++) {
if (s2.charAt(i) == ' ') {
} else {
s2_mod = s2_mod + s2.charAt(i);
}
}
s1_mod = s1_mod.toUpperCase();
s2_mod = s2_mod.toUpperCase();
System.out.println(s1_mod);
System.out.println(s2_mod);
Arrays.sort(s1_arr);
Arrays.sort(s2_arr);
26 / 57
Java Interview Specific Codes.md 2024-05-28
} else {
System.out.println("Strings are not anagrams");
}
} else {
System.out.println("Strings are not anagrams");
}
}
}
} else {
s2 = s2 + s1.charAt(i);
}
}
}
System.out.println(s3);
System.out.println(s3.charAt(s3.length() - 1));
}
}
{
int cond1=0,cond2=0,cond3=0,cond4=0;
if(s.length()>=8 && s.length()<=15)
{
for(int i=0;i<=s.length()-1;i++) {
if(s.contains(" ")) {
System.out.println("Not a valid Password");
}
else {
if(s.charAt(i)>=48 && s.charAt(i)<=57)
{
cond1++;
}
if(s.charAt(i)>=97 && s.charAt(i)<=122)
{
cond2++;
}
if(s.charAt(i)>=65 && s.charAt(i)<=90)
{
cond3++;
}
if(s.charAt(i)>=35 && s.charAt(i)<=38)
{
cond4++;
}
}
}
}
else {
System.out.println("Invalid Password");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String :");
String s1 = sc.nextLine();
validation(s1);
}
}
28 / 57
Java Interview Specific Codes.md 2024-05-28
//arr[0] = "gninevE";
//arr[1] = "doog";
//arr[2] = "iH";
for(int i=arr.length-1;i>=0;i--) {
System.out.print(arr[i]+" ");
}
}
}
29 / 57
Java Interview Specific Codes.md 2024-05-28
for(int j=0;j<arr.length;j++) {
if(arr[i]==arr[j]) {
count++;
}
}
if(count%2 != 0) {
ts.add(arr[i]);
}
count=0;
}
System.out.println(ts);
}
}
class DataTypes{
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
//int t=sc.nextInt();
// for(int i=1;i<=t;i++)
// {
// try
// {
long x=sc.nextLong();
System.out.println(x+" can be fitted in: ");
if(x>=-128 && x<=127) {System.out.println("* byte");}
if(x>=Short.MIN_VALUE && x<=Short.MAX_VALUE)
{System.out.println("* short");}
if(x>=Integer.MIN_VALUE && x<=Integer.MAX_VALUE)
{System.out.println("* int");}
if(x>=Long.MIN_VALUE && x<=Long.MAX_VALUE)
{System.out.println("* long");}
// }
// catch(Exception e)
// {
// System.out.println(sc.next()+" can't be fitted
anywhere.");
// }
//
30 / 57
Java Interview Specific Codes.md 2024-05-28
// }
}
}
System.out.println("---------");
for(int i=0;i<arr.length;i++)//school
{
System.out.println("inside school no: "+(i+1));
for(int j=0;j<arr[i].length;j++) {
System.out.println("inside class no: "+(j+1));
for(int k=0;k<arr[i][j].length;k++) {
System.out.println("Student "+(k+1)+" Please Enter
the feedback:");
arr[i][j][k] = sc.next();
}
}
}
for(int i=0;i<arr.length;i++)//school
{
System.out.println("inside school no: "+(i+1));
for(int j=0;j<arr[i].length;j++) {
System.out.println("inside class no: "+(j+1));
for(int k=0;k<arr[i][j].length;k++) {
System.out.println("Feedback from Student-"+(k+1+" is
: "+arr[i][j][k]));
}
}
}
}
}
31 / 57
Java Interview Specific Codes.md 2024-05-28
for(int i=0;i<arr.length;i++) {
System.out.println("inside class no: "+(i+1));
for(int j=0;j<arr[i].length;j++) {
System.out.println("Student "+(j+1)+" Please Enter the
name:");
arr[i][j] = sc.next();
}
}
for(int i=0;i<arr.length;i++) {
System.out.println("inside class no: "+(i+1));
for(int j=0;j<arr[i].length;j++) {
System.out.println("Student "+(j+1)+" name is : "+arr[i]
[j]);
}
}
}
}
32 / 57
Java Interview Specific Codes.md 2024-05-28
33 / 57
Java Interview Specific Codes.md 2024-05-28
c1.add(a,b);
System.out.println(c1);
Calcy2 c2 = ()->{
// int a = 10;
// int b = 20;
int res = a+b;
System.out.println(res);
};
c2.add();
}
}
stmt = con.createStatement();
stmt.executeUpdate(sql);
System.out.println("Database Created");
stmt.close();
con.close();
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Scanner;
stmt = con.createStatement();
// pstmt = con.prepareStatement(sql);
stmt.executeUpdate(sql);
// pstmt.executeUpdate();
System.out.println("Table Created");
stmt.close();
con.close();
}
}
35 / 57
Java Interview Specific Codes.md 2024-05-28
pstmt.close();
con.close();
}
36 / 57
Java Interview Specific Codes.md 2024-05-28
stmt = con.createStatement();
res = stmt.executeQuery(sql);
while(res.next()) {
System.out.println(res.getInt(1));
System.out.println(res.getString(2));
System.out.println(res.getInt(3));
System.out.println(res.getInt(4));
System.out.println(res.getInt(5));
System.out.println(res.getString(6));
}
res.close();
stmt.close();
con.close();
}
37 / 57
Java Interview Specific Codes.md 2024-05-28
con.close();
}
56. 1D Array
import java.util.Scanner;
57. 2D Array
import java.util.Scanner;
/* (i) (j)
* class students
* 0 5
* 1 5
* 2 5
*/
public class ArrayCode2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the count of classes: ");
int cls = sc.nextInt();
System.out.println("Enter the count of students: ");
int stu = sc.nextInt();
int arr[][] = new int[cls][stu];//two dimensional array
//storing the marks
for(int i=0;i<arr.length;i++) {
System.out.println("Inside class :"+(i+1));
for(int j=0;j<arr[i].length;j++) {
System.out.println("Enter marks of student no: "+(j+1));
arr[i][j] = sc.nextInt();
}
}
//fetching the marks
for(int i=0;i<arr.length;i++) {
System.out.println("Inside class :"+(i+1));
for(int j=0;j<arr[i].length;j++) {
System.out.println("The marks of student no: "+(j+1)+" is:
"+arr[i][j] );
39 / 57
Java Interview Specific Codes.md 2024-05-28
}
}
}
}
59. WAP to replace all the vowels with a special character specified
/*
* a --> @
* e --> #
* i --> $
* o --> %
* u --> &
*
* Sample i/p : aeiou
* Sample o/p : @#$%&
*/
40 / 57
Java Interview Specific Codes.md 2024-05-28
import java.util.Scanner;
public class StringCode12 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = sc.next();
str = str.toLowerCase();
int vowels_cnt=0;
int cons_cnt=0;
String str2="";
for(int i=0;i<str.length();i++) {
if(str.charAt(i)=='a') {
str2=str2+"@";
}
else if(str.charAt(i)=='e') {
str2=str2+"#";
}
else if(str.charAt(i)=='i') {
str2=str2+"$";
}
else if(str.charAt(i)=='o') {
str2=str2+"%";
}
else if(str.charAt(i)=='u') {
str2=str2+"&";
}
else {
str2=str2+str.charAt(i);
}
}
System.out.println(str2);
}
}
String str2="";
for(int i=0;i<str.length();i++) {
if(str.charAt(i)==' ' && str.charAt(i+1)==' ') {
}
else {
str2=str2+str.charAt(i);
41 / 57
Java Interview Specific Codes.md 2024-05-28
}
}
System.out.println(str2);
}
}
//System.out.println(s1.charAt(50));//ArrayIndexOutOfBoundException
System.out.println(s1.indexOf('B'));
System.out.println(s1.indexOf('z'));
System.out.println(s1.startsWith("MaHa"));//true
System.out.println(s1.startsWith("Vaha"));//false
System.out.println(s1.endsWith("ratHA"));//true
System.out.println(s1.endsWith("Vaha"));//false
System.out.println(s1.contains("Bharat"));//true
System.out.println(s1.contains("Vharat"));//false
System.out.println(s1.getClass());
System.out.println(s1.isBlank());
System.out.println(s1.length());
System.out.println(s1.lastIndexOf('a'));
}
}
62. find the longest common prefix string amongst an array of strings
import java.util.Arrays;
}
public static void main(String[] args) {
String str[] = {"flower","flow","blight"};
String prefix = findPrefix(str);
System.out.println(prefix);
}
}
case 'I':
num = 1;
break;
case 'V':
num = 5;
break;
case 'X':
num = 10;
break;
case 'L':
num = 50;
break;
case 'C':
num = 100;
break;
case 'D':
num = 500;
break;
case 'M':
num = 1000;
break;
}
if (4 * num < ans)
ans = ans - num;
else
ans = ans + num;
}
return ans;//14
}
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(i==0 || i==(n-1) // first and last rows
|| j==0 || j==(n-1)// first and last column
|| i==(n/2) || j==(n/2)// middle row and column
|| i==j // first big diagonal (left to right)
44 / 57
Java Interview Specific Codes.md 2024-05-28
66. Pattern-1
/*
* 1 1 1 1 1
* 2 2 2 2 2
* 3 3 3 3 3
* 4 4 4 4 4
* 5 5 5 5 5
*/
public class Program{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value:");
int n = sc.nextInt();
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
System.out.print(i+" ");
}
System.out.println();
}
}
}
66. Pattern-2
45 / 57
Java Interview Specific Codes.md 2024-05-28
/*
* 1 2 3 4 5
* 1 2 3 4 5
* 1 2 3 4 5
* 1 2 3 4 5
* 1 2 3 4 5
*/
public class Program{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value:");
int n = sc.nextInt();
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
System.out.print(j+" ");
}
System.out.println();
}
}
}
67. Pattern-3
/*
* 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 Program{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value:");
int n = sc.nextInt();
int count = 1;
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
System.out.print(count+" ");
count++;
}
System.out.println();
}
}
}
46 / 57
Java Interview Specific Codes.md 2024-05-28
68. Pattern-4
/*
* 25 24 23 22 21
* 20 19 18 17 16
* 15 14 13 12 11
* 10 9 8 7 6
* 5 4 3 2 1
*/
public class Program{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value:");
int n = sc.nextInt();
int count = n*n;
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
System.out.print(count+" ");
count--;
}
System.out.println();
}
}
}
69. Pattern-5
/*
* 1 6 11 16 21
* 2 7 12 17 22
* 3 8 13 18 23
* 4 9 14 19 24
* 5 10 15 20 25
*/
public class Program{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value:");
int n = sc.nextInt();
int count = 1;
for(int i=1;i<=n;i++) {
count=i;
for(int j=1;j<=n;j++) {
System.out.print(count+" ");
count = count+n;
}
System.out.println();
}
47 / 57
Java Interview Specific Codes.md 2024-05-28
}
}
70. Pattern-6
/*
* @
* @ @
* @ @ @
* @ @ @ @
* @ @ @ @ @
*/
public class Program{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value:");
int n = sc.nextInt();
for(int i=1;i<=n;i++) {
for(int j=1;j<=i;j++) {
System.out.print("@ ");
}
System.out.println();
}
}
}
71. Patter-7
/*
* * * * * *
* * * * *
* * * *
* * *
* *
*/
public class Program{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value:");
int n = sc.nextInt();
for(int i=1;i<=n;i++) {
for(int j=n;j>=i;j--) { //*
System.out.print("* ");
}
System.out.println();
}
48 / 57
Java Interview Specific Codes.md 2024-05-28
}
}
72. Pattern-8
/*
* 1
* 2 2
* 3 3 3
* 4 4 4 4
* 5 5 5 5 5
*/
public class Program{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value:");
int n = sc.nextInt();
for(int i=1;i<=n;i++) {
for(int j=1;j<=i;j++) {
System.out.print(i+" ");
}
System.out.println();
}
}
}
73. Pattern-9
/*
* 1
* 1 2
* 1 2 3
* 1 2 3 4
* 1 2 3 4 5
*/
49 / 57
Java Interview Specific Codes.md 2024-05-28
}
}
74. Pattern-10
/* 1
* 2 3
* 4 5 6
* 7 8 9 10
* 11 12 13 14 15
*/
public class Program{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value:");
int count=1;
int n = sc.nextInt();
for(int i=1;i<=n;i++) {
for(int j=1;j<=i;j++) {
System.out.print(count+" ");
count++;
}
System.out.println();
}
}
}
* {
* this block of code will execute if the condition inside else if is
satisfied
* }
*
* ----- in-case condition inside else if also fails then the else
condition will be executed directly ----
*
* else
* {
* this block of code is directly executed if both if and else if
fails.
* }
*
*/
76. Pattern - 11
/*
* # # # # #
* # - - - #
* # - - - #
* # - - - #
* # # # # #
*/
51 / 57
Java Interview Specific Codes.md 2024-05-28
}
}
77. Pattern-12
/*
* #
* # #
* # - #
* # - - #
* # # # # #
*/
}
}
78. Alphabet-A
52 / 57
Java Interview Specific Codes.md 2024-05-28
}
}
79. Alphabet-B
public class Program{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value:");
int n = sc.nextInt();
for(int i=0;i<=n-1;i++) {
for(int j=0;j<=n-1;j++) {
if(j==0 || (j==n/2 && i!=0 && i!=n/2 && i!=(n-1))||
(i==n/2 && j<n/2) ||
(i==0 && j<n/2) ||
(i==(n-1) && j<n/2)) {
System.out.print("# ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
80. Alphabet-C
53 / 57
Java Interview Specific Codes.md 2024-05-28
}
}
81. Alphabet-D
public class Program{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value:");
int n = sc.nextInt();
for(int i=0;i<=n-1;i++) {
for(int j=0;j<=n-1;j++) {
if(j==0 || (i==0 && j<n/2) || (i==n-1 && j<n/2)
|| (j==n/2 && i!=0 && i!=n-1)) {
System.out.print("# ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
82. Alphabet-E
54 / 57
Java Interview Specific Codes.md 2024-05-28
}
}
83. Alphabet-F
public class Program{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value:");
int n = sc.nextInt();
for(int i=0;i<=n-1;i++) {
for(int j=0;j<=n-1;j++) {
if(j==0 || (i==0 && j<n/2)
|| (i==n/2 && j<n/2)) {
System.out.print("# ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
84. Alphabet-G
public class Program{
public static void main(String[] args) {
55 / 57
Java Interview Specific Codes.md 2024-05-28
}
System.out.println();
}
}
}
import java.util.Scanner;
void getData()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the age : ");
age = sc.nextInt();
56 / 57
Java Interview Specific Codes.md 2024-05-28
}
void verify()
{
if(age<18)
{
UnderAgeExpcetion uae = new UnderAgeExpcetion();
System.out.println(uae.getMessage());
}
else if(age>60) {
OverAgeExpcetion oae = new OverAgeExpcetion();
System.out.println(oae.getMessage());
}
else {
System.out.println("Accepted");
}
}
}
public class CustomException {
public static void main(String[] args) {
Customer c = new Customer();
c.getData();
c.verify();
}
}
57 / 57