• Slider Banner

    The Programming World

    Learn about top programming languages, tools and frameworks.

    Read more
  • Slider Banner

    Certification MCQ

    Practice for certification from 1000+ multiple choice questions.

    Read more
  • Slider Banner

    Prepare for Technical Interview

    Over 1 Million+ interview questions for different technical profiles.

    Read more

In Java, Find the closest number to 0 in a given integer Array.

Check below code, output will be: Nearest to 0: -1

package testjava.arrays;

import java.util.Arrays;

public class ClosestZeo 
{
	public static void main(String args[])
	{
		int[] data = {-1, -5, 3, 2, 7, 4, -3, -2, -10, 2};
		
		int current = 0;
		int near = data[0];
		
		Arrays.sort(data);
		
		for(int i = 0; i < data.length; i++)
		{
			current = data[i] * data[i];
			
			if(current <= (near * near))
			{
				near = data[i];
			}
		}
		System.out.println("Nearest to 0: "+near);
	}
}

 

What will be the output of below code when b = 2 or b = 0.

Here the confusion is finally always execute and method call return; once return statement is used.

package testjava.exceptionhandling;

public class MultipleReturn 
{
	public static void main(String args[])
	{
		int a = 10, b = 0;
		
		System.out.println(multipleReturnTest(10, b));
	}
	
	
	public static int multipleReturnTest(int num1, int num2)
	{
		try
		{
			num1 = num1 / num2;
			return num1;
		}
		catch(Exception e)
		{
			num1 = 7;
			return num1;
		}
		finally
		{
			return num1;
		}
	}	
}

Output

How to handle keyboard and mouse actions using Selenium?

We can handle special keyboard and mouse events by using Advanced User Interactions API. The Advanced User Interactions API contains the Actions and the Action Classes that are needed for executing these events. Most commonly used keyboard and mouse events provided by the Actions class are mentioned below:

What is the use of JavaScriptExecutor in Selenium ?

JavaScriptExecutor is an interface which provides a mechanism to execute Javascript through the Selenium WebDriver. It provides "executescript" and "executeAsyncScript" methods, to run JavaScript in the context of the currently selected frame or window. An example of that is:

JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript(Script,Arguments);

 

Write a Selenium code to wait for an alert to appear.

We can write a code such that we specify the XPath of the web element that needs to be visible on the page and then ask the WebDriver to wait for a specified time. Look at the sample piece of code below:

WebDriverWait wait=new WebDriverWait(driver, 20);
Element = wait.until(ExpectedConditions.alertIsPresent());

 

Subscribe to