Java for Testers – Interview Questions and Answers Part-4
1) What is the use of static keyword in Main()?
Since main() method is the entry point of any Java application, hence making the main() method as static is mandatory for launching the Java application. Since the main method is static, Java virtual Machine can call it without creating any instance of a class which contains the main method.
2) Can a class without main() gets compilation successful?
The main method is only used when the Java Virtual Machine is executing your code. Code cannot be executed without a main method but it can still be compiled.
3) Can we create private access specifier inside interface?
We cannot declare class/interface with private or protected access specifiers.
4) Is there any way to deallocate memory in JAVA?
We should dereference the object, then memory will be deallocated automatically during garbage collection
5) Write a program for removing white spaces in a String?
class RemoveWhiteSpaces{ public static void main(String[] args){ String str = " Java Interview Questions "; str = str.replaceAll("\\s", ""); System.out.println(str); } }
6) What is Object class?
The Object class is the parent class of all the classes in java by default. Every class in Java is directly or indirectly derived from the Object class.
7) Write a Java program for pascle triangle?
class PascalTriangle { static void printPascal(int n) { for (int line = 0; line < n; line++) { for (int i = 0; i <= line; i++) System.out.print(pattern(line, i)+" "); System.out.println(); } } static int pattern(int n, int k) { int res = 1; if (k > n - k) k = n - k; for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } public static void main(String args[]) { int n = 7; printPascal(n); } }
8) What are the differences between interface and inheritance?
– Inheritance is an OOP concept to derive new classes from the existing classes. Interface is a mechanism in OOP to implement abstraction and multiple inheritance.
– Inheritance provides code reusability. Interfaces provide abstraction and multiple inheritance.
9) If we close the driver in try block then,FINALLY will execute or not?
Java finally block is always executed whether exception is handled or not.
10) What is difference between method overloading and constructor overloading with example?
If we want to have different ways of initializing an object using different number of parameters, then we must do constructor overloading and we do method overloading when we want different definitions of a method based on different parameters.
11) What is the difference between normal class and final class?
Final class cannot be inherited by any other class but normal class can be inherited by other classes
12) Adapter design in java?
Adapter design pattern is one of the structural design pattern and its used so that two unrelated interfaces can work together. The object that joins these unrelated interface is called an Adapter.
13) Write a Java program for sorting of numbers?
public class SortNumbers { public static void main(String[] args) { int[] arr = {13, 7, 6, 45, 21, 9, 101, 102}; Arrays.sort(arr); System.out.printf("Modified arr[] : %s", Arrays.toString(arr)); } }
14) Write a Java program for searching a letter in string?
public class SearchLetter { public static void main(String args[]) { String str = "Automation Questions" int index = str.indexOf('Q'); System.out.println("Index of the letter Q :: "+index); } }
15) Write a Java program for sorting an array?
public class SortArray { public static void main(String[] args) { int[] arr = {13, 7, 6, 45, 21, 9, 101, 102}; Arrays.sort(arr); System.out.printf("Modified arr[] : %s", Arrays.toString(arr)); } }
16) What is a good approach to throw an exception?
By using throws keyword.
public class SortArray { public static void main(String[] args) { int[] arr = {13, 7, 6, 45, 21, 9, 101, 102}; Arrays.sort(arr); System.out.printf("Modified arr[] : %s", Arrays.toString(arr)); } }
17) Find how many duplicate values in Array List?
public static Set<Integer> findDuplicates(int[] input) { Set<Integer> duplicates = new HashSet<Integer>(); for (int i = 0; i < input.length; i++) { if(duplicates.add(input[i])==false){ System.out.println("Found duplicate element in array") } } }
18) String [] str={“abc”,”efg”,”fgh”}; convert array to string?
Arrays.toString (str);
19) Explain the features of JAVA?
Most important features are:
– Simple
– Object-Oriented
– Portable
– Platform independent
– Secured
– Robust
– Architecture neutral
– Interpreted
– High Performance
– Multithreaded
– Distributed
– Dynamic
20) How do u say JAVA is platform independent?
Platform independent language means once compiled you can execute the program on any platform (OS). Java is platform independent. Because the Java compiler converts the source code to bytecode, which is Intermidiate Language. Bytecode can be executed on any platform (OS) using JVM( Java Virtual Machine).
21) Is JVM platform independent?
Yes. Java is platform independent.
22) What is the difference between a Class and Object?
– Object is an instance of a class. Class is a blueprint or template from which objects are created.
– Object is a real world identity. Class is a group of similar objects
– Object allocates memory when it is created. Class doesn’t allocated memory when it is created.
23) Write a Java program to check whether an year is leap year or not?
public class LeapYear { public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter a year"); int year = sc.nextInt(); System.out.println(isLeapYear(year)? "Leap Year": "Not a Leap Year"); } private static boolean isLeapYear(int num){ if(num%400 == 0) return true; if(num%100 == 0) return true; if(num%4 == 0) return true; return false; } }
24) Where to use Hashtable in real time?
Hashtable should be used in multithreading applications as it is thread safe and synchronized.
25) What is the difference between Volatile & Transient in Java?
– transient keyword is used along with instance variables to exclude them from serialization process
– if a field is transient its value will not be persisted. On the other hand volatile keyword can also be used in variables to indicate compiler and JVM that always read its value from main memory
– transient keyword can not be used along with static keyword but volatile can be used along with static.
– transient variables are initialized with default value during de-serialization and there assignment or restoration of value has to be handled by application code.
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.