CS 2336 Ch. 8

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Both compile fine.

Suppose TestCircle1 and Circle1 in Listing 8.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 static method

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 static method or an instance method

Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class. public MyClass() { xMethod(); }

public boolean isFinished()

Suppose you wish to provide an accessor method for a boolean property finished, what signature of the method should be?

class variables

Variables that are shared by every instances of a class are __________.

test.date

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(_________________); } }

an instance method

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

A class

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

A constructor

________ is invoked to create an object.

An object

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

class

An object is an instance of a __________.

B. dates[0] is null. C. dates = new java.util.Date[5] is fine, which assigns a new array to dates.

Assume java.util.Date[] dates = new java.util.Date[10], which of the following statements are true?

x contains a reference to a Circle object.

Given the declaration Circle x = new Circle(), 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.

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

unlimited

How many JFrame objects can you create and how many can you display?

public class Foo { private int x; public static void main(String[] args) { Foo foo = new Foo(); System.out.println(foo.x); } }

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.

false, 0, null

The default value for data field of a boolean type, numeric type, object type is ___________, respectively.

class

The keyword __________ is required to declare a class.

final static double MAX_LENGTH = 99.98;

To declare a constant MAX_LENGTH as a member of the class, you write

use the private modifier on the constructor.

To prevent a class from being instantiated, _____________________

101

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

0

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

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

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

f2.i is 1 f2.s is 2

What will be displayed by 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++; } }

f3.i is 1 f3.s is 3

What will be displayed by 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++; } }

the reference of the object

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

It changes the implementation without changing a class's contract and causes no consequential changes to other code.

Which is the advantage of encapsulation?

A. B.

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 reference variable refers to an object. An object may contain the references of other objects.

Which of the following statement is most accurate?

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.

Which of the following statements are correct?

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

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.

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.

Which of the following statements are true?

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

Which of the following statements are true?

in line 4

You should add the static keyword in the place of ? in Line ________ in the following code: 1 public class Test { 2 private int age; 3 4 public ? int square(int n) { 5 return n * n; 6 } 7 8 public ? int getAge() { 9 } 10}

1234567 7654321

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

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

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 runtime NullPointerException because test is null while executing test.x.

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

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

public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } }

The program has a compilation error because the NClass class has a private constructor.

public class Test { public static void main(String args[]) { NClass nc = new NClass(); nc.t = nc.t++; } } class NClass { int t; private NClass() { } }

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

public class Test { public static void main(String[] args) { A a = new A(); a.print(); } } class A { String s; A(String newS) { s = newS; } void print() { System.out.println(s); } }

The program has compile errors because the variable radius is not initialized.

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

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

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++; } }


Kaugnay na mga set ng pag-aaral

Network Pro, TestOut, Chapter 1 - Networking Basics

View Set

Life Insurance Chapter 7 Qualified Plans

View Set

Ch 1 - World of Innovative Management

View Set

Customer Service 4.2 Think Critically and Check Point Questions

View Set

Monetary Policy (Chapter 15 quiz)

View Set