HomeAutomation

Java for Testers Interview Questions And Answers – Part 10

Java for Testers Interview Questions And Answers – Part 10

Java for Testers – Interview Questions and Answers Part-10


1) What is a Class in Java?

A class is the blueprint from which individual objects are created. It is the basic building block of an object-oriented language such as Java.

Ex-
class ClassName {
// variables
// methods
}


2) What is the difference between heap and stack?

– The main difference between heap and stack is that stack memory is used to store local variables and function call while heap memory is used to store objects in Java.
– If there is no memory left in the stack for storing function call or local variable, JVM will throw java.lang.StackOverFlowError, while if there is no more heap space for creating an object, JVM will throw java.lang.OutOfMemoryError: Java Heap Space.
– The size of stack memory is a lot lesser than the size of heap memory in Java.
– Variables stored in stacks are only visible to the owner Thread while objects created in the heap are visible to all thread. In other words, stack memory is kind of private memory of Java Threads while heap memory is shared among all threads.


3) What is a Constructor and the different types of Constructors?

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created.It can be used to set initial values for object attributes. There are two type of constructor in Java:
– No-argument constructor: A constructor that has no parameter is known as default constructor. If we don’t define a constructor in a class, then compiler creates default constructor(with no arguments) for the class.
– Parameterized Constructor: A constructor that has parameters is known as parameterized constructor. If you want to initialize fields of the class with your own values, then use a parameterized constructor.


4) What is the difference between break and continue statements?

– Break leaves the loop completely and executes the statements after the loop. Continue leaves the current iteration and executes with the next value in the loop.
– Break completely exits the loop. Continue skips the statements after the continue statement and keeps looping.


5) What is the command used in Java for exiting the system from current execution?

The java.lang.System.exit() method exits current program by terminating running Java virtual machine. This method takes a status code.


6) What is the method from which Java Programs starts execution?

A Java program starts by executing the main method of some class.


7) Give some examples for compile time errors in Java Programs?

– File Not Found Exception
– No Such Field Exception
– Interrupted Exception
– No Such Method Exception
– Class Not Found Exception


8) What is the difference between print and println statements in Java?

The only difference between println() and print() method is that println() throws the cursor to the next line after printing the desired result whereas print() method keeps the cursor on the same line.


9) What are the different types of comments in Java?

In Java there are three types of comments:

– Singleline comments: The single line comment is used to comment only one line.
– Multiline comments: The multi line comment is used to comment multiple lines of code.
– Documentation comments: The documentation comment is used to create documentation API.  To create documentation API, you need to use javadoc tool.


10) What are the different things required for storing data in Java?

– Stack Memory
– Heap Memory
– External data source


11) What is the different data types in Java and what is their purpose?

Data types are divided into two groups:
Primitive data types – includes byte, short, int, long, float, double, boolean and char
Non-primitive data types – such as String, Arrays and Classes


12) Is String a primitive data type?

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


13) What are the different types of Operators in Java?

– Arithmetic Operators: They are used to perform simple arithmetic operations on primitive data types.
– Unary Operators: Unary operators need only one operand. They are used to increment, decrement or negate a value.
– Assignment Operator: Assignment operator is used to assign a value to any variable.
– Relational Operators: These operators are used to check for relations like equality, greater than, less than. They return boolean result after the comparison
– Logical Operators: These operators are used to perform “logical AND” and “logical OR” operation, i.e. the function similar to AND gate and OR gate in digital electronics.
– Ternary Operator: Ternary operator is a shorthand version of if-else statement. It has three operands and hence the name ternary.
– Bitwise Operators: These operators are used to perform manipulation of individual bits of a number. They can be used with any of the integer types.
– Shift Operators: These operators are used to shift the bits of a number left or right thereby multiplying or dividing the number by two respectively.


14) What are the different flow control structures in Java?

There are 3 types of flow control structures:
– Decision-making statements : if-then, if-then-else, switch.
– Looping statements : for, while, do-while.
– Branching statements : break, continue, return.


15) What is the difference between while and do..while loop in Java?

If the condition in a while loop is false, not a single statement inside the loop is executed. In contrast, if the condition in ‘do-while’ loop is false, then also the body of the loop is executed at least once then the condition is tested.


16) Can we call the same method multiple times in Java?

We can use multiple threads to call same method multiple times.


17) What is the keyword to be used in Java while declaring methods, when the method don’t have anything to return?

Void() can be used for methods which don’t return anything.


18) What are the different types of Arrays in Java?

There are two types of array.

– Single Dimensional Array: In it each element is represented by a single subscript. The elements are stored in consecutive memory locations.
– Multidimensional Array: In it each element is represented by two subscripts.


19) What is Abstraction in Java?

Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces.


20) What is the difference between Java and JavaScript?

– Java is strongly typed language and variable must be declare first to use in program. JavaScript is weakly typed language and have more relaxed syntax and rules.
– Java is an object oriented programming language. JavaScript is an object based scripting language.
– Java applications can run in any virtual machine(JVM) or browser. JavaScript code run on browser only as JavaScript is developed for browser only.
– Java program uses more memory. JavaScript requires less memory therefore it is used in web pages.
– Java has a thread based approach to concurrency. Javascript has event based approach to concurrency.


21) How to find duplicate elements in a Java Array?

public class DuplicateElement {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 2, 7, 8, 8, 3};
System.out.println("Duplicate elements in given array: ");
//Searches for duplicate element
for(int i = 0; i < arr.length; i++) {
for(int j = i + 1; j < arr.length; j++) {
if(arr[i] == arr[j])
System.out.println(arr[j]);
      }
    }
  }
}

22) How to find the smallest and largest numbers in a Java Array?

public class FindLargestSmallestNumber {

public static void main(String[] args) {
//numbers array
int numbers[] = new int[]{55,32,45,98,82,11,9,39,50};
//assign first element of an array to largest and smallest
int smallest = numbers[0];
int largest= numbers[0];
for (int i = 1; i & lt; numbers.length; i++) {
if (numbers[i] > largest)
largest= numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largest);
System.out.println("Smallest Number is : " + smallest);
  }
}

23) Explain String manipulation in Java?

Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.


24) Write a Java program to swap two strings without using temp or third variable?

class SwapNumber {
public static void main(String a[])
{
int x = 10;
int y = 5;
x = x + y;
y = x - y;
x = x - y;
System.out.println("After swaping:"
+ " x = " + x + ", y = " + y);
   }
}

25) How to achieve multiple inheritance in Java?

Multiple inheritance in Java can be achieved by using interfaces.


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 *

For FREE Testing Tutorials & Videos

X
Open chat
Contact Us on Whatsapp