CHS Unit 7,8,9

Ace your homework & exams now with Quizwiz!

Given the definitions of class A and class B class A { public int i; public int j; public A() { i = 1; j = 2; } } class B extends A { int a; public B() { super(); } } what is the output of this code B obj = new B(); System.out.println(obj.i + " " + obj.j); 1 2 2 1 Runtime Error Compilation Error

1 2

Given the following: double[][] something = { {2.5, 6.8, 8.3, 2.3, 0.0}, {6.1, 10.2, 1.3, -2.5, -9.9}, {1.1, 2.3, 5.8, 13.21, 34.55} }; What is the value of something[1][2]? 6.8 6.1 2.3 1.3 The array does not have an element at that location.

1.3

Given the following: double[][] something = { {2.5, 6.8, 8.3, 2.3, 0.0}, {6.1, 10.2, 1.3, -2.5, -9.9}, {1.1, 2.3, 5.8, 13.21, 34.55} }; What is the value of something[2][1]? 6.8 6.1 2.3 1.3 The array does not have an element at that location.

2.3

Given the following: double[][] something = { {2.5, 6.8, 8.3, 2.3, 0.0}, {6.1, 10.2, 1.3, -2.5, -9.9}, {1.1, 2.3, 5.8, 13.21, 34.55} }; What is the value of something.length? 5 4 3 12 8

3

Given the following: double[][] something = { {2.5, 6.8, 8.3, 2.3, 0.0}, {6.1, 10.2, 1.3, -2.5, -9.9}, {1.1, 2.3, 5.8, 13.21, 34.55} }; What is the value of something[2].length? 5 4 3 12 8

5

What will the following code print? ArrayList<Integer> list = new ArrayList<Integer>(); list.add(0); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); int sum = 0; for (int i = 0; i < list.size(); i+= 2) { sum += list.get(i); } System.out.println(sum); 0 5 6 9 15

6

What would the proper initialization and declaration be for an ArrayList myTemps meant to store precise temperature measurements? ArrayList<double> myTemps = new ArrayList<double>(); ArrayList<Double> myTemps = new ArrayList<Double>(); ArrayList<Integer> myTemps = new ArrayList<Integer>(); ArrayList<double> myTemps = ArrayList<double>(); ArrayList<Double> myTemps = ArrayList<Double>();

ArrayList<Double> myTemps = new ArrayList<Double>();

Which of the following is the best example of a Superclass / Subclass relationship? Balloon / Color Team / Mascot Student / Grade Shirt / Button Fruit / Banana

Fruit / Banana

Given the class definitions of Vector2D and Vector3D below: public class Vector2D { private int x; private int y; public Vector2D() {} public Vector2D(int x,int y) { this.x = x; this.y = y; } // other code } public class Vector3D extends Vector2D { public int z; // other code } Which of the constructors that follow would be valid to put in the Vector3D class? Possible constructors for Vector3D:I. public Vector3D() {} II. public Vector3D(int x, int y, int z) { super(x,y); this.z = z; } III. public Vector3D(int x, int y) { super.x = x; super.y = y; this.z = 0; } I, II, and III I and II II and III Only III Only II

I and II

Consider the following class declarations public class Student { public void printSchool() { System.out.println("City Schools"); } } public class HSStudent extends Student { public void schoolName() { System.out.println("City High"); } } public class MSStudent extends Student { public void printSchool() { System.out.println("City Middle"); } } Which of the following will print City Schools? I. Student jackson = new Student(); jackson.printSchool(); II. HSStudent jackson = new HSStudent(); jackson.printSchool(); III. MSStudent jackson = new MSStudent(); jackson.printSchool(); I only I, II only II, III only I, II, III

I, II only

Consider the following class declarations public class Car { /* Implementation Not Shown */ } public class ElectricCar extends Car { /* Implementation Not Shown */ } The following line of code appears in a test class: ArrayList<Car> cars = new ArrayList<Car>(); Which of the following objects can be added to the ArrayList? I. Car leaf = new ElectricCar();II. Car mustang = new Car();III. ElectricCar model3 = new ElectricCar(); I Only I and II only I and III only III only I, II, III

I, II, III

Given the following class declaration public class BaseballTeam { private String name; private String location; public BaseballTeam(String name, String location) { this.name = name; this.location = location; } public boolean equals(BaseballTeam team) { if (other == null) { return false; } if (name.equals(team.name) && location.equals(team.location)) { return true; } return false; } } If 4 BaseballTeam objects are created as such: BaseballTeam one = new BaseballTeam("Athletics", "Oakland"); BaseballTeam two = new BaseballTeam("Athletics", "Kansas City"); BaseballTeam three = new BaseballTeam("Athletics", "Oakland"); BaseballTeam four = two; Which of the following will print true? I. System.out.println(one.equals(three)); II. System.out.println(two.equals(four)); III. System.out.println(one == three); IV. System.out.println(two == four); I and II only III and IV only II only III Only I, II, and IV Only

I, II, and IV Only

Consider the following statement: ArrayList<String> newList = /* Missing Code */ Which of the following can be replaced with /* Missing Code */ so that the statement works as intended? I. new ArrayList<String>; II. new ArrayList(); III. new ArrayList<String>(); I only III only I and III II and III I, II, and III

II and III

Consider the following class declarations public class Park { private String name; public Park(String name) { this.name = name; } public String toString() { return name + " park"; } } public class Field extends Park { private String type; public Field(String name, String type) { super(name); this.type = type; } public String toString() { return super.toString() + " with a " + type + " field"; } } A code segment located in a different class produced the following output: Centennial park with a soccer field Which of the following code segments will produce that output? I. Park cent = new Park("Centennial", "soccer"); System.out.println(cent); II. Park cent = new Field("Centennial", "soccer"); System.out.println(cent); III. Field cent = new Field("Centennial", "soccer"); System.out.println(cent); I Only II Only III only II and III Only I, II, III

II and III Only

What will the following code print? ArrayList<String> list = new ArrayList<String>(); list.add("I"); list.add("love"); list.add("coding"); list.add("in"); list.add("Java"); list.remove(3); System.out.println(list.get(3)); 3 coding in Java It will throw an IndexOutOfBoundsException.

Java

The following method is a search method intended to return the index at which a String value occurs within an ArrayList: public int search(ArrayList<String> list, String target) //Line 1 { int counter = 0; while(counter < list.size()) { if(list.get(counter).equals(target)) { return list.get(counter); } counter++; } return -1; } Java However, there is currently an error preventing the method to work as intended. Which of the following Lines needs to be modified in order for the method to work as intended? Line 1 - The method should be returning a String value, not an int. Line 4 - The loop should go to < list.size() - 1 so as to avoid an IndexOutofBoundsException. Line 8 - The return should be the counter, not list.get(counter). Line 10 - As written, the counter will skip every other value. Line 12 - The return value should be a String, not an int.

Line 8 - The return should be the counter, not list.get(counter).

Given the following class structure, which class is on the top of the hierarchy? public class Grandparent { /* Implementation not shown */ } public class Parent extends Grandparent { /* Implementation not shown */ } public class Child extends Parent { /* Implementation not shown */ } public class GrandChild extends Child { /* Implementation not shown */ } Child Object parent Grandparent Grandchild

Object

The Java Classes Skeleton, Spider, and Zombie all extend the Java Class Monster. The Monster Class is defined below. public class Monster { private String name; private String type; private int x; private int y; public Monster(String name, String type) { this.name = name; this.type = type; this.x = 0; this.y = 0; } public void move(int x, int y) { this.x = x; this.y = y; } } What variables and methods do the Skeleton, Spider, and Zombie Classes inherit? Everything Only the variables Only the method move Everything except the constructor

Only the method move

Consider the following class declarations public class Parent { public void name() { System.out.println("Parent"); } public void age() { System.out.println("Old"); } } public class Child extends Parent { public void name() { System.out.println("Child"); } public void age() { System.out.println("Young"); } public void grade() { System.out.println("10"); } } Which of the following statements will cause a compile-time error? Parent person = new Child(); person.grade(); Child person = new Child(); person.grade(); Parent person = new Parent(); person.age(); Parent person = new Child(); person.age();

Parent person = new Child(); person.grade();

Based on this code snippet public class Shape { public String getShapeName() { return "Shape"; } } public class Rectangle extends Shape { public String getShapeName() { return "Rectangle"; } } public class Square extends Rectangle {} public class Oval extends Shape { public String getShapeName() { return "Oval"; } } public class Circle extends Oval { public String getShapeName() { return "Circle"; } } what does this program output? Shape shape1 = new Shape(); Shape shape2 = new Rectangle(); Shape shape3 = new Square(); Shape shape4 = new Circle(); System.out.println(shape1.getShapeName()); System.out.println(shape2.getShapeName()); System.out.println(shape3.getShapeName()); System.out.println(shape4.getShapeName()); Shape Rectangle Rectangle Oval Shape Rectangle Square Circle This code will error Shape Rectangle Rectangle Circle Shape Shape Shape Shape

Shape Rectangle Rectangle Circle

Given the following class declarations public class Team { private String name; private String location; public Team(String name, String location) { this.name = name; this.location = location; } public String getName() { return name; } public String getLocation() { return location; } } public class BaseballTeam extends Team { private String name; private String location; public BaseballTeam(String name, String location) { super(name, location); } public String toString() { return super.getLocation() + " " + super.getName(); } } Which of the following will print Los Angeles Dodgers? Team dodgers = new BaseballTeam("Dodgers", "Los Angeles"); System.out.println(dodgers); Team dodgers = new BaseballTeam("Dodgers", "Los Angeles"); dodgers.toString(); Team dodgers = new Team("Dodgers", "Los Angeles"); dodgers.toString(); Team dodgers = new Team("Dodgers", "Los Angeles"); System.out.println(dodgers); All of these will print correctly

Team dodgers = new BaseballTeam("Dodgers", "Los Angeles"); System.out.println(dodgers);

Question: 2 What will the following code print? ArrayList<String> list = new ArrayList<String>(); list.add("Hello"); list.add("World"); System.out.println(list[0]); "Hello" "World" "list[0]" There will be a compiler error None of the above

There will be a compiler error

What is wrong with the following code? ArrayList<int> list = new ArrayList<int>(); list.add(1); System.out.println(list.get(0)); It will throw an IndexOutOfBoundsException. You cannot use int as the type in an ArrayList. list is a reserved word, so you can't call the variable that. You need to define the new ArrayList on a new line of code. Nothing. The above code is correct.

You cannot use int as the type in an ArrayList.

Consider the following code segment: ArrayList<String> list = new ArrayList<String>(); list.add("One"); list.add("Two"); list.add("Three"); list.add("Four"); list.add("Five"); list.add("Six"); for(int i= 0; i < list.size(); i++) { list.remove(i); } System.out.println(list.toString()); What is printed when the code segment is executed? [] ["Two", "Four"] ["Two", "Four", "Six"] ["Four", "Five", "Six"] ["Six"]

["Two", "Four", "Six"]

Consider the following code segment: ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(10); nums.add(20); nums.add(30); nums.add(40); nums.add(50); int x = nums.remove(3); int y = x + nums.remove(0); int z = x + y; nums.add(2, z); Which of the following represents the value of nums after the code segment has been executed? [20, 40, 90, 50] [10, 20, 30, 40, 50] [20, 30, 90, 50] [20, 40, 50, 90] [20, 30, 80, 50]

[20, 30, 90, 50]

Consider the following code segment: ArrayList<Integer> nums = new ArrayList<Integer>(); int sum = 10; while(nums.size() <= sum) { nums.add(sum); sum--; } for(int i = 0; i < nums.size(); i++) { if(nums.get(i) %5 == 0) { nums.remove(i); } } System.out.println(nums.toString()); What is printed when the code segment is executed? [9, 8, 7, 6, 5] [10, 9, 8, 7, 6] [9, 8, 7, 6] [9, 8, 7, 6, 4, 3] Nothing will print, as the while loop condition occurs infinitely.

[9, 8, 7, 6]

Consider the following code segment: ArrayList<String> scales = new ArrayList<String>(); scales.add("DO"); scales.add("RE"); scales.add("MI"); scales.add("FA"); scales.add("SO"); String swap = scales.get(2); scales.remove(2); String set = scales.remove(scales.size()-1); scales.add(scales.get(0)); scales.set(0,set); scales.add(scales.size()/2, swap); Which of the following represents the value of scales after the code has been executed? [SO, RE, FA, DO] [SO, RE, MI, FA, DO] [SO, RE, MI, DO] [FA, RE, MI, DO] [FA, SO, RE, MI, DO]

[SO, RE, MI, FA, DO]

We want to create a 2D double array with 6 rows and 7 columns and assign it to connectFour. Which of these is correct? double[][] connectFour = new double[6][7]; double[][] connectFour = new connectFour[6][7]; double[][] connectFour = double[6][7]; new double[6][7] connectFour; double[6][7] connectFour = new double[][];

double[ ][ ] connectFour = new double[6][7];

Given the following: double[][] something = { {2.5, 6.8, 8.3, 2.3, 0.0}, {6.1, 10.2, 1.3, -2.5, -9.9}, {1.1, 2.3, 5.8, 13.21, 34.55} }; We want to replace the row {6.1, 10.2, 1.3, -2.5, -9.9} with a completely new array that looks like {3.1, 4.1, 5.9, 2.6, 8.4}. Which of the following lines of code will accomplish this? double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4}; something[2] = temp; something[1] = {3.1, 4.1, 5.9, 2.6, 8.4}; something[2] = new {3.1, 4.1, 5.9, 2.6, 8.4}; double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4}; something[1] = temp; something[1][0] = 3.1; something[1][1] = 4.1; something[1][2] = 5.9; something[1][3] = 2.6; something[1][4] = 8.4;

double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4}; something[1] = temp;

Given the following: String[][] poem = { {"I", "am", "the", "cookie", "monster."}, {"Would", "you", "like", "a", "cookie?"}, {"COOOOKIE", "OM", "NOM", "NOM", "NOM"} }; Which of the following code fragments would produce the following output? I am the cookie monster. Would you like a cookie? COOOOKIE OM NOM NOM NOM. for (int line = 0; line < poem.length; line++) { for (int word = 0; word < poem.length; word++) { System.out.print(poem[line][word] + " "); } System.out.println(); } for (int line = 0; line < poem.length; line++) { for (int word = 0; word < poem[word].length; word++) { System.out.print(poem[line][word] + " "); } System.out.println(); } for (int line = 0; line < poem.length; line++) { for (int word = 0; word < poem[line].length; word++) { System.out.print(poem[line][word] + " "); } System.out.println(); } for (int line = 0; line < poem.length; line++) { for (int word = 0; word < poem[line].length; word++) { System.out.print(poem[word][line] + " "); } System.out.println(); } for (int line = 0; line < poem.length; line++) { for (int word = 0; word < poem[line].length; line++) { System.out.println(poem[line][word] + " "); } System.out.println(); }

for (int line = 0; line < poem.length; line++) { for (int word = 0; word < poem[line].length; word++) { System.out.print(poem[line][word] + " "); } System.out.println(); }

Which is the correct way to construct and assign a 2D array, with 8 rows and 10 columns, to the variable popcorn? int[8][10] popcorn; int[8][10] popcorn = new int[8][10]; int[][] popcorn = new int[8][10]; int[8][10] popcorn = new int[][]; int[][] popcorn = new int[][](8, 10);

int[ ][ ] popcorn = new int[8][10];

What would be a correct way to instantiate this 2D array? int[][] table = { 1, 0, 10, 0, 3, 8, 38, 0 }; Java int[][] table = { {1, 0, 10, 0}, {3, 8, 38, 0} }; Java int[][] table = { {1}, {0}, {10}, {0}, {3}, {8}, {38}, {0} }; Java int[][] table = { "1, 0, 10, 0", "3, 8, 38, 0" }; Java int[][] table = new int[1, 0, 10, 0][3, 8, 38, 0];

int[][] table = { {1, 0, 10, 0}, {3, 8, 38, 0} };

What is the proper way to get the number of items in an ArrayList called list? list.length list.size list.length() list.size()

list.size()

Given the following: double[][] something = { {2.5, 6.8, 8.3, 2.3, 0.0}, {6.1, 10.2, 1.3, -2.5, -9.9}, {1.1, 2.3, 5.8, 13.21, 34.55} }; How do you replace the value 13.21 with 8.8? something[2][3] = 8.8; something[2][4] = 8.8; something[13.21] = 8.8; something[3][4] = 8.8; something[3][3] = 8.8;

something[2][3] = 8.8;

Consider the following code segment, which is intended to display the word boards. String[][] twoLetters = {{"ab", "ac", "ad","ar","af"}, {"bo", "be", "bi", "ba", "bu"}, {"ca", "ce", "ck", "co", "cs"}, {"da", "ds", "do", "de", "di"}}; System.out.println(/*Missing Code*/); Which of the following can replace /*Missing Code*/ so the code segment works as intended? twoLetters[1][0] + twoLetters[0][3]+ twoLetters[3][1] twoLetters[0] + twoLetters[3]+ twoLetters[1] twoLetters[1][1] + twoLetters[0][4]+ twoLetters[3][2] twoLetters[0][1] + twoLetters[3][0]+ twoLetters[1][3] twoLetters[1][0] + twoLetters[0][3]+ twoLetters[twoLetters.length][1]

twoLetters[1][0] + twoLetters[0][3]+ twoLetters[3][1]

Consider the following code segment, which is intended to create and initialize the 2D array words where the length of each word corresponds to the product of the indices of the row and the column it resides in. String[][] words = /*missing code */; Which of the following initializer lists could replace /*missing code*/ so that the code segment works as intended? {{"", "a", "as"}, {"", "b", "be"}, {"", "d", "don"}} {{"a", "as", "ask"}, {"b", "be", "bet"}, {"d", "do", "don"}} {{"", "", ""}, {"", "b", "be"}, {"", "do", "dont"}} {{"a", "a", "a"}, {"", "b", "be"}, {"d", "do", "dont"}} {{"", "", ""}, {"", "b", "be"}, {"", "d", "do"}}

{{"", "", ""}, {"", "b", "be"}, {"", "do", "dont"}}


Related study sets

Cambridge English Advanced unit 1 reading

View Set

Understanding the European Economy

View Set

Kappa Alpha Psi Session 5 Review Questions

View Set

Psych Stats Exam 2 Hilmire at William and Mary

View Set

Geriatric Exam 1 NCLEX Practice Questions

View Set

Psychology Chapter 14 Test 3. 9-11, 14

View Set

6.6.7 Practice Questions IP Configuration

View Set

Chapter 10 (trucks and hauling Equipment)

View Set