1. Which of the following can fill in the blank in this code to make it compile?
public class Ant {
_______ void method() {}
}
Each correct answer represents a complete solution. Choose all that apply.
A) Final
Private
2. Which of the following methods compile?
Each correct answer represents a complete solution. Choose all that apply.
A) final static void rain() {}
static final void sleet() {}
3. Which of the following methods compile?
Each correct answer represents a complete solution. Choose all that apply.
A) public void january() { return; }
public void march() {}
public int april() { return 9;}
4. Which of the following methods compile?
Each correct answer represents a complete solution. Choose all that apply.
A) public void violin(int… nums) {}
public void viola(String values, int… nums) {}
public void oboe(String[] values, int[] nums) {}
5. Which of the following statements is correct?
A) You can use access modifiers to allow access to all methods and not any instance variables.
6. Given the following class definitions, which lines in the main() method generate a compiler error?
// Classroom.java
package my.school;
public class Classroom {
private int roomNumber;
protected static String teacherName;
static int globalKey = 54321;
public static int floor = 3;
Classroom(int r, String t) {
roomNumber = r;
teacherName = t; } }
// School.java
package my.city;
import my.school.*;
public class School {
public static void main(String[] args) {
System.out.println(Classroom.globalKey);
Classroom room = new Classroom(101, “Mrs. Anderson”); System.out.println(room.roomNumber);
System.out.println(Classroom.floor);
System.out.println(Classroom.teacherName); } }
Each correct answer represents a part of the solution. Choose all that apply.
A)
Line 5
Line 6
Line 7
Line 9
7. Which statements about the final modifier are correct?
Each correct answer represents a complete solution. Choose all that apply.
A) Instance and static variables can be marked final.
A primitive that is marked final cannot be modified.
8. Which of the following can fill in the blank and allow the code to compile?
final _______ song = 6;
Each correct answer represents a complete solution. Choose all that apply.
A) int
Integer
long
double
9. How many variables in the following method are effectively final? 10. public void feed() {
11. int monkey = 0;
12. if(monkey > 0) {
13. var giraffe = monkey++;
14. String name;
15. name = “geoffrey”;
16. }
17. String name = “milly”;
18. var food = 10;
19. while(monkey <= 10) {
20. food = 0;
21. }
22. name = null;
23. }
A) 2
10. Given the following method, which of the method calls returns 2?
public int juggle(boolean b, boolean… b2) {
return b2.length;
}
Each correct answer represents a complete solution. Choose all that apply.
A) juggle(true, true, true);
juggle(true, new boolean[2]);
11. What is the output of executing the Chimp program?
1. // Rope.java
2. package rope;
3. public class Rope {
4. public static int LENGTH = 5;
5. static {
6. LENGTH = 10;
7. }
8. public static void swing() {
9. System.out.print(“swing “);
10. } }
11.
12. // Chimp.java
13. import rope.*;
14. import static rope.Rope.*;
15. public class Chimp {
16. public static void main(String[] args) { 17. Rope.swing();
18. new Rope().swing();
19. System.out.println(LENGTH); 20. } }
A) swing swing 10
12. Which statements are true of the following code?
1. public class Rope {
2. public static void swing() {
3. System.out.print(“swing”);
4. }
5. public void climb() {
6. System.out.println(“climb”);
7. }
8. public static void play() {
9. swing();
10. climb();
11. }
12. public static void main(String[] args) {
13. Rope rope = new Rope();
14. rope.play();
15. Rope rope2 = null;
16. System.out.print(“-“);
17. rope2.play();
18. } }
Each correct answer represents a complete solution. Choose all that apply. A)
There is exactly one compiler error in the code.
If the line(s) with compiler errors are removed, the output is swing swing.
13. What is the output of the following code?
// RopeSwing.java
import rope.*;
import static rope.Rope.*;
public class RopeSwing {
private static Rope rope1 = new Rope();
private static Rope rope2 = new Rope();
{
System.out.println(rope1.length);
}
public static void main(String[] args) {
rope1.length = 2;
rope2.length = 8;
System.out.println(rope1.length);
}
}
// Rope.java
package rope;
public class Rope {
public static int length = 0;
}
A)
8
14. How many lines in the following code have compiler errors?
1. public class RopeSwing {
2. private static final String leftRope;
3. private static final String rightRope;
4. private static final String bench;
5. private static final String name = “name”;
6. static {
7. leftRope = “left”;
8. rightRope = “right”;
9. }
10. static {
11. name = “name”;
12. rightRope = “right”;
13. }
14. public static void main(String[] args) {
15. bench = “bench”;
16. }
17. }
A) 4
15. Which of the following can replace line 2 to make this code compile?
1. import java.util.*;
2. // INSERT CODE HERE
3. public class Imports {
4. public void method(ArrayList<String> list) {
5. sort(list);
6. }
7. }
A) import static java.util.Collections.*;
16. Which of the following will compile when independently inserted in the following code?
1. public class Order3 {
2. final String value1 = “red”;
3. static String value2 = “blue”;
4. String value3 = “yellow”;
5. {
6. // CODE SNIPPET 1
7. }
8. static {
9. // CODE SNIPPET 2
10. } }
Each correct answer represents a part of the solution. Choose all that apply.
A) Insert at line 6: value2 = “purple”;
Insert at line 6: value3 = “orange”;
Insert at line 9: value2 = “cyan”;
17. What is the result of the following program?
1. public class Squares {
2. public static long square(int x) {
3. var y = x * (long) x;
4. x = -1;
5. return y;
6. }
7. public static void main(String[] args) {
8. var value = 9;
9. var result = square(value);
10. System.out.println(value);
11. } }
A) 9
18. Which of the following are output(s) of the following code?
public class StringBuilders {
public static StringBuilder work(StringBuilder a,
StringBuilder b) {
a = new StringBuilder(“a”);
b.append(“b”);
return a;
}
public static void main(String[] args) {
var s1 = new StringBuilder(“s1”);
var s2 = new StringBuilder(“s2”);
var s3 = work(s1, s2);
System.out.println(“s1 = ” + s1);
System.out.println(“s2 = ” + s2);
System.out.println(“s3 = ” + s3);
}
}
Each correct answer represents a complete solution. Choose all that apply.
A)
s1 = s1
s2 = s2b
s3 = a
19. What is the result of the following statements?
1. public class Test {
2. public void print(byte x) {
3. System.out.print(“byte-“);
4. }
5. public void print(int x) {
6. System.out.print(“int-“);
7. }
8. public void print(float x) {
9. System.out.print(“float-“);
10. }
11. public void print(Object x) {
12. System.out.print(“Object-“);
13. }
14. public static void main(String[] args) {
15. Test t = new Test();
16. short s = 123;
17. t.print(s);
18. t.print(true);
19. t.print(6.789);
20. }
21. }
A) int-Object-Object
20. Which of the following are true about the following code? public class Run {
static void execute() {
System.out.print(“1-“);
}
static void execute(int num) {
System.out.print(“2-“);
}
static void execute(Integer num) {
System.out.print(“3-“);
}
static void execute(Object num) {
System.out.print(“4-“);
}
static void execute(int… nums) {
System.out.print(“5-“);
}
public static void main(String[] args) {
Run.execute(100);
Run.execute(100L);
}
}
Each correct answer represents a complete solution. Choose all that apply.
A) The code prints out 2-4-.
The code prints 3-4- if you remove the method static void execute(int num).
21. Which method signatures are valid overloads of the following method signature?
public void moo(int m, int… n)
Each correct answer represents a complete solution. Choose all that apply.
A) public int moo(char ch)
private void moo(int… x)
Other Links:
See other websites for quiz:
Check on QUIZLET