String name = "Alice";
name = name.toUpperCase();
System.out.println("Hello " + name +"!");
What's the output of it when embedded correctly in a complete program? 2. Now consider this:
String name = "Alice";
/* name = */ name.toUpperCase();
System.out.println("Hello " + name +"!");
What's the output of it when embedded correctly in a complete program? Consider this:
3. What doesString vehicle = "snowmobile";
vehicle + "vehicle" evaluate to?
4. What does vehicle.length() + "vehicle".length() evaluate to?
5. Please simplify the following expression (in which x is an int
variable)
That is, write a simpler, equivalent expression.(x > 3) && (x < 5)
6-8. Same question for each of the following expressions
(x > 3) || (x > 5)
(x > 3) || (x > 4)
9. Consider this (and that(x > 3) && (x > 4)
x and y are ints):
if (x > 3) {
if (x < 5)
y = 1;
else
if (x != 6)
y = 2;
else
y = 3;
} else
y = 4;
If y is 2 at the end, what value did x have in the beginning? 10. Now consider this:
if (x > 3) {
if (x > 5)
y = 1;
else
if (x != 6)
y = 2;
} else
y = 4;
If x is 6, what value does y have at the end? 11. Now we eliminate the curly braces altogether:
if (x > 3)
if (x > 5)
y = 1;
else
if (x != 6)
y = 2;
else
y = 4;
If x is 6, what value does y have at the end? 12. What's the value of
13. What's the value of(1 + 2 + 3 + "4").length()
whereInteger.parseInt(a.substring(0, 1))
a is the string "345"?
14. What's the value of
wherea.charAt(0) - a.charAt(2)
a is the string "345"?
15. What's the value of
wherea.charAt(0) - '0'
a is the string "345"?
16. Consider this:
int x = 0;
while (x > 0) {
x += 1;
}
Is it an infinite loop?
17. Consider this:
int x = 0;
do {
x += 1;
} while (x > 0);
Is it an infinite loop?
18. What's the output of this program when compiled and run:
class One {
int n = 3;
One() {
this(7);
}
One(int n) {
this.n = n;
}
public static void main(String[] args) {
One a = new One(9);
One b = new One();
System.out.println(a.n + b.n);
}
}
19. Consider this:
public class Two {
public static void main(String[] args) {
System.out.print(fun(1) + " ");
}
public static int fun(int x) {
System.out.print(x + " ");
return x + 1;
}
}
What does it print?
20. Consider this:
public class Three {
public static void main(String[] args) {
int x = 2;
System.out.print(fun(x) + " ");
System.out.print(x + " ");
}
public static int fun(int x) {
x = x - 1;
return x;
}
}
What does it print?
21. Consider this:
public class Five {
public static void main(String[] args) {
boolean yo = "yo".equals("yo");
System.out.println(yo(yo));
}
public static boolean yo (boolean yo) {
return yo && (! yo);
}
}
What does it print?
22. What does this program print?
class Six {
public static String mix(String s, String t) {
String ans = "";
int slen = s.length();
int tlen = t.length();
for (int i = 0; i < slen && i < tlen; i++) {
ans = t.charAt(i) + s.charAt(i) + ans;
}
return ans;
}
public static void main(String[] args) {
System.out.println(Six.mix("mix", "six"));
}
}
23. Now suppose we change one line in mix as follows:
class Six {
public static String mix(String s, String t) {
String ans = "";
int slen = s.length();
int tlen = t.length();
for (int i = 0; i < slen && i < tlen; i++) {
ans = ans + t.charAt(i) + s.charAt(i);
}
return ans;
}
public static void main(String[] args) {
System.out.println(Six.mix("mix", "six"));
}
}
Now, what does it print?
24. And suppose we change that line one more time:
class Six {
public static String mix(String s, String t) {
String ans = "";
int slen = s.length();
int tlen = t.length();
for (int i = 0; i < slen && i < tlen; i++) {
ans = t.charAt(i) + ans + s.charAt(i);
}
return ans;
}
public static void main(String[] args) {
System.out.println(Six.mix("mix", "six"));
}
}
Now, what does it print?
25. And we change it one more time:
class Six {
public static String mix(String s, String t) {
String ans = "";
int slen = s.length();
int tlen = t.length();
for (int i = 0; i < slen && i < tlen; i++) {
ans = s.charAt(i) + ans + t.charAt(i);
}
return ans;
}
public static void main(String[] args) {
System.out.println(Six.mix("mix", "six"));
}
}
26. What's the output of this program when compiled and run?
public class Seven {
public static void main(String[] args) {
System.out.println(fun(1, fun(fun(2, fun(3, 4)), 5)));
}
public static int fun(int a, int b) {
return b - a;
}
}
27. What does this evaluate to:
ifa[a[a[a[0]]]]
a is defined as follows: int[] a = {5, 4, 0, 1, 3, 2}; ?
28. Same question about a[a[a[a[2]]]].
29. Same question about a[1 + a[1 + a[1 + a[2]]]].
Consider the following definition:
int[][] a = { { 1, 2, 3, 4}, { 5, 6, 7, 8}, { 9, 1, 2, 3} };
30. What does System.out.println(a[0][1] + a[1][0]); produce?
31. What does System.out.println(a.length); produce?
32. What does System.out.println(a[0].length); produce?