unit 5 writing classes

0.0(0)
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/9

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

10 Terms

1
New cards

When this type of variable is passed into a method, any changes made to it in the method are saved and the previous value of the variable is overwritten outside the method.
(A) primitive
(B) parameter
(C) class
(D) method
(E) argument

A

2
New cards

In Java, data fields and methods can be designated public or private. Which of the following best characterizes the designation that should be used?

(A) Data fields and methods should always be public. This makes it easier for client programs to access data fields and use the methods of the class.
(B) Data fields should be either public or private, depending on whether or not it is beneficial for client programs to access them directly. All methods should be public. A private method is useless because a client program can’t access it.
(C) Keep all methods and data fields private. This enforces encapsulation.
(D) Data fields should always be private so that clients can’t directly access them. Methods can be either public or private.
(E) All data fields should be public so client programs can access them, and all methods should be private.

D

3
New cards

Consider the following method:
public static String
midString(String s) {

String output = “”;
int b = 0;
int t = s.length() - 1;
while(b <= t) {
output += s.substring(b, b+1);
int m = (b+t)/2;
b = m + 1;
}
return output;
}

How many times would the body of the while loop execute when the following statement is executed: midString(“responsibility”);

(A) 3 (B) 4 (C) 5 (D) 6 (E) 7

B

4
New cards

Consider the definition of the Person class below. The class uses the instance variable adult to indicate whether a person is an adult or not.

public class Person {
private String name;
private int age;
private boolean adult;
public Person (String n, int a) {
name = n;
age = a;
if (age >= 18) {
adult = true;
}
else {
adult = false;
}
} }

Which of the following statements will create a Person object that represents an adult person?
(A) Person p = new Person ("Jackie", "adult");
(B) Person p = new Person ("Jackie", 23);
(C) Person p = new Person ("Jackie", "23");
(D) Person p = new Person ("Jackie", true);
(E) Person p = new Person ("Jackie", 17);

B

5
New cards

Consider the following method.
//*Precondition: num > 0 *//
public static int doWhat(int num) {
int var = 0;
for(int loop = 1; loop <= num; loop = loop+2) {
var += loop;
}
return var;
}

Which of the following best describes the value returned from a call to doWhat?
(A) num
(B) The sum of all integers between 1 and num inclusive.
(C) The sum of all even integers between 1 and num inclusive.
(D) The sum of all odd integers between 1 and num, inclusive.
(E) No value is returned because of an infinite loop.

D

6
New cards

public double mystery(int x, double y) {
return x / 2.0;
}

Which of the following lines of code, if located in a method in the same class as mystery, will compile without error?

(A) int answer = mystery(4, 4);
(B) int answer = mystery(4, 4.0);
(C) double answer = mystery(4, 4.0);
(D) double answer = mystery(4.0, 4);
(E) double answer = mystery(4.0, 4.0);

C

7
New cards

Consider the following methods, which appear in the same class.
public void methodA(int arg) {
int num = arg * 10;
methodB(num);
}
public void methodB(int arg) {
System.out.println(arg + 10);
}

Consider the call methodA(4), which appears in a method in the same class. What, if anything, is printed as a result of the call methodA(4)?
(A) 14

(B) 40

(C) 50

(D) 140

(E) Nothing is printed

C

8
New cards

Consider the following class declaration:
public class Student {
private String name;
private int age;
public Student(String n, int a) {
name = n;
age = a;
}
public boolean isOlderThan4() {
if(age > 4)
return true;
}
}
Which of the following best describes the reason this code segment will not compile?
(A) The return type for isOlderThan4 should be void.
(B) The isOlderThan4 method should be static.
(C) age is private and cannot be used in isOlderThan4 method.
(D) The isOlderThan4 method is missing a return statement for the case when age is less than or equal to 4.

(E) The isOlderThan4 method should receive the variable age as a parameter.

D

9
New cards

public class Point {
private double x;
private double y;
public Point() {

this(0, 0);

}

public Point(double a, double b) {

/* missing code */

}

public double getX() {

return x;

}

public double getY() {

return y;

}

}

Which of the following correctly implements the equals method, assuming that the equals method checks to make sure the x and y values of each point are the same?

(A) public boolean equals(Point p) {

return (x p.x && y p.y);

}

(B) public boolean equals(Point p) {

return (this.x p.x && this.y p.y);

}

(C) public boolean equals(Point p) {

return (x Point.getX() && y Point.getY());

}

(D) public boolean equals(Point p) {

return (this.x Point.getX() && this.y Point.getY());

}

(E) public boolean equals(Point p) {

return (this.x p.getX() && this.y p.getY());

E

10
New cards

public class Point {
private double x;
private double y;
public Point() {

this(0, 0);

}

public Point(double a, double b) {

/* missing code */

}

public double getX() {

return x;

}

public double getY() {

return y;

}

}

The default constructor sets x and y to (0, 0) by calling the second constructor. What could be used to replace /* missing code */ so that this works as intended?
(A) a = x; b = y;

(B) this(0, 0);

(C) a = 0; b = 0;

(D) this(x, y);

(E) x = a; y = b;

E