Quiz 4 Flashcards
A method that is associated with an individual object is called ________.
A class instance.
An object is an instance of a ________.
class
Analyze the following code.
class Test { public static void main(String[ ] args) { String s; System.out.println("s is " + s); } }
The program has a compilation error because s is not initialized, but it is referenced in the println statement.
Analyze the following code.
public class Test { int x;
public Test(String t) { System.out.println("Test"); }
public static void main(String[ ] args) { Test test = new Test(); System.out.println(test.x); } }
The program has a compile error because Test does not have a default constructor.
Given the declaration Circle x = new Circle(), which of the following statement is most accurate?
x contains a reference to a Circle object.
Given the declaration Circle[ ] x = new Circle[10], which of the following statement is most accurate?
x contains a reference to an array and each element in the array can hold a reference to a Circle object.
Suppose s is a string with the value “java”. What will be assigned to x if you execute the following code?
char x = s.charAt(4);
Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.
The keyword ________ is required to declare a class.
class
What is displayed by the following code?
public static void main(String[ ] args) throws Exception {
String[ ] tokens = “Welcome to Java”.split(“o”);
for (int i = 0; i
Welc me t Java
What is the output of the following code?
String s = “University”;
s.replace(“i”, “ABC”);
System.out.println(s);
University
public class Test { public static void main(String[ ] args) { String s1 = "Welcome to Java!"; String s2 = "Welcome to Java!";
if (s1 == s2) System.out.println("s1 and s2 reference to the same String object"); else System.out.println("s1 and s2 reference to different String objects"); } }
s1 and s2 reference to the same String object
What is the output of the following code?
public class Test { public static void main(String[ ] args) { String s1 = new String("Welcome to Java!"); String s2 = new String("Welcome to Java!");
if (s1 == s2) System.out.println("s1 and s2 reference to the same String object"); else System.out.println("s1 and s2 reference to different String objects"); } }
s1 and s2 reference to different String objects
What is the output of the following code?
public class Test { public static void main(String[ ] args) { String s1 = new String("Welcome to Java!"); String s2 = new String("Welcome to Java!");
if (s1.equals(s2)) System.out.println("s1 and s2 have the same contents"); else System.out.println("s1 and s2 have different contents"); } }
s1 and s2 have the same contents
What is wrong in the following code?
class TempClass { int i; public void TempClass(int j) { int i = j; } }
public class C { public static void main(String[ ] args) { TempClass temp = new TempClass(2); } }
The program has a compilation error because TempClass does not have a constructor with an int argument.
When invoking a method with an object argument, ________ is passed.
the reference of the object