Most Frequently Asked Java Interview Programs. PART-4
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. This is the 4th part of the interview program, 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. Swap values.
Note: This program is using the third variable "int c" to swap values of variables a and b.
public class Swap {
public static void main(String arg[]) {
int a = 10;
int b = 20;
int c ;
System.out.println("Actual values A:"+a+", B:"+b);
c = a;
a = b;
b = c;
System.out.println("After Swap");
System.out.println("Swaped values A:"+a+", B:"+b);
}
}
OUTPUT
Actual values A:10, B:20
After Swap
Swaped values A:20, B:10
2. Swap without using third variable.
Note: You can swap values without using third vairable here is the two ways to do so, you can simply compile this code and also you can uncomment the code block which is showing another way to swap valus without using third variable.
public class SwapWithoutThirdVariable {
public static void main(String arg[]) {
int a = 10;
int b = 20;
System.out.println("Actual values. A:"+a+", B:"+b);
a = a + b;
b = a - b;
a = a - b;
/*a = a * b;
b = a / b;
a = a / b;*/
System.out.println("Swaped values. A:"+a+", B:"+b);
}
}
OUTPUT
Actual values. A:10, B:20
Swaped values. A:20, B:10
3. Thread deadlock.
Note: Interviewer may also ask you to create thread deadlock. This simple program may help you to create a simple thread deadlock using synchronized blocks. The Program output may vary everytime.
public class ThreadDeadlock {
String str1= "suraj";
String str2= "singh";
Thread t1 = new Thread("My Thread1") {
public void run() {
while(true) {
synchronized(str1) {
synchronized(str2) {
System.out.println(str1+" : "+str2);
}
}
}
}
};
Thread t2 = new Thread("My Thread2") {
public void run() {
while(true) {
synchronized(str2) {
synchronized(str1) {
System.out.println(str2+" : "+str1);
}
}
}
}
};
public static void main(String arg[]) {
ThreadDeadlock td = new ThreadDeadlock();
td.t1.start();
td.t2.start();
}
}
OUTPUT
suraj : singh
suraj : singh
suraj : singh
suraj : singh
suraj : singh
suraj : singh
suraj : singh
suraj : singh
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
singh : suraj
4. Transpose of Metrics.
Note: Using this program you can create metrics and also you will learn the logic of transpose of metrics.
import java.util.Scanner;
public class TransposeMetrics {
public static void main(String arg[]) {
int i, j;
int arr[][] = new int[3][3];
int arrt[][] = new int[3][3];
Scanner sc= new Scanner(System.in);
System.out.println("Enter 3x3 Array Elements: ");
for(i=0; i<3; i++) {
for(j=0; j<3; j++) {
arr[i][j] = sc.nextInt();
}
}
System.out.println("Simple Metrics");
for(i=0; i<3; i++) {
for(j=0; j<3; j++) {
System.out.print(" "+arr[i][j]);
}
System.out.println();
}
System.out.println("Transpose Metrics");
for(i=0; i<3; i++) {
for(j=0; j<3; j++) {
arrt[i][j] = arr[j][i];
}
}
for(i=0; i<3; i++) {
for(j=0; j<3; j++) {
System.out.print(" "+arrt[i][j]);
}
System.out.println();
}
}
}
OUTPUT
Enter 3x3 Array Elements:
1
2
3
4
5
6
7
8
9
Simple Metrics
1 2 3
4 5 6
7 8 9
Transpose Metrics
1 4 7
2 5 8
3 6 9
5. String lower and upper case conversion without using the builtin method.
Note: You can convert lower to upper case and vice versa using builtin java methods but, Interviewer may ask you to do with your own logic.
public class LowerToUpperCase {
public static void main(String arg[]) {
String str = "suraj pratap singh";
char arr[] = str.toCharArray();
for(char c: arr) {
/**
* Converts Upper case to Lower case.
*/
/*if (c > 64 && c <= 90) {
c += 32;
}*/
/**
* Converts Lower Case to Upper Case.
*/
if (c > 96 && c <= 122) {
c -= 32;
}
System.out.print(c);
}
}
}
OUTPUT
SURAJ PRATAP SINGH
6. Print ; (semicolon) without using ; (semicolon).
Note: This is a tricky program that can be used to judge your native java knowledge.
public class PrintSemicolon {
public static void main(String arg[]) {
if(System.out.printf("%c", 59) != null) {
}
}
}
OUTPUT
;
7. Print comment.
Note: You can print single line comment using program.
public class PrintComment {
public static void main(String arg[]) {
//System.out.println("This comment should not get printed.");
// \u000dSystem.out.println("This comment should get printed.");
}
}
OUTPUT
This comment should get printed.
8. Database program using JDBC driver.
Note: This simple program will help you to create database connectivity and insert a single record into t1 (table) into the database having name test1.
- You need to download mysql-connector-java-5.0.8-bin.jar MySQL connector jar file and set this jar file into your classpath.
import java.sql.*;
public class InsertUsingJDBC {
/**
* @param arg
* @throws Exception
* @info: First you need to download mysql connector jar file and set into the classpath.
*/
public static void main(String arg[]) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test1","root","root");
Statement stmt = con.createStatement();
stmt.executeUpdate("insert into t1 values(1, 'suraj')");
System.out.println("Record inserted...");
con.close();
}
}
OUTPUT
Record inserted...
An example image of test1 DB
9. Named Loop.
Note: This program is used to send control flow one place to another, Recommendation is not to use because it can harm privacy and security into the enterprise application.
public class NamedLoop {
public static void main(String arg[]) {
lbl:
for(int i=0; i<=5; i++) {
for(int j=0; j<=5; j++) {
if (i==3) {
break lbl;
}
System.out.println("i: "+i+", j: "+j);
}
}
}
}
OUTPUT
i: 0, j: 0
i: 0, j: 1
i: 0, j: 2
i: 0, j: 3
i: 0, j: 4
i: 0, j: 5
i: 1, j: 0
i: 1, j: 1
i: 1, j: 2
i: 1, j: 3
i: 1, j: 4
i: 1, j: 5
i: 2, j: 0
i: 2, j: 1
i: 2, j: 2
i: 2, j: 3
i: 2, j: 4
i: 2, j: 5
10. Generate OTP.
Note: This program will take length of the OTP as a parameter of the method, Then it will return a random number of that length.
import java.util.Random;
public class GenerateOTP {
static char[] setOTP(int length) {
System.out.println("Your OTP Generated");
String numbers = "0123456789";
Random r = new Random();
char []otp = new char[length];
for(int i=0; i<length; i++) {
otp[i] = numbers.charAt(r.nextInt(numbers.length()));
}
return otp;
}
public static void main(String arg[]) {
System.out.println(setOTP(4));
}
}
OUTPUT
Your OTP Generated
2772
Comments
Post a Comment