Strings are immutable, meaning they donβt change. Any method that that changes a string returns a new string. So s1 never changes unless you set it to a different string.
he
The substring method returns a new string starting at the first index and ending before the second index.
H
This would be true if we asked what the value of s2 was after the code executes. What is the value of s1?
h
This would be true if we asked what the value of s3 was after the code executes. What is the value of s1?
public class BankAccount
{
private int accountID;
private double total;
public BankAccount(int id, double initialDeposit)
{
accountID = id;
total = initialDeposit;
}
public void deposit(double money)
{
total = total + money;
}
public void withdraw(double money)
{
total = total - money;
}
public void printCurrentTotal()
{
System.out.print(total);
}
public static void main(String[] args)
{
BankAccount newAccount = new BankAccount(12345, 100.00);
newAccount.withdraw(30.00);
newAccount.deposit(40.00);
newAccount.printCurrentTotal();
}
}
100.00
Remember that we have added and withdrawn money
110.00
The constructor sets the total to 100, the withdraw method subtracts 30, and then the deposit method adds 40.
Given the following code segment, what is the value of num when it finishes executing? Math.random() returns a random decimal number between 0 and up to 1, for example 0.4.
double value = Math.random();
int num = (int) (value * 5) + 5;
a random number from 0 to 4
This would be true if it was (int) (Math.random * 5)
a random number from 1 to 5
This would be true if it was ((int) (Math.random * 5)) + 1
a random number from 5 to 9
Math.random returns a value from 0 to not quite 1. When you multiply it by 5 you get a value from 0 to not quite 5. When you cast to int you get a value from 0 to 4. Adding 5 gives a value from 5 to 9.
a random number from 5 to 10
This would be true if Math.random returned a value between 0 and 1, but it wonβt ever return 1. The cast to int results in a number from 0 to 4. Adding 5 gives a value from 5 to 9.
Given the following code segment, what is the value of num when it finishes executing? Math.random() returns a random decimal number between 0 and up to 1, for example 0.4.
double value = Math.random();
int num = (int) (value * 11) - 5;
a random number from 0 to 10
This would be true if it was (int) (value * 11)
a random number from 0 to 9
This would be true if it was (int) (value * 10)
a random number from -5 to 4
This would be true if it was (int) (value * 10) - 5
a random number from -5 to 5
Math.random returns a random value from 0 to not quite 1. After it is multipied by 11 and cast to integer it will be a value from 0 to 10. Subtracting 5 means it will range from -5 to 5.
String s1 = "xyz";
String s2 = s1;
String s3 = s2;
I. s1.equals(s3)
II. s1 == s2
III. s1 == s3
I, II, III
The "equals" operation on strings returns true when the strings have the same characters. The == operator returns true when they refer to the same object. In this case all three references actually refer to the same object so both == and equals will be true.
I only
This is true, since s1 and s3 contain the same characters since s1 and s3 actually refer to the same string object. But, it isnβt the only thing that is true.
II only
This is true since s2 == s1. But, it isnβt the only thing that is true.
III only
This is true since s3 == s2, and s2 == s1 so it follows that s1 == s3. But, it isnβt the only thing that is true.
II and III only
This is true since they all refer to the same string object. But, they also contain the same characters so equals is also true.
The method substring(a,b) means start at a and stop before b. The method substring(a) means start at a and go to the end of the string. The first character in a string is at index 0.
eor
This canβt be true since the e is at index 1 and s2 = s1.substring(2) will start at index 2 and take all characters till the end of the string.
eorg
This canβt be true since the e is at index 1 and s2 = s1.substring(2) will start at index 2 and take all characters till the end of the string.
orgi
This would be true if substring(a,b) included the character at index b, but it doesnβt.
You will get an index out of bounds exception
This would be true if the starting index was invalid or the ending index was past 2 past the last valid index.
This would be true if we had s1 = s4 after s4 = null was executed. Strings are immutable and so any changes to a string returns a new string.
hi there
This would only be correct if we had s1 = s2 after s2.toLowerCaase() was executed. Strings are immutable and so any change to a string returns a new string.
HI THERE
This would be correct if we had s1 = s3 after s3.toUpperCase() was executed. String are immutable and so any change to a string returns a new string.
Hi There
Strings are immutable meaning that any changes to a string creates and returns a new string, so the string referred to by s1 does not change.
hI tHERE
Strings are immutable and so any changes to a string returns a new string.
There is a method called checkString that determines whether a string is the same forwards and backwards. The following data set inputs can be used for testing the method. What advantage does Data Set 2 have over Data Set 1?
A car dealership needs a program to store information about the cars for sale.For each car, they want to keep track of the following information: the number of doors (2 or 4),its average number of miles per gallon, and whether the car has air conditioning. Which of the following is the best design?
String s1 = new String("Hi There");
String s2 = new String("Hi There");
String s3 = s1;
I. (s1 == s2)
II. (s1.equals(s2))
III. (s1 == s3)
IV. (s2.equals(s3))
II and IV
III is also correct.
II, III, and IV
String overrides equals to check if the two string objects have the same characters. The == operator checks if two object references refer to the same object. So II is correct since s1 and s2 have the same characters. Number II is correct since s3 and s1 are referencing the same string, so they will be ==. And s2 and s3 both refer to string that have the same characters so equals will be true in IV. The only one that will not be true is I, since s1 and s2 are two different objects (even though they have the same characters).
I, II, III, IV
I is not correct since s1 and s2 are two different objects (even though they have the same characters). If s1 and s2 were both referring to literals, then I would be correct, but the new operator forces a new object to be created.
This would be correct if it was System.out.println(13 + 5 + 3), but the 13 is a string.
1353
This is string concatenation. When you append a number to a string it get turned into a string and processing is from left to right.
It will give a run-time error
You can append a number to a string in Java. It turns the number into a string and then appends the second string to the first string.
138
This would be correct if it was System.out.println("13" + (5 + 3)), but the 5 is turned into a string and appended to the 13 and then the same is done with the 3.
It will give a compile-time error
You can append a number to a string in Java. It will compile.
class SomeClass
{
int someVar;
}
public class MainClass
{
public static void main(String[] args)
{
SomeClass x = new SomeClass();
System.out.println(x.someVar);
}
}
unknown value
ints get initialized to 0 by default if not explicitly initialized.
0
ints get initialized to 0 by default if not explicitly initialized.