HomeAutomation

Java for Testers Interview Questions And Answers – Part 12

Java for Testers Interview Questions And Answers – Part 12

Java for Testers – Interview Questions and Answers Part-12

1) Explain about Diamond problem in Java?

The “diamond problem” is an ambiguity that can arise as a consequence of allowing multiple inheritance. The problem occurs when there exist methods with same signature in both the super classes and subclass. On calling the method, the compiler cannot determine which class method to be called and even on calling which class method gets the priority.

2) What are primitive data types in Java?

There are 8 primitive data types:
– boolean: boolean data type represents only one bit of information either true or false
– byte: The byte data type is an 8-bit signed two’s complement integer.
– short: The short data type is a 16-bit signed two’s complement integer
– int: It is a 32-bit signed two’s complement integer.
– long: The long data type is a 64-bit two’s complement integer
– float: The float data type is a single-precision 32-bit IEEE 754 floating point
– double: The double data type is a double-precision 64-bit IEEE 754 floating point
– char: The char data type is a single 16-bit Unicode character

3) Why String is non primitive?

String is not a primitive data type. Java.lang package provides the String class therefore, it is an object type.

4) Can we create an object for an interface?

We can’t instantiate an interface in java. That means we cannot create the object of an interface.

5) Difference between Instantiate and Initialize in Java?

Initialization-Assigning a value to a variable i.e a=0,setting the initial values.
Instantiation- Creating the object i.e when we reference a variable to an object with new operator.

6) Difference between == and =?

== is used to compare values and = is used to assign a value.

7) Are all methods in an abstract class, abstract?

No. Abstract class can have both abstract and non-abstract methods.

8) What’s Singleton class?

Singleton is a design pattern rather than a feature specific to Java. It ensures that only one instance of a class is created.

9) Can we have Finally block without Try & Catch blocks?

We can use finally block without a catch block but we need to have try block.

10) How to execute Java program from the command prompt?

We use Java compiler javac to compile and the Java interpreter java to run the Java program.

11) Any example or practical usage of Run time polymorphism?

Method overriding is an example of runtime polymorphism. In method overriding,
a subclass overrides a method with the same signature as that of in its superclass

12) Why we use public static void for main?

It is made public so that JVM can invoke it from outside the class as it is not present in the current class. The main() method is static so that JVM can invoke it without instantiating the class. As main() method doesn’t return anything, its return type is void. It is the identifier that the JVM looks for as the starting point of the java program.

13) What is the difference between error and exception?

Exceptions are the conditions that occur at runtime and may cause the termination of program.
An Error “indicates serious problems that a reasonable application should not try to catch.”

14) Is Java a value based or reference based?

Java uses only call by value while passing reference variables as well. It creates a copy of references and passes them as valuable to the methods.

15) Explain collections?

The Java collections framework provides a set of interfaces and classes to implement various data structures and algorithms.

16) How to synchronize collection class?

The synchronizedCollection() method of Java Collections class is used to get a synchronized (thread-safe) collection backed by the specified collection.

17) How do you know when to use abstract class and Interface?

If the functionality we are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes.

18) What are the different access specifiers in Java?

There are four types of access modifiers available in java:
Default – When no access modifier is specified for a class , method or data member. It is said to be having the default access modifier by default.
Private – The methods or data members declared as private are accessible only within the class in which they are declared.
Protected – The methods or data members declared as protected are accessible within same package or sub classes in different package.
Public – The public access modifier is specified using the keyword public.

19) Write a Java program to demonstrate the advantage of finally in Exception Handling?

public class Example {

public static void main(String[] args) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("C:\\sample.txt"));
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

 

20) Is it mandatory to have the same name for Class name and Java file ?

While writing a java program first it is saved as a “.java” file, when it is compiled it forms byte code which is a “.class” file

21) How do you write custom class which is immutable?

public final class Student
{
final String name;
final int regNo;

public Student(String name, int regNo)
{
this.name = name;
this.regNo = regNo;
}
public String getName()
{
return name;
}
public int getRegNo()
{
return regNo;
}
}

 

22) How to execute multiple Java programs at a time?

We can execute multiple java prograns using multithreading.

23) How do you call function in java?

To call a function in Java, write the function’s name followed by two parentheses () and a semicolon.

24) Can we execute java code without main()?

Yes, we can execute a java program without a main method by using a static block.
Static block in Java is a group of statements that gets executed only once when the class is loaded into the memory by Java ClassLoader, It is also known as a static initialization block.

25) What are abstract classes and methods in Java?

Abstract classes may or may not contain abstract methods, i.e., methods without body.

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 *