Which of the following options represents the output of the given code snippet?
public static int addsub(int a, boolean isSub)
{
return (isSub ? sub(a) : a + 1);
} public static int sub(int a)
{
return a - 1;
}
public static void main(String[] args)
{
System.out.println("Sub 5 = " + addsub(5, true) + ", Add 6 = " + addsub(6, false));
} correct answers Sub 5 = 4, Add 6 = 7
What is the output of the following Java program?
public class Test01
{
public static void main(String[] args)
{
for (int i = 0; i < 4; i++)
{
System.out.print(myFun(i) + " ");
}
}
public static int myFun(int perfect)
{
return ((perfect - 1) * (perfect - 1));
}
} correct answers 1 0 1 4
Given the following method, what method call will return true?
public static boolean isValid(String input)
{
boolean valid = true;
if (input.length() != 11)
{
valid = false;
}
else
{
if (input.charAt(3) != '-' || input.charAt(6) != '-')
{
valid = false;
}
, else
{ valid = Character.isDigit(input.charAt(0)) && Character.isDigit(input.charAt(1)) &&
Character.isDigit(input.charAt(2)) && Character.isDigit(input.charAt(4)) &&
Character.isDigit(input.charAt(5)) && Character.isDigit(input.charAt(7)) &&
Character.isDigit(input.charAt(8)) && Character.isDigit(input.charAt(9)) &&
Character.isDigit(input.charAt(10));
}
}
return valid;
} correct answers isValid("123-45-6789")
Which of the following options represents the output of the given code snippet?
public static int addsub(int a, boolean isSub)
{
if (isSub)
{
return sub(a);
}
else { return a + 1; }
}
public static int sub(int a)
{
return a - 1;
}
public static void main(String[] args)
{
System.out.println("Sub 5 = " + addsub(5, false) + ", Add 6 = " + addsub(6, true));
} correct answers Sub 5 = 6, Add 6 = 5
In the following code snippet, what is the scope of variable b?
public static void m1()
{
int i = 0; double b = 0;
}
public static void m2()
{
double a = b + 1;
}
public static void main(String[] args)
{
m1();
m2();
} correct answers It can be used only in m1.