WHAT IS OUTPUT BY THE FOLLOWING CODE?

Exercise on paper: (Total 8 points)
Ch. 3
What is output by the following code? (Choose all that apply)
1: public class Fish {
2: public static void main(String[] args) {
3: int numFish = 4;
4: String fishType = “tuna”;
5: String anotherFish = numFish + 1;
6: System.out.println(anotherFish + ” ” + fishType);
7: System.out.println(numFish + ” ” + 1);
8: } }
4 1
41
5
5 tuna
5tuna
51tuna
The code does not compile.

Which of the following are output by this code? (Choose all that apply)

3: String s = “Hello”;
4: String t = new String(s);
5: if (“Hello”.equals(s)) System.out.println(“one”);
6: if (t == s) System.out.println(“two”);
7: if (t.equals(s)) System.out.println(“three”);
8: if (“Hello” == s) System.out.println(“four”);
9: if (“Hello” == t) System.out.println(“five”);
one
two
three
four
five
The code does not compile.
Which are true statements? (Choose all that apply)

An immutable object can be modified.
An immutable object cannot be modified.
An immutable object can be garbage collected.
An immutable object cannot be garbage collected.
String is immutable.
StringBuffer is immutable.
StringBuilder is immutable.
What is the result of the following code?

7: StringBuilder sb = new StringBuilder();
8: sb.append(“aaa”).insert(1, “bb”).insert(4, “ccc”);
9: System.out.println(sb);
abbaaccc
abbaccca
bbaaaccc
bbaaccca
An exception is thrown.
The code does not compile.
What is the result of the following code?

2: String s1 = “java”;
3: StringBuilder s2 = new StringBuilder(“java”);
4: if (s1 == s2)
5: System.out.print(“1”);
6: if (s1.equals(s2))
7: System.out.print(“2”);
1
2
12
No output is printed.
An exception is thrown.
The code does not compile.
What is the result of the following code?

public class Lion {
public void roar(String roar1, StringBuilder roar2) {
roar1.concat(“!!!”);
roar2.append(“!!!”);
}
public static void main(String[] args) {
String roar1 = “roar”;
StringBuilder roar2 = new StringBuilder(“roar”);
new Lion().roar(roar1, roar2);
System.out.println(roar1 + “ ” + roar2);
} }
roar roar
roar roar!!!
roar!!! roar
roar!!! roar!!!
An exception is thrown.
The code does not compile.
Which are the results of the following code? (Choose all that apply)

String letters = “abcdef”;
System.out.println(letters.length());
System.out.println(letters.charAt(3));
System.out.println(letters.charAt(6));
5
6
c
d
An exception is thrown.
The code does not compile.
Which are the results of the following code? (Choose all that apply)

String numbers = “012345678”;
System.out.println(numbers.substring(1, 3));
System.out.println(numbers.substring(7, 7));
System.out.println(numbers.substring(7));
12
123
7
78
A blank line.
An exception is thrown.
The code does not compile.
What is the result of the following code?

3: String s = “purr”;
4: s.toUpperCase();
5: s.trim();
6: s.substring(1, 3);
7: s += ” two”;
8: System.out.println(s.length());
2
4
8
10
An exception is thrown.
The code does not compile.
What is the result of the following code? (Choose all that apply)

13: String a = “”;
14: a += 2;
15: a += ‘c’;
16: a += false;
17: if ( a == “2cfalse”) System.out.println(“==”);
18: if ( a.equals(“2cfalse”)) System.out.println(“equals”);
Compile error on line 14.
Compile error on line 15.
Compile error on line 16.
Compile error on another line.
==
equals
An exception is thrown.
What is the result of the following code?

4: int total = 0;
5: StringBuilder letters = new StringBuilder(“abcdefg”);
6: total += letters.substring(1, 2).length();
7: total += letters.substring(6, 6).length();
8: total += letters.substring(6, 5).length();
9: System.out.println(total);
1
2
3
7
An exception is thrown.
The code does not compile.

What is the result of the following code? (Choose all that apply)

StringBuilder numbers = new StringBuilder(“0123456789”);
numbers.delete(2, 8);
numbers.append(“-“).insert(2, “+”);
System.out.println(numbers);
01+89–
012+9–
012+–9
0123456789
An exception is thrown.
The code does not compile.
What is the result of the following code?

StringBuilder b = “rumble”;
b.append(4).deleteCharAt(3).delete(3, b.length() – 1);
System.out.println(b);
rum
rum4
rumb4
rumble4
An exception is thrown.
The code does not compile.
Which of these compile when replacing line 8? (Choose all that apply)

7: char[]c = new char[2];
8: // INSERT CODE HERE
int length = c.capacity;
int length = c.capacity();
int length = c.length;
int length = c.length();
int length = c.size;
int length = c.size();
None of the above.
Which of these compile when replacing line 8? (Choose all that apply)
7: ArrayList l = new ArrayList();
8: // INSERT CODE HERE
int length = l.capacity;
int length = l.capacity();
int length = l.length;
int length = l.length();
int length = l.size;
int length = l.size();
None of the above.
Ch. 4
Which of the following can fill in the blank in this code to make it compile? (Choose all that apply)

public class Ant {
_____ void method() { }
}
default
final
private
Public
String
zzz:
Which of the following compile? (Choose all that apply)
final static void method4() { }
public final int void method() { }
private void int method() { }
static final void method3() { }
void final method() {}
void public method() { }

Which of the following methods compile? (Choose all that apply)
public void methodA() { return;}
public void methodB() { return null;}
public void methodD() {}
public int methodD() { return 9;}
public int methodE() { return 9.0;}
public int methodF() { return;}
public int methodG() { return null;}

Is this the question you were looking for? If so, place your order here to get started!