Java for Testers – Interview Questions and Answers Part-6
1) How many ways we can define a String in Java?
– Using String literal
String str = "Automation";
– Using new keyword
String str1 = new String("Automation");
– Using Character Array
char ch[] = {'A','U','T','O','M','A','T','I','O','N'} String str2 = new String(ch);
2) What is the difference between super and this keyword in Java?
The main difference between this and super in Java is that this is used in context of the class you are working on, while super is used to refer current instance of parent class.
3) What is the difference between final and finally keywords?
The final keyword makes a Java variable, method or class as final and it cannot be changed once it is initialized.
Java finally block is part of try-catch-finally blocks for exception handling. A finally block is guaranteed to be executed despite whether an exception is thrown or not from the try block.
4) Can you access the private method from outside the class?
No. Private method cannot be accessed outside the class.
5) How to convert Array to ArrayList and ArrayList to Array?
Array to ArrayList:
String[] array = {"Java","Pyhton"}; List list = Arrays.asList(array);
Arraylist to Array:
ArrayList<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); //Convert to object array Object[] array = list.toArray();
6) Why static needs to be specified before variables and classes? What actually is its real time purpose/advantage?
Static methods can be utilized without having to instantiate the class they belong to. We declare methods static so they can be accessed without holding an instance of an Object based on that class.
7) Why we have to use the synchronized block in Java?
We synchronize a block of code to avoid any modification in state of the Object and to ensure that other threads can execute rest of the lines within the same method without any interruption.
8) What is the difference between HashSet and TreeSet?
– HashSet gives better performance (faster) than TreeSet for the operations like add, remove, contains, size etc. HashSet offers constant time cost while TreeSet offers log(n) time cost for such operations.
– HashSet does not maintain any order of elements while TreeSet elements are sorted in ascending order by default.
9) What is the difference between public and private access modifier?
– Public methods are accessible from anywhere but private methods are only accessible inside the same class where it is declared.
– Public keyword provides lowest level of Encapsulation and Private modifier provides highest level of Encapsulation in Java.
10) What is the difference between String array and Char array?
– String refers to a sequence of characters represented as a single data type. Character Array is a sequential collection of data type char.
– Strings are immutable. Character Arrays are mutable.
– Strings can be stored in any any manner in the memory. Elements in Character Array are stored contiguously in increasing memory locations.
– All Strings are stored in the String Constant Pool. All Character Arrays are stored in the Heap.
11) Is null a keyword in Java?
No. null is not a keyword in Java.
12) What are class level variables in Java?
– These variables are created when an object of the class is created and destroyed when the object is destroyed.
– Initilisation of Instance Variable is not Mandatory. Its default value is 0
– Instance Variable can be accessed only by creating objects.
13) What is the default value of a boolean variable in Java?
Default value of boolean variable is false.
14) What is the default value of an integer variable in Java?
Default value of integer variable is 0.
15) What is the default value of an object in Java?
Default value of an object is null.
16) What is final?
The final keyword makes a Java variable, method or class as final and it cannot be changed once it is initialized.
17) How to read data from the XML files?
File xmlFile = new File("/Users/mkyong/staff.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
18) What is JDBC and how it can be used to connect to Database?
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query with the database.
Connect to Oracle DB:
Class.forName(“oracle.jdbc.driver.OracleDriver”); DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection con = DriverManager.getConnection(url,user,password); Statement st = con.createStatement(); int m = st.executeUpdate(sql);
19) Class.forName(X). What will you write in place of X?
X represents the Driver class file for the Database.
20) If we write FileInputStream statements in try block, what are the statements we will write in finally block?
if (fileInputStream != null) { fileInputStream.close(); }
21) Explain about typecasting?
The process of converting the value of one data type (int, float, double, etc.) to another data type is known as typecasting.
Two types of typecasting:
– Widening or Automatic
Widening conversion takes place when two data types are automatically converted. This happens when:
The two data types are compatible.
When we assign value of a smaller data type to a bigger data type.
Byte -> Short -> Int -> Float -> Double
– Narrowing or Explicit
If we want to assign a value of larger data type to a smaller data type we perform explicit type casting or narrowing.
This is useful for incompatible data types where automatic conversion cannot be done.
Here, target-type specifies the desired type to convert the specified value to.
Double -> Float -> Long -> Int -> Short -> Byte
22) Explain stack and heap in Java for memory management?
Stack Memory in Java is used for static memory allocation and the execution of a thread. It contains primitive values that are specific to a method and references to objects that are in a heap, referred from the method.
Heap space in Java is used for dynamic memory allocation for Java objects and JRE classes at the runtime. New objects are always created in heap space and the references to this objects are stored in stack memory.
23) Explain SingletonDesign pattern in Java?
Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine.
24) How to remove Junk or special characters in a String?
We can use regular expression and replaceAll() method of java.lang.String class to remove all special characters from String.
25) What is Reflection and Singleton in Java?
Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine. Reflection can be caused to destroy singleton property of singleton class.
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 🙂
Connect to me on Linked In (Click here)
On a mission to contribute to the Software Testing Community in all possible ways.