Saddleback CS 4A Java Final Chapter 9 – Flashcards
Unlock all answers in this set
Unlock answersquestion
A constructor can access ________.
1. a local variable defined in any method
2. a private instance variable
3. a public instance variable
4. a static variable
answer
2. a private instance variable
3. a public instance variable
4. a static variable
question
A method that is associated with an individual object is called ________.
1. a static method
2. a class method
3. an instance method
4. an object method
answer
3. an instance method
question
T/F
A static data field can be accessed from any method in the same class.
answer
True
question
T/F
A static method in a class can access the class variables in the same class.
answer
True
question
T/F
A static method in a class can access the instance variables in the same class
answer
False
question
T/F
All data fields in an object have default values.
answer
True
question
An immutable class cannot have ________.
1. public data fields
2. private data fields
3. public constructors
4. no-arg constructors
5. static data fields
answer
1. public data fields
question
An object is an instance of a ________.
1. program
2. class
3. method
4. data
answer
2. class
question
Analyze the following code and choose the best answer:
public class Foo {
private int x;
public static void main(String[] args) {
Foo foo = new Foo();
System.out.println(foo x);
}
}
1. Since x is private, it cannot be accessed from an object foo.
2. Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code.
3. Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code.
4. You cannot create a self-referenced object; that is, foo is created inside the class Foo.
answer
3. Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code.
question
Analyze the following code:
class Test{
privat double i;
public Test(double i) {
this.t();
this.i = i;
}
public Test() {
System.out.println("Default constructor");
this(1);
}
public void t() {
System.out.println("Invoking t");
}
}
1. this.t() may be replaced by t().
2. this.i may be replaced by i.
3. this(1) must be called before System.out.println("Default constructor").
4. this(1) must be replaced by this(1.0).
answer
1. this.t() may be replaced by t().
3. this(1) must be called before System.out.println("Default constructor").
question
________ is invoked to create an object.
1. A constructor
2. The main method
3. A method with a return type
4. A method with the void return type
answer
1. A constructor
question
________ can be accessed from any instance method in the class.
1. A local variable
2. An instance variable
3. A static variable
answer
2. An instance variable
3. A static variable
question
________ can be accessed from any static method in the class.
1. A local variable
2. An instance variable
3. A static variable
answer
3. A static variable
question
You use the ________ operator to access members of an object.
.
()
*
%
answer
.
question
You should add the static keyword in the place of ? in Line ________ in the following code:
public class Test{
private int age;
public ? int square(int n) {
return n*n;
}
public ? int getAge() {
}
}
1. in line 4
2. in line 8
3. in both line 4 and line 8
4. none
answer
1. in line 4
question
T/F
You cannot use the private modifier on classes.
answer
False
question
You can declare two variables with the same name in ________.
1. a method one as a formal parameter and the other as a local variable
2. a block
3. two nested blocks in a method (two nested blocks means one being inside the other)
4. different methods in a class
answer
4. different methods in a class
question
Which of the following statements are true?
1. A default constructor is provided automatically if no constructors are explicitly declared in the class.
2.At least one constructor must always be defined explicitly.
3. Every class has a default constructor.
4. The default constructor is a no-arg constructor.
answer
1. A default constructor is provided automatically if no constructors are explicitly declared in the class.
4. The default constructor is a no-arg constructor.
question
Which is the advantage of encapsulation?
1. Only public methods are needed.
2. Making the class final causes no consequential changes to other code.
3. It changes the implementation without changing a class's contract and causes no consequential changes to other code.
4. It changes a class's contract without changing the implementation and causes no consequential changes to other code.
answer
3. It changes the implementation without changing a class's contract and causes no consequential changes to other code.
question
Suppose you declare Date d. d is now called ________.
1. an object
2. a reference variable for an object
3. an object value
4. a variable that holds an integer value
answer
2. a reference variable for an object
question
T/F
Each class in the file is compiled into a separate bytecode file.
answer
True
question
The pillars of object-oriented programming are:
answer
encapsulation
inheritance
polymorphism