Quiz 4 Flashcards Preview

Computer Science 111 > Quiz 4 > Flashcards

Flashcards in Quiz 4 Deck (20)
Loading flashcards...
1

A method that is associated with an individual object is called ________.

A class instance.

2

An object is an instance of a ________.

class

3

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.

4

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.

5

Given the declaration Circle x = new Circle(), which of the following statement is most accurate?

x contains a reference to a Circle object.

6

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.

7

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.

8

The keyword ________ is required to declare a class.

class

9

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

10

What is the output of the following code?

String s = "University";
s.replace("i", "ABC");
System.out.println(s);

University

11

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

12

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

13

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

14

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.

15

When invoking a method with an object argument, ________ is passed.

the reference of the object

16

Which of the following statements is most accurate? (Choose two.)

A reference variable refers to an object.
An object may contain the references of other objects.

17

Which of the following statements is preferred to create a string "Welcome to Java"?

String s = "Welcome to Java";

18

________ is a construct that defines objects of the same type.

A class

19

________ is invoked to create an object.

A constructor

20

________ represents an entity in the real world that can be distinctly identified.

An object