Shared Flashcard Set

Details

SCJP For Java 5
Flash Cards For SCJP Java 5
49
Computer Science
Professional
04/22/2007

Additional Computer Science Flashcards

 


 

Cards

Term
What can valid identifiers begin with?
Definition
Letter, underscore or currently symbol.
Term
What is the maximum length of an identifier.
Definition
Any
Term
JavaBeans must be named using _______ and depending on the methods purpose must start with ___, ___, __, ___, or ______.
Definition
JavaBeans must be named using camelCase and depending on the methods purpose must start with get, set, is, add, or remove.
Term
How many public classes can a source file have?
Definition
Only one
Term
How many package statements are allowed and where must they be?
Definition
Only one is allowed and it must be the first statement in the file.
Term
Where must the import statement go?
Definition
At the begining of the file AFTER the package statement (if present) otherwise it must be the first statement in the file.
Term
How many public classes can be in a source file?
Definition
Only one.
Term
True/False there can be only one top level class.
Definition
False, you can have many top level classes, but only one Public class.
Term
True/False When a thread is sleeping it releases its locks.
Definition
False, Locks are only released on wait()
Term
What access modifiers can interfaces have for declarations?
Definition
Public and default only.
Term
Will This Compile?
interface Arachnid{ void bite();}
public class BlackWidow implements Arachnid{
void bite();}
Definition
No, the interfaces are declared as public whether they say so or not. The class then changes it to default (package) access. It will not compile.
Term
What is the output?
int k=8;
int s=0;
for(int i=5;i>2;i--){
   s=k-i;
   break;
}
System.out.println(s);
Definition
S would be 3.

It's easy to think that S might be 4 instead abut the postfix operator doesnt occur until AFTER the code block has executed.
Term
What is the result of compilation?
class Fuel{
  static int getFuel(){return 23}
}
class Gas extends Fuel{
 public static void main(String[] args){
   System.out.println(super.getFuel());
}
}
Definition
Compilation fails.

Only instance methods can be overridden. Calls to super only apply to overridden methods.
Term
Will this compile?
List<Number> numbers = new ArrayList<Integer>();
Definition
No, Interger is a subtype of Number.
Term
Will this compile?
List<JButton> numbers = new ArrayList<JButton>();
Definition
Yes
Term
What will
accept? 1. Animal 2. Dog 3. Mammal 4. Cat
Definition
Animal, Dog, Cat

Since Mammal does not extend from Animal and is a supertype of Animal it will fail type checking.
Term
What will
accept? 1. Animal 2. Dog 3. Mammal 4. Cat
Definition
Animal, Mammal, Dog

Since Cat is not a supertpe of Dog it will fail type checking.
Term
Will this compile?
1) List list = new ArrayList();
Definition
Yes
Term
Will this compile?
2) List aList = new ArrayList();
Definition
Yes
Term
Will this compile?
3) List foo = new ArrayList();
Definition
No.
Problem: you cannot use wildcard notation in the object creation. So the
new ArrayList() will not compile.
Term
Will this compile?
4) List cList = new ArrayList();
Definition
No

Problem: You cannot assign an Integer list to a reference that takes only a
Dog (including any subtypes of Dog, of course).
Term
Will this compile?
5) List bList = new ArrayList();
Definition
Yes
Term
Will this compile?
6) List dList = new ArrayList();
Definition
No Problem: You cannot assign a Dog to . The Dog is too "low" in the class hierarchy. Only or would have been legal.
Term
Will this compile?
public class NumberHolder{}
Definition
No.

The ? will not work for generic class and method declarations.
Term
Will this compile?
public static  List backwards(List  input)
Definition
No.

You cant use wildcards in the type variable declaration.
Term
Will this compile?
public static  List backwards(List  input)
Definition
No

You can not use wildcards in the type variable declaration.
Term
Will this compile?
public static  List backwards(List  input)
Definition
Yes.

You can define the type passed to a declaration with the ? wildcard parameter.
Term
Is this statement true?

If the equals() method returns true, the hashCode() comparison == might return false.
Definition
No, This is a negation of the hashCode() and equals() contract. (Objective 6.2)

x.equals(y) == true then x.hasCode() == y.hashCode() (REQUIRED)
Term
Is this statement true?

If the hashCode() comparison != returns true, the equals() method might return true.
Definition
No, This is a negation of the hashCode() and equals() contract. (Objective 6.2)

x.hasCode() != y.hashCode() then x.equals(y) == false (REQUIRED)
Term
Is this statement true?

If the equals() method returns false, the hashCode() comparison == might return true.
Definition
Yes
Term
Is this statement True?

If the hashCode() comparison == returns true, the equals() method might return true.
Definition
Yes
Term
What is the result?
String s="Pod";
s.substring(1,7);
s.insert(0,"i");
System.out.println(s);
Definition
Compilation fails. Strings do not have an insert method.
Term
The HashMap uses what methods for inserting and retreiving data?
Definition
put(K key, V value) and get(Object key)
Term
The LinkedList uses what methods for inserting and retreving data?
Definition
add(E o)
Appends the specified element to the end of this list

add(int index, E element)
Inserts the specified element at the specified position in this list.

get(int index)
Term
Will this compile?
short [][]b = new short[4][4];
short b3=8;
b[1][0]=b3;
Definition
Yes

A short fits into the arra element directly.
Term
Will this compile?
short [][]b = new short[4][4];
short b2[][][][] = new short[2][3][2][2];
b2[1][1][0][1]=b1[1][0];
Definition
Yes because your assigning a value from the 2 dimension b1 to the single demsion item in b2.
Term
What is the result?
int x=0;
for(int y=2;y>0;y--){
  x++;
}
System.out.println(x+y);
Definition
Compillation fails.

y is out of scope.
Term
What is the result?
int x=2;
int y;
for(y=2;y>0;y--){
  x++;
}
System.out.println(x+y);
Definition
4 is the output
Term
Scope1.java Contains:
class ForNext{
	public static void main(String arg[]){
int y; for(y=2;y>0;y--){System.out.println(x+y);}
}
What happens when you run it with "java Scope1"
Definition
Exception in thread "main" java.lang.NoClassDefFoundError: Scope1

This is because the compiler creates a ForNext.class
Term
Will this compile?

throw new RuntimeException();
Definition
yes, you can throw a new RuntimeException();
Term
What calls will request the Garbage Collector to run?
Definition
System.gc()
and
Runtime.getRuntime().gc()
Term
What is the result?
class crap{
  public void static main(String[] args){poo();}
  static void poo(){System.out.println("hi");
  {System.out.println("bye");}
}
Definition
Just "Hi"

The {} init block is never called because the constructor was never called. The static method doesnt require the constructor.
Term
What is the result?
class myNum{
  public void static main(String[] x){
  int i;
  System.out.printlin(go(i));
  }
  int go(int z){return ++z;}
}
Definition
Compilation fails.

go (instance method) is being called from a static method
Term
True or False

protected is less restrictive that default
Definition
True
Term
True/False

Protected member can be accessed by a subclass regardless of the package it's in.
Definition
True
Term
What are the access levels for a class in order from less restrictive to most restrictive?
Definition
Public
Protected
Default
Private
Term
Which of the following can be used in a switch or case statement?

byte
char
int
short
long
double
Definition
All but long and double.
Term
In regex what does \w search for
Definition
Word characters (Digits, letters or seperators)
Term
What is the ^ operator for?
Definition
XOR
Supporting users have an ad free experience!