1. Suppose that the integer variables n and m have been initialized. Check all the correct statements about the following code fragment.
String s2 = (n>=m) ? "one" : ( (m<=2*n) ? "two": "three" );
Question 5 options:
If m is strictly greater than 2n then the value of s2 is "two" |
|
If n is equal to m then the value of s2 is "two" |
|
If m is strictly greater than 2n then the value of s2 is "three" |
|
If m is not zero and is equal to 2n, then the value of s2 is "two" |
2. Suppose that our Java program is in a package consisting of the classes Utilities, House, and Main, and the class Main contains the main method of the program. Suppose further that class House has a default constructor.
Check below all the correct statements.
Question 6 options:
If class House defines no constructors, then the main method can use the following statement House hers = new House(2500.30, 5000.50); |
|
If class House has the public accessor method getArea() that returns double, then it is legal to have the following statements in the main method House mine = new House(); |
|
If class Utilities has the public static String variable mainOffice then it is legal to have the following statement in some method of the class House System.out.printf("Main office: %s", Utilities.mainOffice); |
|
If class House has the private instance variable price of type double, then it is legal to have the following statements in the main method House mine = new House(); |
3.
A balanced brackets string (BBS) is one of the following:
1. the string {}
2. the string {b}, if b is a BBS
For example, {}, {{}} are valid BBSs, but }{, {}}, {3} are invalid
BBSs. The empty string is also an invalid BBS.
Select below the one method that correctly implements recursively
whether a given string s is a valid BBS.
Question 10 options:
public static boolean isBBS(String s) { |
|
public static boolean isBBS(String s) { |
|
public static boolean isBBS(String s) { |
|
public static boolean isBBS(String s) { |
1)
The correct answers are:
2)
The correct answers are:
If class Utilities has the public static String variable mainOffice then it is legal to have the following statement in some method of the class House System.out.printf("Main office: %s", Utilities.mainOffice); |
|
3)
The correct code is:
public static boolean isBBS(String s) {
int l = s.length();
if (l<2) return false;
if (s.equals("{}")) return true;
if (s.charAt(0)=='{' &&
s.charAt(l-1)=='}')
return isBBS(s.substring(1, l-1));
return false;
}
Get Answers For Free
Most questions answered within 1 hours.