Java interviu questions – Senior QA automation position

Quality assurance, Devs and SEO teams ready to help your business grow

Java interviu questions – Senior QA automation position

1. What is the difference between abstract classes and interface in Java?

Abstract classes should be used when the Java developer wants to use non static, non final, non public fields. Interfaces can also have implemented methods (default methods in Java 8), this would not be a difference between the 2 anymore. Interfaces allow to declare only final, public and static components.

2. What is polymorphism?
Many faces. Declare an object of a certain type and instantiate it with an object of a compatible type (Example Class A implements the interface MyTest.
We can declare MyTest mt = new A(); )

3. How can you compare 2 java objects from the same class?

Objects instantiated from a Java class can be compared by overwriting the equals() method inherited from Object class.

4. What classes are derived from Throwable?

Exception and Error.

5. Is it possible that the Java developer treats an Error?

Yes though its not a good practice

 try {
            new Experts().cluster();
            
        } catch (Error e) {
            System.out.println(e);
        }

 

6. How can we insert in a Hashmap where the keys are objects from a class unique values?

Using hash code and overriding hash code() method.

7. How can an immutable class can be declared in Java?

Private constructor.

Make all muttable elements inside the class immutable.

Declare setters.

 

8. What is a static variable in Java?

A static variable in a Java class is a variable that belongs to the class and when its value is changed, this change is reflected in all the objects of that class.

A static variable should be declared right in the beginning of the class or inside a static block.

9. Concurrency? How to use it in Java?

Low level: Extend Runnable interface by a declared class.

Create an instance of Thread class and pass the above instance of the class as a parameter to the constructor of the Thread class.

High Level: Executor Service and other Java high level objects (Ex Semaphore)

10. What methods cannot be changed in Java?

Final methods cannot be overridden.

Private methods cannot be accessed from outside the class.

The constructor method cannot be changed.

11. What is a Java interface? Use example.

A java interface is a contract. It is used in correlation with polymorphism and has advantaged in certain design patterns as Abstract Factory Design pattern.

Pass to a method a variable of interface type and inside the method when the instantion occurs check what kind of object is (Should be an object derived from a class that implemented that interface). Each class that implemented the interface should have overriden the interface methods in their own specific way and thus depending on what kind of object is called a different method implementation would be accessed.

12. Why use syncronized?

We can avoid deadlock by using syncronized on a certain block of instructions to let just one thread access that block of instructions at a certain time.

Though should carefully make use of syncronized because we should let other instructions that do not need syncronization to be executed in parallel.

13. XML parsing

Can use JaxB or Chain of responsibilities design pattern.

14. What is the difference between arrays in Java and Lists?

15. How to implement a queue in java?

16. How to create the development plan for an online shop?

Make use of Model View Controller Architecture.

Make use of Data Access Object and Data Transfer Object design patterns.

17. Singleton design pattern

For class MyTest

Declare a static variable of type MyTest:

MyTest mt ;

Create a private constructor

Create a getInstance method which whose return type is MyTest that will return a

18. Builder design pattern

Create a nested static class. Create a build method. Create setters for each field variable.

19. object.clone() when can be used? If the constructor is private, can we clone an object?

when cloning an object,  no constructor is called

We cannot count the correct number of objects

20. String class

String a = “expertscloud”;

String b = new String(“expertscloud”);

String c = “expertscloud”;

Observations: a and c objects are not equal.

a and c objects are equal. They will point to the same object in the String pool.

21.If System.exit(0) block is called inside a catch block, will the instructions included in the finally block be executed?

The finally block will not execute if System.exit(0) is called (since the Java VM exits when that statement is called)

 

22. Exception handling in java7/8?

Catching More Than One Type of Exception with One Exception Handler in se7
Try with resources that will close the resources for classes that implemented AutoCloseable.
As per Oracle:
The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

Because the Java programming language does not require methods to catch or to specify unchecked exceptions (RuntimeException, Error, and their subclasses), programmers may be tempted to write code that throws only unchecked exceptions or to make all their exception subclasses inherit from RuntimeException.
Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way.

If an argument is null, the method might throw a NullPointerException, which is an unchecked exception.

Generally speaking, do not throw a RuntimeException or create a subclass of RuntimeException simply because you don’t want to be bothered with specifying the exceptions your methods can throw.

Here’s the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception

The following example reads the first line from a file. It uses an instance of BufferedReader to read data from the file. BufferedReader is a resource that must be closed after the program is finished with it:

static String readFirstLineFromFile(String path) throws IOException {
try (BufferedReader br =
new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}

Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.

 

Suppressed exceptions can be extracted calling the Throwable.getSuppressed method from the exception thrown by the try block

Throwable objects are instances of any subclass of the Throwable class

Only this class or one of its subclasses can be the argument type in a catchclause.

23. Can a static method be overriden in a subclass?

No. If the child class tries to override a method, the method from parent class is hidden and a new class is reffered to in the subclass.

Methods that can not be overriden from outside a class: private methods, static methods, constructor

Other Java interview questions

 

24. What is a Java object?

An object is a runtime instance of a class in memory. All the various objects of all the different classes represent the state of your program.

an object is an instance of a class

To create an instance of a class, all you have to do is write new before it. For example: Random r = new Random();

Declare the type that you’ll be creating (Random) and give the variable a name (r). This gives Java a place to store a reference to the object. Then you write new Random() to actually create the object.

Random() looks like a method since it is followed by parentheses. It’s called a construc- tor, which is a special type of method that creates a new object.

There are two key points to note about the constructor: the name of the constructor matches the name of the class, and there’s no return type

The purpose of a constructor is to initialize elds, although you can put any code in there. Another way to initialize elds is to do so directly on the line on which they’re declared.

For most classes, you don’t have to code a constructor—the compiler will supply a “do nothing” default constructor for you.

25. What is Java main() method?

A Java program begins execution with its main() method. A main() method is the gateway between the startup of a Java process, which is managed by the Java Virtual Machine (JVM), and the beginning of the programmer’s code. The JVM calls on the underlying system to allocate memory and CPU time, access les, and so on.

The main() method lets us hook our code into this process, keeping it alive long enough to do the work we’ve coded.

the main() method’s parameter list, represented as an array of java. lang.String objects. In practice, you can write String[] args, String args[] or String… args; the compiler accepts any of these. The variable name args hints that this list contains values that were read in (arguments) when the JVM started.

Java doesn’t need to create an object to call the main() method

If a main() method isn’t present in the class we name with the .java executable, the process will throw an error and terminate. Even if a main() method is present, Java will throw an exception if it isn’t static. A nonstatic main() method might as well be invisible from the point of view of the JVM.

26. How to compile a java class?

To compile and execute a java class code, type it into file called Ex.java and execute the following:

$ javac Ex.java

$ java Ex

To compile Java code, the le must have the extension .java. The name of the le must match the name of the class. The result is a le of bytecode by the same name, but with
a .class lename extension. Bytecode consists of instructions that the JVM knows how to execute. Notice that we must omit the .class extension to run Zoo.java because the period has a reserved meaning in the JVM.

26. What are Java access modifiers? : Public, private(access only at class level), default(package level), protected(package level+subclasses)

they define (the method’s or class’ or field’s)  level of exposure to potential callers in the program

The keyword static binds a method to its class so it can be called by just the class name.

27. Return types

The keyword void represents the return type. A method that returns no data returns control to the caller silently. In general, it’s good practice to use void for methods that change an object’s state

28. What is a Java list?

An array is a xed-size list of items that are all of the same type.

29. Java imports

The * is a wildcard that matches all classes in the package.

Every class in the java.util package is available to this program when Java compiles it.

It doesn’t import child packages, fields, or methods; it imports only classes.  there’s a special type of import called the “static import” that imports other types.

When the class is found in multiple packages, Java gives you the compiler error:

Example:

import java.util.Date;

import java.sql.*;

If you explicitly import a class name, it takes precedence over any wildcards present

What does Java do with “ties” for precedence?

import java.util.Date; import java.sql.Date;

Java is smart enough to detect that this code is no good. As a programmer, you’ve claimed to explicitly want the default to be both the java.util.Date and java.sql.Date implementations. Because there can’t be two defaults, the compiler tells you:

The import java.sql.Date collides with another import statement

If  You Really Need to Use Two Classes with the Same Name:

can pick one to use in the import and use the other’s fully qualified class name (the package name, a dot, and the class name) to specify that it’s special. For example:

import java.util.Date;

public class Conflicts { Date date;

java.sql.Date sqlDate;

}

could have neither with an import and always use the fully qualified class name

30. What are Class Paths and JARs?

can also specify the location of the other files explicitly using a class path. This tech- nique is useful when the class files are located elsewhere or in special JAR files. A JAR file is like a zip file of mainly Java class files.

 

No Comments

Add your comment