Introduction:
Preparing for a Java interview at Hexaware? This guide covers essential Java concepts, coding challenges, and Selenium automation-related questions that are frequently asked. Whether you’re a beginner or an experienced developer, these questions will help you refresh key topics and boost your confidence. Each question includes a detailed answer and code snippets to enhance understanding.
Q1. Can you explain the difference between a traditional for loop and an enhanced for loop? In what scenarios would you prefer one over the other?
Answer:
Feature | For Loop | Enhanced For Loop |
Syntax | Uses an index or counter variable | Iterates directly over elements |
Use Case | Best when index manipulation is needed | Best for collections/arrays |
Flexibility | Can iterate in any order, modify elements | Iterates only in sequence |
Performance | Slightly faster for indexed access | More readable and concise |
Modification | Allows modifying the collection | Cannot modify collection |
Example of a For Loop:
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
Example of an Enhanced For Loop:
for (int num : array) {
System.out.println(num);
}
The enhanced for loop is more readable but does not allow index manipulation.
Q2: Have you ever created a custom exception in Java? How does it work?
Answer: A user-defined exception is a custom exception class created by extending the Exception class in Java. It is useful when built-in exceptions do not meet specific requirements.
Example:
class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}public class Main {
public static void main(String[] args) {
try {
throw new CustomException(“This is a user-defined exception”);
} catch (CustomException e) {
System.out.println(e.getMessage());
}
}
}
This program defines a custom exception and demonstrates how to throw and catch it.
Boost your confidence with the Capgemini Interview Guide: Questions and Tips to Ace Interview.
Q3: Given the string John_Victor_Allen, how would you sort the names alphabetically?
Answer: Using split() method to separate strings into words and Arrays.sort() to sort them.
Code Implementation:
String names = “John_Victor_Allen”;
String[] nameArray = names.split(“_”);
Arrays.sort(nameArray);
for (String name : nameArray) {
System.out.println(name);
}
Output:
Q4: If a button label changes dynamically between ‘Submit’ and ‘Save’, how would you locate it in Selenium?
Answer: You can use XPath to identify the button by both text values.
Using Text Matching:
//button[text()=’Submit’ or text()=’Save’]
Using Contains for Partial Matching:
//button[contains(text(), ‘Submit’) or contains(text(), ‘Save’)]
Q5: Can you explain constructors in Java? How do constructor chaining and parameterized constructors work?
Answer:
- Constructor: A special method that initializes an object. It has the same name as the class and no return type.
- Constructor Chaining: Calling one constructor from another within the same class (this()) or parent class (super()).
- Parameterized Constructor: Accepts parameters to initialize an object with specific values.
Example:
class Person {
String name;
int age;// Default Constructor
public Person() {
this(“Unknown”, 0); // Calls parameterized constructor
}// Parameterized Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Q6: How do you delete all cookies in Selenium WebDriver? Why is it necessary?
Answer: Deleting all cookies ensures a fresh session during automation testing, preventing cached data from interfering with test results.
driver.manage().deleteAllCookies();
Deleting a Specific Cookie by Name:
driver.manage().deleteCookieNamed(“session_id”);
Creating and Adding a Cookie in Selenium:
Cookie cookie = new Cookie(“user”, “testUser”);
driver.manage().addCookie(cookie);
Fetching and Printing All Cookies Stored in a Session:
Set<Cookie> cookies = driver.manage().getCookies();
for (Cookie cookie : cookies) {
System.out.println(cookie.getName() + ” : ” + cookie.getValue());
}
Q7: Taking Screenshots in Selenium WebDriver
To take a screenshot, we use the getScreenshotAs() method from the TakesScreenshot interface.
Snippet:
File srcFile = driver.getScreenshotAs(OutputType.FILE);
File destFile = new File(“screenshot.png”);
FileUtils.copyFile(srcFile, destFile);
Q8: How do you extract a ZIP file in Java?
Answer: Use ZipInputStream to extract files.
public static void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdirs();
}
try (ZipInputStream zipIn = new ZipInputStream(Files.newInputStream(Paths.get(zipFilePath)))) {
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
File filePath = new File(destDirectory, entry.getName());
if (!entry.isDirectory()) {
try (FileOutputStream fos = new FileOutputStream(filePath)) {
byte[] buffer = new byte[1024];
int len;
while ((len = zipIn.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
} else {
filePath.mkdirs();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
}
}
Conclusion
This guide covers some of the most common Java interview questions asked at Hexaware, including looping structures, exceptions, file handling, and Selenium automation. Understanding these concepts and practicing code implementations will help you ace your interview.
Pro Tip: Always explain your logic during the interview and discuss potential edge cases.
Good luck!
Also Read: Infosys Interview Questions with Expert Answers
We Also Provide Training In:
- Advanced Selenium Training
- Playwright Training
- Gen AI Training
- AWS Training
- REST API Training
- Full Stack Training
- Appium Training
- DevOps Training
- JMeter Performance Training
Author’s Bio:
As CEO of TestLeaf, I’m dedicated to transforming software testing by empowering individuals with real-world skills and advanced technology. With 24+ years in software engineering, I lead our mission to shape local talent into global software professionals. Join us in redefining the future of test engineering and making a lasting impact in the tech world.
Babu Manickam
CEO – Testleaf