Monday, May 13, 2024

Java coding interview questions for 3 years experience

 Q1. Write a program to Reverse a Number in Java.

public class Main {

public static void main(String[] args)

{

int number = 677654, reverse = 0;

while(number != 0){

int remainder = number % 10;

reverse = reverse * 10 + remainder;

number = number/10;

}

System.out.println("The reverse of the given number is: " + reverse);

}

}

Output:The reverse of the given number is: 456776

Q2.Write a program to Left Rotate an Array in Java?

class Main {

public static void main(String[] args) {

int [] arr = {1, 2, 3, 5, 6, 4, 5};

System.out.println("Original array: ");

for (int i = 0; i < arr.length; i++) {

System.out.print(arr[i] + " ");

}

System.out.println();

int j, first;

first = arr[0];

for(j = 0; j < arr.length-1; j++){

arr[j] = arr[j+1];

}

arr[j] = first;

System.out.println("Array after left rotation: ");

for(int i = 0; i< arr.length; i++){

System.out.print(arr[i] + " ");

}

}

}

Output:

Original array:

1 2 3 5 6 4 5

Array after left rotation:

2 3 5 6 4 5 1

Q3.Write a program to check whether Two Matrices are identical or not.

class Matrices{

static final int N = 4;

static int areSame(int A[][], int B[][]){

int i, j;

for (i = 0; i < N; i++)

for (j = 0; j < N; j++)

if (A[i][j] != B[i][j])

return 0;

return 1;

}

public static void main (String[] args){

int A[][] = { {1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3}, {4, 4, 4, 4}};

int B[][] = { {1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3}, {4, 4, 4, 4}};

if (areSame(A, B) == 1)

System.out.print("Matrices are identical");

else

System.out.print("Matrices are not identical");

}

}

Output:Matrices are identical

Q4.Convert Binary Number into Decimal Equivalent in Java?

class Main {public static int convertBinaryToDecimal(long num) {

int decimalNumber = 0, i = 0;

long remainder;

while (num != 0) {

remainder = num % 10;

num /= 10;

decimalNumber += remainder * Math.pow(2, i);

++i;

}

return decimalNumber;

}

public static void main(String[] args) {

long num = 110111;

int decimal = convertBinaryToDecimal(num);

System.out.println("Binary to Decimal");

System.out.println(num + " = " + decimal);

}

}

Output:

Binary to Decimal

110111 = 55

Q5.Write a Program to Second Largest Number in an array?

public class Main{

public static int getSecondLargest(int[] a, int total){

int temp;

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

for (int j = i+1; j<total; j++){

if (a[i]>a[j]){

temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

}

return a[total-2];

}

public static void main(String args[]){

int a[]={1,2,5,6,3,2};

System.out.println("Second Largest: "+getSecondLargest(a,6));

}

}

Output:-Second Largest: 5

Q6. Which of the following would the below Java coding snippet return as its output?

class Super {

public int index = 1;

}

class App extends Super {

public App(int index) {

index = index;

}

public static void main(String args[]) {

App myApp = new App(10);

System.out.println(myApp.index);

}

}

Output: 1

Q7. Which of the following combinations would the below Java coding snippet print?

class TestApp {

protected int x, y;

}class Main {

public static void main(String args[]) {

TestApp app = new TestApp();

System.out.println(app.x + " " + app.y);

}

}

Output:0,0

Q8. What would be the outcome of the below Java code?

class TestApp {

public static void main(String[] args) {

for (int index = 0; 1; index++) {

System.out.println("Welcome");

break;

}

}

}

Output:Type mismatch error

Q9. What would the below Java coding snippet print?

class TestApp {

public static void main(String[] args) {

for (int index = 0; true; index++) {

System.out.println("Welcome");

break;

}

}

}

Output: Welcome

Q10. Which of the following values would the below Java coding snippet print in the

results?

class TestApp {

int i[] = {

0

};

public static void main(String args[]) {

int i[] = {

1

};alter(i);

System.out.println(i[0]);

}

public static void alter(int i[]) {

int j[] = {

2

};

i = j;

}

}

Output:1

Q11. Which of the following is the result of the following Java code?

class TestApp {

String args[] = {

"1",

"2"

};

public static void main(String args[]) {

if (args.length > 0)

System.out.println(args.length);

}

}

Output:The program compiles but prints nothing

Q12. What is the result of the following Java coding snippet?

class TestApp {

public static void main() {

int odd = 1;

if (odd) {

System.out.println("odd");

} else {

System.out.println("even");

}

}

}

Output:Type mismatch error

Q13. What would the following function yield when called?

public void test(boolean a, boolean b) {

if (a) {System.out.println("A");

} else if (a && b) {

System.out.println("A && B");

} else {

if (!b) {

System.out.println("!B");

} else {

System.out.println("None");

}

}

}

Output:If a is false and b is true, then the output is “None”.

Q14. What would the following Java coding snippet return as its output?

class TestApp {

public static void main(String[] args) {

class Tutorial {

public String name;

public Tutorial(String tutorial) {

name = tutorial;

}

}

Object obj = new Tutorial("Java Quiz");

Tutorial tutorial = (Tutorial) obj;

System.out.println(tutorial.name);

}

}

Output:It’ll print “Java Quiz”.

Q15. What does the following Java coding snippet print?

import java.io.CharArrayReader;

import java.io.IOException;

class TestApp {

public static void main(String[] args) {

String obj = "abcdef";

int length = obj.length();

char c[] = new char[length];

obj.getChars(0, length, c, 0);

CharArrayReader io_1 = new CharArrayReader(c);

CharArrayReader io_2 = new CharArrayReader(c, 0, 3);

int i;try {

while ((i = io_1.read()) != -1) {

System.out.print((char) i);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

Output:abcdef

No comments:

Post a Comment