Classes and Objects

¡Supera tus tareas y exámenes ahora con Quizwiz!

Which of the following statements are true? a. Local variables do not have default values. b. Data fields have default values. c. A variable of a primitive type holds a value of the primitive type. d. A variable of a reference type holds a reference to where an object is stored in the memory.

All of the above

Which of the following statements are true? a. Multiple constructors can be defined in a class. b. Constructors do not have a return type, not even void. c. Constructors must have the same name as the class itself. d. Constructors are invoked using the new operator when an object is created.

All of the above

25. You use the ___ operator to access members of an object. a. . b. () c. * d. %

a. .

____ is a construct that defines objects of the same type. a. A class b. An object c. A method d. A data field

a. A class

____ is invoked to create an object. a. A constructor b. The main method c. A method with a return type d. A method with the void return type

a. A constructor

Which of the following statements are true? a. A default constructor is provided automatically if no constructors are explicitly declared in the class. b. At least one constructor must always be defined explicitly. c. Every class has a default constructor. d. The default constructor is a no-arg constructor.

a. A default constructor is provided automatically if no constructors are explicitly declared in the class. d. The default constructor is a no-arg constructor.

Which of the following statements are true? a. Use the private modifier to encapsulate data fields. b. Encapsulating data fields makes the program easy to maintain. c. Encapsulating data fields makes the program short. d. Encapsulating data fields helps prevent programming errors.

a. Use the private modifier to encapsulate data fields. b. Encapsulating data fields makes the program easy to maintain. d. Encapsulating data fields helps prevent programming errors.

Suppose the xMethod() is invoked from a main method in a class as follows, xMethod() is ____ in the class. public static void main(String[] args) { xMethod(); } a. a static method b. an instance method c. a static method or an instance method

a. a static method

Analyze the following code: public class Test { private 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"); } } a. this.t() may be replaced by t(). b. this.i may be replaced by i. c. this(1) must be called before System.out.println("Default constructor"). d. this(1) must be replaced by this(1.0).

a. this.t() may be replaced by t(). c. this(1) must be called before System.out.println("Default constructor").

____ represents an entity in the real world that can be distinctly identified. a. A class b. An object c. A method d. A data field

b. An object

Analyze the following code. public class Test { public static void main(String[] args) { int n = 2; xMethod(n); System.out.println("n is " + n); } public void xMethod(int n) { n++; } } a. The code has a compile error because xMethod does not return a value. b. The code has a compile error because xMethod is not declared static. c. The code prints n is 1. d. The code prints n is 2.

b. The code has a compile error because xMethod is not declared static.

What is wrong in the following code? public class C { public static void main(String[] args) { TempClass temp = new TempClass(2); } } public class TempClass { private int i; public void TempClass(int j) { int i = j; } } a. The program has a compilation error because TempClass does not have a default constructor. b. The program has a compilation error because TempClass does not have a constructor with an int argument. c. The program compiles fine, but it does not run because class C is not public. d. The program compiles and runs fine.

b. The program has a compilation error because TempClass does not have a constructor with an int argument. Explanation: The program would be fine if the void keyword is removed from public void TempClass(int j).

Analyze the following code: public class Test { public static void main(String[] args) { A a = new A(); a.print(); } } public class A { private String s; A(String s) { this.s = s; } public void print() { System.out.println(s); } } a. The program has a compilation error because class A is a public class. b. The program has a compilation error because class A does not have a default constructor. c. The program compiles and runs fine and prints nothing. d. The program would compile and run if you change A a = new A() to A a = new A("5").

b. The program has a compilation error because class A does not have a default constructor. d. The program would compile and run if you change A a = new A() to A a = new A("5").

Analyze the following code: public class Circle { private double radius; public Circle(double radius) { radius = radius; } } a. The program has a compilation error because it does not have a main method. b. The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0. c. The program has a compilation error because you cannot assign radius to radius. d. The program does not compile because Circle does not have a default constructor.

b. The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0. Explanation: You have to replace radius = radius by this.radius = radius

An object is an instance of a ____. a. program b. class c. method d. data

b. class

The default value for data field of a boolean type, numeric type, object type is ____, respectively. a. true, 1, Null b. false, 0, null c. true, 0, null d. true, 1, null

b. false, 0, null

Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is ____ in the class. public MyClass() { xMethod(); } a. a static method b. an instance method c. a static method or an instance method

c. a static method or an instance method

A method that is associated with an individual object is called ____. a. a static method b. a class method c. an instance method d. an object method

c. an instance method

The keyword ____ is required to declare a class. a. public b. private c. class d. All of the above

c. class

Suppose you wish to provide an accessor method for a boolean property finished, what signature of the method should be? a. public void getFinished() b. public boolean getFinished() c. public boolean isFinished() d. public void isFinished()

c. public boolean isFinished()

Analyze the following code: public class Test { private int t; public static void change() { int x; System.out.println(t); } } a. The variable t is not initialized and therefore causes errors. b. The variable t is private and therefore cannot be accessed in the change method. c. t is non-static and it cannot be referenced in a static context in the change method. d. The variable x is not initialized and therefore causes errors.

c. t is non-static and it cannot be referenced in a static context in the change method.

When invoking a method with an object argument, ___ is passed. a. the contents of the object b. a copy of the object c. the reference of the object d. the object is copied, then the reference of the copied object

c. the reference of the object

Given the declaration Circle x = new Circle(), which of the following statement is most accurate. a. x contains an int value. b. x contains an object of the Circle type. c. x contains a reference to a Circle object. d. You can assign an int value to x.

c. x contains a reference to a Circle object.

Variables that are shared by every instances of a class are ____. a. public variables b. private variables c. instance variables d. class variables

d. class variables

To declare a constant MAX_LENGTH as a member of the class, you write: a. final static MAX_LENGTH = 99.98; b. final static float MAX_LENGTH = 99.98; c. static double MAX_LENGTH = 99.98; d. final static double MAX_LENGTH = 99.98;

d. final static double MAX_LENGTH = 99.98;


Conjuntos de estudio relacionados

11.1 - Apply Trigonometric Functions to solve problems involving distance and angles

View Set

Verbal Reasoning DOST-SEI Practice Test

View Set

Chapter 10 - Manipulating Variables

View Set

J.K. Rowling Speaks at Harvard Commencement

View Set

Bio 1407 Ch. 12. The forces of evolutionary change

View Set