What is the file extension that my code is saved as after compiling.
.class
Which of the following is not a loop type:
If-Else
For
Do-While
While
If-Else
You are supposed to write this out before starting any code. It describes what your code does.
Algorithm
What does a branch statement take advantage of in order to control flow?
Hint: It rhymes with shmoolean shmexpression.
boolean expression
10/2.0 would return what?
5.0
Why is it not 5?
How many children can each parent class have?
How many parents can a child class have?
Infinite
1
How do you create an array of integers with size of 10?
Can my array grow after being built at size 10?
int[] numbers = new int[10];
No, I would have to create a new array with the larger size.
How do I start out a Class? What is the first line?
Assume our Class name is MyClass for this question. Also assume our class is a normal concrete class.
public class MyClass
{
}
Does every class required to have its own main method?
No, you can have a class that is only ever used via a second class.
The Java compiler translates your Java program into what?
Byte-Code.
Is this a legal use of an if-else statement? Assume there is no other code above.
int i = 0;
else
{
i--;
}
if(i == 0)
{
i++;
}
No. You cannot have an else without an if.
How would you fix this in this particular case?
What is the % used for in java?
What is it called?
To return the remainder.
Ex: 14%3 = 2
It is called a modulo or mod.
A Switch statements case can be type
char, int, short, byte or ???
String
double price = 19.8;
If you already have imported the DecimalFormat class and have an object:
DecimalFormat twoDP = new DecimalFormat("0.00");
How would I output my price to the screen? Including a dollar sign.
System.out.println("$" + twoDP.format(price));
or
System.out.print("$" + twoDP.format(price));
What is the syntax required to inherit from another class?
Use "Parent" as the name of parent class and
"Child" as the name of the child class.
public class Child extends Parent
What are the two ways we have used for traversal of an array?
For loop and for-each loop.
What are the minimum things every concrete class should have?
1) Instance Variables
2) Constructors (Default, Full, and maybe Copy)
3) Getters and Setters for every instance variable unless you have a good reason not to.
4) Equals() and toString().
When comparing two objects of a class can I use "==" to reliably do so? Why or why not? If no what must we use instead?
No you can not use "==" because that compares memory location and two objects can be equal without having equal memory locations.
Instead you must use the equals method defined in the class in order to compare two objects.
Java variable names are case sensitive?
True or False
True.
Do-while and while loops both have the potential to run x times, what is the minimum number of times that each could run?
Do-while runs at least once.
While has the potential to never run if the boolean statement is false.
String is a primitive type variable.
True or False.
False, it is a class.
Within the below switch statement if my case = 1 will my default print out its statement? Why? Or why not?
switch(case) {
case 1:
break;
default:
System.out.println("Hoopla");
break;
}
No, since my case is equal to 1 is taken into account my switches default case will never be reached.
What is NumberFormat most commonly used for?
Money (Currency) Formatting
If a child's class constructor makes use of the super constructor, does it need to be the first or last thing called?
First
What would the subscript/indices be for an array of size 10?
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
What is the difference between a static and an instance variable?
Each object of a class has its own copy of each instance variable, but only a single copy of each static variable is shared by all objects.
Write a for loop that will allow you to iterate through a the following array array and insert a 1 at every index.
int[ ] myGrid = new int[5];
for(int i = 0; i < myGrid.length; i++) {
myGrid[i] = 1;
}
Declare an integer named highScore and initialize with the value of 200.
int highScore = 200;
Is this a legal use of two for loops:
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
}
}
Yes
String name = "Joe";
int age = 26;
Output the name variable with the age added to it within one print line. (Hint: Use concatenation)
System.out.println(name + " " + age);
What is && mean? What about || ? Where are these used?
&& means and || means or. These are used in combining two or more boolean expressions.
When using the DecimalFormat class what is missing below:
DecimalFormat myFormat = new DecimalFormat(???)
A String Pattern.
Where would you see the final modifier used? What does it do?
You will see it used with instance variables, methods and classes.
It indicates that the variable, or method can not be redefined. If used with a class it indicates that the class can not be used as a base/derived class.
An array with an array within each index is called a what?
Multidimensional array
Specifically a two dimensional in this case.
Math is a static class within java. If I want to use its method random() in order to generate a random number what is the syntax required to do so?
Math.random();
//If I want to store the returned number then must use some variation of below:
double randomNumber = Math.random();
How do I retrieve an int from a String?
Assuming our String is named "theString".
Using static parseInt method from Integer class.
Integer.parseInt(theString);
The header for your main method is:
public static void main(String[] args)
A for statement is broken into three parts, those being?
for(??? ; ??? ; ???)
Start, End and Increment.
String class has all of the following methods except:
length() charAt()
equals(Other_String) nextLine()
toLowerCase() indexOf()
nextLine() - This is a Scanner method.
What the heck is lexographical order used for?
It is a way to compare two strings.
Uses ASCII ordering to assign a value to each character in the alphabet, see Appendix 3.
double price = 19.8;
Using a DecimalFormat object how would I format my price to include two decimal points and a dollar sign?
Assume DecimalFormat is not imported.
import java.text.DecimalFormat;
DecimalFormat twoDP = new DecimalFormat("0.00");
System.out.println("$" + twoDP.format(price));
What is overriding? In the parent-child class relationship where does it take place?
Overriding is when you rewrite the method that has been inherited from your parent. However you have to be sure that the method signature remains the same.
What happens if the method signature is different? Is it truly overriding or is it doing something else?
What is the one instance variable for an array?
What does it store?
Length and it stores the number of indices within the array.
If you do not create an equals and toString methods within a class you write what will be compared when calling the equals method?
What will be returned if you call the toString method?
The memory location of the objects will be compared rather then every variable inside (You should check all variables, not memory location).
The memory address will be returned as a String that will be a hexadecimal representation of the memory address.
Can you write one out?
It is a mini if/else statement that is embeddable within other functions. For example within a System.out.println().
(condition) ? trueValue : falseValue;
(i < 0) ? i += 2 : i++;