Chapter 5: Methods Flashcards

1
Q

Which statements about the final modifier are correct?

Instance and static variables can be marked final

A primitive marked as final cannot be modified

A

both statements are correct

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

what can fill in the blank?
public class Ant {
______ void method() {}
}

final
private

A

void is a return type, only the access modifier or optional specifiers are allowed before the return type

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

which compile?

final static void rain() {}
static final void sleet() {}

A

both
optional specifiers ‘final’ or ‘static’ are allowed in any order

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

fill in the blank

final _____ song = 6;

int
Integer
long
double

A

the value ‘6’ can be promoted to any of the primitive types meaning int, long and double are all correct

it can be autoboxed to Integer

it can’t be both promoted and autoboxed so can’t become a Double or a Long.

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

which compiles?

public void january() { return;}

A

a void method is an allowed an optional return statement as long as it doesn’t try to return anything.

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

which compiles?

public void violin(int… nums) {};
public void violin2(String values, int… nums) {};
public void violin3(String… values, int… nums) {};

public void oboe(String[] values, int[] nums) {};
public void oboe2(String… values, int… nums) {};

A

violin, violin2: the single varargs param is the last param declared

and oboe: doesn’t use any varargs param

2 varargs aren’t allowed in the same method

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

given the following method, which method calls return 2

public int juggle(boolean b, boolean… b2) {return b2.length; }

A. juggle (true, new boolean[2]);
B. juggle(true, true, true);

A

A. just gives a straight array of length 2, which varargs (boolean…) will accept
B. passes in the initial first param + 2 more which turn into a varargs array of size 2

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

Which of the following statements is correct?

D. You can use access modifiers to allow access to all methods and not any instance variables

a. Package access is more lenient than protected access
b. a public class w/ private fields and package methods is not visible to classes outside of the package
c. you can use access modifiers so only some of the classes in a package see a particular package class

e. you can use access modifiers to restrict access to all classes that begin with the word Test

A

D is correct.
A common practice is to set all fields to be private and all methods to be public.

A: incorrect because protected allows everything that package access allows and additionally allows subclass access.
B: incorrect: the class is public, = other classes can see the class. however, w/ private fields & package methods, those other classes cannot call any of the methods or read any of the fields. Essentially a useless class.
C: incorrect because package access applies to the whole package
e: incorrect because java has no such wildcard access capability

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

Given the following class, which lines in the main() method generate a compiler error?

// Classroom.java
package my.school;
public class Classroom {
    private int roomNumber;
    protected static String teacherName;
    static int globalKey = 54321;;
    public static int floor = 3;
    Classroom (int r, String t) { 
        roomNumber = r;
        teacherName = t; }   }
// School.java
1: package my.city;
2: import my.school.*;
3: public class School {
4: main method() {
5:         print(Classroom.globalKey);
6.         Classroom room = new Classroom(101, "Ms Anderson");
7.         print (room.roomNumber);
8.         print(Classroom.floor);
9.         print(Classroom.teachName); }   }
A

Line 5, 6, 7:
The 2 classes are in different packages, which means private access and package access will not compile. Causing compiler errors on lines 5, 6, 7.

Line 9
protected access will not compile since School does not inherit from Classroom. This causes the compiler error on line 9

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

output of executing the Chimp program?

//Rope.java
public class Rope { 
public static int LENGTH = 5;
static { LENGTH = 10; }
public static void swing() { print("swing") };
}

// Chimp.java
import rope.*;
import static rope.Rope.*;
public class Chimp {
    main method() {
		Rope.swing();
        new Rope().swing();
        print(LENGTH)
	}  }
A

swing swing 10
LENGTH is set to 5, then immediately after runs the static initializer, setting it to 10.
Chimp calls the static method normally and prints swing
then it calls it again.
java allows calling a static method through an instance variable, although it is not recommended.

you get away with print(LENGTH) because you imported the value with

import static rope.Rope.*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Compiler error

public class Example {
public static void x() {};
public void y() {};
public static void z() { x(); y();}
main method
}

A

z() is a static method calling a non-static instance method.
Illegal. Compiler error.

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

How many variables in the following method are effectively final?

public void x() {
int a = 0;
if( a > 0) {
var b = a++;
String c;
c = “c”;
}
String c = “c2”;
var d = 10;
while (a <= 10) { d = 0; }
c = null;
}

A

2
the test for effectively final is adding final to the code and seeing if the code still compiles

a isn’t effectively final because it gets modified

b and String c within the if block are effectively final as local variables that are set, never modified, and go out of scope

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

output?

`import rope.;
import static rope.Rope.
;
public class RopeSwing {
private static Rope rope1 = new Rope();
private static Rope rope2 = new Rope();
{ print rope1.length }

main method { rope1.length = 2; rope2.length =3;
print rope1.length;
}

// Rope.java
package rope;
public class Rope { public static int length = 0 };
`

A

3
there are two things to notice here
RopeSwing has an instance initializer and not a static initializer. Since RopeSwing is never constructed, the instance initializer does not run.
i.e. { print rope1.length } doesn’t have static in front of it. So it’s an instance block, but no

new RopeSwing
is ever created, therefore that block doesn’t run. Only the main method runs, and that updates length before printing, see below —-v

length is static. Changes from any object update this common static variable.
So rope1.length = 2; rope2.length = 3 just means that the static length variable found in Rope.java is 3.
The code prints 3

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

If a variable is static final, what properties must be in place for it to compile?

A

it must be set exactly once, it must be in the declaration line or in a static initialization block.

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

what can replace line 2 to make the code compile?

1: import java.util.*;
2: // insert here
3: public class Imports {
4: public void method( ArrayList<String> list) {
sort(list);
} }
A

import static java.util.Collections.*;

import static java.util.Collections.sort;

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