In this article, "Top 100+ Java Coding Interview Questions and Answers," we will see some of the most common questions asked by teachers and interviewers.
Top 100+ java coding interview questions and answers
Top 100+ java coding interview questions and answers - Short Answer Question
Java coding interview questions and answers |
---|
Q1. Who is known as father of Java? A.1 James Gosling |
Q2. Latest version of Java A.2 Java 19 |
Q3. What is the original name of Java Programming language? A.3 OAK |
Q4. Which component is used to compile execute the java programs? A.4 JDK provides all the components required to compile and execute a Java Program. |
Q5. What is the extension of java program files? A.5 .java extension |
Q6. What is the size of float and double in java? A.6 Float = 32 and double = 64 |
Q7. What are Data Types in Java? A.7 A data type is a classification of data. A data type tells the compiler about the kind of value a variable is going to store. |
Q8. What is the meaning of polymorphism in Java? A.8 The word polymorphism is a combination of two greek words "poly" and "morphism" which means many forms. Polymorphism is a concept by which a single action can be performed in different ways. |
Q9. Which one of the following is not a Java feature? 1) Object-oriented 2) Use of pointers 3) Dynamic and Extensible 4) Portable A.9 2 Use of pointers |
Q10. Where Java was invented? A.10 Sun Microsystems |
Q11. What is Immutable object in java? A.11 Objects which cannot be changed after initialization |
Q12. Write example of immutable class in java A.12 String |
Q13. What is Casting in Java? A.13 It is a process to convert one data type to another. |
Q14. What is Inheritence in Java? A.14 Inheritance is a process in which one object acquires the properties of another object. |
Q15. What do you understand by an instance variable in Java? A.15 Instance variables are the variables which can be accessed by all the methods in the class. |
Q16. What do you understand by a local variable in java? A.16 Local variables are the variables present within a block, function, or constructor. Local Variables can be accessed only within the block. |
Q17. What is Data Encapsulation in java? A.17 Data Encapsulation is a concept of hiding the data attributes and their behaviours in a single unit. |
Q18. What is Heap memory in java? A.18 Heap memory is a memory which will be available for use by the java program only when it is required during the runtime of the program. |
Q19. What is Stack Memory in java? A.19 Stack memory is the portion of memory assigned to every individual program. |
Q20. What do you mean by wrapper classes in Java? A.20 The wrapper class helps to convert primitive into object and object into primitive. |
Top 100+ java coding interview questions and answers - Long Answer Question - Pattern Based Questions
Java Ques 1: Java program that accepts two doubles as its command line arguments, multiple these together and display the product.
class q1d{
public static void main(String ab[])
{
int len=ab.length;
System.out.println("array length is"+len);
if(len==0)
{
System.out.print("no command line values passed");
}
else {
double a,b,product;
a=Double.parseDouble(ab[0]);
b=Double.parseDouble(ab[1]);
product=a*b;
System.out.println("product of a and b is: "+product);
}
}
}
Java Ques 1: Java program that accepts two doubles as its command line arguments, multiple these together and display the product.
class q1d{ public static void main(String ab[]) { int len=ab.length; System.out.println("array length is"+len); if(len==0) { System.out.print("no command line values passed"); } else { double a,b,product; a=Double.parseDouble(ab[0]); b=Double.parseDouble(ab[1]); product=a*b; System.out.println("product of a and b is: "+product); } } }
Top 100+ java coding interview questions and answers - Long Answer Question - Pattern Based Questions
1 1
1 2 1
public class demopascal { public static void main(String[] args) { int row=5, i, j, space, number; for(i=0; i<row; i++) { for(space=row; space>i; space--) { System.out.print(" "); } number=1; for(j=0; j<=i; j++) { System.out.print(number+ " "); number = number*(i-j)/(j+1); } System.out.print("\n"); } } }
Java Ques 2: Java program to print the following pattern
public class demopattern { public static void main(String args[]) { System.out.println("Pattern Program \n"); for(int i = 1; i <= 6; i++) { for(int j = 6-i; j < 5; j++) { System.out.print(" "); } for(int k = i; k <= 6; k++) { System.out.print(k); } System.out.println(); } } }
101010
010101
101010
010101
101010
010101
public class demopattern { public static void main(String args[]) { int n; System.out.println("pattern program \n"); for(int i = 1; i <= 5; i++) { if(i%2 == 0) { n = 0; for(int j = 1; j <= 6; j++) { System.out.print(n); n = (n == 0)? 1 : 0; } } else { n = 1; for(int j = 1; j <= 6; j++) { System.out.print(n); n = (n == 0)? 1 : 0; } } System.out.println(); } } }
public class Pattern1demo { public static void main(String args[]) { System.out.println("Alphabet pattern \n"); for(int i = 1; i <10; i++)//for printing 5 rows { //create local variable with ASCII value of A i.e 65 int ch = 65; for(int j = 1; j <= i; j++) { System.out.print((char)ch); //casting of int value to char for printing character ch++; } System.out.println(); } } }
public class Pattern1demo { public static void main(String args[]) { System.out.println("Alphabet pattern \n"); int i = 1; int j = 1; while(i<10) { j=1; int ch = 65; while(j<=i) { System.out.print((char)ch); //casting of int value to char for printing character ch++; j++; } System.out.println(); i++; } } }
public class Demo1{ public static void main(String args[]){ int row=5; for(int i=0; i<row; i++) { for(int j=0; j<=i; j++) { System.out.print("* "); } System.out.println(); } } }
public class Demo2{ public static void main(String args[]) { for(int i = 5; i > 0; i--) { /* Prints one row of triangle */ for(int j = i; j > 0; j--) { System.out.print("* "); } /* move to next row */ System.out.print("\n"); } } }
***
****
*****
******
public class Demo3{ public static void main(String args[]) { int sp,star=0; int rows=6; //outer loop for printing rows for(int i = 1; i <= rows; i++) { // print leading space for(sp = 1; sp <= rows-i; sp++) { System.out.print(" "); }// print star while(star != (2*i - 1)){ System.out.print("*"); star++; } star=0; // move to next row System.out.print("\n"); } } }
public class Demo4{ public static void main(String args[]){ int rows=6; int space, star=0; for(int i = rows;i >= 1; i--) {// for leading space for(space = 0; space <= rows-i; space++) { System.out.print(" "); } //print pattern star = 0; while(star != (2*i - 1)) { System.out.print("*"); star++; } System.out.print("\n"); } } }
public class Demo5 { public static void main(String args[]) { for(int i = 1; i <= 7; i++) { for(int j = 1; j <= i; j++) { if(j%2 == 0){ System.out.print(0); }else{System.out.print(1);} } System.out.println(); } } }
5 6 7 8
9 10 11 12
public class Demo6 { public static void main(String args[]) { for(int i = 1; i <= 12; i++) { for(int j = 1; j <= 4; j++) { System.out.print(" "+i); i++; } i--; System.out.println(); } } }
101010
010101
101010
010101
101010
public class Demo7 { public static void main(String args[]) { for(int i = 1; i <= 5; i++) { int n; if(i%2 == 0) { n = 0; for(int j = 1; j <= 5; j++) { System.out.print(n); n = (n == 0)? 1 : 0; } } else { n = 1; for(int j = 1; j <= 6; j++) { System.out.print(n); n = (n == 0)? 1 : 0; } } System.out.println(); } } }
public class demopattern { public static void main(String args[]) { int t = 1; System.out.println("pattern program \n"); for(int i = 5; i >= 1; i--) { for(int j = 1; j <= i; j++) { System.out.print(t++ + " "); } System.out.println(); } } }
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
public class demopattern { public static void main(String[] args) { int rows = 5; System.out.println("Pattern Program"); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } //Printing lower half of the pattern for (int i = rows-1; i >= 1; i--) { for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } } }
Output:-
2 2
3 3 3
4 4 4 4
5 5 5 5 5
public class demosxcpattern { public static void main(String[] args) { int rows = 5; System.out.println("Pattern Program"); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(i+" "); } System.out.println(); } } }
Java Ques 16: Java program to print the following pattern
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
public class demopattern { public static void main(String[] args) { int rows = 5; System.out.println("Pattern Program \n"); for (int i = rows; i >= 1; i--) { for (int j = i; j >= 1; j--) { System.out.print(j+" "); } System.out.println(); } } }
public class demop { public static void main(String[] args) { int rows = 5; System.out.println("Pattern Program \n"); //for upper pattern for (int i = rows; i >= 1; i--) { for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } //for lower for (int i = 2; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } } }