Be Prepared Ch 3-4 - Java Features Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

arguments and parameters

A

the values passed in a method call (in the parentheses) The words Argument and parameters are used interchangeably

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The concept of “privacy” applies to the ____________ and not to individual objects. Different objects of the same class have full access to each other’s fields and methods.

A

class as a whole

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

• Requierement on ap exam to make all instance variables _____

A

private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

• Instance vs static variables

A

object vs class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

static method rules

A

§ Static methods cannot access or modify any instanec variables and cannot refer to this (a reference to a particular object) because this is undefined when a static method is running. Static variables can be initialized in a class ocnstructor and can be accessed and modified in instance methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Should you print the return value using System.out from inside the method (when it is not requested)?

A

NO. • It is a minor error (-1/2) to print the return value to a System.out from inside the method (when it is not requested)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Parameters of primitive data types are passed to methods ___________

A

“by value”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

• Given § Public void fun(int a, int b){ □ a+=b; □ b+=a; § } • The output from the following code: § Int x = 3, y = 5; § Fun(x,y); § System.out.println(x + “ “ + y); • Will print ____________

A

“3 5” • This is beause x and y are ints, so they are passed to fun by value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

§ All objects are passed to methods as _____. A method receives a _______ to (the address of ) the object

A

references a copy of a reference

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

immutable objects

A

objects without modifier methods String, Integer, and Double classes For example, there is no way in Java to write a method □ Public void toUpperCase(String s) { . . . } § Because the method has no way to change the string passed to it. For immutable objects, the method has to create and return a new object with the desired properties: □ Public String toUpperCase(String s) { . . .}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

§ A method whose return type is a class can also return

A

a null (a reference with a zero value that indicates that it does not refer to any valid object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

§ If a method returns an ArrayList, write the

A

full ArrayList type, including its elements’ type in angle brackets, as the method’s return type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

• The compiler treats overloaded methods as ________.

A

different methods It figures out whichh one to call depending on the number and types of the parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Random Numbers [1,n]

A

• Int r = (int) (n * Math.random()) + 1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Input and Output

A

• Ap subset does not include any classes or methods for data input. The Scanner class is not in the AP subset. • If a question ivolves user input it may be stated as follows: § Double x = < call to a method that reads a floating-point number> § Or § Int x = IO.readInt(); //reads user input

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

• An exception

A

is a run-time event that signals an abnormal condition in the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Checked Exceptions

A

• Some run-time errors, such as invalid user input or an attempt to read past the end of a file, are considered fixable,. The try-catch-finally syntax allows the programmer to catch and process the exception and have the program recover. This type of exception is called a checked exception § Checked exceptions and the try-catch statements are not in the AP subset.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Unchecked exceptions

A

• Other errors, such as an array index out of bounds or an attempt to call a method of a non-existing objet (null reference) are considere fataL: the program displays an error message with information about where the error occurred, then quits. This type of exception is called an unchecked exception.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

• In Java, an exception is ____________

A

an object. The Jaba library impletments many types of exceptions, and if necessary you can derive your own exception class from one of the library classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Exceptions to understand

A

§ (ArithemeticException), § IndexOutOfBoundsException, - what ArrayList methods throw § (ArrayIndexOutOfBoundsException) - what Arrays throw § (NullPointerException), § IllegalArgumentException, and § (ClassCastException) § Java automatically throws the bolded exception listed in parentheses, when the trigegering condition occurs, (meaning your code does not need to explicitly throw an ArithemeticException)

21
Q

• Throwing your own exceptions

A

§ Sometimes you need to “throw” your own exceptions, e.g. such as NoSuchElementException or IllegalStateException § If (items.size() ==0) □ “throw new NoSuchElementException();” § If an object is not ready for a particular method call □ Public void stop() □ { ® If (!isMoving()) ◊ Throw new IllegalStateException(); //displays a message and quits ® Speed = 0; ® … § }

22
Q

• Procedural abstraction

A
  • eg #1. Java isolates a developer from a particular platform. e.g #2 : an algorithm: can be described using pseudocode, flowcharts, or other tools independent of any particular programming language
23
Q

• You can write ____class object = new ___class();

A

super; sub § e.g. □ School sch - new HighSchool();

24
Q

• A superclass’s private methods ______ callable in subclass

A

are not

25
Q

• Constructors _____ inherited: a subclass ______ provide its own

A

are not has to

26
Q

• A subclass’s constructors can explicity call the superclass’s constructors using the keyword ____

A

super

27
Q

§ If super is used, it must be the ___________________ in the subclass’s constructor

A

first statement

28
Q

• A subclass inherits all the class (static) variables and instance variables of its supercalss. However, the instance variables are usually private, thus they are ____

A

>>not directly accessible in its subclass, so you have to use the public accessors and modifiers

29
Q

• Superclass public constants (public static final) are

A

directly accessible everywhere

30
Q

• Abstract classes can have constructors and methods; however, some of its methods may be declared abstract and left without code

A

§ Public abstract class Solid{ □ …public abstract double getVolume(); □ …}

31
Q

• A class in which all the methods are defined is called

A

concrete.

32
Q

• You cannot instantiate an abstract class, but you can ___________

A

declare variables or arrays of its type § Solid s1 = new sphere(radius); § Solid s2 = new cube(side); § Solid[] solids = {new Sphere(100), new cube(100)};

33
Q

§ it seems pointless to have a pure abstract class (e.g. an abstract class where every method and variable is abstract). It would make more sense to use an ____

A

interface

34
Q

§ Also interfaces can extend _______

A

interfaces, but not other classes (not even abstract classes, or purely abstract classes)

35
Q

• Polymorphism

A

§ Is a mechanism that ensures that the ocrrect method is called for an object disguised as a more generic type. § For(Solid solid :solids) □ totalVolume+= solid.getVolume(); //for different shapes. spheres pyramids boxes etc.

36
Q

an interface is different from a class in several ways, including:

A

You cannot instantiate an interface. An interface does not contain any constructors. All of the methods in an interface are abstract. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. An interface is not extended by a class; it is implemented by a class. An interface can extend multiple interfaces.

37
Q

§ Public interface fillable{ □ Void fill(int x); □ Int getCurrentAmount(); □ Int getMaximumCapacity(); § } are those methods public abstract or something else?

A

They are implicitly understood as public abstract // no need to repeat public abstract for each method in an interface - it is understood

38
Q

§ A class implements an interface by

A

supplying code for all the interface methods. □ Public class Car implements Fillable{ □ …. □ Public void fill(int gallons) { fuelAmount += gallons;} □ Public int getCurrentAmount() { return fuelAmount;} □ Public int getMaximumcapacity() {return fuelTankCapacity;} □ }

39
Q

§ A class that claims it implements an interface but does not define some of the interface methods must be declared

A

abstract

40
Q

§ A class can extend only one class, but it can implement

A

several interfaces. □ Public class Car extends Vehicle implements Fillable, Sellable { . . .}

41
Q

□ Each interface adds a secondary data type to the objects of the class that implements it. If a class C implements interface I, objects of C can

A

be disguised as type I objects and polymorphism applies ( the same way as for subclasses). For example, we can have a method: ® Public void fillUp(fillable[] a) ® { ◊ For (Fillable f:a) } f.fill (f.getMaximumCapacity() - f.getCurrentAmount()); ® } ® The formula works polymorphically for any types of different Fillable objects tha might be stored in the arra a ®

42
Q

If class C implements interface I,

A

all subclasses of C automatically implement I.

43
Q

§ Comparable interface

A

□ Built in library: java.lang.Comparable □ Used to compare objects for various methods □ Specifies only one method: ® Int compareTo(T other);

44
Q

Comparable interface ® Int compareTo(T other);

A

◊ Returns a positive integer if this object is greater than the other, 0 if same, - if less ◊ Up to programmer to decide what smaller and greater mean when comparing objects of his class ◊ Takes one parameter of the type T - and it is usually assumed that the parameter belongs to the same class that implements Comparable } Public class flight implements Comparable } { – … – Public int compareTo(Flight other) – { – Return getDegartureTime() - other.getDepartureTime(); – } } } ◊ String, Integer, and bouble all implement Comparable, but naturally do so in different ways such as lexicographically vs or numerically

45
Q

◊ The comparable interface does not specify an equals method, and a class that implements comparable does not need an equals method to complie. But it is better to override the equals method inherited from Object and make it consistent with compareTo, to avoid possible errors later. For example:

A

Public boolean equals(Object x) } { } Return x instanceof Flight && compareTo((Flight)x) == 0; } } } The boolean operator instanceof is not in the AP subset. } Note that the argument to your equals method should have the type Object, so that your equals indeed overrides Object’s equals.

46
Q

• For each loop

A

§ For(T x: a){ □ System.out.println(x); § } § Where t is the data type of the elements of a

47
Q

If a constructor does not explicitly invoke a superclass constructor,

A

the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

48
Q

Breaking out of nested loops using labels

A