CS 160 Midterm Review

¡Supera tus tareas y exámenes ahora con Quizwiz!

Which XXX is the proper default constructor? public class Employee { private double mySalary; XXX }

public Employee( ) { mySalary = 10000; }

Select mutator XXX to set the temperature. public class WeatherData { private double myTemp; XXX }

public void setTemp(double temp) { myTemp = temp; }

How many elements does the array declaration create? int[] scores = new int[10]; scores[0] = 25; scores[1] = 22; scores[2] = 18; scores[3] = 28;

10

What is output? ArrayList<Integer> numList = new ArrayList<Integer>(); int count; for(count = 1; count < 10; ++count) { numList.add(count); } System.out.println(numList.get(4));

5

How many elements are in the array? int[][] userVals = new int[2][4];

8

The keyword this ___.

Implicitly refers to an object within instance methods

How many references are declared? Dog labrador = new Dog(); Dog poodle; Dog beagle = new Dog(); poodle = beagle; poodle = labrador;

3

What is the ending value of the element at index 0? int[] numbers = new int[10]; numbers[0] = 35; numbers[1] = 37; numbers[1] = numbers[0] + 4;

35

Which is true? A private helper method can be called by a class user A private helper method can call public methods in the same class A private helper method can not call other private methods in the same class A private helper method can be called from main( ) in another class

A private helper method can call public methods in the same class

8) A subclass class can directly access A) Only public and protected members of the superclass class B) Only public and private members of the superclass class C) Only protected and private members of the superclass class D) All members of the superclass class

A) Only public and protected members of the superclass class

7) If you do not provide an access specifier for a class member, the class member is given this access by default. A) package B) public C) private D) protected

A) package

9) Which of the following statements should be used to complete the setDay method? A) newDay = day; B) day = newDay; C) int newDay = day; D) int day = newDay;

B) day = newDay;

2) What is wrong with the following method call? displayValue (double x); A) There is nothing wrong with the statement B) x should be a string C) The data type of the argument x should not be included in the method call D) displayValue will not accept a parameter

C) The data type of the argument x should not be included in the method call

Which is true? public class Vehicle { protected String name; private int ID; } public class Car extends Vehicle { protected int miles; }

Car members have access to Vehicle name

3) Suppose your method does not return any value, which of the following keywords can be used as a return type? A) int B) double C) public D) void E) None of the above

D) void

What is output? public class UnitTest{ public int square(int value) { return value * 1; } public static void main(String[] args) { UnitTest testObject = new UnitTest(); if(testObject.square(3) != 9) { System.out.println("Error: square(3)"); } if(testObject.square(1) != 1) { System.out.println("Error: square(1)"); } if(testObject.square(-1) != 1) { System.out.println("Error: square(-1)"); } System.out.println("Tests complete."); } } }

Error: square(3) Error: square(-1) Tests complete.

What is output? public class KitchenAppliance { protected String appName; protected String appUse; public void setDetails(String name, String use) { appName = name; appUse = use; } public void printDetails() { System.out.println("Name: " + appName); System.out.println("Use: " + appUse); } } public class Blender extends KitchenAppliance { private double appPrice; void setPrice(double price) { appPrice = price; } public void printDetails () { super.printDetails(); System.out.println("Price: $" + appPrice); } public static void main(String [] args) { Blender mxCompany = new Blender(); mxCompany.setDetails("Blender", "blends food"); mxCompany.setPrice(145.99); mxCompany.printDetails(); } }

Name: MyMixer Use: blend food Price: $145.99

___ refers to determining which program behavior to execute depending on data types.

Polymorphism

The following program generates an error. Why? final int NUM_ELEMENTS = 5; int[] userVals = new int[NUM_ELEMENTS]; int i; userVals[0] = 1; userVals[1] = 7; userVals[2] = 4; for (i = 0; i <= userVals.length; ++i) { System.out.println(userVals[i]); }

The for loop tries to access an index that is out of the array's valid range.

6) What would be the results of the following code? int[] x = { 55, 33, 88, 22, 99, 11, 44, 66, 77 }; int a = 10; if(x[2] > x[5]) a = 5; else a = 8; a. a = 5 b. a = 8 c. a = 10 d. This is a compilation error, you cannot compare array elements

a. a = 5

1) If a program compiles fine, but it produces incorrect result, then the program suffers ________. a.a logic error. b.a compilation error. c.a runtime error. d. a migraine headache.

a.a logic error.

5) Which of the following would be a valid method call for the following method? public void showProduct(double num1, int num2) { double product; product = num1 * num2; System.out.println("The product is " + product); } a. showProduct("5", "40"); b. showProduct(10.0, 4.6); c. showProduct(10, 4.5); d. showProduct(3.3, 55);

d. showProduct(3.3, 55);

4) Which of the following lists contains all Java keywords? a. string, case, import, finally b. Class, Double, Integer, String, Math c. public, static, void, main d. try, throw, catch, final

d. try, throw, catch, final

The built-in Object class _____.

defines multiple methods including toString() and equals()

Which replaces "Apples" with "Bananas"? ArrayList<String> groceryList; groceryList = new ArrayList<String>(); groceryList.add("Bread"); groceryList.add("Apples"); groceryList.add("Grape Jelly");

groceryList.set(1, "Bananas");

Given class SimpleCar, which line has a syntax error? public class SimpleCar { private int odometer; public void drive(int miles) { odometer = odometer + miles; } } 1 Object objCar; 2 objCar = new SimpleCar(); 3 System.out.println(objCar.toString()); 4 objCar.drive();

line 4 - drive( ) is not defined in class Object

Which comparison should not be used? Integer score1 = 20; Integer score2 = 30; int score3 = 40;

score1 == score2

Autoboxing is ___.

the automatic conversion of primitive types to the corresponding wrapper classes

Given x = 4 and y = 8, what are the ending values of x and y? x = y; y = x; x = y;

x = 8, y = 8


Conjuntos de estudio relacionados

تربية اسلامية توجيهي

View Set

Health Insurance - Chapter 7: Health Insurance Underwriting

View Set

VNSG 1323: Chapter 24 Prep U Questions

View Set

Chapter 52: Assessment and Management of Patients With Endocrine Disorders

View Set

Manhattan Advanced 500 with Mnemonic

View Set