APCS Mid-Term Study
String s1 = "Aardvark"; String s2 = s1.replace('a','o');
"Aordvork"
what does <indexOf> method return if the string is not found?
-1
2) The set of all instances of a subclass is a subset of the instances of its superclass. A) true B) false
A
In the following code segment, assume that the ArrayList data has been initialized to contain the Integer values [4, 3, 4, 5, 3, 4]. int j = 0; while (j < data.size() - 1) { if (data.get(j) > data.get(j + 1)) { System.out.print(data.get(j + 1) + " "); } j++; } What, if anything, is printed as a result of executing the code segment?
A. 3 3
6) Which of the following statements are true? A) "class A extends B" means B is a subclass of A. B) A subclass is a subset of a superclass. C) A subclass is usually extended to contain more functions and more detailed information than its superclass. D) "class A extends B" means A is a subclass of B.
C,D
What is the class interaction between the Pumpkin class and the Face class? a. Both inheritance and composition b. There is no class interaction c. Composition only d. Inheritance only
Composition only
Which of the following is an example of a Utility class? a. A Car class which has an attribute that is an object of the Engine class. b. A Truck class that extends a car class. c. The Math class d. A TireSwing class which extends the Swing class and contains a Tire object.
The Math class
A string literal is
a set of characters delimited with double quotations.
29) Which of the following statements are true? A) Override the hashCode method whenever the equals method is overridden. By contract, two equal objects must have the same hash code. B) A public default no-arg constructor is assumed if no constructors are defined explicitly. C) Override the methods equals and toString defined in the Object class whenever possible. D) You should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods.
abcd
String processing is done with
all of these.
A String variable is
an object, which stores a set of characters, which behaves as a single unit.
which class or classes have access to the toString method?
any java standards library class or user-defined class
10) The visibility of these modifiers increases in this order ________. A) none (if no modifier is used), protected, private, and public B) private, none (if no modifier is used), protected, and public C) private, protected, none (if no modifier is used), and public D) none (if no modifier is used), private, protected, and public
b
16) Analyze the following code: double[] c = {1, 2, 3}; System.out.println(java.util.Collections.max(c)); A) The code is correct and displays 3.0. B) The code has a compile error on Collections.max(c). c cannot be an array. C) The code has a compile error on Integer[] c = {1, 2, 3}. D) The code is correct and displays 3.
b
The creation of a String object
can be done with or without the new operator
able to indicate the distance between strings that are not equal; returns an int value based on the difference between characters
compareTo method
27) Which of the following classes cannot be extended? A) class A { } B) class A { private A();} C) class A { protected A();} D) final class A { }
d
45) Which of the following are Java keywords? A) casting B) cast C) instanceOf D) instanceof
d
what array uses the size method?
dynamic ArrayList class
using the compareTo method, if the int value is zero s3 and s4 are what? int difference = s3.compareTo (s4)
equal
Use of Object class methods a. is restricted to primitive data types only. b. is automatically available in any class. c. requires re-definition of the Object methods. d. is possible only with the used of extends. e. is possible only by including import java.util.Object in your program.
is automatically available in any class.
what is the equality operator == used for?
meant for simple data types, designed to check object equality (only shallow values)
A program uses the Point, Trunk, Leaves Tree, PineTree and XmasTree classes. The drawOrnaments method in the XmasTree class is a. first defined in the PineTree class. b. first defined in the Tree class. c. newly-defined in the XmasTree class d. re-defined for the XmasTree class
newly-defined in the XmasTree class
is String a simple data type?
no, String is a class
What is the output of the following program segment? String s1 = "North"; String s2 = s1.substring(1,4); System.out.println(s2);
ort
this method converts a String into an int
parseInt
how would you define a <toString> method?
public String toString() {return ...}
(a) Write the StudentAssignments method getNumberOfAs. The method returns the number of assignments in a given category that have a grade greater than or equal to 90. Complete method getNumberOfAs. /** Returns the number of assignments in a given category that have a grade greater than or * equal to 9090, as described in part (a) */ public int getNumberOfAs(String cat)
public int getNumberOfAs(String cat) { int count = 0; for(Assignment a : assignmentList) { if(a.getGrade() >= 90 && a.getCategory().equals(cat)) { count++; } } return count; }
substring s2 = s1.substring (j,k)
returns set of consecutive characters from s1; starting at index j, ending at k-1
what method returns the number of characters in the String object
str.length
set of characters delimited with double quotations (String name = "John Smith"
string literal
an object, which stores a set of characters, which behaves as a single unit
string variable
string s1 = "Munster"; string s2 = s1.substring (1,4); what is the output
uns
Which method(s) can be used to convert simple data types into String objects?
valueOf
what would be the outcome: s1 = "Aardvark"; s2 = s1.substring(4);
vark (start at 4, continue through the word)
When is it frequently necessary to convert String values into int values or double values?
when numerical values are entered into the main argument, entered using readLine method, entered in a GUI window box
Is the new operator required or not required for the creation of a String object?
with or without
What String value is stored in s2 after the program segment below executes? String s1 = "100 AARDVARKS"; String s2 = s1.toLowerCase();
"100 aardvarks"
What String value is stored in s2 after the program segment below executes? String s1 = "aardvark"; String s2 = s1.toUpperCase();
"AARDVARK"
49) Analyze the following code. // Program 1 public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new A(); System.out.println(((A)a1).equals((A)a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } // Program 2 public class Test { public static void main(String[] args) { A a1 = new A(); A a2 = new A(); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } A) Program 1 displays true and Program 2 displays true B) Program 1 displays false and Program 2 displays true C) Program 1 displays true and Program 2 displays false D) Program 1 displays false and Program 2 displays false
A
An offroad-truck, which is a special truck converted for off-roading with special shocks, mud tires and four-wheel drive is an example of a. composition b. a superclass c. instantiation d. inheritance
inheritance
The is-a relationship describes a. encapsulation b. polymorphism c. composition d. inheritance
inheritance
String variables in early Java computer course programs are declared in the same manner as what?
simple data types like Int and Double
print and println request display instructions from what method?
the <toString> method
what array uses the length field?
the Java static array
where are methods "toString" and "equals" first defined?
the Object class
where are methods "toString" and "equals" re-defined at?
the String class
the equals method, as defined by the Object class compares what type of values?
the original equals method, from our Object class definition; only concerns itself with shallow values in the same manner as the original toString method
35) Every class has a toString() method and an equals() method. A) true B) false
a
36) Normally you depend on the JVM to perform garbage collection automatically. However, you can explicitly use ________ to request garbage collection. A) System.gc() B) System.gc(0) C) System.exit(0) D) System.exit()
a
39) If a method is declared protected in the superclass, you may declare the method public in the subclass. A) true B) false
a
42) If a parameter is of the java.lang.Object type, you can pass any object to it. This is known as generic programming. A) true B) false
a
47) You can use the operator == to check whether two variables refer to the same object, and use the equals() method to check the equality of contents of the two objects. A) true B) false
a
15) Analyze the following code: ArrayList<String> list = new ArrayList<String>(); list.add("Beijing"); list.add("Tokyo"); list.add("Shanghai"); list.set(3, "Hong Kong"); A) If you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine. B) The last line in the code has a compile error because there is no element at index 3 in the array list. C) If you replace the last line by list.add(4, "Hong Kong"), the code will compile and run fine. D) The last line in the code causes a runtime error because there is no element at index 3 in the array list.
a, d
Which class or classes have access to the toString method? a. The Object class only b. Any Java standard library class only c. Any user-defined class only d. Any Java standards library class or user-defined class e. The String class only
Any Java standards library class or user-defined class
What criteria is used by the == operator to test for equality? a. It compares both the shallow values and the deep values. b. It compares values that were assigned to the variables during program execution c. It compares the deep values only. d. It compares the shallow values only.
It compares the shallow values only.
What is the output of this program? public class Java1055 { public static void main (String args[]) { Student student1 = new Student("Jessica Schram",17,3.99); Student student2 = new Student("John Smith",18,1.21); System.out.println(student1); System.out.println(student2); } } class Student { private String name; private int age; private double gpa; public Student(String n, int a, double g) { name = n; age = a; gpa = g; } public String toString() { return "Jessica Schram"; } }
Jessica Schram Jessica Schram
Which of the following standard Java classes has redefined the equals method? a. String b. Object c. System d. Math e. Both A & B
String
which standard Java class has been redefined the equals method?
String
What is the output of this program? public class Java1053 { public static void main (String args[ ]) { Student student1 = new Student("Jessica Schram",17,3.99); Student student2 = new Student("John Smith",18,1.21); System.out.println(student1); System.out.println(student2); } } class Student { private String name; private int age; private double gpa; public Student(String n, int a, double g) { name = n; age = a; gpa = g; } }
Student@d457f9 (or other memory address) Student@06be2a (or other memory address)
34) If a method is declared private in the superclass, you may declare the method public in the subclass. A) true B) false
a
Consider the following program: public class Test100910 { public static void main(String args[]) { Engine engine = new Engine(); Car car = new Car(); } } class Engine { public Engine() { System.out.println("Engine Constructor"); } } class Car { public Car() { System.out.println("Car Constructor"); } } What is the class interaction between Car and Engine? a. Improper class interaction b. Composition c. Inheritance d. There is no class interaction
There is no class interaction
Consider the following program: public class Test101516 { public static void main(String args[]) { Car car = new Car(); } } class Engine extends Car { public Engine() { System.out.println("Engine Constructor"); } } class Car { public Car() { System.out.println("Car Constructor"); } } What is the class interaction between Car and Engine? a. Inheritance b. Improper class interaction c. Composition d. There is no class interaction
There is no class interaction
What is the output of the following program segment? String s1 = "United States"; String s2 = s1.substring(0,6); System.out.println(s2);
United
What is the output of the following program segment? String s1 = "United States"; String s2 = s1.substring(1,5); System.out.println(s1);
United States
20) Which of the statements regarding the super keyword is incorrect? A) You can use super.super.p to invoke a method in superclass's parent class. B) You can use super to invoke a super class constructor. C) You cannot invoke a method in superclass's parent class. D) You can use super to invoke a super class method.
a
33) You can always successfully cast a superclass to a subclass. A) true B) false
b
38) A class design requires that a particular member variable must be accessible by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this? A) The variable should be marked public. B) The variable should be marked protected. C) The variable should be marked private. D) The variable should be marked private and an accessor method provided. E) The variable should have no special access modifier.
b
5) Analyze the following code: public class Test extends A { public static void main(String[] args) { Test t = new Test(); t.print(); } } class A { String s; A(String s) { this.s = s; } public void print() { System.out.println(s); } } A) The program compiles, but it has a runtime error due to the conflict on the method name print. B) The program has an implicit default constructor Test(), but it cannot be compiled, because its super class does not have a default constructor. The program would compile if the constructor in the class A were removed. C) The program does not compile because Test does not have a default constructor Test(). D) The program would compile if a default constructor A(){ } is added to class A explicitly.
b
7) Which of the following statements is false? A) A private method cannot be accessed by a class in a different package. B) A protected method can be accessed by a subclass in a different package. C) A method with no visibility modifier can be accessed by a class in a different package. D) A public class can be accessed by a class from a different package.
c
14) Which statements are most accurate regarding the following classes? class A { private int i; protected int j; } class B extends A { private int k; protected int m; } A) An object of B contains data fields j, k, m. B) An object of B contains data fields k, m. C) An object of B contains data fields j, m. D) An object of B contains data fields i, j, k, m.
d
26) Which statements are most accurate regarding the following classes? class A { private int i; protected int j; } class B extends A { private int k; protected int m; // some methods omitted } A) In the class B, an instance method can only access k, m. B) In the class B, an instance method can only access i, j, k, m. C) In the class B, an instance method can only access j, m. C) that a variable of supertype can refer to a subtype object D) that a class can contain another class 26) Which statements are most accurate regarding the following classes? class A { private int i; protected int j; } class B extends A { private int k; protected int m; // some methods omitted } A) In the class B, an instance method can only access k, m. B) In the class B, an instance method can only access i, j, k, m. C) In the class B, an instance method can only access j, m. D) In the class B, an instance method can only access j, k, m.
d
How should you use equals to compare String?
if (s1.equals(s2)); designed to ignore the shallow values and compare the deeper values
method parseInt is a member of which class?
it is a member of the Integer class
using the compareTo method, if the int value is negative what comes first? int difference = s3.compareTo (s4)
s3 goes after s4
using the compareTo method, if the int value is positive what comes first? int difference = s3.compareTo (s4)
s4 goes after s3
when are two arrays considered equal?
same size, and store the same information at each index
parseDouble
this method converts a String into a double
3) The equals method is defined in the Object class. Which of the following is correct to override it in the String class? A) public static boolean equals(String other) B) public boolean equals(Object other) C) public boolean equals(String other) D) public static boolean equals(Object other)
B
6) Analyze the following code: public class Test { public static void main(String[] args) { new B(); } } class A { int i = 7; public A() { setI(20); System.out.println("i from A is " + i); } public void setI(int i) { this.i = 2 * i; } } class B extends A { public B() { // System.out.println("i from B is " + i); } public void setI(int i) { this.i = 3 * i; } } A) The constructor of class A is not called. B) The constructor of class A is called and it displays "i from A is 60". C) The constructor of class A is called and it displays "i from A is 40". D) The constructor of class A is called and it displays "i from A is 7".
B
Consider the following program: public class Test101718 { public static void main(String args[]) { Engine engine = new Engine(); } } class Engine { private Car car; public Engine() { car = new Car(); System.out.println("Engine Constructor"); } } class Car { public Car() { System.out.println("Car Constructor"); } } What is the output of this program?
Car Constructor Engine constructor
18) Which of the following methods override the toString method in the Object class? A) public void toString(String s) B) public static String toString() C) public String toString(String s) D) public String toString()
D
In the following code segment, assume that the ArrayList numList has been properly declared and initialized to contain the Integer values [1, 2, 2, 3]. The code segment is intended to insert the Integer value val in numList so that numList will remain in ascending order. The code segment does not work as intended in all cases. int index = 0; while (val > numList.get(index)) { index++; } numList.add(index, val); For which of the following values of val will the code segment not work as intended?
E. 4
Consider the following program: public class Test101920 { public static void main(String args[]) { Engine engine = new Engine(); } } class Engine { private Car car; public Engine() { car = new Car(); System.out.println("Engine Constructor"); } } class Car { private Engine engine; public Car() { engine = new Engine(); System.out.println("Car Constructor"); } } What is the class interaction between Car and Engine? a. There is no class interaction b. Improper class interaction c. Inheritance d. Composition
Improper class interaction
Consider the following program: public class Test102122 { public static void main(String args[]) { Car car = new Car(); Engine engine = new Engine(); } } class Engine extends Car { public Engine() { System.out.println("Engine Constructor"); } } class Car extends Engine { public Car() { System.out.println("Car Constructor"); } } What is the class interaction between Car and Engine? a. Improper class interaction b. Composition c. Inheritance d. There is no class interaction
Improper class interaction
Consider the following program: public class Test101314 { public static void main(String args[]) { Car car = new Car(); } } class Engine { public Engine() { System.out.println("Engine Constructor"); } } class Car extends Engine { public Car() { System.out.println("Car Constructor"); } } What is the class interaction between Car and Engine? a. There is no class interaction b. Improper class interaction c. Composition d. Inheritance
Inheritance
Consider the following class heading. public class Person extends Student What is not true about the class interaction of that class heading? a. It indicates an inheritance relationship between Person and Student b. It indicates that Student is the superclass and Person is the subclass. c. It indicates an "is-a" class interaction between the two classes. d. It indicates that Person is the superclass and Student is the subclass.
It indicates that Person is the superclass and Student is the subclass.
What is the output of this program? public class Question1052 { public static void main (String args[ ]) { Student student1 = new Student("Jessica Schram",17,3.99); Student student2 = new Student("John Smith",18,1.21); System.out.println(student1); System.out.println(student2); } } class Student { private String name; private int age; private double gpa; public Student(String n, int a, double g) { name = n; age = a; gpa = g; } public String toString() { return name; } }
Jessica Schram John Smith
What is the output of this program? public class Java1054 { public static void main (String args[ ]) { Student student1 = new Student("Jessica Schram",17,3.99); Student student2 = new Student("John Smith",18,1.21); System.out.println(student1); System.out.println(student2); } } class Student { private String name; private int age; private double gpa; public Student(String n, int a, double g) { name = n; age = a; gpa = g; } public String toString() { return "[" + name + ", " + age + ", " + gpa + "]"; } }
[Jessica Schram, 17, 3.99] [John Smith, 18, 1.21]
12) Suppose you create a class Square to be a subclass of GeometricObject. Analyze the following code: class Square extends GeometricObject { double length; Square(double length) { GeometricObject(length); } } A) The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally. B) The program compiles fine, but it has a runtime error because of invoking the Square class's constructor illegally. C) The program compiles fine, but you cannot create an instance of Square because the constructor does not specify the length of the Square.
a
22) If a data field is declared in the superclass, you may hide it by redeclaring it in the subclass. A) true B) false
a
23) Every object is an instance of the Object class. A) true B) false
a
31) What is the output of the following code? ArrayList<java.util.Date> list = new ArrayList<java.util.Date>(); java.util.Date d = new java.util.Date(); list.add(d); list.add(d); System.out.println((list.get(0) == list.get(1)) + " " + (list.get(0)).equals(list.get(1))); A) true true B) true false C) false true D) false false
a
32) What is the output of the following code?public class Test { public static void main(String[] args) { String s1 = new String("Java"); String s2 = new String("Java"); System.out.print((s1 == s2) + " " + (s1.equals(s2))); } } A) false true B) false false C) true true D) true false
a
50) Which of the following statements are true? A) Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. B) To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. C) It is a compilation error if two methods differ only in return type in the same class. D) A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. E) A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.
abcde
37) Analyze the following code: public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new Object(); System.out.println(a1); System.out.println(a2); } } class A { int x; public String toString() { return "A's x is " + x; } } A) When executing System.out.println(a2), the toString() method in the Object class is invoked B) When executing System.out.println(a1), the toString() method in the Object class is invoked. C) When executing System.out.println(a1), the toString() method in the A class is invoked. D) The program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by System.out.println(a1.toString());
ac
19) Analyze the following code: Cylinder cy = new Cylinder(1, 1); Circle c = cy; A) The code has a compile error. B) The code is fine. C) The code has a runtime error.
b
21) Analyze the following code. // Program 1: public class Test { public static void main(String[] args) { Object circle1 = new Circle(); Circle circle2 = new Circle(); System.out.println(circle1.equals(circle2)); } } class Circle { double radius; public boolean equals(Circle circle) { return this.radius == circle.radius; } } // Program 2: public class Test { public static void main(String[] args) { Circle circle1 = new Circle(); Circle circle2 = new Circle(); System.out.println(circle1.equals(circle2)); } } class Circle { double radius; public boolean equals(Object circle) { return this.radius == ((Circle)circle).radius; } } A) Program 1 displays true and Program 2 displays true B) Program 1 displays false and Program 2 displays true C) Program 1 displays true and Program 2 displays false D) Program 1 displays false and Program 2 displays false
b
24) The order in which modifiers appear before a class or a method is important. A) true B) false
b
25) Encapsulation means ________. A) that a class can extend another class B) that data fields should be declared private C) that a variable of supertype can refer to a subtype object D) that a class can contain another class
b
41) Given the following code:class C1 {} class C2 extends C1 { } class C3 extends C2 { } class C4 extends C1 {} C1 c1 = new C1(); C2 c2 = new C2(); C3 c3 = new C3(); C4 c4 = new C4(); Which of the following expressions evaluates to false? A) c1 instanceof C1 B) c4 instanceof C2 C) c3 instanceof C1 D) c2 instanceof C1
b
43) If a method is declared public in the superclass, you may declare the method private in the subclass. A) true B) false
b
48) Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code? String element = "red"; for (int i = list.size() - 1; i >= 0; i--) if (list.get(i).equals(element)) list.remove(element); A) {"red", "red", "green"} B) {"green"} C) {} D) {"red", "green"}
b
9) The UML uses ________ before a member name to indicate that the member is private. A) + B) - C) # D) ?
b
17) Consider the following declaration for a class A. class A { private int x; private int y; public A(int x, int y) { this.x = x; this.y = y; } } Class B is a subclass of A. Which of the following can be constructors in B? I: public B() { } II: public B(int x, int y) { super(x, y); } III: public B() { super(0, 0); } IV: public B(int x, int y) { this.x = x; this.y = y; } A) I B) II C) III D) IV
b,c
28) What is the output of running class C? class A { public A() { System.out.println( "The default constructor of A is invoked"); } } class B extends A { public B() { System.out.println( "The default constructor of B is invoked"); } } public class C { public static void main(String[] args) { B b = new B(); } } A) "The default constructor of A is invoked" B) "The default constructor of B is invoked" "The default constructor of A is invoked" C) "The default constructor of A is invoked" "The default constructor of B is invoked" D) Nothing displayed E) "The default constructor of B is invoked"
c
40) Which of the following methods override the equals method in the Object class? A) public static boolean equals(Object o) B) public void equals(Object o) C) public boolean equals(Object o) D) public boolean equals(SomeType o)
c
44) Analyze the following code.// Program 1: public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new A(); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(Object a) { return this.x == ((A)a).x; } } // Program 2: public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new A(); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } A) Program 1 displays true and Program 2 displays true B) Program 1 displays false and Program 2 displays true C) Program 1 displays true and Program 2 displays false D) Program 1 displays false and Program 2 displays false
c
8) Given the following classes and their objects: class C1 {}; class C2 extends C1 {}; class C3 extends C1 {}; C2 c2 = new C2(); C3 c3 = new C3(); Analyze the following statement: c2 = (C2)((C1)c3); A) You will get a runtime error because the Java runtime system cannot perform multiple casting in nested form. B) c3 is cast into c2 successfully. C) You will get a runtime error because you cannot cast objects from sibling classes. D) The statement is correct.
c
30) Given the following code, find the compile error. public class Test { public static void main(String[] args) { m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); } public static void m(Student x) { System.out.println(x.toString()); } } class GraduateStudent extends Student { } class Student extends Person { public String toString() { return "Student"; } } class Person extends Object { public String toString() { return "Person"; } } A) m(new GraduateStudent()) causes an error B) m(new Student()) causes an error C) m(new Object()) causes an error D) m(new Person()) causes an error
c,d
The equals method, as defined by the Object class a. compares shallow values only. b. compares values that were assigned to the variables during program execution. c. compares deep values only. d. compares both shallow values and deep values
compares shallow values only.
what criteria is used by the == operator to test for equality?
compares the shallow value only
parameters the include non-numerical characters will what?
compile, but cause a run-time error
The has-a relationship describes a. polymorphism b. composition c. inheritance d. encapsulation
composition
11) Invoking ________ removes all elements in an ArrayList x. A) x.empty() B) x.delete() C) x.remove() D) x.clear() E) x.clean()
d
13) The printout from the following code is ________. java.util.ArrayList<String> list = new java.util.ArrayList<String>(); list.add("New York"); java.util.ArrayList<String> list1 = list; list.add("Atlanta"); list1.add("Dallas"); System.out.println(list1); A) [New York] B) [New York, Dallas] C) [New York, Atlanta] D) [New York, Atlanta, Dallas]
d
4) What is the output of the following code? public class Test { public static void main(String[] args) { Object o1 = new Object(); Object o2 = new Object(); System.out.print((o1 == o2) + " " + (o1.equals(o2))); } } A) true false B) true true C) false true D) false false
d
Analyze the following code: public class Test { public static void main(String[] args) { new B(); } } class A { int i = 7; public A() { System.out.println("i from A is " + i); } public void setI(int i) { this.i = 2 * i; } } class B extends A { public B() { setI(20); // System.out.println("i from B is " + i); } public void setI(int i) { this.i = 3 * i; } } A) The constructor of class A is called and it displays "i from A is 40". B) The constructor of class A is called and it displays "i from A is 60". C) The constructor of class A is not called. D) The constructor of class A is called and it displays "i from A is 7".
d
If a class has three attributes, age, name and gender, then redefining the equals method a. requires that all three of the attributes are compared for equality. b. requires that at most one of the attributes is compared for equality. c. requires that one or more of the attributes is compared for equality. d. does not require that any of the attributes are compared for equality.
does not require that any of the attributes are compared for equality.
What is the output of the following program segment? String s1 = "North"; String s2 = s1.charAt(4); System.out.println(s2);
h
this method returns the first occurrence of a substring
indexOf
Method toString a. must be included in any user-defined class. b. is first defined in the Object class c. is only used by the String class d. converts any object to a String object
is first defined in the Object class
String name = "Peyton Puckett"; what is name?
the string Object
the Object class
the superclass for all Java classes
What static method is used to convert simple data types into String objects?
valueOf is used to convert data types into String objects (<String.valueOf>
what would be the outcome: s1 = "Aardvark"; s2 = s1.substring(4,7);
var
does the length method include spaces as a character?
yes
are Object class methods automatically available in any class?
yes, any class has excess to the method definitions that exist in the Object class
Imagine that you must define a Person class. Which of the following Person class headings must be incorrect? a. public class Person b. public class Person extends Object c. public class Person extends ArrayList d. class Person e. All of the above Person class headings can be correct.
All of the above Person class headings can be correct.
Consider the following method countNegatives, which searches an ArrayList of Integer objects and returns the number of elements in the list that are less than 0. public static int countNegatives(ArrayList<Integer> arr) { int count = 0; for (int j = 0; j < arr.size(); j++) // Line 4 { if (arr.get(j) < 0) { count++; } } return count; } Which of the following best explains the impact to the countNegatives method when, in line 4, j < arr.size() is replaced with j <= arr.size() - 1 ?
A. It has no impact on the behavior of the method.
What is the output of the following program segment? ) String s1 = "Aardvark"; String s2 = ""; for (int k = 0; k <= 5; k++) ....s2 += s1.charAt(k); System.out.println(s2);
Aardva
Composition is used between which of the following classes? a. Train and Caboose b. Train and Locomotive c. Train and TrainCar d. All of the above
All of the above
Consider the following method, inCommon, which takes two Integer ArrayList parameters. The method returns true if the same integer value appears in both lists at least one time, and false otherwise. public static boolean inCommon(ArrayList<Integer> a, ArrayList<Integer> b) { for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) // Line 5 { if (a.get(i).equals(b.get(j))) { return true; } } } return false; } Which of the following best explains the impact to the inCommon method when line 5 is replaced by for (int j = b.size() - 1; j > 0; j--) ?
B. After the change, the method will never check the first element in list b.
Consider the following program output of a pine tree, shown in chapter X about class interaction. The program uses the Point, Trunk, Leaves Tree and PineTree classes. What is the class interaction between those classes?
Both inheritance and composition
Consider the following method, remDups, which is intended to remove duplicate consecutive elements from nums, an ArrayList of integers. For example, if nums contains {1, 2, 2, 3, 4, 3, 5, 5, 6}, then after executing remDups(nums), nums should contain {1, 2, 3, 4, 3, 5, 6}. public static void remDups(ArrayList<Integer> nums) { for (int j = 0; j < nums.size() - 1; j++) { if (nums.get(j).equals(nums.get(j + 1))) { nums.remove(j); j++; } } } The code does not always work as intended. Which of the following lists can be passed to remDups to show that the method does NOT work as intended?
B. {1, 2, 2, 3, 3, 4, 5}
A program uses the Point, Trunk, Leaves Tree, PineTree and XmasTree classes. What is the class interaction between those classes? a. Both inheritance and composition b. Inheritance only c. Composition only d. There is no class interaction
Both inheritance and composition
Consider the program output of a Train. The program is created with the interaction of a TrainCar class, Locomotive class, Caboose class and a Train class. What is the class interaction between those four classes? a. Composition only b. Inheritance only c. Both inheritance and composition d. There is no class interaction
Both inheritance and composition
In the code segment below, assume that the ArrayList object numbers has been properly declared and initialized to contain [0, 2, 4, 5]. for (int k = numbers.size() - 1; k >= 0; k--) { if (numbers.get(k) > k) { System.out.print(k + " "); } } What, if anything, is printed as a result of executing the code segment?
C. 3 2 1
Consider the following method findValue, which takes an ArrayList of String elements and a String value as parameters and returns true if the String value is found in the list and false otherwise. public static boolean findValue(ArrayList<String> arr, String key) { for (int j = 0; j < arr.size(); j++) // Line 3 { if (arr.get(j).equals(key)) { return true; } } return false; } Which of the following best explains the impact to the findValue method when, in line 3, int j = 0 is replaced by int j = 1 ?
C. It will cause the method to return a different result when the key value is found only at the first index in the list.
Consider the following program: public class Test101516 { public static void main(String args[]) { Car car = new Car(); } } class Engine extends Car { public Engine() { System.out.println("Engine Constructor"); } } class Car { public Car() { System.out.println("Car Constructor"); } } What is the output of this program?
Car Constructor
Consider the following program: public class Test102122 { public static void main(String args[]) { Car car = new Car(); Engine engine = new Engine(); } } class Engine extends Car { public Engine() { System.out.println("Engine Constructor"); } } class Car extends Engine { public Car() { System.out.println("Car Constructor"); } } What is the output of this program?
Compile Error Message
Consider the following program: public class Test101112 { public static void main(String args[]) { Car car = new Car(); } } class Engine { public Engine() { System.out.println("Engine Constructor"); } } class Car { private Engine engine; public Car() { engine = new Engine(); System.out.println("Car Constructor"); } } What is the class interaction between Car and Engine? a. Composition b. Inheritance c. There is no class interaction d. Improper class interaction
Composition
Consider the following program: public class Test101718 { public static void main(String args[]) { Engine engine = new Engine(); } } class Engine { private Car car; public Engine() { car = new Car(); System.out.println("Engine Constructor"); } } class Car { public Car() { System.out.println("Car Constructor"); } } What is the class interaction between Car and Engine? a. There is no class interaction b. Inheritance c. Improper class interaction d. Composition
Composition
What is the output of the following program? public class Test1023 { public static void main(String[] args) { Point point = new Point(500,300); System.out.println("Point at (" + point.getX() + "," + point.getY() + ")"); } } class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } }
Point at (500,300)
In the following code segment, assume that the ArrayList wordList has been initialized to contain the String values ["apple", "banana", "coconut", "lemon", "orange", "pear"]. int count = 0; for (String word : wordList) { if (word.indexOf("a") >= 0) { count++; } } System.out.println(count); What is printed as a result of executing the code segment?
D. 4
Consider the following program: public class Test100910 { public static void main(String args[]) { Engine engine = new Engine(); Car car = new Car(); } } class Engine { public Engine() { System.out.println("Engine Constructor"); } } class Car { public Car() { System.out.println("Car Constructor"); } } What is the output?
Engine constructor Car constructor
Consider the following program: public class Test101314 { public static void main(String args[]) { Car car = new Car(); } } class Engine { public Engine() { System.out.println("Engine Constructor"); } } class Car extends Engine { public Car() { System.out.println("Car Constructor"); } } What is the output of this program?
Engine constructor Car constructor
A Dog is-a Mammal and a Mammal is-an Animal. In computer science this type of class-interaction is called a. Multiple inheritance b. Double inheritance c. Complex inheritance d. Multi-level inheritance
Multi-level inheritance
A Square is-a Rhombus and a Square is-a Rectangle. In computer science this type of class-interaction is called a. Double inheritance b. Multi-level inheritance c. Complex inheritance d. Multiple inheritance
Multiple inheritance
Consider this program: public class Test104344 { public static void main(String args[]) { Student tom = new Student(12); tom.showData(); } } class Person { public int age; public Person() { System.out.println("Person Parameter Constructor"); age = 17; } public int getAge() { return age; } } class Student extends Person { public int grade; public Student(int g) { grade = g; System.out.println("Student Parameter Constructor"); } public int getGrade() { return grade; } public void showData() { System.out.println("Student's Grade is " + grade); System.out.println("Student's Age is " + age); } } What are the last 2 lines of output?
Student's Grade is 12 Student's Age is 17
What is the consequence of declaring and using a totally empty class body in a program? a. The subclass will not compile. b. The subclass will compile and execute, but have no output. c. The subclass will behave exactly like the superclass. d. The subclass will has a a runtime exception during program execution.
The subclass will behave exactly like the superclass.
The engine, transmission, seats and other components required to make a car is an example of a. inheritance b. a superclass c. composition d. inheritance
composition
The Object class a. is the super class for any Java class. b. must be individually extended for any class. c. is the super class for any standard Java class only. d. is the super class for any user-defined Java class only. e. used only with primitive data types.
is the super class for any Java class.
String s1 = "Ford"; String s2 = "Chevy"; String s3 = // keyboard input "Ford"; if (s1.equals(s2)) ....System.out.println("s1 and s2 are equal"); else ....System.out.println("s1 and s2 are not equal"); if (s1.equals(s3)) ....System.out.println("s1 and s3 are equal"); else ....System.out.println("s1 and s3 are not equal");
s1 and s2 are not equal s1 and s3 are equal
An established class, whose members can be accessed by a newly declared class is a. an overloaded class b. a superclass c. a subclass d. a static class
a superclass
"AARDVARK"
I, II and III
What information will be stored in string3 as a result of the statement below? String string3 = "100" + "200";
The String value "100200"
Methods toString and equals are a. first defined in the String class. b. first defined in the Object class. c. re-defined in your own subclass if necessary. d. re-defined in the Object class. e. both B and C.
both B and C.
What types of methods can be found in a subclass? a. No methods at all. The subclass can be an empty class declaration. b. Re-defined methods c. Newly-defined method d. All of the above
All of the above
Consider the program output of a Jack-O'Lantern. The program is created with the interaction of a Pumpkin class, Face class and a JackOLantern class. What is the class interaction between those three classes? a. Inheritance only b. There is no class interaction c. Both inheritance and composition d. Composition only
Both inheritance and composition
Consider the following program output of a tree, shown in chapter X about class interaction. The program uses the Point, TreeTrunk, Leaves and Tree classes. What is the class interaction between those classes?
Composition only
The removeElement method is intended to remove all instances of target from the ArrayList object data passed as a parameter. The method does not work as intended for all inputs. public void removeElement(ArrayList<Integer> data, int target) { for (int j = 0; j < data.size(); j++) { if (data.get(j).equals(target)) { data.remove(j); } } } Assume that the ArrayList object scores and the int variable low_score have been properly declared and initialized. In which of the following cases will the method call removeElement(scores, low_score) fail to produce the intended result?
D. When scores is [8, 8, 4, 3, 3, 6] and low_score is 3
Consider the following program: public class Test101112 { public static void main(String args[]) { Car car = new Car(); } } class Engine { public Engine() { System.out.println("Engine Constructor"); } } class Car { private Engine engine; public Car() { engine = new Engine(); System.out.println("Car Constructor"); } } What is the output of this program?
Engine Constructor Car constructor
What is the class interaction between the Pumpkin class and the JackOLantern class? a. Composition only b. Inheritance only c. Both inheritance and composition d. There is no class interaction
Inheritance only
Which of the following Java class headings declares Square a subclass with multiple inheritance? a. Public class Square extends (Rhombus, Rectangle) b. Java does not support multiple inheritance. c. public class Square extends both Rhombus, Rectangle d. public class Square extends Rhombus & Rectangle e. public class Square Rhombus, Square
Java does not support multiple inheritance.
Consider this program: public class Test104344 { public static void main(String args[]) { Student tom = new Student(12); tom.showData(); } } class Person { public int age; public Person() { System.out.println("Person Parameter Constructor"); age = 17; } public int getAge() { return age; } } class Student extends Person { public int grade; public Student(int g) { grade = g; System.out.println("Student Parameter Constructor"); } public int getGrade() { return grade; } public void showData() { System.out.println("Student's Grade is " + grade); System.out.println("Student's Age is " + age); } } What are the first 2 lines of output?
Person Parameter Constructor Student Parameter Constructor
What is the output of the following program? public class Test1024 { public static void main(String[] args) { Point point = new Point(500,300); System.out.println("Point at (" + point.getX() + "," + point.getY() + ")"); } } class Point { private int x; private int y; public Point(int x, int y) { x = x; y = y; } public int getX() { return x; } public int getY() { return y; } }
Point at (0,0)
Consider the following program: public class Test101920 { public static void main(String args[]) { Engine engine = new Engine(); } } class Engine { private Car car; public Engine() { car = new Car(); System.out.println("Engine Constructor"); } } class Car { private Engine engine; public Car() { engine = new Engine(); System.out.println("Car Constructor"); } } What is the output of this program?
Runtime Exception Error Message
Consider the following program: public class Test104142 { public static void main(String args[]) { Student tom = new Student(); System.out.println("tom's age is " + tom.getAge()); System.out.println("tom's grade is " + tom.getGrade()); } } class Person { private int age; public int getAge() { return age; } } class Student extends Person { private int grade; public int getGrade() { return grade; } } What is the consequence of removing extends Person from the program above? a. The class interaction will change from inheritance to composition. b. The class interaction will change from composition to inheritance. c. The program will compile, but it will not execute correctly. d. The program will no longer compile, since the getAge method is not accessible.
The program will no longer compile, since the getAge method is not accessible.
Consider the following program: public class Test104142 { public static void main(String args[]) { Student tom = new Student(); System.out.println("tom's age is " + tom.getAge()); System.out.println("tom's grade is " + tom.getGrade()); } } class Person { private int age; public int getAge() { return age; } } class Student extends Person { private int grade; public int getGrade() { return grade; } } What evidence exists that proves that inheritance is functional in this program? a. The tom object has access to the getAge method. b. There is evidence of class interaction with composition, but not with inheritance. c. The Student class extends the Person class. d. The tom object has access to the getGrade method
The tom object has access to the getAge method.
What is the output of the following program segment? String s1 = "Hello"; String s2 = "World"; System.out.println(s2 + s1);
WorldHello
Is comparing String values different from comparing simple data type values?
Yes, it is different. Simple types use the == operator and strings use the equals method.
String s1 = "Aardvark"; String s2 = ""; for (int k = 0; k <= 5; k++) ....s2 = s1.charAt(k); System.out.println(s2);
a
The statement String name = "Kathy Smith"; constructs name as:
a String object.
A class, which can use all the features of an established superclass is a. a subclass b. a superclass c. a static class d. an overloaded class
a subclass
The replace method
does both B and C.
Object methods a. behave only according to the definitions in Object subclasses b. behave the same for all classes c. follow the definitions originated in the Object class, unless re-defined d. are available to classes of the java.util.* package only.
follow the definitions originated in the Object class, unless re-defined
The program output shows five traincars. This means that a Locomotive a. has-a TrainCar b. is-a TrainCar. c. has-a Caboose. d. is-a Train.
is-a TrainCar.
String s1 = "Noel"; int n = s1.length() - 1; String s2 = ""; for (int k = n; k >= 0; k--) ....s2 += s1.charAt(k); System.out.print(s2);
leoN
What is the output of the following program segment? String s = "Noel"; int n = s.length() - 1; for (int k = n; k >= 0; k--) ....System.out.print(s.charAt(k));
leoN
(c) The programmer would like to add a method called getAllGroupAssignments that returns a list of assignments that have been designated as having parts where students worked in groups. A group assignment can appear in any category. Write a description of how you would change the Assignment and StudentAssignments classes in order to support this modification. Make sure to include the following in your response. Write the method header for the getAllGroupAssignments method. Identify any new or modified variables, constructors, or methods aside from the getAllGroupAssignments method. Do not write the program code for this change. Describe, for each new or revised variable, constructor, or method, how it would change or be implemented, including visibility and type. You do not need to describe the getAllGroupAssignments method. Do not write the program code for this change.
public ArrayList<Assignment> getAllGroupAssignments() The Assignment class would have to be changed to have a private boolean value inGroup which returns either true or false depending on if students worked in groups. The Assignment class should also include a public getGroups() method which returns the groups the students worked in. The StudentAssignments class should include the getAllGroupAssignments() method because it has an array list of Assignment objects which can be iterated through.
(b) Write the availableMechanics method, which returns an ArrayList containing the mechanic numbers of all available mechanics. If there is no available mechanic, an empty list is returned. A mechanic is available if the mechanic's identifier does not appear in schedule. Suppose schedule has the following contents. For these contents of schedule, availableMechanic should return an ArrayList containing the values 2, 3, 4, and 5 (in any order). Complete the availableMechanics method. Assume that addRepair works as specified, regardless of what you wrote in part (a). /** Returns an ArrayList containing the mechanic identifiers of all available mechanics, * as described in part (b). */ public ArrayList<Integer> availableMechanics()
public ArrayList<Integer> availableMechanics() { ArrayList<Integer> availableList = new ArrayList<Integer>(); for(int i = 0; i < numberOfMechanics; i++) { int count = 0; for(CarRepair c : schedule) { if(c.getMechanicNum() = i) { count++; } } if(count == 0) { availableList.add(i); } } return availableList; }
(c) A programmer would like to add a method called getMostLuggageCapacity, which returns a Flight object that can carry the greatest amount of luggage, by weight. Write a description of how you would change the Flight and Airport classes in order to support this modification. Make sure to include the following in your response. Write the method header for the getMostLuggageCapacity method. Identify any new or modified variables, constructors, or methods aside from the getMostLuggageCapacity method. Do not write the program code for this change. Describe, for each new or revised variable, constructor, or method, how it would change or be implemented, including visibility and type. You do not need to describe the implementation of the getMostLuggageCapacity method. Do not write the program code for this change.
public Flight getMostLuggageCapacity() The Flight class would have to be changed by adding a private instance variable luggage and a public getLuggage() method. This method should return the amount of luggage carried by a flight. The Airport class would be where the getMostLuggageCapacity() method would be added because it has an array list of Flight objects. This list can then be iterated over to compare each flight's luggage capacity.
Consider the following class that represents the test results of a group of students that took a multiple-choice test. Write the TestResults method highestScoringStudent, which returns the name of the student who received the highest score on the test represented by the parameter key. If there is more than one student with the highest score, the name of any one of these highest-scoring students may be returned. You may assume that the size of each answer sheet represented in the ArrayList sheets is equal to the size of the ArrayList key. In writing highestScoringStudent, assume that getScore works as specified, regardless of what you wrote in part (a). Complete method highestScoringStudent below
public String highestScoringStudent(ArrayList<String> key) { StudentAnswerSheet highestScorer = sheets.get(0); for(StudentAnswerSheet sheet : sheets) { if(sheet.getScore(key) > highestScorer.getScore(key)) { highestScorer = sheet; } } return highestScorer.getName();}
(a) Write the addRepair method. The method attempts to schedule a repair by the mechanic with identifier m in the bay with identifier b. The repair can be scheduled if mechanic m and bay b are both available. A mechanic is available if the given mechanic number does not appear in an element of schedule and a bay is available if the given bay number does not appear in an element of schedule. If the mechanic and bay are both available, the addRepair method adds the repair to schedule and returns true. If either the mechanic or the bay are not available, the addRepair method returns false. The following sequence of statements provides examples of the behavior of the addRepair method. The statement RepairSchedule r = new RepairSchedule(6); constructs a new RepairSchedule object r. No repairs have been scheduled. Mechanics are numbered 0 through 5. The call r.addRepair(3, 4) returns true because neither mechanic 3 nor bay 4 are present in schedule. The contents of schedule after the call are as follows. The call r.addRepair(0, 1) returns true because neither mechanic 0 nor bay 1 are present in schedule. The contents of schedule after the call are as follows. The call r.addRepair(0, 2) returns false because mechanic 0 is present in schedule. The contents of schedule after the call are as follows. The call r.addRepair(2, 4) returns false because bay 4 is present in schedule. The contents of schedule after the call are as follows. The call r.carOut(4) removes the repair in bay 4 from schedule. The carOut method is shown here to illustrate that bays and mechanics become available when car repairs are complete. You do not need to write or call this method. The contents of schedule after the call are as follows. The call r.addRepair(1, 4) returns true because neither mechanic 1 nor bay 4 are present in schedule. The contents of schedule after the call are as follows. Complete the addRepair method. /** Attempts to schedule a repair by a given mechanic in a given bay as described in part (a). * Precondition: 0 <= m < numberOfMechanics and b >= 0 */ public boolean addRepair(int m, int b)
public boolean addRepair (int m, int b) { for(CarRepair c : schedule) { if(c.getMechanicNum() == m) || c.getBayNum == b) { return false; } } schedule.add(new CarRepair(m, b)); return true; }
Write the StudentAnswerSheet method getScore. The parameter passed to method getScore is an ArrayList of strings representing the correct answer key for the test being scored. The method computes and returns a double that represents the score for the student's test answers when compared with the answer key. One point is awarded for each correct answer and ¼ of a point is deducted for each incorrect answer. Omitted answers (indicated by "?") do not change the student's score. Complete method getScore below.
public double getScore(ArrayList<String> key) { double score = 0; for(int i = 0; i < answers.size(); i++) { if(answers.get(i).equals(key.get(i))) { score+=1; } else if(!answers.get(i).equals("?")) { score -= 0.25; } } return score; }
(a) Write the Airport method getTotalRevenue. The method returns the total revenue for all flights into and out of the airport. Revenue for a flight is the product of the number of passengers on the flight and the price of a seat on the flight. All seats on a flight have the same price. Some flights sell more seats than there is capacity for, since passengers sometimes cancel or modify reservations. If there are more passengers on a flight than the flight has capacity for, the revenue for the flight is the product of the capacity and the price of a seat on the flight. For example, assume that capitalHub has been declared as an Airport object and ArrayList allFlights contains the following flights. Number of Passengers: 25Price: 50.00Capacity: 30Number of Passengers: 10Price: 100.50Capacity: 60Number of Passengers: 50Price: 200.00Capacity: 40Number of Passengers: 20Price: 100.00Capacity: 120 The following table shows the revenue that would be generated by each flight in allFlights. Number of PassengersPrice of a SeatCapacityRevenue Calculation2525$50.00$50.00303025×$50.00=$1,250.0025×$50.00=$1,250.001010$100.50$100.50606010×$100.50=$1,005.0010×$100.50=$1,005.005050$200.00$200.00404040×$200.00=$8,000.0040×$200.00=$8,000.002020$100.00$100.0012012020×$100.00=$2,000.0020×$100.00=$2,000.00 The call capitalHub.getTotalRevenue() should return the value 12255.0. Complete method getTotalRevenue. /** Returns the revenue generated by all flights at the airport, as described in part (a) */ public double getTotalRevenue()
public double getTotalRevenue() { double totalRev = 0; for(Flight f : allFlights) { if(f.getNumPassengers() > f.getCapacity()) { totalRev += f.getPrice() * f.getCapacity(); } else { totalRev += f.getPrice() * f.getNumPassengers(); } } return totalRev; }
(b) Write the Airport method updateFlights. The method removes from the ArrayList allFlights any flight where the number of passengers is less than 2020 percent of the total capacity. The method should return the total number of passengers whose flight was removed. For example, assume that capitalHub has been declared as an Airport object and ArrayList allFlights contains the following flights. Number of Passengers: 25Price: 50.00Capacity: 30Number of Passengers: 10Price: 100.50Capacity: 60Number of Passengers: 50Price: 200.00Capacity: 40Number of Passengers: 20Price: 100.00Capacity: 120 The call capitalHub.updateFlights() should return the value 30, and after the method finished executing, the ArrayList allFlights should contain the following two flights. Number of Passengers: 25Price: 50.00Capacity: 30Number of Passengers: 50Price: 200.00Capacity: 40 Complete method updateFlights. /** Updates the list of flights by removing certain flights and returns the total number of * passengers whose flights were removed, as described in part (b) */ public int updateFlights()
public int updateFlights() { int sum = 0; for(int k = allFlights.size() - 1; k >= 0; k--) { Flight temp = allFlights.get(k); int min = (int) (temp.getCapacity() * .2); int pass = temp.getNumPassengers(); if(pass < min) { sum+= pass; allFlights.remove(k); } } return sum; }
(b) Write the StudentAssignments method updateGrade. The method replaces one and only one grade below 7070 in a given category, if any such grade exists and if the given category has more than five assignments with a grade of A. The identified grade is replaced with the value 7070. If more than one such assignment exists, the grade of any one of those assignments may be replaced. Assume that getNumberOfAs works as specified, regardless of what you wrote for part (a). You must use getNumberOfAs appropriately to receive full credit. Complete method updateGrade. /** Replaces a single assignment grade in a given category if the number of As in * the category is greater than 55 but at least one score is below 7070, as described in part (b) */ public void updateGrade(String cat)
public void updateGrade(String cat) { int aCount = getNumberOfAs(cat); if(aCount > 5) { for(Assignment a : assignmentList) { if(a.getGrade() < 70 && a.getCategory().equals(cat)) { a.setGrade(70); return; } } } }
Given a string, return a "rotated right 2" version where the last 2 chars are moved to the start. The string length will be at least 2. Examples... right2("Hello") → "loHel" right2("java") → "vaja" right2("Hi") → "Hi" public String right2(String str) {
return str.substring(str.length()-2, str.length()) + str.substring(0,str.length()-2);
What is the output of the following program segment? String s1 = "Hello"; String s2 = "World"; System.out.println("s1 + s2");
s1 + s2
What is the output of the program segment below? String s1 = "Ford"; String s2 = "Chevy"; String s3 = // keyboard input "Ford"; if (s1 == s2) ....System.out.println("s1 and s2 are equal"); else ....System.out.println("s1 and s2 are not equal"); if (s1 == s3) ....System.out.println("s1 and s3 are equal"); else ....System.out.println("s1 and s3 are not equal");
s1 and s2 are not equal s1 and s3 are not equal
What is the output of the program segment below? String s1 = "Ford"; String s2 = "Chevy"; String s3 = // keyboard input "Nissan"; if (s1 == s2) ....System.out.println("s1 and s2 are equal"); else ....System.out.println("s1 and s2 are not equal"); if (s1 == s3) ....System.out.println("s1 and s3 are equal"); else ....System.out.println("s1 and s3 are not equal");
s1 and s2 are not equal s1 and s3 are not equal
Class interaction is the process of a. dividing a program into multiple related files for each class in the program. b. using classes in the established standard Java Language library. c. combining data and the methods, which process the data, inside the same module. d. using features from an existing class.
using features from an existing class.