• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/83

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

83 Cards in this Set

  • Front
  • Back
What package is the Scanner class located in? What is the fully qualified name of the Scanner class?
java.util

java.util.Scanner
What is accomplished when you type “import java.awt.Color;” at the top of a file?
Whenever you type “Color”, the compiler knows that you are talking about the Color class that resides in the package called “java.awt”.
What is accomplished when you type “import java.awt.*;” at the top of a file?
You are importing EVERYTHING that is in the package java.awt.
Which java package is automatically imported in it’s entirety into every Java program you write?
java.lang
What method of the String class can be used to pick out one particular character in the string?
charAt
What method of the String class can tell you how many characters are in the String?
length
What method of the String class can be used to compare two Strings for alphabetical order?
compareTo
What method of the String class can select a portion of an existing String?
substring
What does API stand for?
Application Programming Interface
If someone showed you a Java class, how can you quickly identify which members were part of the API for that class?
everything that is public
If a member is declared as “public”, can it be accessed from inside the same class?
Yes
If a member is declared as “public”, can it be accessed from another class?
Yes
If a member is declared as “private”, can it be accessed from inside the same class?
Yes
If a member is declared as “private”, can it be accessed from another class?
No
What is a “getter”?
A method that returns the value of an instance variable of the object.
What is a “setter”?
A method that modifies the value of an instance variable of the object.
Explain why it is important to limit the number of “public” members.
Encapsulation of the fields (instance variables) of a class is very important. We frequently need to make changes to a class. If these changes do not modify the class API, then the modified class will work perfectly well within an existing project. However, if you modify the class in such a way that the API is changed, then you will have to re-program other components of the project so that they will work with the new class. By limiting the API we are free to make more extensive changes to the class without having to re-program other parts of the project.
Name and describe the two visibility specifiers that you should know at this point.
public – these members are visible everywhere

private – these members are visible only within the class
True/False – if you change a class in such a way that the API changes, then other classes which depend on this one will have to be re-coded.
True
True/False – if you change a class without modifying the API, then other classes which depend on this one will have to be re-coded.
False
True/False – in java, when you pass a reference variable as an argument to a method, it is possible for the method to modify the object to which the variable refers.
True
Assume that you have a class called “Stuff”, which contains the following method:

public static void eat(Cat c)

Show the syntax for a statement which will make a call to this method, passing in a brand new Cat object (without using a variable).
Stuff.eat(new Cat());
Is it better to write many small tests, or a few long ones?
Lots of small ones.
What is JUnit?
A useful feature of Java that allows us to quickly write “unit tests” for classes we are writing.
Give an example of the correct syntax for “assertEquals” and “assertTrue” in a JUnit test.
assertEquals(5, 2 + 3);

assertTrue(!“cat”.equals(“dog”));
If a JUnit test has several separate assertions in it, will the test continue after one of the assertions fails?
NO, but other tests in the entire suite will still run.
If a JUnit test suite has several different JUnit tests in it (separate methods), will the rest of the tests run after one of them fails?
YES.
What is a Stack (in general, not just in Java)?
A stack is a simple “linear” data structure in which elements are both inserted (“pushed”) and removed (“popped”) from the same end. (We usually picture the stack vertically, and say that we “push” and “pop” items from the top.)
When you push an entry into the stack does it go on the top or bottom?
Top
When you pop an entry from the stack, does it come off the top or bottom?
Top
If you do not write any constructors, what values will instance variables of the following primitive types be assigned: int, double, boolean, char ?
int 0
double 0.0
boolean false
char ‘\0’ (character with ascII code)
If you do not write any constructors, what values will instance variables that are references by assigned?
null
Under what circumstances will Java provide a default constructor for you automatically?
If you don’t write ANY constructors, Java will give you a default constructor automatically.
What is a copy constructor? Give an example.
The copy constructor accepts an argument that is the same type as the object being constructed. It initializes the fields of the current object to match that of the parameter. Example:

public Cat(Cat x) {

numWhiskers = x.numWhiskers;

name = x.name;

}
In what kind of methods does it make sense to use “this”?
Instance methods only.
What does “this” refer to?
“this” is a reference to the current object
How many ints are created by the statement: int[] a = new int[5];
5
How many Strings are created by the statement: String[] a = new String[5];

(Hint: The answer to this question and the previous question are different!)
None. What is created is an array of null-references.
Are the elements of an array of primitives automatically initialized? If so, to what values?
Yes, to 0 or false.
Are the elements of an array of references to objects initialized? If so, to what values?
Yes, to null.
Define the terms mutable and immutable.
A mutable class has a public method (not a constructor) that can be used to change the State of the current object.

Any class that is not mutable is called immutable.
Are String objects mutable?
No
Are Integer objects mutable? (If you’re not sure, inspect the online API documentation and find out!)
No
Suppose you are passing (or returning) an array of primitives to/from a method. Is it safe to make a reference copy only?
No
Suppose you are passing (or returning) an array of references to immutable objects to/from a method. Is it safe to make a reference copy only? Is it safe to make a shallow copy?
Reference copy is unsafe. Shallow is safe.
Suppose you are passing or returning an array of references to mutable objects to/from a method. Is it safe to make a reference copy only? Is it safe to make a shallow copy?
Neither one is safe. Only a deep copy is safe in this case.
What is a Java interface?
Typically, it is just a collection of method prototypes that are implemented by several related classes.

Set of public methods through which we can interact with an object
What is polymorphism?
Sometimes a single variable can refer to objects of different types. For example, if you have an interface called “CanFly”, then you can create a variable of type “CanFly” which can refer to any kind of object that knows how to fly. (In other words, it can refer to an object of any class which implements the “CanFly” interface.) This kind of variable is “polymorphic”.
What is meant by the term “algorithm”?
A sequence of instructions or steps that can be followed in order to solve a relatively simple well-defined problem. Frequently there are many different known algorithms for solving a problem.
Explain the relationship between exception handling and the call stack. What happens if an exception is thrown but not caught anywhere in your program?
When an exception is thrown, Java looks for an appropriate “catch” block in the place where the problem occurred. If a catch is not found there, then Java discards the current frame on the call stack, and looks for an appropriate catch block in the previous frame. If none is found, that frame is discarded, etc. If the exception is not caught anywhere, then the program terminates, and the exception propagates to the “outside world”, which for us is the Eclipse IDE. Eclipse displays that red and blue error notification with stack trace in the console.
Under what circumstances will the finally block run?
The finally block ALWAYS runs.
What does “overloading” mean?
Writing more than one method in the same class with the same name. (This only works if the signatures of the

methods are different – i.e. the parameter lists have different types.)
Where can you use a “continue” statement?
Inside of any loop.
Describe how a continue statement behaves in a while loop.
Goes to top of loop immediately and checks the boolean condition to see if looping should continue.
Describe how a continue statement works in a for-loop.
It will execute the statement that is usually processed at the end of an iteration through the loop, and then check the boolean condition to see if looping should continue.
Where can you use a “break” statement?
Either in a switch statement or in a loop.
Describe how a break statement behaves.
Causes the inner-most loop or switch statement to be exited immediately.
Which kinds of variables are given default values if you do not initialize them?
(Local/ Instance/ Static)
-Instance and static variable are given default values
-Local variables must be initialized by the programmer explicitly
What is a reference variable?
Reference variables are variables that refer a class name..
Demo d1;
d1 = new Demo();
//d1 would be the reference variable
What are the default values used for Local, instance, and static variables?
primitive values are set to 0
(Primitive values are ints, booleans, chars, etc...)
reference variables are set to "null"
(Reference variables are variables that refer a class name..)
CanDance x = new CanDance(); ?

Suppose you have an interface called CanDance &
three classes (Student, Penguin, and Cow), all which implement the CanDance interface.
Also asume that there is a method available with the following prototype:
public static doSquareDance (CanDance a).
No,
Interface can declare itself
CanDance x = new Student; ?

Suppose you have an interface called CanDance &
three classes (Student, Penguin, and Cow), all which implement the CanDance interface.
Also asume that there is a method available with the following prototype:
public static doSquareDance (CanDance a).
Yes

The reference variable x refers to the object of the class 'Student'
Student z = new CanDance();

Suppose you have an interface called CanDance &
three classes (Student, Penguin, and Cow), all which implement the CanDance interface.
Also asume that there is a method available with the following prototype:
public static doSquareDance (CanDance a).
No

Class can not reference another method
Student z = new Penguin();

Suppose you have an interface called CanDance &
three classes (Student, Penguin, and Cow), all which implement the CanDance interface.
Also asume that there is a method available with the following prototype:
public static doSquareDance (CanDance a).
No

Class can not reference another class
CanDance a;
a = new Student();
a = new Penguin();
a = new Cow();

Suppose you have an interface called CanDance &
three classes (Student, Penguin, and Cow), all which implement the CanDance interface.
Also asume that there is a method available with the following prototype:
public static doSquareDance (CanDance a).
Yes, yes, and yes

The interface name, CanDance, declared 'a' as an object reference variable

The reference variable 'a' can be used to refer to any object of any class that implements CanDance interface

Student, penguin, and cow all reference Speaker
Penguin b = new Penguin();
doSquareDance(b);

Suppose you have an interface called CanDance &
three classes (Student, Penguin, and Cow), all which implement the CanDance interface.
Also asume that there is a method available with the following prototype:
public static doSquareDance (CanDance a).
Yes

The penguin class calls a new penguin class and assigns doSquareDance to that class
Student c = new Student();
doSquareDance(c);

Suppose you have an interface called CanDance &
three classes (Student, Penguin, and Cow), all which implement the CanDance interface.
Also asume that there is a method available with the following prototype:
public static doSquareDance (CanDance a).
Yes
The student class calls a new penguin class and assigns doSquareDance to that class
Cow d = new Cow();
doSquareDance(d);

Suppose you have an interface called CanDance &
three classes (Student, Penguin, and Cow), all which implement the CanDance interface.
Also asume that there is a method available with the following prototype:
public static doSquareDance (CanDance a).
Yes
The cow class calls a new penguin class and assigns doSquareDance to that class
CanDance e = new Student();
doSquareDance(e);

Suppose you have an interface called CanDance &
three classes (Student, Penguin, and Cow), all which implement the CanDance interface.
Also asume that there is a method available with the following prototype:
public static doSquareDance (CanDance a).
Yes

The reference variable e refers to the object of the class 'Student' and then assigns the doSquareDance method
Write a public static count method that takes one parameter (a reference to a string)
public static int count (String s) {
}
Write a copy constructor
public Cat (Cat x) {
numWhiskers = x.numWhiskers;
name = x.name;
Example of mutability
public class StringsAndMutability {

public static void main(String [] args) {
StringHolder s1 = new StringHolder("One");

s1.addOn("Two");
StringHolder s2 = new StringHolder(s1);
s2.addOn("Three");

System.out.println("S1: " + s1);
System.out.println("S2: " + s2);
}

}
A java source-code file can contain more than one class.
True
A java source-code file can contain more than one public class.
False
Can you use a public class outside of its package? Do you need to use a fully qualified name?
Yes, but then you must use the fully qualified name
When you use a public class inside of its own package, do you need to use a fully qualified name?
No
When you use a non-public class (with no visibility specified) inside of its own package, do you need to use a fully qualified name?
No
Can you use a non-public class outside of its package?
No
If you write a class without specifying a particular package, which package will it become a part of?
default package
What syntax would you use at the top of a source-code file to specify that the class belongs in a subpackage called "stuff" ths is part of a larger package called "junk"
package junk.stuff;
What do you accomplish by using "extends" (What does it mean to write: public class A extends B)
"Extends" allows the class A to "inherit" all of the elements from B (variables and methods). A can also have other features of its own, and it can "over-ride" some of B's features if you want.
What does it mean to say a class is derived from another class?
"a is derived from b" means the same thing as "A extends B"
What does it mean to say a class inherits members from another class?
The derived class automatically shares those features