Chapter 1: Declaration and Access control Flashcards

1
Q

What are valid starting characters for identifiers?

A

letters, $, _

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

True or False: foo and FOO are the same identifiers.

A

False

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

Name the valid getter methods for a boolean.

A

getBoolean, isBoolean

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

Name the JavaBean methods for MyListener.

A

addMyListener(MyListener listener)

removeMyListener(MyListener listener)

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

Name the access modifiers starting with the most restrictive

A
private -> class only
default -> package only 
protected -> inheritance and package only
public -> anywhere
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the non-access modifiers for classes?

A

final, abstract, strictfp

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
What is wrong?
class MyObject {
    public abstract void myMethod();
}
A

Both the class and the method should have a abstract modifier.

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

True or False: All methods in an interface are public abstract.

A

True

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

What are variables declared in a interface?

A

public static final

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
What is wrong?
class foo {
   void doStuff() {
      private final int x = 7;
   }
}
A

Access modifiers can’t be used in conjuction with local variables.

Final is the only non-acces modifier that can be used

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

Synchronized key word can be applied to:

A

methods & code-blocks

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

Which of these methods are valid var-arg methods:

1: void doStuff(int x…)
2: void doStuff(int… x, int… y)
3. void doStuff(int… x)
4. void doStuff(int… x, int y)
5. void doStuff(int y, Animal… a)

A

3 & 5

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

Name the 8 primitives

A

byte, short, int, long, float, double, boolean, char

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

Describe the integer types from small to large

A

byte: 8 bits (2^7) (we lose one bit for the sign (-/+))
short: 16 bits (2^15)
int: 32 bits (2^31)
long: 64 bits (2^63)

float: 32 bits
double: 64 bits

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

Describe the max range for a char

A

char: 16 bits
No signing bit
2^16 = 0-65535

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

What are valid non-access modifiers for instance variables?

A

final and transient

17
Q

What is wrong?

public class MyClass {
   public void doStuff() {
      int a;
      a++;
   }
}
A

local variables should be initialized (don’t get a default value)

18
Q

What is shadowing?

A

declaring a local variable with the same name as an instance variable.

19
Q

Name the two ways of declaring an array.

A

type[] key (for example: int[] a)

type key[] (for example: int a[])

20
Q

What does the transient keyword?

A

exclude a instance variable from serialization

21
Q

What are valid acces modifiers for classes?

A

pulbic and default

22
Q

What will be the result?

class TestClass
{
   int i = getInt();
   int k = 20;
   public int getInt() {  return k+1;  }
   public static void main(String[] args)
   {
      TestClass t = new TestClass();
      System.out.println(t.i+"  "+t.k);
   }
}
A

Answer: 1 20 is printed

First the memory is allocated for TestClass Object referred to by variable t. Here, all the variables get their default values. so i and k are both zeros.
Now, the initialilization starts. To initialize i it calls getInt(). This method returns k+1. At this point of time ‘k’ is 0. (because it has not been initialized yet.) so it returns 1. Next, k is init’ed to 20. So, 1 20 is printed.
Note that had k = 20; occured before i = getInt(); it would have printed 21 20.