HomeAutomation

Java for Testers Interview Questions And Answers – Part 14

Java for Testers Interview Questions And Answers – Part 14

Java for Testers – Interview Questions and Answers Part-14

1) Write a Java Program to print the below output: * 1 * 12 * 123 * 1234 * 12345 * 123456 * 1234567

public class Patterns {
public static void main(String[] args){
int c = 2;
int num = 1;
System.out.print("*" + " " + 1);
for(int i=1;i<7;i++){
int sum = (num*10) + c;
System.out.print(" *" + " " +sum);
num = sum;
c++;
}
}
}

 

2) Write a Java program to create an integer array int[] a = {9,3,6,8,4,7} and print the elements of the array in reverse?

public class ReverseArray {
public static void main(String[] args){
int[] a = {9,3,6,8,4,7};
for(int i=a.length-1;i>=0;i--){
System.out.println(a[i]);
}
}
}

 

3) Write a Java program to print alternative elements in a String array String[] a = {“One”,”Two”,”Three”,”Four”} ?

public class PrintAlternateElements {public static void main(String[] args){
String[] a = {"One","Two","Three","Four"};
for(int i=0;i<a.length;i=i+2){
System.out.println(a[i]);
}
}
}

 

4) Write a Java program to find the greatest number in an integer array int[] a = {9,3,6,4,8,5} ?

public class MaxNumber {
public static void main(String[] args){
Integer[] a = {9,3,6,4,8,5};
int max = Collections.max(Arrays.asList(a));
System.out.println(max);
}
}

 

5) Write a Java program to find the least number in an integer array int[] a = {9,3,6,4,8,5} ?

public class MinNumber {
public static void main(String[] args){
Integer[] a = {9,3,6,4,8,5};
int min = Collections.min(Arrays.asList(a));
System.out.println(min);
}
}

 

6) What is the predefined variable of Arrays, which can be used to find the size of the arrays?

length variable can be used to find the size of the arrays.

7) Provide an example for using for loop with single dimensional arrays?

public class ForLoopInArray {
public static void main(String[] args){
Integer[] a = {9,3,6,4,8,5};
for(int i=0;i<a.length-1;i++)
System.out.println(a[i]);
}
}

 

8) Provide an example for using for-each loop with single dimensional arrays?

public class ForEachLoopInArray {
public static void main(String[] args){
Integer[] a = {9,3,6,4,8,5};
for(int arr: a)
System.out.println(arr);
}
}

 

9) What are the different access modifiers in Java and explain each of them?

Following are different access modifiers in Java:
– private: Methods and variables declared as private can only be accessed within the same class in which they are declared
– protected: Methods and variables declared as protected can only be accessed within the same package or sub classes in different package
– public: Methods and variables declared as public can be accessed from anywhere
– default: Methods and variables which are not declared using any access modifier are accessible within the same package

10) How to find the length of the String without using length function?

public class FindLengthOfString {
public static void main(String[] args){
String s = "qascript";
int i = 0;
for(char c: s.toCharArray())
{
i++;
}
System.out.println(i);
}
}

 

11) How to find out the part of the string from a string?

We can use the substring function to find a part of the string from a string.
Ex –
String str = “QASCRIPT”;
System.out.println(str.substring(2));

12) What is data binding (Early and late binding)?

Early or static binding happens at compile time while late or dynamic binding happens at run time. The method definition and method call are linked during the compile time for early binding while the method definition and method call are linked during the run time for late binding.

13) Write a Java program to find find the biggest number among 1,2,3,4,5,65,76,5,4,33,4,34,232,3,2323?

public class BiggestNumber {
public static void main(String[] args){
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.add(65);
numbers.add(76);
numbers.add(5);
numbers.add(4);
numbers.add(33);
numbers.add(4);
numbers.add(34);
numbers.add(232);
numbers.add(3);
numbers.add(2323);
int maxNumber = Collections.max(numbers);
System.out.println(maxNumber);
}
}

 

14) What is String class and its methods?

String class has many methods to perform different operations on a string. Some of the methods are:
– length()
– charAt()
– substring()
– concat()
– indexOf()
– trim()
– replace()

15) Is multilevel inheritance is possible in java? Give reason.

Multilevel inheritance is not possible in java because multiple inheritance leads to ambiguity. Also dynamic loading of classes makes multiple inheritance implementation difficult.

16) What Java API is required for generating PDF reports?

iText and PdfBox APIs can be used for generatingPDF reports

17) What’s the difference between a Maven project and a Java project?

In a Java project we need to manually download and configure the jar files. But in Maven project all project dependencies can be downloaded by including them in the POM.xml file.

18) Write a Java program to read and write a file?

public class ReadFile {
public static void main(String[] args)
{
FileReader fr;
int c=0;
try {
fr = new FileReader("src/main/resources/File.txt");
while((c=fr.read())!= -1){
System.out.print((char) c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

 

public class WriteFile {
public static void main(String[] args){
try {
FileWriter fileWriter = new FileWriter("src/main/resources/File.txt");
fileWriter.write("Add new line to file");
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

 

19) Why do we provide “//” in java while fetching a path of excel?

Double backslash is provided in java to avoid compiler interpret words for system directories

20) What is a list?

List is an ordered collection. It is an interface that extends Collection interface.

21) Why vector class is defined Synchronized ?

Vector is a legacy class and was made synchronized in JDK 1.0.

22) String x=”ABC”; String x=”ab”; does it create two objects?

Yes. It creates two objects because string is immutable.

23) What is String tokenizer?

String tokenizer allows an application to break a string into tokens.

24) What is the method to convert string to integer?

Integer.parseInt(String) can be used to convert string to integer.

25) What is the method to convert integer to string?

Integer.toString() can be used to convert integer to string.

Next Steps:

> More interview questions and answers on Java, continue to the next post (Click on Next Post link below)

> Check complete Java interview questions and answers here (Click here)

Please leave your questions/comments/feedback below.

Happy Learning 🙂

About Me > Bijan Patel

Connect to me on Linked In (Click here)

Visit me on my site (Click here)

On a mission to contribute to the Software Testing Community in all possible ways.

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *