AP Computer Science Chapter 4 & 5 Test

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

objects can represent...

- real world objects - GUI components - software entities - abstract concepts (ex.: rules of a game)

what extension are classes/source files?

.java

What is a field?

A field is a data item in an object. A field describes an attribute or a variable value in an object

What is a method?

A method is a fragment of code that performs a certain calculation or task and can be called from other constructors and methods.

What is a variable?

A named container that can hold a value

Consider the following methods, which appear in the same class. public void slope(int x1, int y1, int x2, int y2) { int xChange = x2 - x1; int yChange = y2 - y1; printFraction(yChange, xChange); } public void printFraction(int numerator, int denominator) { System.out.print(numerator + "/" + denominator); } Assume that the method call slope(1, 2, 5, 10) appears in a method in the same class. What is printed as a result of the method call? A: 8/4 B: 5/1 C: 4/8 D: 2/1 E: 1/5

A: 8/4

Consider the following method. public void doSomething() { System.out.println("Something has been done"); } Each of the following statements appears in a method in the same class as doSomething. Which of the following statements are valid uses of the method doSomething ? doSomething(); String output = doSomething(); System.out.println(doSomething()); A: I only B: II only C: I and II only D: I and III only E: I, II, and III

A: I only

Consider the following methods, which appear in the same class. public int function1(int i, int j) { return i + j; } public int function2(int i, int j) { return j - i; } Which of the following statements, if located in a method in the same class, will initialize the variablex to 11? A: int x = function2(4, 5) + function1(1, 3); B: int x = function1(4, 5) + function2(1, 3); C: int x = function1(4, 5) + function2(3, 1); D: int x = function1(3, 1) + function2(4, 5); E: int x = function2(3, 1) + function1(4, 5);

B: int x = function1(4, 5) + function2(1, 3);

A student has created an OrderedPair class to represent points on an xy-plane. The class contains the following. An int variable called x to represent an x-coordinate. An int variable called y to represent a y-coordinate. A method called printXY that will print the values of x and y. The object origin will be declared as type OrderedPair. Which of the following descriptions is accurate? A: origin is an instance of the printXY method. B: origin is an instance of the OrderedPair class. C: origin is an instance of two int objects. D: OrderedPair is an instance of the origin object. E: printXY is an instance of the OrderedPair class.

B: origin is an instance of the OrderedPair class.

Consider the following method. public double myMethod(int a, boolean b) { /* implementation not shown */ } Which of the following lines of code, if located in a method in the same class as myMethod, will compile without error? A: int result = myMethod(2, false); B: int result = myMethod(2.5, true); C: double result = myMethod(0, false); D: double result = myMethod(true, 10); E: double result = myMethod(2.5, true);

C: double result = myMethod(0, false);

A student has created a Song class. The class contains the following variables. A String variable called artist to represent the artist name A String variable called title to represent the song title A String variable called album to represent the album title The object happyBirthday will be declared as type Song. Which of the following statements is true? A: artist, title, and album are instances of the Song class. B: happyBirthday is an instance of three String objects. C: happyBirthday is an instance of the Song class. D: Song is an instance of the happyBirthday object. E: Song is an instance of three String objects.

C: happyBirthday is an instance of the Song class.

Consider the following class definition. public class Thing { public void talk() { System.out.print("Hello "); } public void name() { System.out.print("my friend"); } public void greet() { talk(); name(); } /* Constructors not shown */ } Which of the following code segments, if located in a method in a class other than Thing, will cause the message "Hello my friend" to be printed? A: Thing a = new Thing(); Thing.talk(); Thing.name(); B: Thing a = new Thing(); Thing.greet(); C: Thing a = new Thing(); a.talk(); D: Thing a = new Thing(); a.greet(); E: Thing a = new Thing(); a.name(); a.talk();

D: Thing a = new Thing(); a.greet();

Consider the following class definition. public class ExamScore { private String studentId; private double score; public ExamScore(String sid, double s) { studentId = sid; score = s; } public double getScore() { return score; } public void bonus(int b) { score += score * b/100.0; } } Assume that the following code segment appears in a class other than ExamScore. ExamScore es = new ExamScore("12345", 80.0); es.bonus(5); System.out.println(es.getScore()); What is printed as a result of executing the code segment? A: 4.0 B: 5.0 C: 80.0 D: 84.0 E: 85.0

D: 84.0

A student has created a Car class. The class contains variables to represent the following. A String variable called color to represent the color of the car An int variable called year to represent the year the car was made A String variable called make to represent the manufacturer of the car A String variable called model to represent the model of the car The object vehicle will be declared as type Car. Which of the following descriptions is accurate? A: An instance of the vehicle class is Car. B: An instance of the Car object is vehicle. C: An attribute of the year object is int. D: An attribute of the vehicle object is color. E: An attribute of the Car instance is vehicle.

D: An attribute of the vehicle object is color.

Consider the following class declaration. public class GameClass { private int numPlayers; private boolean gameOver; public Game() { numPlayers = 1; gameOver = false; } public void addPlayer() { numPlayers++; } public void endGame() { gameOver = true; } } Assume that the GameClass object game has been properly declared and initialized in a method in a class other than GameClass. Which of the following statements are valid? I. game.numPlayers++; II. game.addPlayer(); III. game.gameOver(); IV. game.endGame(); A: IV only B: I and III only C: I and IV only D: II and IV only E: II, III, and IV only

D: II and IV only

Which of the following statements assigns a random integer between 1 and 10, inclusive, to rn ? A: int rn = (int) (Math.random()) * 10; B: int rn = (int) (Math.random()) * 10 + 1; C: int rn = (int) (Math.random() * 10); D: int rn = (int) (Math.random() * 10) + 1; E: int rn = (int) (Math.random() + 1) * 10;

D: int rn = (int) (Math.random() * 10) + 1;

public class Student { private int studentID; private int gradeLevel; private boolean honorRoll; public Student(int s, int g) { studentID = s; gradeLevel = g; honorRoll = false; } public Student(int s) { studentID = s; gradeLevel = 9; honorRoll = false; } } Which of the following code segments would successfully create a new Student object? I. Student one = new Student(328564, 11); II. Student two = new Student(238783); III. int id = 392349;int grade = 11;Student three = new Student(id, grade); A: I only B: II only C: III only D: I and II only E: I, II, and III

E: I, II, and III

Consider the following Point2D class. public class Point2D { private double xCoord; private double yCoord; public Point2D(double x, double y) { xCoord = x; yCoord = y; } } Which of the following code segments, appearing in a class other than Point2D, will correctly create an instance of a Point2D object? A: Point2D p = (3.0, 4.0); B: Point2D p = Point2D(3.0, 4.0); C: new p = Point2D(3.0, 4.0); D: new Point2D = p(3.0, 4.0); E: Point2D p = new Point2D(3.0, 4.0);

E: Point2D p = new Point2D(3.0, 4.0);

The Student class has been defined to store and manipulate grades for an individual student. The following methods have been defined for the class. /* Returns the sum of all of the student's grades */ public double sumOfGrades() { /* implementation not shown */ } /* Returns the total number of grades the student has received */ public int numberOfGrades() { /* implementation not shown */ } /* Returns the lowest grade the student has received */ public double lowestGrade() { /* implementation not shown */ } Which of the following statements, if located in a method in the Student class, will determine the average of all of the student's grades except for the lowest grade and store the result in the double variable newAverage ? A: newAverage = sumOfGrades() / numberOfGrades() - 1; B: newAverage = sumOfGrades() / (numberOfGrades() - 1); C: newAverage = sumOfGrades() - lowestGrade() / (numberOfGrades() - 1); D: newAverage = (sumOfGrades() - lowestGrade()) / numberOfGrades() - 1; E: newAverage = (sumOfGrades() - lowestGrade()) / (numberOfGrades() - 1);

E: newAverage = (sumOfGrades() - lowestGrade()) / (numberOfGrades() - 1);

What is the scope of a local variable?

From its declaration down to the closing brace of the block in which it is declared

Is it OK to give the same name to a field and to a local variable of the same class?

Never.

What is the difference between private and public methods?

Private methods can be called only from constructors or methods of the same class. Public methods can be called from other classes as well.

Why are fields usually private?

So that a programmer can change the number, names, and types of fields without affecting other classes in the project.

What is meant by the scope of a variable?

The area in the source code where the variable is visible

What is the type of a variable that holds an object?

The class to which the object belongs

What is the scope of a field?

The whole class

What are import statements used for?

To be able to use short names for library classes instead of fully-qualified names.

When is a cast to double used?

When the operands are ints but we want the correct result as a double. (Be sure to cast an individual operand, not the final result.

Is it OK to give the same name to variables in different methods?

Yes, and moreover it is desirable if their roles are similar.

Class

a piece of the program's source code that describes a particular type of objects

an object can call ____________ _____________'s __________

another object's methods

Fields have a ....

data type

a variable must be _______________ before it is used

declared

Class ________ an object

defines

What is the range for ints?

from -231 to 231 - 1

scope of a local variable

from its declaration down to the closing brace of the block

Fields are usually _____________ ____________ at the top or bottom of the class

grouped together

wildcard ( .* )

imports names for all the classes in a package

Constructors ___________ an object's fields and take ____________

initialize, take parameters

An object is called an ___________ of a class

instance

Fields are also known as...

instance variables

_________._______ is imported automatically into all classes; defines Sytem, Math, Object, String, and other commonly used classes.

java.lang

Java programs are not usually written from scratch. You can get classes from ___________

libraries

variables usually start with a _______________ letter

lowercase

void

method does not return any value

Which operator is used to construct an object?

new

In a method, the _________ and _________ of parameters must match the method's parameters

number and type

variables can hold __________; the type is the ________ of the object

objects, class

an object can create ____________ ____________

other objects

Fields are declared ___________ all contractors and methods

outside

you cannot use a variable ____________ of its scope

outside

fields are usually __________

private

import statements at the top of the source file let you...

refer to library classes by their short names

Constructors always have the ________ _________ as the class

same name

What is a constructor?

short procedure for creating objects of a class

the scope of a field is the ___________ ___________

whole class


Kaugnay na mga set ng pag-aaral

Business Law Chapter 16 Questions

View Set

HESI Health and Physical Assessment

View Set

Anatomy and Physiology 2 Respiratory Homework

View Set

Management of Information Security CYBR 3300 - Chapter 10 Planning for Contingencies

View Set

Beyond basic Java -- Java Implementations/Servlets/JSPs/JDBC/JavaDocs

View Set