Programming Exam 1 ch 8

Ace your homework & exams now with Quizwiz!

public class Secret {private int x;private static int y; public static int count; public int z; public Secret() {x = 0; z = 1; } public Secret(int a) {x = a; } public Secret(int a, int b) {x = a; y = b; } public String toString() { return ("x = " + x + ", y = " + y + ", } count = " + count); public static void incrementY() {y++; } } How many constructors are present in the class definition in the accompanying figure? a. 0 b. 1 c. 2 d. 3

3

What is the default definition of the method toString? a. There is no default definition; you must define the method yourself. b. It creates a string that is the name of the object's class, followed by the hash code of the object. c. It creates a string that is the name of the program. d. It creates a string that is the name of the package, followed by the name of the class, followed by the hash of the object.

It creates a string that is the name of the object's class, followed by the hash code of the object.

public class Secret {private int x;private static int y; public static int count; public int z; public Secret() {x = 0; z = 1; } public Secret(int a) {x = a; } public Secret(int a, int b) {x = a; y = b; } public String toString() { return ("x = " + x + ", y = " + y + ", } count = " + count); public static void incrementY() {y++; } } Based on the class in the accompanying figure, which of the following statements is illegal? a. Secret.incrementY(); b. Secret.count++; c. Secret.z++: d. Secret secret = new Secret(4);

Secret.z++:

Constructors have the same name as the ____. 1. data members 2. member methods 3. class 4. package

class

When writing data to a file, which method should be called to ensure that data is actually written to the file? a. write c. close b. exist d. print

close

In ____ copying, each reference variable refers to its own object. a. shallow b. deep c. method d. object

deep

Which of the following is a constructor without parameters? a. copy constructor b. default constructor c. finalizer d. modifier

default constructor

Which of the following statements creates an instance of FileReader for the filename "file.txt"? a. new FileReader(file.txt) c. FileReader("file.txt") b. FileReader(file.txt) d. newFileReader("file.txt")

newFileReader("file.txt")

Given String name = "myfile", which of the following can be used to create an object for writing to a file called "myfile"? a. new PrintWriter(myfile); b. newPrintWriter(name); c. PrintWriter(myfile); d. newPrintWriter("name");

newPrintWriter(name);

Which of the following class definitions is correct in Java? (i) public class Employee {private String name; private double salary; private int id; public Employee() {name = ""; REF: 468 REF: 471 c. pre-defined methods d. methods c. finalizer d. modifier salary = 0.0; id = 0; } public Employee(String n, double s, int i) {name = n; salary = s; id = i; } public void print(){ System.out.println(name+""+id+""+salary); (ii)} }public class Employee {private String name; private double salary; private int id; public void Employee() {name = ""; salary = 0.0; id = 0; } public void Employee(String n, double s, int i) {name = n; salary = s; id = i; } public void print() {System.out.println(name + " " + id + " " + salary); } } a. only i b. only ii c. both i and ii d. neither is correct

only i

Consider the following class definition. public class Rectangle {private double length; private double width; public Rectangle() {length = 0; width = 0; } public Rectangle(double l, double w) {length = l; width = w; } public void set(double l, double w) {length = l; width = w; } public void print() {System.out.println(length + " " + width); } public double area() { return length * width; } public double perimeter() { return 2 * length + 2 * width; } } Which of the following statements correctly instantiates the Rectangle object myRectangle? (i) myRectangle = new Rectangle(12.5, 6);(ii) Rectangle myRectangle = new Rectangle(12.5, 6);(iii) class Rectangle myRectangle = new Rectangle(12.5, 6); a. only i b. only ii c. only iii d. both ii and iii

only ii

Class members consist of all of the following EXCEPT ____. a. named constants b. variable declarations c. pre defined methods d. methods

pre defined methods

A constructor has no type and is therefore a void method.

False

An accessor (getter) method of a class first accesses the values of the data members of the class and then changes the values of the data members.

False

Constructors are called like any other method.

False

Given the declaration public class MyClass {private int x; public void print() {System.out.println("x = " + x); } private void setX(int y) {x = y; } } MyClass myObject = new MyClass(); The following statement is legal. myObject.setX(10);

False

If a member of a class is a private method, you can access it outside the class.

False

In Java, the reference this is used to refer to only the methods, not the instance variables of a class.

False

In shallow copying, each reference variable refers to its own object.

False

Members of a class are usually classified into three categories: public, private, and static.

False

The components of a class are called fields.

False

The methods of a class must be public.

False

When no object of the class type is instantiated, static data members of the class fail to exist.

False

Which of the following statements is true? a. If the file "temp.txt" does not exist, new PrintWrite("temp.txt") throws an IOException. b. If the file "temp.txt" does not exist, new PrintWrite("temp.txt") returns null. c. If the file "temp.txt" does not exist, new PrintWrite("temp.txt") creates a new file. d. If the file "temp.txt" does not exist, new PrintWrite("temp.txt") prints an error message to the console.

If the file "temp.txt" does not exist, new PrintWrite("temp.txt") throws an IOException.

Which of the following is used to allocate memory for the instance variables of an object of a class? a. the reserved word public b. the reserved word static c. the operator new d. the operator +

the operator new

public class Secret {private int x;private static int y; public static int count; public int z; public Secret() {x = 0; z = 1; } public Secret(int a) {x = a; } public Secret(int a, int b) {x = a; y = b; } public String toString() { return ("x = " + x + ", y = " + y + ", } count = " + count); public static void incrementY() {y++; } } What does the default constructor do in the class definition in the accompanying figure? a. Sets the value of x to 0 b. Sets the value of z to 1 c. Sets the value of x to 0 and the value of z to 1 d. There is no default constructor.

Sets the value of x to 0 and the value of z to 1

Analyze the following code: { public class TestProg public static void func(int n) { n = 10; } public static void main(String[] args) { int myNum = 0; func(myNum); System.out.println(myNum); } } a. The code compiles, runs fine, and prints 0 to the console. b. The code compiles, runs fine, and prints 10 to the console. c. The code does not compile because the return of func(myNum); is not assigned to a variable. d. The code does not compile because the method func does not contain a return statement.

The code compiles, runs fine, and prints 0 to the console.

Analyze the following code: public class Circle {private double radius; Circle() {radius = 0; } public double getRadius() { return radius; } public void setRadius(double r) {radius = r; } } public class TestProgCircle {public static void func(Circle c) { c.setRadius(10); } public static void main(String[] args) { Circle myCircle = new Circle(); func(myCircle); } System.out.println(myCircle.getRadius()); } a. The code compiles, runs fine, and prints 0 to the console. b. The code compiles, runs fine, and prints 10 to the console. c. The code does not compile because the return of func(myCircle); is not assigned to a variable. d. The code does not compile because the method func does not contain a return statement.

The code compiles, runs fine, and prints 10 to the console.

A mutator (setter) method of a class changes the values of the data members of the class.

True

Every object has access to a reference to itself.

True

Given the declaration public class MyClass {private int x; public void print() {System.out.println("x = " + x); } private void setX(int y) {x = y; } } MyClass myObject = new MyClass(); The following statement is legal. myObject.print();

True

If the object is created in the definition of a method of the class, then the object can access both thepublic and private members of the class.

True

In deep copying, each reference variable refers to its own object.

True

Modifiers are used to alter the behavior of the class.

True

The built-in operation that is valid for classes is the dot operator.

True

The non-static methods of a class are called instance methods.

True

Which of the following words indicates an object's reference to itself? a. this b. that c. public d. protected

this


Related study sets

ENDOCRINE/GI&NUTRITION/INFECTIOUS DX/URINARY-Pediatrics

View Set

CISSP Information Security and Risk Management (Set 1)

View Set

NUR 168: CHAPTER 41: SELF CONCEPT

View Set

8.2 Las profesiones con descripciónes

View Set