1. If String str1 = \"Forest\" and String str2 = \"School\", then what is the va
ID: 3801380 • Letter: 1
Question
1. If String str1 = "Forest" and String str2 = "School", then what is the value of
str1.compareTo(str2);?
Question 1 options:
2. Assume that age has been declared as an int variable. Which expression is true
whenever age indicates that the person is a teenager?
Question 2 options:
3. !((x > y) || (y <= 0)) is equivalent to which of the following expressions?
I.!(x > y) || !(y <= 0)
II.!(x > y) &&! (y <= 0)
III. (x <= y) && (y > 0)
Question 3 options:
4. Assume that these String variables have been declared:
String str1 = new String("hello");
String str2 = new String("hello");
What is the value of the following expression?
str1.equals(str2)
Question 4 options:
5. When is the following expression true?
!(!a || b) || (!a && b)
Question 5 options:
6. What is output by the following program?
int i = 6;
while (i >= 2) {
System.out.print (i + " ");
if ((i % 2) == 0) {
i = i / 2;
} else {
i = i + 1;
}
}
Question 6 options:
7. Given the following code:
if (n == 2)
{
k -= 2;
}
else if (n == 3)
{
k -= 3;
}
can be rewritten as if (< condition >)
{
< assignment statement >;
}
where < condition > and < assignment statement > are chosen so that the rewritten code
performs the same task as the original code. Assume that both n and k are integer
variables.
Which of the following could be used as < condition >?
I. (n == 2) || (n == 3)
II. (n == 2) && (n == 3)
III. (n >= 2) && (n <= 3)
Question 7 options:
8. What is stored in str after the following code executes?
String str = "Computer Science";
int i = 0;
while (i < 8) {
if (str.indexOf("m") < i) {
str = str.substring(0, 2) + str;
}
i += 2;
}
Question 8 options:
9. Given the following code:
int i = 100;
int j = 10;
while (i > 0)
{
i = i / j;
j = 1 + j % 5;
}
What is the value of i after this code executes?
Question 9 options:
10. Consider the following code:
String[ ] words = {"cat", "dog", "lamb", "giraffe", "elephant"};
int i = 0, index = -1, temp;
while (i < words.length) {
temp = words[i].indexOf("a");
if (temp > index) {
index = temp;
}
i++;
}
What is the value of index after this code executes?
Question 10 options:
11. Consider the following code examples, where all variables are of type int.
Example 1
x = n;
y = x;
while (x > 0) {
y += 1;
x /= 2;
}
Example 2
x = n;
y = x;
if (x > 0) {
while (x > 1) {
y += 1;
x /= 2;
}
}
Assume that the two examples start with the same value for variable n. For which
value(s) of n do the two code examples compute the same value for variable y ?
I.Any value less than zero
II.The value zero
III.Any value greater than zero
Question 11 options:
12. Suppose the following array is declared:
int[ ] grades = {88, 92, 95, 83};
What is the value ofgrades[3]?
Question 12 options:
int[ ] grades = {88, 92, 95, 83};
and the following integer is declared:
int index = 10 - 2 * 4;
What is the value of grades[index]?
Question 13 options:
14. Suppose the following array is declared:
int[ ] grades = {88, 92, 95, 83};
What are the values in grades after the following code executes?
grades[0] = grades[1];
grades[1] = grades[0];
Question 14 options:
15. Suppose the following array is declared:
int[ ] grades = {88, 92, 95, 83};
What is the value of grades[grades[0] / 22]?
Question 15 options:
1) -1 2) a value less than 0 3) 0 4) 1 5) a value greater than 0Explanation / Answer
1. The answer is 2.a value less then 0, as str1 is less than str2.
Explanation:compareTo() returns the value 0 if the argument is a string lexicographically equal to this string, a value less than 0 if the argument is a string lexicographically greater than this string and a value greater than 0 if the argument is a string lexicographically less than this string.
2. The answer is 1. ((age < 20) && (age >= 13)).
Explanation: The teenage is considered between 13 to 19 , in other words the age that end with 'teen'. So we need to check the condition that says age has to be greater tha or equal to thirteen but at the same time, age has to be less than 20. So we expect both the conditions to be true. && opertaor solves the purpose of checking both the conditions to be true, hence the answer.
3. The answer is 4.I and III only.
4. The answer is 1. true as equals method returns true if the strings are equal;false otherwise.
5. The answer is 1. If and only if a and b have different values
Explanation: as !(!a||b) is (a && !b), thus the expression becomes (a && !b) || (!a && b)
6. The answer is 2. 6 3 4 2
Explanation: i is initialized to 6., it than check while (i >= 2), so 6>2, the condition is true so the control goes to System.out.print (i + " "); and value of i is printed whch is 6.
next statement is if ((i % 2) == 0), it checks whether 6 is divisible by 2, true.
so the statement i = i / 2; gets executed resulting into i=3.
Control goes back to while loop, condition is true, so it prints 3 .
3 is not divisible by 2, so the control goes to else i = i + 1; and the value of i become 4.
since 4 > 2, once again while loop gets executed, resulting in printing value of i i.e. 4
Since 4 is divisible by 2, the next value of 2/2 becomes 1.
1 is not greater than 2, while gets exited.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.