Quiz 4 Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

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

A

A class instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

An object is an instance of a ________.

A

class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Analyze the following code.

class Test {  
     public static void main(String[ ] args) { 
          String s;
          System.out.println("s is " + s);
     }
}
A

The program has a compilation error because s is not initialized, but it is referenced in the println statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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);
     }
}
A

The program has a compile error because Test does not have a default constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

x contains a reference to a Circle object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Given the declaration Circle[ ] x = new Circle[10], which of the following statement is most accurate?

A

x contains a reference to an array and each element in the array can hold a reference to a Circle object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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);

A

Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The keyword ________ is required to declare a class.

A

class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

Welc me t Java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the output of the following code?

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

A

University

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
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");
 } }
A

s1 and s2 reference to the same String object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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");
 } }
A

s1 and s2 reference to different String objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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");
 } }
A

s1 and s2 have the same contents

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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);
     }
}
A

The program has a compilation error because TempClass does not have a constructor with an int argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

the reference of the object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

A reference variable refers to an object.

An object may contain the references of other objects.

17
Q

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

A

String s = “Welcome to Java”;

18
Q

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

A

A class

19
Q

________ is invoked to create an object.

A

A constructor

20
Q

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

A

An object