D9 - M

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

A source code file may contain several classes. a. true b. false

a

A static data field can be accessed from any method in the same class. a. True b. False

a

A static method in a class can access the class variables in the same class. a. true b. false

a

All data fields in an object have default values. a. true b. false

a

An immutable class cannot have _______. a. public data fields b. private data fields c. public constructors d. no-arg constructors e. static data fields

a

Analyze the following code: 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 after System.out.println("Default constructor"). d. this(1) must be replaced by this(1.0).

a

Analyze the following code: public class Test { public static void main(String args[]) { NClass nc = new NClass(); nc.t = nc.t++; } } class NClass { int t; private NClass() { } } a. The program has a compilation error because the NClass class has a private constructor. b. The program does not compile because the parameter list of the main method is wrong. c. The program compiles, but has a runtime error because t has no initial value. d. The program compiles and runs fine.

a

Analyze the following code: public class Test { public static void main(String[] args) { double radius; final double PI= 3.15169; double area = radius * radius * PI; System.out.println("Area is " + area); } } a. The program has compile errors because the variable radius is not initialized. b. The program has a compile error because a constant PI is defined inside a method. c. The program has no compile errors but will get a runtime error because radius is not initialized. d. The program compiles and runs fine.

a

Array variable are reference variables. a. true b. false

a

Each class in the file is compiled into a separate bytecode file. a. true b. false

a

Every instance data field f in the class can be referenced using this.f in an instance method the same class. a. True b. False

a

If the parameter is an object, both formal parameter and actual parameter reference to the same object. a. true b. false

a

Java assigns a default value to a data member of a class if the data is not initialized. a. true b. false

a

Java uses _______ to reference the current object. a. this b. that c. thisObject d. null

a

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

The default constructor has no arguments. a. true b. false

a

The default value null is assigned to a data member of object type, even though the data member is not created yet. a. true b. false

a

The internal state of an immutable class cannot be changed. String is an immutable class. a. true b. false

a

The order of methods in a class is immaterial. a. true b. false

a

What code may be filled in the blank without causing syntax or runtime errors: public class Test { java.util.Date date; public static void main(String[] args) { Test test = new Test(); System.out.println(_________________); } } a. test.date b. date c. test.date.toString() d. date.toString()

a

What is the printout for the third statement in the main method? public class Foo { static int i = 0; static int j = 0; public static void main(String[] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } } a. j is 0 b. j is 1 c. j is 2 d. j is 3

a

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 Key: c What is the value of myCount.count displayed? public class Test { public static void main(String[] args) { Count myCount = new Count(); int times = 0; for (int i=0; i<100; i++) increment(myCount, times); System.out.println( "myCount.count = " + myCount.count); System.out.println("times = "+ times); } public static void increment(Count c, int times) { c.count++; times++; } } class Count { int count; Count(int c) { count = c; } Count() { count = 1; } } a. 101 b. 100 c. 99 d. 98

a

Which of the following are properties of a constructor? a. A constructor must have the same name as the class. b. A constructor is called using the new operator. c. Constructors may be overloaded.

a

Which of the following can be placed in the blank line in the following code? public class Test { private int id; public void m1() { _____.id = 45; } } a. this b. Test

a

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 one arg constructor.

a

You can access a class variable using a syntax like objectName.classVariable or ClassName.classVariable. a. true b. false

a

You can declare a local variable in a method that has same name as an instance variable in the class. a. true b. false

a

You can declare variables of the same name in a method if they are in non-nesting blocks. a. true b. false

a

You cannot use modifiers on local variables inside a method except final. a. true b. false

a

You cannot use the private modifier on classes. a. true b. false

a

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; } 7 public ? int getAge() { } } a. in line 4 b. in line 8 c. in both line 4 and line 8 d. none

a

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

a

You use the plus sign (+) to denote public data or methods. a. true b. false

a

You use underline to denote static variables and methods. a. true b. false

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

________ 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 can access ___________. a. A local variable defined in any method b. A private instance variable c. A static variable

b

A static method in a class can access the instance variables in the same class. a. true b. false

b

All local variables in a method have default values. a. true b. false

b

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

b

Analyze the following code. public class Test { public static void main(String[] args) { int n = 2; xMethod(n); System.out.println("n is " + n); } 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. e. The code prints n is 3.

b

Analyze the following code: 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

Analyze the following code: public class Test { public static void main(String[] args) { A a = new A(); a.print(); } } class A { String s; A(String s) { this.s = s; } void print() { System.out.println(s); } } a. The program has a compilation error because class A is not 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

Assume java.util.Date[] dates = new java.util.Date[10], which of the following statements are true? a. dates is null. b. dates[0] is null. c. dates = new java.util.Date[0] is fine, which assigns a new array to dates. d. dates = new Date() is fine, which creates a new Date object and assigns to dates.

b

Every instance data field f in the class can be referenced using this.f in a static method the same class. a. True b. False

b

If the parameter is of a primitive type, both formal parameter and actual parameter reference to the same memory. a. true b. false

b

Java assigns a default value to a local variable in a method if the variable is not initialized. a. true b. false

b

Suppose you declare Date d. d is now called _______. a. an object b. a reference variable for an object c. an object value d. a variable that holds an integer value

b

The default value for a data member of boolean type is true. a. true b. false

b

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 e. false, 1, null

b

What is the output of the following program? import java.util.Date; public class Test { public static void main(String[] args) { Date date = new Date(1234567); m1(date); System.out.print(date.getTime() + " "); m2(date); System.out.println(date.getTime()); } public static void m1(Date date) { date = new Date(7654321); } public static void m2(Date date) { date.setTime(7654321); } } a. 1234567 1234567 b. 1234567 7654321 c. 7654321 1234567 d. 7654321 7654321

b

What is the printout for the first statement in the main method? public class Test { private int i = 0; static int j = 0; public static void main(String[] args) { new Test(); } public Test() { i++; j++; int i = 1; int j = 1; System.out.println("i is " + j + " j is " + j); } } a. i is 0 j is 0 b. i is 1 j is 1 c. i is 2 j is 2 d. i is 0 j is 1 E. i is 1 j is 0

b

What is the printout of the second println statement in the main method? public class Foo { int i; static int s; public static void main(String[] args) { Foo f1 = new Foo(); System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s); Foo f2 = new Foo(); System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s); Foo f3 = new Foo(); System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s); } public Foo() { i++; s++; } } a. f2.i is 1 f2.s is 1 b. f2.i is 1 f2.s is 2 c. f2.i is 2 f2.s is 2 d. f2.i is 2 f2.s is 1

b

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

Which of the following statement is most accurate? a. A reference variable is an object. b. A reference variable refers to an object. c. An object may contain other objects. d. An object may not contain the references of other objects.

b

You can always use the default constructor even though the non-default constructors are defined in the class. a. true b. false

b

You can create an instance of the Math class. a. true b. false

b

You can declare variables of the same name in a method even though they are in the same block. a. true b. false

b

__________ 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

___________ can be accessed from any instance method in the class. a. A local variable b. An instance variable c. A method variable

b

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

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); } } a. Since x is private, it cannot be accessed from an object foo. b. 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. c. 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. d. You cannot create a self-referenced object; that is, foo is created inside the class Foo.

c

Analyze the following code: public class Test { private int t; public static void main(String[] args) { 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 main method. c. t is non-static and it cannot be referenced in a static context in the main method. d. The variable x is not initialized and therefore causes errors. e. The program compiles and runs fine.

c

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

Given the declaration Circle[] x = new Circle[10], which of the following statement is most accurate? a. x contains an array of ten int values. b. x contains an array of ten objects of the Circle type. c. x contains a reference to an array and each element in the array can hold a reference to a Circle object. d. x contains a reference to an array and each element in the array can hold a Circle object.

c

Suppose TestCircle1 and Circle1 in Listing 9.1 are in two separate files named TestCircle1.java and Circle1.java, respectively. What is the outcome of compiling TestCircle.java and then Circle.java? a. Only TestCircle1.java compiles. b. Only Circle1.java compiles. c. Both compile fine. d. Neither compiles successfully.

c

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

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

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

c

To prevent a class from being instantiated, _____________________ a. don't use any modifiers on the constructor. b. use the public modifier on the constructor. c. use the private modifier on the constructor. d. use the static modifier on the constructor.

c

What is the printout for the second statement in the main method? public class Foo { static int i = 0; static int j = 0; public static void main(String[] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } } a. k is 0 b. k is 1 c. k is 2 d. k is 3

c

What is the printout of the third println statement in the main method? public class Foo { int i; static int s; public static void main(String[] args) { Foo f1 = new Foo(); System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s); Foo f2 = new Foo(); System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s); Foo f3 = new Foo(); System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s); } public Foo() { i++; s++; } } a. f3.i is 1 f3.s is 1 b. f3.i is 1 f3.s is 2 c. f3.i is 1 f3.s is 3 d. f3.i is 3 f3.s is 1 e. f3.i is 3 f3.s is 3

c

Which is the advantage of encapsulation? a. Only public methods are needed. b. Making the class final causes no consequential changes to other code. c. It changes the implementation without changing a class's contract and causes no consequential changes to other code. d. It changes a class's contract without changing the implementation and causes no consequential changes to other code.

c

___________ can be accessed from any static method in the class. a. A local variable b. An instance variable c. A static variable

c

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 System.out.println method cannot be invoked from the constructor. b. The program has a compile error because x has not been initialized. c. The program has a compile error because you cannot create an object from the class that defines the object. d. The program has a compile error because Test does not have a default constructor.

d

How many JFrame objects can you create and how many can you display? a. one b. two c. three d. unlimited

d

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

d

What is the printout for the first statement in the main method? public class Foo { static int i = 0; static int j = 0; public static void main(String[] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } } a. i + j is 5 b. i + j is 6 c. i + j is 22 d. i + j is 23

d

Which of the following code in A or B, or both creates an object of the Date class: A: public class Test { public Test() { new java.util.Date(); } } B: public class Test { public Test() { java.util.Date date = new java.util.Date(); } } a. A. b. B. c. Neither d. Both

d

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 helps prevent programming errors. d. All the above.

d

You can declare two variables with the same name in __________. a. a method one as a formal parameter and the other as a local variable b. a block c. two nested blocks in a method (two nested blocks means one being inside the other) d. different methods in a class

d

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 = null; System.out.println(test.x); } } a. The program has a compile error because test is not initialized. b. The program has a compile error because x has not been initialized. c. The program has a compile error because you cannot create an object from the class that defines the object. d. The program has a compile error because Test does not have a default constructor. e. The program has a runtime NullPointerException because test is null while executing test.x.

e

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 double MAX_LENGTH = 99.98; e. final static double MAX_LENGTH = 99.98;

e

What is the value of times displayed? public class Test { public static void main(String[] args) { Count myCount = new Count(); int times = 0; for (int i=0; i<100; i++) increment(myCount, times); System.out.println( "myCount.count = " + myCount.count); System.out.println("times = "+ times); } public static void increment(Count c, int times) { c.count++; times++; } } class Count { int count; Count(int c) { count = c; } Count() { count = 1; } } a. 101 b. 100 c. 99 d. 98 e. 0

e

Which of the following statements are correct? a. When creating a Random object, you have to specify the seed or use the default seed. b. If two Random objects have the same seed, the sequence of the random numbers obtained from these two objects are identical. c. The nextInt() method in the Random class returns the next random int value. d. The nextDouble() method in the Random class returns the next random double value. e. All the above.

e

Which of the following statements are true about an immutable object? a. The contents of an immutable object cannot be modified. b. All properties of an immutable object must be private. c. An immutable object contains no mutator methods. d. An object type property in an immutable object must also be immutable. e. All the above

e

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. e. All the above

e

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. e. All the above

e


Conjuntos de estudio relacionados

Module 12 - US Parties & Interest Groups

View Set

Organizational Behavior Exam #2 Ch. 8-13 Quizzes

View Set

ACCT 302 Conceptual MC Earnings Per Share

View Set

Técnicas de investigación 3er examen

View Set

Intro to Mass Communication Quiz #1

View Set